Debian Codegraph @

src/Base/Type.h

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
26 #ifndef BASE_TYPE_H
27 #define BASE_TYPE_H
28
29 // Std. configurations
30
31 #include <string>
32 #include <map>
33 #include <set>
34 #include <vector>
35 #ifndef FC_GLOBAL_H
36 # include <FCGlobal.h>
37 #endif
38
39 namespace Base
40 {
41
42 struct TypeData;
43
44
45 /** Type system class
46 Many of the classes in the FreeCAD must have their type
47 information registered before any instances are created (including,
48 but not limited to: App::Feature, App::Property, Gui::ViewProvider
49 ). The use of Type to store this information provides
50 lots of various functionality for working with class hierarchies,
51 comparing class types, instantiating objects from classnames, etc
52 etc.
53
54 It is for instance possible to do things like this:
55
56 \code
57 void getRightFeature(Base::Base * anode)
58 {
59 assert(anode->isDerivedFrom<App::Feature>());
60
61 if (anode->is<Mesh::MeshFeature>()) {
62 // do something..
63 }
64 else if (anode->is<Part::Feature>()) {
65 // do something..
66 }
67 else {
68 Base::Console().warning("getRightFeature", "Unknown feature type %s!\n",
69 anode->getTypeId().getName());
70 }
71 }
72 \endcode
73
74 A notable feature of the Type class is that it is only 16 bits
75 long and therefore should be passed around by value for efficiency
76 reasons.
77
78 One important note about the use of Type to register class
79 information: super classes must be registered before any of their
80 derived classes are.
81 */
82 class BaseExport Type final
83 {
84 public:
85 using TypeId = unsigned int;
86 /// Construction
87 Type(const Type& type) = default;
88 Type(Type&& type) = default;
89 Type() = default;
90 /// Destruction
91 ~Type() = default;
92
93 /// Creates an instance of this type
94 [[nodiscard]] void* createInstance() const;
95 /// Checks whether this type can instantiate
96 [[nodiscard]] bool canInstantiate() const;
97 /// Creates an instance of the named type
98 [[nodiscard]] static void* createInstanceByName(const char* typeName, bool loadModule = false);
99
100 using instantiationMethod = void* (*)();
101
102 /// Returns a type object by name
103 [[nodiscard]] static const Type fromName(const char* name);
104 /// Returns a type object by key
105 [[nodiscard]] static const Type fromKey(TypeId key);
106 /// Returns the name of the type
107 [[nodiscard]] const char* getName() const;
108 /// Returns the parent type
109 [[nodiscard]] const Type getParent() const;
110 /// Checks whether this type is derived from "type"
111 [[nodiscard]] bool isDerivedFrom(const Type type) const;
112 /// Returns all descendants from the given type
113 static int getAllDerivedFrom(const Type type, std::vector<Type>& list);
114 /// Returns the given named type if is derived from parent type, otherwise return bad type
115 [[nodiscard]] static const Type getTypeIfDerivedFrom(
116 const char* name,
117 const Type parent,
118 bool loadModule = false
119 );
120 /// Returns the number of types created so far
121 [[nodiscard]] static int getNumTypes();
122 /// Creates a new type with the given name, parent and instantiation method
123 [[nodiscard]] static const Type createType(
124 const Type parent,
125 const char* name,
126 instantiationMethod method = nullptr
127 );
128 /// Returns the inner index of the type
129 [[nodiscard]] TypeId getKey() const;
130 /// Checks if the type is invalid
131 [[nodiscard]] bool isBad() const;
132
133 Type& operator=(const Type& type) = default;
134 Type& operator=(Type&& type) = default;
135 bool operator==(const Type& type) const;
136 bool operator!=(const Type& type) const;
137
138 bool operator<(const Type& type) const;
139 bool operator<=(const Type& type) const;
140 bool operator>=(const Type& type) const;
141 bool operator>(const Type& type) const;
142
143 static const Type BadType;
144 static void init();
145 static void destruct();
146
147 /// Returns the name of the module the class is defined in
148 static const std::string getModuleName(const char* className);
149
150 private:
151 [[nodiscard]] instantiationMethod getInstantiationMethod() const;
152 static void importModule(const char* TypeName);
153
154 TypeId index {BadTypeIndex};
155
156 static std::map<std::string, TypeId> typemap;
157 static std::vector<TypeData*> typedata; // use pointer to hide implementation details
158 static std::set<std::string> loadModuleSet;
159
160 static constexpr TypeId BadTypeIndex = 0;
161 };
162
163
164 inline Type::TypeId Type::getKey() const
165 {
166 return this->index;
167 }
168
169 inline bool Type::operator!=(const Type& type) const
170 {
171 return (this->getKey() != type.getKey());
172 }
173
174 inline bool Type::operator==(const Type& type) const
175 {
176 return (this->getKey() == type.getKey());
177 }
178
179 inline bool Type::operator<(const Type& type) const
180 {
181 return (this->getKey() < type.getKey());
182 }
183
184 inline bool Type::operator<=(const Type& type) const
185 {
186 return (this->getKey() <= type.getKey());
187 }
188
189 inline bool Type::operator>=(const Type& type) const
190 {
191 return (this->getKey() >= type.getKey());
192 }
193
194 inline bool Type::operator>(const Type& type) const
195 {
196 return (this->getKey() > type.getKey());
197 }
198
199 inline bool Type::isBad() const
200 {
201 return this->index == BadTypeIndex;
202 }
203
204 } // namespace Base
205
206
207 #endif // BASE_TYPE_H