File: as_configgroup.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (187 lines) | stat: -rw-r--r-- 5,581 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2020 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/


//
// as_configgroup.cpp
//
// This class holds configuration groups for the engine
//



#include "as_config.h"
#include "as_configgroup.h"
#include "as_scriptengine.h"
#include "as_texts.h"

BEGIN_AS_NAMESPACE

asCConfigGroup::asCConfigGroup() {
	refCount = 0;
}

asCConfigGroup::~asCConfigGroup() {
}

int asCConfigGroup::AddRef() {
	refCount++;
	return refCount;
}

int asCConfigGroup::Release() {
	// Don't delete the object here, the engine will delete the object when ready
	refCount--;
	return refCount;
}

asCTypeInfo *asCConfigGroup::FindType(const char *obj) {
	for (asUINT n = 0; n < types.GetLength(); n++)
		if (types[n]->name == obj)
			return types[n];

	return 0;
}

void asCConfigGroup::RefConfigGroup(asCConfigGroup *group) {
	if (group == this || group == 0) return;

	// Verify if the group is already referenced
	for (asUINT n = 0; n < referencedConfigGroups.GetLength(); n++)
		if (referencedConfigGroups[n] == group)
			return;

	referencedConfigGroups.PushLast(group);
	group->AddRef();
}

void asCConfigGroup::AddReferencesForFunc(asCScriptEngine *engine, asCScriptFunction *func) {
	AddReferencesForType(engine, func->returnType.GetTypeInfo());
	for (asUINT n = 0; n < func->parameterTypes.GetLength(); n++)
		AddReferencesForType(engine, func->parameterTypes[n].GetTypeInfo());
}

void asCConfigGroup::AddReferencesForType(asCScriptEngine *engine, asCTypeInfo *type) {
	if (type == 0) return;

	// Keep reference to other groups
	RefConfigGroup(engine->FindConfigGroupForTypeInfo(type));

	// Keep track of which generated template instances the config group uses
	if (type->flags & asOBJ_TEMPLATE && engine->generatedTemplateTypes.Exists(CastToObjectType(type)) && !generatedTemplateInstances.Exists(CastToObjectType(type)))
		generatedTemplateInstances.PushLast(CastToObjectType(type));
}

bool asCConfigGroup::HasLiveObjects() {
	for (asUINT n = 0; n < types.GetLength(); n++)
		if (types[n]->externalRefCount.get() != 0)
			return true;

	return false;
}

void asCConfigGroup::RemoveConfiguration(asCScriptEngine *engine, bool notUsed) {
	asASSERT(refCount == 0);

	asUINT n;

	// Remove global variables
	for (n = 0; n < globalProps.GetLength(); n++) {
		int index = engine->registeredGlobalProps.GetIndex(globalProps[n]);
		if (index >= 0) {
			globalProps[n]->Release();

			engine->registeredGlobalProps.Erase(index);
		}
	}
	globalProps.SetLength(0);

	// Remove global functions
	for (n = 0; n < scriptFunctions.GetLength(); n++) {
		int index = engine->registeredGlobalFuncs.GetIndex(scriptFunctions[n]);
		if (index >= 0)
			engine->registeredGlobalFuncs.Erase(index);
		scriptFunctions[n]->ReleaseInternal();
	}
	scriptFunctions.SetLength(0);

	// Remove behaviours and members of object types
	for (n = 0; n < types.GetLength(); n++) {
		asCObjectType *obj = CastToObjectType(types[n]);
		if (obj)
			obj->ReleaseAllFunctions();
	}

	// Remove object types (skip this if it is possible other groups are still using the types)
	if (!notUsed) {
		for (n = asUINT(types.GetLength()); n-- > 0;) {
			asCTypeInfo *t = types[n];
			asSMapNode<asSNameSpaceNamePair, asCTypeInfo *> *cursor;
			if (engine->allRegisteredTypes.MoveTo(&cursor, asSNameSpaceNamePair(t->nameSpace, t->name)) &&
			        cursor->value == t) {
				engine->allRegisteredTypes.Erase(cursor);

				if (engine->defaultArrayObjectType == t)
					engine->defaultArrayObjectType = 0;

				if (t->flags & asOBJ_TYPEDEF)
					engine->registeredTypeDefs.RemoveValue(CastToTypedefType(t));
				else if (t->flags & asOBJ_ENUM)
					engine->registeredEnums.RemoveValue(CastToEnumType(t));
				else if (t->flags & asOBJ_TEMPLATE)
					engine->registeredTemplateTypes.RemoveValue(CastToObjectType(t));
				else if (t->flags & asOBJ_FUNCDEF) {
					engine->registeredFuncDefs.RemoveValue(CastToFuncdefType(t));
					engine->RemoveFuncdef(CastToFuncdefType(t));
				} else
					engine->registeredObjTypes.RemoveValue(CastToObjectType(t));

				t->DestroyInternal();
				t->ReleaseInternal();
			} else {
				int idx = engine->templateInstanceTypes.IndexOf(CastToObjectType(t));
				if (idx >= 0) {
					engine->templateInstanceTypes.RemoveIndexUnordered(idx);
					asCObjectType *ot = CastToObjectType(t);
					ot->DestroyInternal();
					ot->ReleaseInternal();
				}
			}
		}
		types.SetLength(0);
	}

	// Release other config groups
	for (n = 0; n < referencedConfigGroups.GetLength(); n++)
		referencedConfigGroups[n]->refCount--;
	referencedConfigGroups.SetLength(0);
}

END_AS_NAMESPACE