23
#include "PreCompiled.h"
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;
68
bool Type::canInstantiate() const
70
instantiationMethod method = typedata[index]->instMethod;
71
return method != nullptr;
74
void* Type::createInstanceByName(const char* TypeName, bool bLoadModule)
78
importModule(TypeName);
82
Type type = fromName(TypeName);
83
if (type == badType()) {
87
return type.createInstance();
90
void Type::importModule(const char* TypeName)
93
string Mod = getModuleName(TypeName);
95
if (Mod != "App" && Mod != "Gui" && Mod != "Base") {
97
set<string>::const_iterator pos = loadModuleSet.find(Mod);
98
if (pos == loadModuleSet.end()) {
99
Interpreter().loadModule(Mod.c_str());
100
#ifdef FC_LOGLOADMODULE
101
Console().Log("Act: Module %s loaded through class %s \n", Mod.c_str(), TypeName);
103
loadModuleSet.insert(Mod);
108
string Type::getModuleName(const char* ClassName)
110
string temp(ClassName);
111
std::string::size_type pos = temp.find_first_of("::");
113
if (pos != std::string::npos) {
114
return {temp, 0, pos};
127
Type Type::createType(const Type& parent, const char* name, instantiationMethod method)
130
newType.index = static_cast<unsigned int>(Type::typedata.size());
131
TypeData* typeData = new TypeData(name, newType, parent, method);
132
Type::typedata.push_back(typeData);
135
Type::typemap[name] = newType.getKey();
143
assert(Type::typedata.empty());
146
Type::typedata.push_back(new TypeData("BadType"));
147
Type::typemap["BadType"] = 0;
152
for (auto it : typedata) {
157
loadModuleSet.clear();
160
Type Type::fromName(const char* name)
162
std::map<std::string, unsigned int>::const_iterator pos;
164
pos = typemap.find(name);
165
if (pos != typemap.end()) {
166
return typedata[pos->second]->type;
169
return Type::badType();
172
Type Type::fromKey(unsigned int key)
174
if (key < typedata.size()) {
175
return typedata[key]->type;
178
return Type::badType();
181
const char* Type::getName() const
183
return typedata[index]->name.c_str();
186
Type Type::getParent() const
188
return typedata[index]->parent;
191
bool Type::isDerivedFrom(const Type& type) const
199
temp = temp.getParent();
200
} while (temp != badType());
205
int Type::getAllDerivedFrom(const Type& type, std::vector<Type>& List)
209
for (auto it : typedata) {
210
if (it->type.isDerivedFrom(type)) {
211
List.push_back(it->type);
218
int Type::getNumTypes()
220
return static_cast<int>(typedata.size());
223
Type Type::getTypeIfDerivedFrom(const char* name, const Type& parent, bool bLoadModule)
229
Type type = fromName(name);
231
if (type.isDerivedFrom(parent)) {
235
return Type::badType();