PageRenderTime 48ms CodeModel.GetById 31ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  5   This software is provided 'as-is', without any express or implied 
  6   warranty. In no event will the authors be held liable for any 
  7   damages arising from the use of this software.
  8
  9   Permission is granted to anyone to use this software for any 
 10   purpose, including commercial applications, and to alter it and 
 11   redistribute it freely, subject to the following restrictions:
 12
 13   1. The origin of this software must not be misrepresented; you 
 14      must not claim that you wrote the original software. If you use
 15      this software in a product, an acknowledgment in the product 
 16      documentation would be appreciated but is not required.
 17
 18   2. Altered source versions must be plainly marked as such, and 
 19      must not be misrepresented as being the original software.
 20
 21   3. This notice may not be removed or altered from any source 
 22      distribution.
 23
 24   The original version of this library can be located at:
 25   http://www.angelcode.com/angelscript/
 26
 27   Andreas Jonsson
 28   andreas@angelcode.com
 29*/
 30
 31
 32//
 33// as_configgroup.cpp
 34//
 35// This class holds configuration groups for the engine
 36//
 37
 38
 39
 40#include "as_config.h"
 41#include "as_configgroup.h"
 42#include "as_scriptengine.h"
 43
 44BEGIN_AS_NAMESPACE
 45
 46asCConfigGroup::asCConfigGroup()
 47{
 48	refCount = 0;
 49	defaultAccess = true;
 50}
 51
 52asCConfigGroup::~asCConfigGroup()
 53{
 54}
 55
 56int asCConfigGroup::AddRef()
 57{
 58	refCount++;
 59	return refCount;
 60}
 61
 62int asCConfigGroup::Release()
 63{
 64	// Don't delete the object here, the engine will delete the object when ready
 65	refCount--;
 66	return refCount;
 67}
 68
 69asCObjectType *asCConfigGroup::FindType(const char *obj)
 70{
 71	for( asUINT n = 0; n < objTypes.GetLength(); n++ )
 72		if( objTypes[n]->name == obj )
 73			return objTypes[n];
 74
 75	return 0;
 76}
 77
 78void asCConfigGroup::RefConfigGroup(asCConfigGroup *group)
 79{
 80	if( group == this || group == 0 ) return;
 81
 82	// Verify if the group is already referenced
 83	for( asUINT n = 0; n < referencedConfigGroups.GetLength(); n++ )
 84		if( referencedConfigGroups[n] == group )
 85			return;
 86
 87	referencedConfigGroups.PushLast(group);
 88	group->AddRef();
 89}
 90
 91bool asCConfigGroup::HasLiveObjects()
 92{
 93	for( asUINT n = 0; n < objTypes.GetLength(); n++ )
 94		if( objTypes[n]->GetRefCount() != 0 )
 95			return true;
 96
 97	return false;
 98}
 99
100void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine)
101{
102	asASSERT( refCount == 0 );
103
104	asUINT n;
105
106	// Remove global variables
107	for( n = 0; n < globalProps.GetLength(); n++ )
108	{
109		int index = engine->registeredGlobalProps.IndexOf(globalProps[n]);
110		if( index >= 0 )
111		{
112			globalProps[n]->Release();
113
114			// TODO: global: Should compact the registeredGlobalProps array
115			engine->registeredGlobalProps[index] = 0;
116		}
117	}
118
119	// Remove global functions
120	for( n = 0; n < scriptFunctions.GetLength(); n++ )
121	{
122		scriptFunctions[n]->Release();
123		engine->registeredGlobalFuncs.RemoveValue(scriptFunctions[n]);
124		if( engine->stringFactory == scriptFunctions[n] )
125			engine->stringFactory = 0;
126	}
127	scriptFunctions.SetLength(0);
128
129	// Remove behaviours and members of object types
130	for( n = 0; n < objTypes.GetLength(); n++ )
131	{
132		asCObjectType *obj = objTypes[n];
133
134		obj->ReleaseAllFunctions();
135	}
136
137
138	// Remove object types
139	for( n = 0; n < objTypes.GetLength(); n++ )
140	{
141		asCObjectType *t = objTypes[n];
142		int idx = engine->objectTypes.IndexOf(t);
143		if( idx >= 0 )
144		{
145#ifdef AS_DEBUG
146			ValidateNoUsage(engine, t);
147#endif
148
149			engine->objectTypes.RemoveIndex(idx);
150
151			if( t->flags & asOBJ_TYPEDEF )
152				engine->registeredTypeDefs.RemoveValue(t);
153			else if( t->flags & asOBJ_ENUM )
154				engine->registeredEnums.RemoveValue(t);
155			else
156				engine->registeredObjTypes.RemoveValue(t);
157
158			asDELETE(t, asCObjectType);
159		}
160	}
161
162	// Release other config groups
163	for( n = 0; n < referencedConfigGroups.GetLength(); n++ )
164		referencedConfigGroups[n]->refCount--;
165	referencedConfigGroups.SetLength(0);
166}
167
168#ifdef AS_DEBUG
169void asCConfigGroup::ValidateNoUsage(asCScriptEngine *engine, asCObjectType *type)
170{
171	for( asUINT n = 0; n < engine->scriptFunctions.GetLength(); n++ )
172	{
173		asCScriptFunction *func = engine->scriptFunctions[n];
174		if( func == 0 ) continue;
175
176		// Ignore factory and members
177		if( func->name == "_beh_2_" || func->objectType == type )
178			continue;
179
180		asASSERT( func->returnType.GetObjectType() != type );
181
182		for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
183		{
184			asASSERT(func->parameterTypes[p].GetObjectType() != type);
185		}
186	}
187
188	// TODO: Check also usage of the type in global variables 
189
190	// TODO: Check also usage of the type in local variables in script functions
191
192	// TODO: Check also usage of the type as members of classes
193
194	// TODO: Check also usage of the type as sub types in other types
195}
196#endif
197
198int asCConfigGroup::SetModuleAccess(const char *module, bool hasAccess)
199{
200	if( module == asALL_MODULES )
201	{
202		// Set default module access
203		defaultAccess = hasAccess;
204	}
205	else
206	{
207		asCString mod(module ? module : "");
208		asSMapNode<asCString,bool> *cursor = 0;
209		if( moduleAccess.MoveTo(&cursor, mod) )
210		{
211			moduleAccess.GetValue(cursor) = hasAccess;
212		}
213		else
214		{
215			moduleAccess.Insert(mod, hasAccess);
216		}
217	}
218
219	return 0;
220}
221
222bool asCConfigGroup::HasModuleAccess(const char *module)
223{
224	asCString mod(module ? module : "");
225	asSMapNode<asCString,bool> *cursor = 0;
226	if( moduleAccess.MoveTo(&cursor, mod) )
227		return moduleAccess.GetValue(cursor);
228	
229	return defaultAccess;
230}
231
232END_AS_NAMESPACE