Debian Codegraph @

src/App/ExtensionContainer.h

raw
1 /***************************************************************************
2 * Copyright (c) 2016 Stefan Tröger <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
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. *
10 * *
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. *
15 * *
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 *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef SRC_APP_EXTENSIONCONTAINER_H_
25 #define SRC_APP_EXTENSIONCONTAINER_H_
26
27 #include <vector>
28 #include <string>
29 #include <map>
30
31 #include "PropertyContainer.h"
32
33
34 namespace App
35 {
36
37 class Extension;
38
39 /**
40 * @brief A container that can hold extensions.
41 * @ingroup ExtensionFramework
42 *
43 * This class provides a container for extensions. It is used to hold
44 * extensions of a document object. For a more high-level discussion of
45 * extensions see the topic @ref ExtensionFramework "Extension Framework".
46 */
47 class AppExport ExtensionContainer: public App::PropertyContainer
48 {
49
50 TYPESYSTEM_HEADER_WITH_OVERRIDE();
51
52 public:
53 /// A type alias for iterating extensions.
54 using ExtensionIterator = std::map<Base::Type, App::Extension*>::iterator;
55
56 /// Construct an extension container.
57 ExtensionContainer();
58 /// Destruct an extension container.
59 ~ExtensionContainer() override;
60
61 /**
62 * @brief Register an extension.
63 *
64 * @param[in] extension The type of the extension to register.
65 * @param[in] ext The extension to register.
66 */
67 void registerExtension(Base::Type extension, App::Extension* ext);
68
69 /**
70 * @brief Whether this container has an extension of the given type.
71 *
72 * This version checks for derived types. It returns the first extension
73 * of the given type or the first extension that is derived from the type
74 * if @p derived is true.
75 *
76 * @param[in] type The type of the extension to check for.
77 * @param[in] derived (Optionally) Whether to check for derived types, true by default.
78 *
79 * @return True if the container has the (derived) extension, false otherwise.
80 */
81 bool hasExtension(Base::Type type, bool derived = true) const;
82
83 /**
84 * @brief Whether this container has an extension of the given type.
85 *
86 * This version checks for derived types. It returns the first extension
87 * of the given type. Note that this function does not check for derived
88 * types.
89 *
90 * @param[in] name The name of the extension to check for.
91 * @return True if the container has the extension, false otherwise.
92 */
93 bool hasExtension(const std::string& name) const;
94
95 /**
96 * @brief Whether this container has extensions.
97 *
98 * @return True if the container has extensions, false otherwise.
99 */
100 bool hasExtensions() const;
101
102 /**
103 * @brief Get the extension of the given type.
104 *
105 * This version checks for derived types. It returns the first extension
106 * of the given type or the first extension that is derived from the type
107 * if @p derived is true. If @p no_except is true, it returns nullptr,
108 *otherwise it throws an exception if no extension of the given type is
109 * found.
110 *
111 * @param[in] type The type of the extension to get.
112 * @param[in] derived (Optionally) Whether to check for derived types, true by default.
113 * @param[in] no_except (Optionally) Whether to throw an exception if no extension is found,
114 * false by default.
115 *
116 * @return The extension of the given type or `nullptr` if not found.
117 * @throws Base::TypeError if no extension of the given type is found and
118 * @p no_except is false.
119 */
120 App::Extension* getExtension(Base::Type type, bool derived = true, bool no_except = false) const;
121
122 /**
123 * @brief Get the extension with the given name.
124 *
125 * This version does not check for derived types.
126 *
127 * @param[in] name The name of the extension to get.
128 *
129 * @return The extension with the given name or `nullptr` if not found.
130 */
131 App::Extension* getExtension(const std::string& name) const;
132
133 /**
134 * @brief Get the extension of the given type.
135 *
136 * This version checks for derived types. It returns the first extension
137 * of the given type or the first extension that is derived from the type.
138 * It doesn't throw an exception but returns `nullptr` if not found.
139 *
140 * @tparam ExtensionT The type of the extension to get.
141 * @return The extension of the given type or `nullptr` if not found.
142 */
143 template<typename ExtensionT>
144 ExtensionT* getExtension() const
145 {
146 return static_cast<ExtensionT*>(
147 getExtension(ExtensionT::getExtensionClassTypeId(), true, true));
148 }
149
150 /**
151 * @brief Get the extension with the given type.
152 *
153 * This version checks for derived types if @p derived is true. It returns the first
154 * extension of the given type or the first extension that is derived from the type
155 * if @p derived is true. If not found, it returns `nullptr` if @p no_except is true,
156 * otherwise it throws an exception.
157 *
158 * @tparam ExtensionT The type of the extension to get.
159 *
160 * @param[in] no_except (Optionally) Whether to throw an exception if no extension is found,
161 * false by default.
162 * @param[in] derived (Optionally) Whether to check for derived types, true by default.
163 *
164 * @return The extension of the given type or `nullptr` if not found if @p no_except is true.
165 * @throws Base::TypeError if no extension of the given type is found and
166 * @p no_except is false.
167 */
168 template<typename ExtensionT>
169 ExtensionT* getExtensionByType(bool no_except = false, bool derived = true) const
170 {
171 return static_cast<ExtensionT*>(
172 getExtension(ExtensionT::getExtensionClassTypeId(), derived, no_except));
173 }
174
175 /**
176 * @brief Get all extensions with the given type as base class.
177 *
178 * @param[in] type The type of the extension to get.
179 * @return A vector of extensions of the given type.
180 */
181 std::vector<Extension*> getExtensionsDerivedFrom(Base::Type type) const;
182
183 /**
184 * @brief Get all extensions with the given type as base class.
185 *
186 * @tparam ExtensionT The type of the extension to get.
187 * @return A vector of extensions of the given type.
188 */
189 template<typename ExtensionT>
190 std::vector<ExtensionT*> getExtensionsDerivedFromType() const
191 {
192 std::vector<ExtensionT*> typevec;
193 for (const auto& entry : _extensions) {
194 if (entry.first.isDerivedFrom(ExtensionT::getExtensionClassTypeId())) {
195 typevec.push_back(static_cast<ExtensionT*>(entry.second));
196 }
197 }
198 return typevec;
199 }
200
201 /**
202 * @brief Get the begin iterator for the extensions.
203 *
204 * @return The begin iterator for the extensions.
205 */
206 ExtensionIterator extensionBegin()
207 {
208 return _extensions.begin();
209 }
210
211 /**
212 * @brief Get the end iterator for the extensions.
213 *
214 * @return The end iterator for the extensions.
215 */
216 ExtensionIterator extensionEnd()
217 {
218 return _extensions.end();
219 }
220
221
222 /** @name Access properties
223 *
224 * @{
225 */
226
227 Property* getPropertyByName(const char* name) const override;
228
229 /**
230 * @brief Find a property by its name and cast it to the specified type.
231 *
232 * This method finds a property by its name and casts it to the specified
233 * type with a `freecad_cast`.
234 *
235 * @tparam T The property type to which the property is cast.
236 * @param[in] name The name of the property to find.
237 * @return The property if found cast to the specified type, or `nullptr` if not found.
238 */
239 template<typename T>
240 T* getPropertyByName(const char* name) const {
241 return freecad_cast<T*>(this->getPropertyByName(name));
242 }
243
244 const char* getPropertyName(const Property* prop) const override;
245
246 void getPropertyMap(std::map<std::string, Property*>& Map) const override;
247
248 void visitProperties(const std::function<void(Property*)>& visitor) const override;
249
250 void getPropertyList(std::vector<Property*>& List) const override;
251
252 short getPropertyType(const Property* prop) const override;
253
254 short getPropertyType(const char* name) const override;
255
256 const char* getPropertyGroup(const Property* prop) const override;
257
258 const char* getPropertyGroup(const char* name) const override;
259
260 const char* getPropertyDocumentation(const Property* prop) const override;
261
262 const char* getPropertyDocumentation(const char* name) const override;
263 ///@}
264
265 void onChanged(const Property*) override;
266
267 void Save(Base::Writer& writer) const override;
268
269 void Restore(Base::XMLReader& reader) override;
270
271 // those methods save/restore the dynamic extensions without handling properties, which is
272 // something done by the default Save/Restore methods.
273 /**
274 * @brief Save the extensions to the given writer.
275 *
276 * This method saves the dynamic extensions to the given writer without
277 * handling properties, which is something done by the default Save/Restore
278 * methods.
279 *
280 * @param[in,out] writer The writer to save the extensions to.
281 */
282 void saveExtensions(Base::Writer& writer) const;
283
284 /**
285 * @brief Restore the extensions from the given reader.
286 *
287 * This method restores the dynamic extensions from the given reader
288 * without handling properties, which is something done by the default
289 * Save/Restore methods.
290 *
291 * @param[in,out] reader The reader to restore the extensions from.
292 */
293 void restoreExtensions(Base::XMLReader& reader);
294
295 /**
296 * @brief Handle a changed property name during restore.
297 *
298 * This method extends the rules for handling property name changed, so
299 * that extensions are given an opportunity to handle it. If an extension
300 * handles a change, neither the rest of the extensions, nor the container
301 * itself get to handle it.
302 *
303 * Extensions get their Extension::extensionHandleChangedPropertyName()
304 * called.
305 *
306 * If no extension handles the request, then
307 * PropertyContainer::handleChangedPropertyName() is called.
308 *
309 * @param[in,out] reader The reader to restore the extensions from.
310 * @param[in] TypeName The name of the type of the property to handle.
311 * @param[in] PropName The name of the property to handle.
312 */
313 void handleChangedPropertyName(Base::XMLReader& reader,
314 const char* TypeName,
315 const char* PropName) override;
316
317 /**
318 * @brief Handle a changed property type during restore.
319 *
320 * This method extends the rules for handling property type changed, so
321 * that extensions are given an opportunity to handle it. If an extension
322 * handles a change, neither the rest of the extensions, nor the container
323 * itself get to handle it.
324 *
325 * Extensions get their Extension::extensionHandleChangedPropertyType() called.
326 *
327 * If no extension handles the request, then
328 * PropertyContainer::handleChangedPropertyType() is called.
329 *
330 * @param[in,out] reader The reader to restore the extensions from.
331 * @param[in] TypeName The name of the type of the property to handle.
332 * @param[in] prop The property that needs to be restored. Its type differs from `TypeName`.
333 */
334 void handleChangedPropertyType(Base::XMLReader& reader,
335 const char* TypeName,
336 Property* prop) override;
337
338 private:
339 // stored extensions
340 std::map<Base::Type, App::Extension*> _extensions;
341 };
342
343 #define PROPERTY_HEADER_WITH_EXTENSIONS(_class_) PROPERTY_HEADER_WITH_OVERRIDE(_class_)
344
345 /// We make sure that the PropertyData of the container is not connected to the one of the extension
346 #define PROPERTY_SOURCE_WITH_EXTENSIONS(_class_, _parentclass_) \
347 PROPERTY_SOURCE(_class_, _parentclass_)
348
349 #define PROPERTY_SOURCE_ABSTRACT_WITH_EXTENSIONS(_class_, _parentclass_) \
350 PROPERTY_SOURCE_ABSTRACT(_class_, _parentclass_)
351
352 } // namespace App
353
354 #endif // SRC_APP_EXTENSIONCONTAINER_H_