as_configgroup.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2010 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. #ifdef AS_DEPRECATED
  36. // Deprecated since 2011-10-04
  37. defaultAccess = true;
  38. #endif
  39. }
  40. asCConfigGroup::~asCConfigGroup()
  41. {
  42. }
  43. int asCConfigGroup::AddRef()
  44. {
  45. refCount++;
  46. return refCount;
  47. }
  48. int asCConfigGroup::Release()
  49. {
  50. // Don't delete the object here, the engine will delete the object when ready
  51. refCount--;
  52. return refCount;
  53. }
  54. asCObjectType *asCConfigGroup::FindType(const char *obj)
  55. {
  56. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  57. if( objTypes[n]->name == obj )
  58. return objTypes[n];
  59. return 0;
  60. }
  61. void asCConfigGroup::RefConfigGroup(asCConfigGroup *group)
  62. {
  63. if( group == this || group == 0 ) return;
  64. // Verify if the group is already referenced
  65. for( asUINT n = 0; n < referencedConfigGroups.GetLength(); n++ )
  66. if( referencedConfigGroups[n] == group )
  67. return;
  68. referencedConfigGroups.PushLast(group);
  69. group->AddRef();
  70. }
  71. bool asCConfigGroup::HasLiveObjects()
  72. {
  73. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  74. if( objTypes[n]->GetRefCount() != 0 )
  75. return true;
  76. return false;
  77. }
  78. void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine, bool notUsed)
  79. {
  80. asASSERT( refCount == 0 );
  81. asUINT n;
  82. // Remove global variables
  83. for( n = 0; n < globalProps.GetLength(); n++ )
  84. {
  85. int index = engine->registeredGlobalProps.IndexOf(globalProps[n]);
  86. if( index >= 0 )
  87. {
  88. globalProps[n]->Release();
  89. // TODO: global: Should compact the registeredGlobalProps array
  90. engine->registeredGlobalProps[index] = 0;
  91. }
  92. }
  93. globalProps.SetLength(0);
  94. // Remove global functions
  95. for( n = 0; n < scriptFunctions.GetLength(); n++ )
  96. {
  97. scriptFunctions[n]->Release();
  98. engine->registeredGlobalFuncs.RemoveValue(scriptFunctions[n]);
  99. if( engine->stringFactory == scriptFunctions[n] )
  100. engine->stringFactory = 0;
  101. }
  102. scriptFunctions.SetLength(0);
  103. // Remove behaviours and members of object types
  104. for( n = 0; n < objTypes.GetLength(); n++ )
  105. {
  106. asCObjectType *obj = objTypes[n];
  107. obj->ReleaseAllFunctions();
  108. }
  109. // Remove function definitions
  110. for( n = 0; n < funcDefs.GetLength(); n++ )
  111. {
  112. engine->registeredFuncDefs.RemoveValue(funcDefs[n]);
  113. funcDefs[n]->Release();
  114. }
  115. funcDefs.SetLength(0);
  116. // Remove object types (skip this if it is possible other groups are still using the types)
  117. if( !notUsed )
  118. {
  119. for( n = 0; n < objTypes.GetLength(); n++ )
  120. {
  121. asCObjectType *t = objTypes[n];
  122. int idx = engine->objectTypes.IndexOf(t);
  123. if( idx >= 0 )
  124. {
  125. #ifdef AS_DEBUG
  126. ValidateNoUsage(engine, t);
  127. #endif
  128. engine->objectTypes.RemoveIndex(idx);
  129. if( t->flags & asOBJ_TYPEDEF )
  130. engine->registeredTypeDefs.RemoveValue(t);
  131. else if( t->flags & asOBJ_ENUM )
  132. engine->registeredEnums.RemoveValue(t);
  133. else
  134. engine->registeredObjTypes.RemoveValue(t);
  135. asDELETE(t, asCObjectType);
  136. }
  137. }
  138. objTypes.SetLength(0);
  139. }
  140. // Release other config groups
  141. for( n = 0; n < referencedConfigGroups.GetLength(); n++ )
  142. referencedConfigGroups[n]->refCount--;
  143. referencedConfigGroups.SetLength(0);
  144. }
  145. #ifdef AS_DEBUG
  146. void asCConfigGroup::ValidateNoUsage(asCScriptEngine *engine, asCObjectType *type)
  147. {
  148. for( asUINT n = 0; n < engine->scriptFunctions.GetLength(); n++ )
  149. {
  150. asCScriptFunction *func = engine->scriptFunctions[n];
  151. if( func == 0 ) continue;
  152. // Ignore factory, list factory, and members
  153. if( func->name == "_beh_2_" || func->name == "_beh_3_" || func->objectType == type )
  154. continue;
  155. asASSERT( func->returnType.GetObjectType() != type );
  156. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  157. {
  158. asASSERT(func->parameterTypes[p].GetObjectType() != type);
  159. }
  160. }
  161. // TODO: Check also usage of the type in global variables
  162. // TODO: Check also usage of the type in local variables in script functions
  163. // TODO: Check also usage of the type as members of classes
  164. // TODO: Check also usage of the type as sub types in other types
  165. }
  166. #endif
  167. #ifdef AS_DEPRECATED
  168. // deprecated since 2011-10-04
  169. int asCConfigGroup::SetModuleAccess(const char *module, bool hasAccess)
  170. {
  171. if( module == asALL_MODULES )
  172. {
  173. // Set default module access
  174. defaultAccess = hasAccess;
  175. }
  176. else
  177. {
  178. asCString mod(module ? module : "");
  179. asSMapNode<asCString,bool> *cursor = 0;
  180. if( moduleAccess.MoveTo(&cursor, mod) )
  181. {
  182. moduleAccess.GetValue(cursor) = hasAccess;
  183. }
  184. else
  185. {
  186. moduleAccess.Insert(mod, hasAccess);
  187. }
  188. }
  189. return 0;
  190. }
  191. bool asCConfigGroup::HasModuleAccess(const char *module)
  192. {
  193. asCString mod(module ? module : "");
  194. asSMapNode<asCString,bool> *cursor = 0;
  195. if( moduleAccess.MoveTo(&cursor, mod) )
  196. return moduleAccess.GetValue(cursor);
  197. return defaultAccess;
  198. }
  199. #endif
  200. END_AS_NAMESPACE