/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_configgroup.cpp

http://github.com/xbmc/xbmc · C++ · 232 lines · 141 code · 43 blank · 48 comment · 40 complexity · 67ff7d8921d1852d14b0019db3763a29 MD5 · raw file

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 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. BEGIN_AS_NAMESPACE
  32. asCConfigGroup::asCConfigGroup()
  33. {
  34. refCount = 0;
  35. defaultAccess = true;
  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. asCObjectType *asCConfigGroup::FindType(const char *obj)
  52. {
  53. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  54. if( objTypes[n]->name == obj )
  55. return objTypes[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. bool asCConfigGroup::HasLiveObjects()
  69. {
  70. for( asUINT n = 0; n < objTypes.GetLength(); n++ )
  71. if( objTypes[n]->GetRefCount() != 0 )
  72. return true;
  73. return false;
  74. }
  75. void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine)
  76. {
  77. asASSERT( refCount == 0 );
  78. asUINT n;
  79. // Remove global variables
  80. for( n = 0; n < globalProps.GetLength(); n++ )
  81. {
  82. int index = engine->registeredGlobalProps.IndexOf(globalProps[n]);
  83. if( index >= 0 )
  84. {
  85. globalProps[n]->Release();
  86. // TODO: global: Should compact the registeredGlobalProps array
  87. engine->registeredGlobalProps[index] = 0;
  88. }
  89. }
  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 object types
  106. for( n = 0; n < objTypes.GetLength(); n++ )
  107. {
  108. asCObjectType *t = objTypes[n];
  109. int idx = engine->objectTypes.IndexOf(t);
  110. if( idx >= 0 )
  111. {
  112. #ifdef AS_DEBUG
  113. ValidateNoUsage(engine, t);
  114. #endif
  115. engine->objectTypes.RemoveIndex(idx);
  116. if( t->flags & asOBJ_TYPEDEF )
  117. engine->registeredTypeDefs.RemoveValue(t);
  118. else if( t->flags & asOBJ_ENUM )
  119. engine->registeredEnums.RemoveValue(t);
  120. else
  121. engine->registeredObjTypes.RemoveValue(t);
  122. asDELETE(t, asCObjectType);
  123. }
  124. }
  125. // Release other config groups
  126. for( n = 0; n < referencedConfigGroups.GetLength(); n++ )
  127. referencedConfigGroups[n]->refCount--;
  128. referencedConfigGroups.SetLength(0);
  129. }
  130. #ifdef AS_DEBUG
  131. void asCConfigGroup::ValidateNoUsage(asCScriptEngine *engine, asCObjectType *type)
  132. {
  133. for( asUINT n = 0; n < engine->scriptFunctions.GetLength(); n++ )
  134. {
  135. asCScriptFunction *func = engine->scriptFunctions[n];
  136. if( func == 0 ) continue;
  137. // Ignore factory and members
  138. if( func->name == "_beh_2_" || func->objectType == type )
  139. continue;
  140. asASSERT( func->returnType.GetObjectType() != type );
  141. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  142. {
  143. asASSERT(func->parameterTypes[p].GetObjectType() != type);
  144. }
  145. }
  146. // TODO: Check also usage of the type in global variables
  147. // TODO: Check also usage of the type in local variables in script functions
  148. // TODO: Check also usage of the type as members of classes
  149. // TODO: Check also usage of the type as sub types in other types
  150. }
  151. #endif
  152. int asCConfigGroup::SetModuleAccess(const char *module, bool hasAccess)
  153. {
  154. if( module == asALL_MODULES )
  155. {
  156. // Set default module access
  157. defaultAccess = hasAccess;
  158. }
  159. else
  160. {
  161. asCString mod(module ? module : "");
  162. asSMapNode<asCString,bool> *cursor = 0;
  163. if( moduleAccess.MoveTo(&cursor, mod) )
  164. {
  165. moduleAccess.GetValue(cursor) = hasAccess;
  166. }
  167. else
  168. {
  169. moduleAccess.Insert(mod, hasAccess);
  170. }
  171. }
  172. return 0;
  173. }
  174. bool asCConfigGroup::HasModuleAccess(const char *module)
  175. {
  176. asCString mod(module ? module : "");
  177. asSMapNode<asCString,bool> *cursor = 0;
  178. if( moduleAccess.MoveTo(&cursor, mod) )
  179. return moduleAccess.GetValue(cursor);
  180. return defaultAccess;
  181. }
  182. END_AS_NAMESPACE