Debian Codegraph @

src/Base/Type.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2011 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <cassert>
26
27 #include "Type.h"
28 #include "Interpreter.h"
29 #include "Console.h"
30
31
32 using namespace Base;
33
34 static_assert(
35 sizeof(Base::Type) == sizeof(Type::TypeId),
36 "Base::Type has been designed to be small to be passed around by value efficiently. "
37 "The size of Base::Type has changed. Be careful when adding more data members."
38 );
39
40 static_assert(
41 sizeof(Base::Type) <= 2 * sizeof(void*),
42 "Base::Type has been designed to be small to be passed around by value efficiently. "
43 "When the size grows larger than ~2 words, consider passing by const reference instead. "
44 "Exact limit depends on the architecture and ABI."
45 );
46
47 struct Base::TypeData
48 {
49 TypeData(const char* name, const Type type, const Type parent, const Type::instantiationMethod instMethod)
50 : name(name)
51 , parent(parent)
52 , type(type)
53 , instMethod(instMethod)
54 {}
55
56 const std::string name;
57 const Type parent;
58 const Type type;
59 const Type::instantiationMethod instMethod;
60 };
61
62 namespace
63 {
64 constexpr const char* BadTypeName = "BadType";
65 }
66
67 std::map<std::string, unsigned int> Type::typemap;
68 std::vector<TypeData*> Type::typedata;
69 std::set<std::string> Type::loadModuleSet;
70
71 const Type Type::BadType;
72
73 Type::instantiationMethod Type::getInstantiationMethod() const
74 {
75 assert(typedata.size() >= 1 && "Type::init() must be called before creating instances");
76 assert(typedata.size() > index && "Type index out of bounds");
77 if (isBad() || typedata.size() <= index) {
78 return nullptr;
79 }
80 return typedata[index]->instMethod;
81 }
82
83 void* Type::createInstance() const
84 {
85 const auto method = getInstantiationMethod();
86 return method ? (*method)() : nullptr;
87 }
88
89 bool Type::canInstantiate() const
90 {
91 const auto method = getInstantiationMethod();
92 return method != nullptr;
93 }
94
95 void* Type::createInstanceByName(const char* typeName, bool loadModule)
96 {
97 // if not already, load the module
98 if (loadModule) {
99 importModule(typeName);
100 }
101
102 // now the type should be in the type map
103 const Type type = fromName(typeName);
104 // let createInstance handle isBad check
105 return type.createInstance();
106 }
107
108 void Type::importModule(const char* typeName)
109 {
110 // cut out the module name
111 const std::string mod = getModuleName(typeName);
112
113 // ignore base modules
114 if (mod == "App" || mod == "Gui" || mod == "Base") {
115 return;
116 }
117
118 // remember already loaded modules
119 if (loadModuleSet.contains(mod)) {
120 return;
121 }
122
123 // lets load the module
124 Interpreter().loadModule(mod.c_str());
125 #ifdef FC_LOGLOADMODULE
126 Console().log("Act: Module %s loaded through class %s \n", mod.c_str(), typeName);
127 #endif
128 loadModuleSet.insert(mod);
129 }
130
131 const std::string Type::getModuleName(const char* className)
132 {
133 std::string_view classNameView(className);
134 auto pos = classNameView.find("::");
135
136 return pos != std::string_view::npos ? std::string(classNameView.substr(0, pos)) : std::string();
137 }
138
139
140 const Type Type::createType(const Type parent, const char* name, instantiationMethod method)
141 {
142 assert(name && name[0] != '\0' && "Type name must not be empty");
143
144 Type newType;
145 newType.index = static_cast<unsigned int>(Type::typedata.size());
146 Type::typedata.emplace_back(new TypeData(name, newType, parent, method));
147
148 // add to dictionary for fast lookup
149 Type::typemap.emplace(name, newType.getKey());
150
151 return newType;
152 }
153
154
155 void Type::init()
156 {
157 assert(Type::typedata.size() == 0 && "Type::init() should only be called once");
158 typedata.emplace_back(new TypeData(BadTypeName, BadType, BadType, nullptr));
159 typemap[BadTypeName] = 0;
160 }
161
162 void Type::destruct()
163 {
164 for (auto it : typedata) {
165 delete it;
166 }
167 typedata.clear();
168 typemap.clear();
169 loadModuleSet.clear();
170 }
171
172 const Type Type::fromName(const char* name)
173 {
174 const auto pos = typemap.find(name);
175 if (pos == typemap.end()) {
176 return Type::BadType;
177 }
178
179 return typedata[pos->second]->type;
180 }
181
182 const Type Type::fromKey(TypeId key)
183 {
184 if (key < typedata.size()) {
185 return typedata[key]->type;
186 }
187
188 return BadType;
189 }
190
191 const char* Type::getName() const
192 {
193 assert(
194 typedata.size() >= 1 && "Type::init() must be called before fetching names, even for bad types"
195 );
196 return typedata[index]->name.c_str();
197 }
198
199 const Type Type::getParent() const
200 {
201 assert(
202 typedata.size() >= 1 && "Type::init() must be called before fetching parents, even for bad types"
203 );
204 return typedata[index]->parent;
205 }
206
207 bool Type::isDerivedFrom(const Type type) const
208 {
209 Type temp(*this);
210 do {
211 if (temp == type) {
212 return true;
213 }
214 temp = temp.getParent();
215 } while (!temp.isBad());
216
217 return false;
218 }
219
220 int Type::getAllDerivedFrom(const Type type, std::vector<Type>& list)
221 {
222 int cnt = 0;
223
224 for (auto it : typedata) {
225 if (it->type.isDerivedFrom(type)) {
226 list.push_back(it->type);
227 cnt++;
228 }
229 }
230 return cnt;
231 }
232
233 int Type::getNumTypes()
234 {
235 return static_cast<int>(typedata.size());
236 }
237
238 const Type Type::getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule)
239 {
240 if (loadModule) {
241 importModule(name);
242 }
243
244 if (const Type type(fromName(name)); type.isDerivedFrom(parent)) {
245 return type;
246 }
247
248 return BadType;
249 }