-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.cpp
258 lines (214 loc) · 6.95 KB
/
Type.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/***************************************************************************
* Copyright (c) Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <cassert>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "Type.h"
//#include "Exception.h"
//#include "Interpreter.h" // Interpreter().loadModule(Mod.c_str());
///#include "Console.h" // for logging only
#if FC_BUILD_PYTHON
#include <Python.h>
#include <pybind11/pybind11.h> // pybind11::module sys = py::module::import("sys");
namespace py = pybind11;
#endif
using namespace Base;
using namespace std;
struct Base::TypeData
{
TypeData(const char* theName, const Type type = Type::badType(), const Type theParent = Type::badType(),
Type::instantiationMethod method = 0, Type::destructTypeMethod destruct = 0)
: name(theName)
, parent(theParent)
, type(type)
, instMethod(method)
, typeDestructMethod(destruct)
{
}
std::string name;
Type parent;
Type type;
Type::instantiationMethod instMethod;
Type::destructTypeMethod typeDestructMethod;
};
map<string, unsigned int> Type::typemap;
vector<TypeData*> Type::typedata;
set<string> Type::loadModuleSet;
//**************************************************************************
// Construction/Destruction
/**
* A constructor.
* A more elaborate description of the constructor.
*/
Type::Type()
: index(0)
{
}
Type::Type(const Type& type)
: index(type.index)
{
}
/**
* A destructor.
* A more elaborate description of the destructor.
*/
Type::~Type()
{
}
void* Type::createInstance(void)
{
return (typedata[index]->instMethod)();
}
void* Type::createInstanceByName(const char* TypeName, bool bLoadModule)
{
// if not already, load the module
if (bLoadModule)
importModule(TypeName);
// now the type should be in the type map
Type t = fromName(TypeName);
if (t == badType())
return 0;
return t.createInstance();
}
Type Type::badType(void)
{
Type bad;
bad.index = 0;
return bad;
}
const Type Type::createType(const Type parent, const char* name, instantiationMethod method,
destructTypeMethod destruct)
{
Type newType;
newType.index = Type::typedata.size();
TypeData* typeData = new TypeData(name, newType, parent, method, destruct);
Type::typedata.push_back(typeData);
// add to dictionary for fast lookup
Type::typemap[name] = newType.getKey();
return newType;
}
void Type::init(void)
{
assert(Type::typedata.size() == 0);
Type::typedata.push_back(new TypeData("BadType"));
Type::typemap["BadType"] = 0;
}
void Type::destruct(void)
{
for (std::vector<TypeData*>::const_iterator it = typedata.begin(); it != typedata.end(); ++it)
{
const destructTypeMethod& dt = (*it)->typeDestructMethod;
if (dt) // the first type, BadType has no such function
dt();
}
for (std::vector<TypeData*>::const_iterator it = typedata.begin(); it != typedata.end(); ++it)
{
delete *it;
}
typedata.clear();
typemap.clear();
loadModuleSet.clear();
}
Type Type::fromName(const char* name)
{
std::map<std::string, unsigned int>::const_iterator pos;
pos = typemap.find(name);
if (pos != typemap.end())
return typedata[pos->second]->type;
else
return Type::badType();
}
Type Type::fromKey(unsigned int key)
{
if (key < typedata.size())
return typedata[key]->type;
else
return Type::badType();
}
const char* Type::getName(void) const
{
return typedata[index]->name.c_str();
}
const Type Type::getParent(void) const
{
return typedata[index]->parent;
}
bool Type::isDerivedFrom(const Type type) const
{
Type temp(*this);
do
{
if (temp == type)
return true;
temp = temp.getParent();
} while (temp != badType());
return false;
}
int Type::getAllDerivedFrom(const Type type, std::vector<Type>& List)
{
int cnt = 0;
for (std::vector<TypeData*>::const_iterator it = typedata.begin(); it != typedata.end(); ++it)
{
if ((*it)->type.isDerivedFrom(type))
{
List.push_back((*it)->type);
cnt++;
}
}
return cnt;
}
int Type::getNumTypes(void)
{
return typedata.size();
}
/////////////////////////////////////////////////////////////////
// todo: this is FreeCAD specific, python specific code
void Type::importModule(const char* TypeName)
{
// cut out the module name
string Mod = getModuleName(TypeName);
// ignore base modules
if (Mod != "App" && Mod != "Gui" && Mod != "Base")
{
// remember already loaded modules
set<string>::const_iterator pos = loadModuleSet.find(Mod);
if (pos == loadModuleSet.end())
{
// Interpreter().loadModule(Mod.c_str());
#ifdef FC_LOGLOADMODULE
Console().Log("Act: Module %s loaded through class %s \n", Mod.c_str(), TypeName);
#endif
loadModuleSet.insert(Mod);
}
}
}
string Type::getModuleName(const char* ClassName)
{
string temp(ClassName);
std::string::size_type pos = temp.find_first_of("::");
if (pos != std::string::npos)
return string(temp, 0, pos);
else
return string();
}