as_configgroup.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2020 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. andreas@angelcode.com
  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. #include "as_texts.h"
  32. BEGIN_AS_NAMESPACE
  33. asCConfigGroup::asCConfigGroup()
  34. {
  35. refCount = 0;
  36. }
  37. asCConfigGroup::~asCConfigGroup()
  38. {
  39. }
  40. int asCConfigGroup::AddRef()
  41. {
  42. refCount++;
  43. return refCount;
  44. }
  45. int asCConfigGroup::Release()
  46. {
  47. // Don't delete the object here, the engine will delete the object when ready
  48. refCount--;
  49. return refCount;
  50. }
  51. asCTypeInfo *asCConfigGroup::FindType(const char *obj)
  52. {
  53. for( asUINT n = 0; n < types.GetLength(); n++ )
  54. if( types[n]->name == obj )
  55. return types[n];
  56. return 0;
  57. }
  58. void asCConfigGroup::RefConfigGroup(asCConfigGroup *group)
  59. {
  60. if( group == this || group == 0 ) return;
  61. // Verify if the group is already referenced
  62. for( asUINT n = 0; n < referencedConfigGroups.GetLength(); n++ )
  63. if( referencedConfigGroups[n] == group )
  64. return;
  65. referencedConfigGroups.PushLast(group);
  66. group->AddRef();
  67. }
  68. void asCConfigGroup::AddReferencesForFunc(asCScriptEngine *engine, asCScriptFunction *func)
  69. {
  70. AddReferencesForType(engine, func->returnType.GetTypeInfo());
  71. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  72. AddReferencesForType(engine, func->parameterTypes[n].GetTypeInfo());
  73. }
  74. void asCConfigGroup::AddReferencesForType(asCScriptEngine *engine, asCTypeInfo *type)
  75. {
  76. if( type == 0 ) return;
  77. // Keep reference to other groups
  78. RefConfigGroup(engine->FindConfigGroupForTypeInfo(type));
  79. // Keep track of which generated template instances the config group uses
  80. if( type->flags & asOBJ_TEMPLATE && engine->generatedTemplateTypes.Exists(CastToObjectType(type)) && !generatedTemplateInstances.Exists(CastToObjectType(type)) )
  81. generatedTemplateInstances.PushLast(CastToObjectType(type));
  82. }
  83. bool asCConfigGroup::HasLiveObjects()
  84. {
  85. for( asUINT n = 0; n < types.GetLength(); n++ )
  86. if( types[n]->externalRefCount.get() != 0 )
  87. return true;
  88. return false;
  89. }
  90. void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine, bool notUsed)
  91. {
  92. asASSERT( refCount == 0 );
  93. asUINT n;
  94. // Remove global variables
  95. for( n = 0; n < globalProps.GetLength(); n++ )
  96. {
  97. int index = engine->registeredGlobalProps.GetIndex(globalProps[n]);
  98. if( index >= 0 )
  99. {
  100. globalProps[n]->Release();
  101. engine->registeredGlobalProps.Erase(index);
  102. }
  103. }
  104. globalProps.SetLength(0);
  105. // Remove global functions
  106. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  107. {
  108. int index = engine->registeredGlobalFuncs.GetIndex(scriptFunctions[n]);
  109. if( index >= 0 )
  110. engine->registeredGlobalFuncs.Erase(index);
  111. scriptFunctions[n]->ReleaseInternal();
  112. }
  113. scriptFunctions.SetLength(0);
  114. // Remove behaviours and members of object types
  115. for( n = 0; n < types.GetLength(); n++ )
  116. {
  117. asCObjectType *obj = CastToObjectType(types[n]);
  118. if( obj )
  119. obj->ReleaseAllFunctions();
  120. }
  121. // Remove object types (skip this if it is possible other groups are still using the types)
  122. if( !notUsed )
  123. {
  124. for( n = asUINT(types.GetLength()); n-- > 0; )
  125. {
  126. asCTypeInfo *t = types[n];
  127. asSMapNode<asSNameSpaceNamePair, asCTypeInfo*> *cursor;
  128. if( engine->allRegisteredTypes.MoveTo(&cursor, asSNameSpaceNamePair(t->nameSpace, t->name)) &&
  129. cursor->value == t )
  130. {
  131. engine->allRegisteredTypes.Erase(cursor);
  132. if( engine->defaultArrayObjectType == t )
  133. engine->defaultArrayObjectType = 0;
  134. if( t->flags & asOBJ_TYPEDEF )
  135. engine->registeredTypeDefs.RemoveValue(CastToTypedefType(t));
  136. else if( t->flags & asOBJ_ENUM )
  137. engine->registeredEnums.RemoveValue(CastToEnumType(t));
  138. else if (t->flags & asOBJ_TEMPLATE)
  139. engine->registeredTemplateTypes.RemoveValue(CastToObjectType(t));
  140. else if (t->flags & asOBJ_FUNCDEF)
  141. {
  142. engine->registeredFuncDefs.RemoveValue(CastToFuncdefType(t));
  143. engine->RemoveFuncdef(CastToFuncdefType(t));
  144. }
  145. else
  146. engine->registeredObjTypes.RemoveValue(CastToObjectType(t));
  147. t->DestroyInternal();
  148. t->ReleaseInternal();
  149. }
  150. else
  151. {
  152. int idx = engine->templateInstanceTypes.IndexOf(CastToObjectType(t));
  153. if( idx >= 0 )
  154. {
  155. engine->templateInstanceTypes.RemoveIndexUnordered(idx);
  156. asCObjectType *ot = CastToObjectType(t);
  157. ot->DestroyInternal();
  158. ot->ReleaseInternal();
  159. }
  160. }
  161. }
  162. types.SetLength(0);
  163. }
  164. // Release other config groups
  165. for( n = 0; n < referencedConfigGroups.GetLength(); n++ )
  166. referencedConfigGroups[n]->refCount--;
  167. referencedConfigGroups.SetLength(0);
  168. }
  169. END_AS_NAMESPACE