| 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_EXTENSION_H_ |
| 25 |
#define SRC_APP_EXTENSION_H_ |
| 26 |
|
| 27 |
#include <map> |
| 28 |
#include <string> |
| 29 |
|
| 30 |
#include "PropertyContainer.h" |
| 31 |
#include <Base/SmartPtrPy.h> |
| 32 |
|
| 33 |
namespace App { |
| 34 |
|
| 35 |
class ExtensionContainer; |
| 36 |
|
| 37 |
// clang-format off |
| 38 |
/// define Extension types |
| 39 |
#define EXTENSION_TYPESYSTEM_HEADER() \ |
| 40 |
public: \ |
| 41 |
static Base::Type getExtensionClassTypeId(void); \ |
| 42 |
virtual Base::Type getExtensionTypeId(void) const; \ |
| 43 |
static void init(void);\ |
| 44 |
static void *create(void);\ |
| 45 |
private: \ |
| 46 |
static Base::Type classTypeId |
| 47 |
|
| 48 |
/// Like EXTENSION_TYPESYSTEM_HEADER, with getExtensionTypeId declared override |
| 49 |
#define EXTENSION_TYPESYSTEM_HEADER_WITH_OVERRIDE() \ |
| 50 |
public: \ |
| 51 |
static Base::Type getExtensionClassTypeId(void); \ |
| 52 |
Base::Type getExtensionTypeId(void) const override; \ |
| 53 |
static void init(void);\ |
| 54 |
static void *create(void);\ |
| 55 |
private: \ |
| 56 |
static Base::Type classTypeId |
| 57 |
|
| 58 |
/// define to implement a subclass of Base::BaseClass |
| 59 |
#define EXTENSION_TYPESYSTEM_SOURCE_P(_class_) \ |
| 60 |
Base::Type _class_::getExtensionClassTypeId(void) { return _class_::classTypeId; } \ |
| 61 |
Base::Type _class_::getExtensionTypeId(void) const { return _class_::classTypeId; } \ |
| 62 |
Base::Type _class_::classTypeId = Base::Type::BadType; \ |
| 63 |
void * _class_::create(void){\ |
| 64 |
return new _class_ ();\ |
| 65 |
} |
| 66 |
|
| 67 |
/// define to implement a subclass of Base::BaseClass |
| 68 |
#define EXTENSION_TYPESYSTEM_SOURCE_ABSTRACT_P(_class_) \ |
| 69 |
Base::Type _class_::getExtensionClassTypeId(void) { return _class_::classTypeId; } \ |
| 70 |
Base::Type _class_::getExtensionTypeId(void) const { return _class_::classTypeId; } \ |
| 71 |
Base::Type _class_::classTypeId = Base::Type::BadType; \ |
| 72 |
void * _class_::create(void){return 0;} |
| 73 |
|
| 74 |
/// define to implement a subclass of Base::BaseClass |
| 75 |
#define EXTENSION_TYPESYSTEM_SOURCE(_class_, _parentclass_) \ |
| 76 |
EXTENSION_TYPESYSTEM_SOURCE_P(_class_)\ |
| 77 |
void _class_::init(void){\ |
| 78 |
initExtensionSubclass(_class_::classTypeId, #_class_ , #_parentclass_, &(_class_::create) ); \ |
| 79 |
} |
| 80 |
|
| 81 |
#define EXTENSION_TYPESYSTEM_SOURCE_TEMPLATE(_class_) \ |
| 82 |
template<> Base::Type _class_::classTypeId = Base::Type::BadType; \ |
| 83 |
template<> Base::Type _class_::getExtensionClassTypeId(void) { return _class_::classTypeId; } \ |
| 84 |
template<> Base::Type _class_::getExtensionTypeId(void) const { return _class_::classTypeId; } \ |
| 85 |
template<> void * _class_::create(void){\ |
| 86 |
return new _class_ ();\ |
| 87 |
} |
| 88 |
|
| 89 |
// init property stuff |
| 90 |
#define EXTENSION_PROPERTY_HEADER(_class_) \ |
| 91 |
EXTENSION_TYPESYSTEM_HEADER(); \ |
| 92 |
protected: \ |
| 93 |
static const App::PropertyData * extensionGetPropertyDataPtr(void); \ |
| 94 |
virtual const App::PropertyData &extensionGetPropertyData(void) const; \ |
| 95 |
private: \ |
| 96 |
static App::PropertyData propertyData |
| 97 |
|
| 98 |
/// Like EXTENSION_PROPERTY_HEADER, but with override declarations. |
| 99 |
#define EXTENSION_PROPERTY_HEADER_WITH_OVERRIDE(_class_) \ |
| 100 |
EXTENSION_TYPESYSTEM_HEADER_WITH_OVERRIDE(); \ |
| 101 |
protected: \ |
| 102 |
static const App::PropertyData * extensionGetPropertyDataPtr(void); \ |
| 103 |
const App::PropertyData &extensionGetPropertyData(void) const override; \ |
| 104 |
private: \ |
| 105 |
static App::PropertyData propertyData |
| 106 |
|
| 107 |
#define EXTENSION_PROPERTY_SOURCE(_class_, _parentclass_) \ |
| 108 |
EXTENSION_TYPESYSTEM_SOURCE_P(_class_)\ |
| 109 |
const App::PropertyData * _class_::extensionGetPropertyDataPtr(void){return &propertyData;} \ |
| 110 |
const App::PropertyData & _class_::extensionGetPropertyData(void) const{return propertyData;} \ |
| 111 |
App::PropertyData _class_::propertyData; \ |
| 112 |
void _class_::init(void){\ |
| 113 |
initExtensionSubclass(_class_::classTypeId, #_class_ , #_parentclass_, &(_class_::create) ); \ |
| 114 |
_class_::propertyData.parentPropertyData = _parentclass_::extensionGetPropertyDataPtr();\ |
| 115 |
} |
| 116 |
|
| 117 |
#define EXTENSION_PROPERTY_SOURCE_TEMPLATE(_class_, _parentclass_) \ |
| 118 |
EXTENSION_TYPESYSTEM_SOURCE_TEMPLATE(_class_)\ |
| 119 |
template<> App::PropertyData _class_::propertyData = App::PropertyData(); \ |
| 120 |
template<> const App::PropertyData * _class_::extensionGetPropertyDataPtr(void){return &propertyData;} \ |
| 121 |
template<> const App::PropertyData & _class_::extensionGetPropertyData(void) const{return propertyData;} \ |
| 122 |
template<> void _class_::init(void){\ |
| 123 |
initExtensionSubclass(_class_::classTypeId, #_class_ , #_parentclass_, &(_class_::create) ); \ |
| 124 |
_class_::propertyData.parentPropertyData = _parentclass_::extensionGetPropertyDataPtr();\ |
| 125 |
} |
| 126 |
// clang-format on |
| 127 |
|
| 128 |
/** |
| 129 |
* @brief A base class for extensions for a DocumentObject. |
| 130 |
* @ingroup ExtensionFramework |
| 131 |
* |
| 132 |
* This class provides a base class for extensions for a DocumentObject. It is |
| 133 |
* used to hold extensions of a document object. For a more high-level |
| 134 |
* discussion of extensions see the topic @ref ExtensionFramework "Extension Framework". |
| 135 |
*/ |
| 136 |
class AppExport Extension |
| 137 |
{ |
| 138 |
|
| 139 |
//The cass does not have properties itself, but it is important to provide the property access |
| 140 |
//functions. see cpp file for details |
| 141 |
EXTENSION_PROPERTY_HEADER(App::Extension); |
| 142 |
|
| 143 |
public: |
| 144 |
/// Construct an extension object. |
| 145 |
Extension() = default; |
| 146 |
/// Destruct an extension object. |
| 147 |
virtual ~Extension(); |
| 148 |
|
| 149 |
/** |
| 150 |
* @brief Initialize the extension. |
| 151 |
* |
| 152 |
* This function is called by the ExtensionContainer to initialize the |
| 153 |
* extension. |
| 154 |
* |
| 155 |
* @param[in,out] obj The ExtensionContainer object to which this extension |
| 156 |
* is attached. |
| 157 |
* |
| 158 |
* @throw Base::RuntimeError If the extension type is not set. |
| 159 |
*/ |
| 160 |
virtual void initExtension(App::ExtensionContainer* obj); |
| 161 |
|
| 162 |
/** |
| 163 |
* @brief Get the container of the extension. |
| 164 |
* |
| 165 |
* @return The ExtensionContainer object to which this extension belongs. |
| 166 |
*/ |
| 167 |
App::ExtensionContainer* getExtendedContainer() {return m_base;} |
| 168 |
|
| 169 |
/** |
| 170 |
* @brief Get the container of the extension. |
| 171 |
* |
| 172 |
* @return The ExtensionContainer object to which this extension belongs. |
| 173 |
*/ |
| 174 |
const App::ExtensionContainer* getExtendedContainer() const {return m_base;} |
| 175 |
|
| 176 |
/** |
| 177 |
* @brief Get the name of the extension. |
| 178 |
* |
| 179 |
* The name is the class name of the extension without the namespace. |
| 180 |
* |
| 181 |
* @return The name of the extension. |
| 182 |
*/ |
| 183 |
std::string name() const; |
| 184 |
|
| 185 |
/** |
| 186 |
* @brief Whether the extension is a Python extension. |
| 187 |
* |
| 188 |
* @return True if the extension is a Python extension, false otherwise. |
| 189 |
*/ |
| 190 |
bool isPythonExtension() {return m_isPythonExtension;} |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief Get the PyObject of the extension. |
| 194 |
* |
| 195 |
* @return The PyObject of the extension. |
| 196 |
*/ |
| 197 |
virtual PyObject* getExtensionPyObject(); |
| 198 |
|
| 199 |
|
| 200 |
/** @name Access properties |
| 201 |
* |
| 202 |
* @{ |
| 203 |
*/ |
| 204 |
|
| 205 |
/** |
| 206 |
* @copydoc PropertyContainer::getPropertyByName() |
| 207 |
*/ |
| 208 |
virtual Property *extensionGetPropertyByName(const char* name) const; |
| 209 |
|
| 210 |
/** |
| 211 |
* @copydoc PropertyContainer::getPropertyName() |
| 212 |
*/ |
| 213 |
virtual const char* extensionGetPropertyName(const Property* prop) const; |
| 214 |
|
| 215 |
/** |
| 216 |
* @copydoc PropertyContainer::getPropertyMap() |
| 217 |
*/ |
| 218 |
virtual void extensionGetPropertyMap(std::map<std::string,Property*> &map) const; |
| 219 |
|
| 220 |
/** |
| 221 |
* @copydoc PropertyContainer::visitProperties() |
| 222 |
*/ |
| 223 |
virtual void extensionVisitProperties(const std::function<void(Property*)>& visitor) const; |
| 224 |
|
| 225 |
/** |
| 226 |
* @copydoc PropertyContainer::getPropertyList() |
| 227 |
*/ |
| 228 |
virtual void extensionGetPropertyList(std::vector<Property*> &List) const; |
| 229 |
|
| 230 |
/** |
| 231 |
* @copydoc PropertyContainer::getPropertyType(const Property*) const |
| 232 |
*/ |
| 233 |
virtual short extensionGetPropertyType(const Property* prop) const; |
| 234 |
|
| 235 |
/** |
| 236 |
* @copydoc PropertyContainer::getPropertyType(const char*) const |
| 237 |
*/ |
| 238 |
virtual short extensionGetPropertyType(const char *name) const; |
| 239 |
|
| 240 |
/** |
| 241 |
* @copydoc PropertyContainer::getPropertyGroup(const Property*) const |
| 242 |
*/ |
| 243 |
virtual const char* extensionGetPropertyGroup(const Property* prop) const; |
| 244 |
|
| 245 |
/** |
| 246 |
* @copydoc PropertyContainer::getPropertyGroup(const char*) const |
| 247 |
*/ |
| 248 |
virtual const char* extensionGetPropertyGroup(const char *name) const; |
| 249 |
|
| 250 |
/** |
| 251 |
* @copydoc PropertyContainer::getPropertyDocumentation(const Property*) const |
| 252 |
*/ |
| 253 |
virtual const char* extensionGetPropertyDocumentation(const Property* prop) const; |
| 254 |
|
| 255 |
/** |
| 256 |
* @copydoc PropertyContainer::getPropertyDocumentation(const char*) const |
| 257 |
*/ |
| 258 |
virtual const char* extensionGetPropertyDocumentation(const char *name) const; |
| 259 |
/// @} |
| 260 |
|
| 261 |
/** @name Persistence |
| 262 |
* @{ |
| 263 |
*/ |
| 264 |
|
| 265 |
/** |
| 266 |
* @copydoc Base::Persistence::Save |
| 267 |
*/ |
| 268 |
virtual void extensionSave(Base::Writer&) const {} |
| 269 |
|
| 270 |
/** |
| 271 |
* @copydoc Base::Persistence::Restore |
| 272 |
*/ |
| 273 |
virtual void extensionRestore(Base::XMLReader&) {} |
| 274 |
/// @} |
| 275 |
|
| 276 |
/** |
| 277 |
* @copydoc Base::Type::isDerivedFrom() |
| 278 |
*/ |
| 279 |
bool extensionIsDerivedFrom(const Base::Type type) const {return getExtensionTypeId().isDerivedFrom(type);} |
| 280 |
|
| 281 |
protected: |
| 282 |
/** |
| 283 |
* @brief Helper function to register the parent. |
| 284 |
* |
| 285 |
* This is used in the macros. It creates a new type in @p toInit based on |
| 286 |
* the parent. @p toInit should be an uninitialized type. |
| 287 |
* |
| 288 |
* @param[out] toInit The type to initialize. |
| 289 |
* @param[in] ClassName The name of the class. |
| 290 |
* @param[in] ParentName The name of the parent class. |
| 291 |
* @param[in] method The instantiation method. |
| 292 |
*/ |
| 293 |
static void initExtensionSubclass(Base::Type &toInit,const char* ClassName, const char *ParentName, |
| 294 |
Base::Type::instantiationMethod method=nullptr); |
| 295 |
|
| 296 |
/** |
| 297 |
* @copydoc PropertyContainer::onChanged() |
| 298 |
*/ |
| 299 |
virtual void extensionOnChanged([[maybe_unused]] const Property* prop) {} |
| 300 |
|
| 301 |
/** |
| 302 |
* @copydoc PropertyContainer::handleChangedPropertyName() |
| 303 |
* |
| 304 |
* @return True if the property name change was handled by the extension, |
| 305 |
* false otherwise. |
| 306 |
*/ |
| 307 |
virtual bool extensionHandleChangedPropertyName(Base::XMLReader &reader, const char * typeName, const char *propName); |
| 308 |
|
| 309 |
/** |
| 310 |
* @copydoc PropertyContainer::handleChangedPropertyType() |
| 311 |
* |
| 312 |
* @return True if the property type change was handled by the extension, |
| 313 |
* false otherwise. |
| 314 |
*/ |
| 315 |
virtual bool extensionHandleChangedPropertyType(Base::XMLReader &reader, const char * typeName, Property * prop); |
| 316 |
|
| 317 |
/// Provide access to the ExtensionContainer. |
| 318 |
friend class App::ExtensionContainer; |
| 319 |
|
| 320 |
protected: |
| 321 |
/** |
| 322 |
* @brief Initialize the extension type. |
| 323 |
* |
| 324 |
* This function is to be called by the constructor to initialize the |
| 325 |
* extension type. |
| 326 |
* |
| 327 |
* @param[in] type The type of the extension. |
| 328 |
* |
| 329 |
* @throw Base::RuntimeError If the extension type is not set. |
| 330 |
*/ |
| 331 |
void initExtensionType(Base::Type type); |
| 332 |
|
| 333 |
/// Whether the extension is a Python extension. |
| 334 |
bool m_isPythonExtension = false; |
| 335 |
|
| 336 |
/// The Python object of the extension. |
| 337 |
Py::SmartPtr ExtensionPythonObject; |
| 338 |
|
| 339 |
private: |
| 340 |
Base::Type m_extensionType; |
| 341 |
App::ExtensionContainer* m_base = nullptr; |
| 342 |
}; |
| 343 |
|
| 344 |
// clang-format off |
| 345 |
// Property define |
| 346 |
#define _EXTENSION_ADD_PROPERTY(_name, _prop_, _defaultval_) \ |
| 347 |
do { \ |
| 348 |
this->_prop_.setValue _defaultval_;\ |
| 349 |
propertyData.addProperty(static_cast<App::Extension*>(this), _name, &this->_prop_); \ |
| 350 |
} while (0) |
| 351 |
|
| 352 |
|
| 353 |
#define EXTENSION_ADD_PROPERTY(_prop_, _defaultval_) \ |
| 354 |
_EXTENSION_ADD_PROPERTY(#_prop_, _prop_, _defaultval_) |
| 355 |
|
| 356 |
#define _EXTENSION_ADD_PROPERTY_TYPE(_name, _prop_, _defaultval_, _group_,_type_,_Docu_) \ |
| 357 |
do { \ |
| 358 |
this->_prop_.setValue _defaultval_;\ |
| 359 |
propertyData.addProperty(static_cast<App::Extension*>(this), _name, &this->_prop_, (_group_),(_type_),(_Docu_)); \ |
| 360 |
} while (0) |
| 361 |
|
| 362 |
#define EXTENSION_ADD_PROPERTY_TYPE(_prop_, _defaultval_, _group_,_type_,_Docu_) \ |
| 363 |
_EXTENSION_ADD_PROPERTY_TYPE(#_prop_, _prop_, _defaultval_, _group_,_type_,_Docu_) |
| 364 |
// clang-format on |
| 365 |
|
| 366 |
|
| 367 |
} // namespace App |
| 368 |
|
| 369 |
#endif // SRC_APP_EXTENSION_H_ |