as_configgroup.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_configgroup.cpp
  25. //
  26. // This class holds configuration groups for the engine
  27. //
  28. #include "as_config.h"
  29. #include "as_configgroup.h"
  30. #include "as_scriptengine.h"
  31. BEGIN_AS_NAMESPACE
  32. asCConfigGroup::asCConfigGroup()
  33. {
  34. refCount = 0;
  35. }
  36. asCConfigGroup::~asCConfigGroup()
  37. {
  38. }
  39. int asCConfigGroup::AddRef()
  40. {
  41. refCount++;
  42. return refCount;
  43. }
  44. int asCConfigGroup::Release()
  45. {
  46. // Don't delete the object here, the engine will delete the object when ready
  47. refCount--;
  48. return refCount;
  49. }
  50. asCObjectType *asCConfigGroup::FindType(const char *obj)
  51. {
  52. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  53. if( objTypes[n]->name == obj )
  54. return objTypes[n];
  55. return 0;
  56. }
  57. void asCConfigGroup::RefConfigGroup(asCConfigGroup *group)
  58. {
  59. if( group == this || group == 0 ) return;
  60. // Verify if the group is already referenced
  61. for( asUINT n = 0; n < referencedConfigGroups.GetLength(); n++ )
  62. if( referencedConfigGroups[n] == group )
  63. return;
  64. referencedConfigGroups.PushLast(group);
  65. group->AddRef();
  66. }
  67. bool asCConfigGroup::HasLiveObjects()
  68. {
  69. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  70. if( objTypes[n]->GetRefCount() != 0 )
  71. return true;
  72. return false;
  73. }
  74. void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine, bool notUsed)
  75. {
  76. asASSERT( refCount == 0 );
  77. asUINT n;
  78. // Remove global variables
  79. for( n = 0; n < globalProps.GetLength(); n++ )
  80. {
  81. int index = engine->registeredGlobalProps.GetIndex(globalProps[n]);
  82. if( index >= 0 )
  83. {
  84. globalProps[n]->Release();
  85. // TODO: global: Should compact the registeredGlobalProps array
  86. engine->registeredGlobalProps.Erase(index);
  87. }
  88. }
  89. globalProps.SetLength(0);
  90. // Remove global functions
  91. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  92. {
  93. scriptFunctions[n]->Release();
  94. engine->registeredGlobalFuncs.RemoveValue(scriptFunctions[n]);
  95. if( engine->stringFactory == scriptFunctions[n] )
  96. engine->stringFactory = 0;
  97. }
  98. scriptFunctions.SetLength(0);
  99. // Remove behaviours and members of object types
  100. for( n = 0; n < objTypes.GetLength(); n++ )
  101. {
  102. asCObjectType *obj = objTypes[n];
  103. obj->ReleaseAllFunctions();
  104. }
  105. // Remove function definitions
  106. for( n = 0; n < funcDefs.GetLength(); n++ )
  107. {
  108. engine->registeredFuncDefs.RemoveValue(funcDefs[n]);
  109. funcDefs[n]->Release();
  110. }
  111. funcDefs.SetLength(0);
  112. // Remove object types (skip this if it is possible other groups are still using the types)
  113. if( !notUsed )
  114. {
  115. for( n = 0; n < objTypes.GetLength(); n++ )
  116. {
  117. asCObjectType *t = objTypes[n];
  118. int idx = engine->objectTypes.IndexOf(t);
  119. if( idx >= 0 )
  120. {
  121. #ifdef AS_DEBUG
  122. ValidateNoUsage(engine, t);
  123. #endif
  124. engine->objectTypes.RemoveIndex(idx);
  125. if( t->flags & asOBJ_TYPEDEF )
  126. engine->registeredTypeDefs.RemoveValue(t);
  127. else if( t->flags & asOBJ_ENUM )
  128. engine->registeredEnums.RemoveValue(t);
  129. else
  130. engine->registeredObjTypes.RemoveValue(t);
  131. asDELETE(t, asCObjectType);
  132. }
  133. }
  134. objTypes.SetLength(0);
  135. }
  136. // Release other config groups
  137. for( n = 0; n < referencedConfigGroups.GetLength(); n++ )
  138. referencedConfigGroups[n]->refCount--;
  139. referencedConfigGroups.SetLength(0);
  140. }
  141. #ifdef AS_DEBUG
  142. void asCConfigGroup::ValidateNoUsage(asCScriptEngine *engine, asCObjectType *type)
  143. {
  144. for( asUINT n = 0; n < engine->scriptFunctions.GetLength(); n++ )
  145. {
  146. asCScriptFunction *func = engine->scriptFunctions[n];
  147. if( func == 0 ) continue;
  148. // Ignore factory, list factory, and members
  149. if( func->name == "_beh_2_" || func->name == "_beh_3_" || func->objectType == type )
  150. continue;
  151. asASSERT( func->returnType.GetObjectType() != type );
  152. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  153. {
  154. asASSERT(func->parameterTypes[p].GetObjectType() != type);
  155. }
  156. }
  157. // TODO: Check also usage of the type in global variables
  158. // TODO: Check also usage of the type in local variables in script functions
  159. // TODO: Check also usage of the type as members of classes
  160. // TODO: Check also usage of the type as sub types in other types
  161. }
  162. #endif
  163. END_AS_NAMESPACE