Debian Codegraph @

src/Base/BaseClass.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_BASECLASS_H
27 #define BASE_BASECLASS_H
28
29 #include "Type.h"
30
31 // Python stuff
32 using PyObject = struct _object;
33
34
35 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
36 /// define for subclassing Base::BaseClass
37 #define TYPESYSTEM_HEADER() \
38 public: \
39 static Base::Type getClassTypeId(void); \
40 virtual Base::Type getTypeId(void) const; \
41 static void init(void); \
42 static void* create(void); \
43 \
44 private: \
45 static Base::Type classTypeId
46
47
48 /// Like TYPESYSTEM_HEADER, but declare getTypeId as 'override'
49 #define TYPESYSTEM_HEADER_WITH_OVERRIDE() \
50 public: \
51 static Base::Type getClassTypeId(void); \
52 Base::Type getTypeId(void) const override; \
53 static void init(void); \
54 static void* create(void); \
55 \
56 private: \
57 static Base::Type classTypeId
58
59
60 /// define to implement a subclass of Base::BaseClass
61 #define TYPESYSTEM_SOURCE_P(_class_) \
62 Base::Type _class_::getClassTypeId(void) \
63 { \
64 return _class_::classTypeId; \
65 } \
66 Base::Type _class_::getTypeId(void) const \
67 { \
68 return _class_::classTypeId; \
69 } \
70 Base::Type _class_::classTypeId = Base::Type::BadType; \
71 void* _class_::create(void) \
72 { \
73 return new _class_(); \
74 }
75
76 /// define to implement a subclass of Base::BaseClass
77 #define TYPESYSTEM_SOURCE_TEMPLATE_P(_class_) \
78 template<> \
79 Base::Type _class_::getClassTypeId(void) \
80 { \
81 return _class_::classTypeId; \
82 } \
83 template<> \
84 Base::Type _class_::getTypeId(void) const \
85 { \
86 return _class_::classTypeId; \
87 } \
88 template<> \
89 void* _class_::create(void) \
90 { \
91 return new _class_(); \
92 }
93
94 /// define to implement a subclass of Base::BaseClass
95 #define TYPESYSTEM_SOURCE_ABSTRACT_P(_class_) \
96 Base::Type _class_::getClassTypeId(void) \
97 { \
98 return _class_::classTypeId; \
99 } \
100 Base::Type _class_::getTypeId(void) const \
101 { \
102 return _class_::classTypeId; \
103 } \
104 Base::Type _class_::classTypeId = Base::Type::BadType; \
105 void* _class_::create(void) \
106 { \
107 return nullptr; \
108 }
109
110
111 /// define to implement a subclass of Base::BaseClass
112 #define TYPESYSTEM_SOURCE(_class_, _parentclass_) \
113 TYPESYSTEM_SOURCE_P(_class_) \
114 void _class_::init(void) \
115 { \
116 initSubclass(_class_::classTypeId, #_class_, #_parentclass_, &(_class_::create)); \
117 }
118
119 /// define to implement a subclass of Base::BaseClass
120 #define TYPESYSTEM_SOURCE_TEMPLATE_T(_class_, _parentclass_) \
121 TYPESYSTEM_SOURCE_TEMPLATE_P(_class_) \
122 template<> \
123 void _class_::init(void) \
124 { \
125 initSubclass(_class_::classTypeId, #_class_, #_parentclass_, &(_class_::create)); \
126 }
127
128 /// define to implement a subclass of Base::BaseClass
129 #define TYPESYSTEM_SOURCE_ABSTRACT(_class_, _parentclass_) \
130 TYPESYSTEM_SOURCE_ABSTRACT_P(_class_) \
131 void _class_::init(void) \
132 { \
133 initSubclass(_class_::classTypeId, #_class_, #_parentclass_, nullptr); \
134 }
135 // NOLINTEND(cppcoreguidelines-macro-usage)
136
137 namespace Base
138 {
139 /// BaseClass class and root of the type system
140 class BaseExport BaseClass
141 {
142 public:
143 static Type getClassTypeId();
144 virtual Type getTypeId() const;
145 bool isDerivedFrom(const Type type) const
146 {
147 return getTypeId().isDerivedFrom(type);
148 }
149
150 static void init();
151
152 virtual PyObject* getPyObject();
153 virtual void setPyObject(PyObject*);
154
155 static void* create()
156 {
157 return nullptr;
158 }
159
160 template<typename T>
161 bool is() const;
162
163 template<typename T>
164 bool isDerivedFrom() const;
165
166 private:
167 static Type classTypeId; // NOLINT
168
169 protected:
170 static void initSubclass(
171 Base::Type& toInit,
172 const char* ClassName,
173 const char* ParentName,
174 Type::instantiationMethod method = nullptr
175 );
176
177 public:
178 /// Construction
179 BaseClass();
180 BaseClass(const BaseClass&) = default;
181 BaseClass& operator=(const BaseClass&) = default;
182 BaseClass(BaseClass&&) = default;
183 BaseClass& operator=(BaseClass&&) = default;
184 /// Destruction
185 virtual ~BaseClass();
186 };
187
188 template<typename T>
189 bool BaseClass::is() const
190 {
191 static_assert(std::is_base_of_v<BaseClass, T>, "T must be derived from Base::BaseClass");
192 return getTypeId() == T::getClassTypeId();
193 }
194
195 template<typename T>
196 bool BaseClass::isDerivedFrom() const
197 {
198 static_assert(std::is_base_of_v<BaseClass, T>, "T must be derived from Base::BaseClass");
199 return getTypeId().isDerivedFrom(T::getClassTypeId());
200 }
201
202 /**
203 * Template that works just like dynamic_cast, but expects the argument to
204 * inherit from Base::BaseClass.
205 */
206 template<typename T, typename U = std::remove_pointer_t<T>>
207 requires(std::is_pointer_v<T>)
208 T freecad_cast(Base::BaseClass* type)
209 {
210 static_assert(std::is_base_of_v<Base::BaseClass, U>, "T must be derived from Base::BaseClass");
211
212 if (type && type->isDerivedFrom(U::getClassTypeId())) {
213 return static_cast<T>(type);
214 }
215
216 return nullptr;
217 }
218
219 /**
220 * Template that works just like dynamic_cast, but expects the argument to
221 * inherit from a const Base::BaseClass.
222 */
223 template<typename T, typename U = std::remove_pointer_t<T>>
224 requires(std::is_pointer_v<T>)
225 const U* freecad_cast(const Base::BaseClass* type)
226 {
227 static_assert(std::is_base_of_v<Base::BaseClass, U>, "T must be derived from Base::BaseClass");
228
229 if (type && type->isDerivedFrom(U::getClassTypeId())) {
230 return static_cast<const U*>(type);
231 }
232
233 return nullptr;
234 }
235
236 } // namespace Base
237
238 // We define global alias for freecad_cast to be used by all FreeCAD files that include
239 // BaseClass.h. While doing using on header level is non-ideal it allows for much easier use
240 // of the important freecad_cast. In that case the name is prefixed with freecad so there is no
241 // chance of symbols collision.
242 using Base::freecad_cast;
243
244 #endif // BASE_BASECLASS_H