1
/***************************************************************************
2
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
29
/// Here the FreeCAD includes sorted by Base,App,Gui......
32
#include "Interpreter.h"
42
TypeData(const char* theName,
43
const Type type = Type::badType(),
44
const Type theParent = Type::badType(),
45
Type::instantiationMethod method = nullptr)
55
Type::instantiationMethod instMethod;
58
map<string, unsigned int> Type::typemap;
59
vector<TypeData*> Type::typedata;
60
set<string> Type::loadModuleSet;
62
void* Type::createInstance()
64
instantiationMethod method = typedata[index]->instMethod;
65
return method ? (*method)() : nullptr;
69
void* Type::createInstanceByName(const char* TypeName, bool bLoadModule)
71
// if not already, load the module
73
importModule(TypeName);
76
// now the type should be in the type map
77
Type type = fromName(TypeName);
78
if (type == badType()) {
82
return type.createInstance();
85
void Type::importModule(const char* TypeName)
87
// cut out the module name
88
string Mod = getModuleName(TypeName);
89
// ignore base modules
90
if (Mod != "App" && Mod != "Gui" && Mod != "Base") {
91
// remember already loaded modules
92
set<string>::const_iterator pos = loadModuleSet.find(Mod);
93
if (pos == loadModuleSet.end()) {
94
Interpreter().loadModule(Mod.c_str());
95
#ifdef FC_LOGLOADMODULE
96
Console().Log("Act: Module %s loaded through class %s \n", Mod.c_str(), TypeName);
98
loadModuleSet.insert(Mod);
103
string Type::getModuleName(const char* ClassName)
105
string temp(ClassName);
106
std::string::size_type pos = temp.find_first_of("::");
108
if (pos != std::string::npos) {
109
return {temp, 0, pos};
122
Type Type::createType(const Type& parent, const char* name, instantiationMethod method)
125
newType.index = static_cast<unsigned int>(Type::typedata.size());
126
TypeData* typeData = new TypeData(name, newType, parent, method);
127
Type::typedata.push_back(typeData);
129
// add to dictionary for fast lookup
130
Type::typemap[name] = newType.getKey();
138
assert(Type::typedata.empty());
141
Type::typedata.push_back(new TypeData("BadType"));
142
Type::typemap["BadType"] = 0;
147
for (auto it : typedata) {
152
loadModuleSet.clear();
155
Type Type::fromName(const char* name)
157
std::map<std::string, unsigned int>::const_iterator pos;
159
pos = typemap.find(name);
160
if (pos != typemap.end()) {
161
return typedata[pos->second]->type;
164
return Type::badType();
167
Type Type::fromKey(unsigned int key)
169
if (key < typedata.size()) {
170
return typedata[key]->type;
173
return Type::badType();
176
const char* Type::getName() const
178
return typedata[index]->name.c_str();
181
Type Type::getParent() const
183
return typedata[index]->parent;
186
bool Type::isDerivedFrom(const Type& type) const
194
temp = temp.getParent();
195
} while (temp != badType());
200
int Type::getAllDerivedFrom(const Type& type, std::vector<Type>& List)
204
for (auto it : typedata) {
205
if (it->type.isDerivedFrom(type)) {
206
List.push_back(it->type);
213
int Type::getNumTypes()
215
return static_cast<int>(typedata.size());
218
Type Type::getTypeIfDerivedFrom(const char* name, const Type& parent, bool bLoadModule)
224
Type type = fromName(name);
226
if (type.isDerivedFrom(parent)) {
230
return Type::badType();