| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2005 Jürgen Riegel <[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_PROPERTYCONTAINER_H_ |
| 25 |
#define SRC_APP_PROPERTYCONTAINER_H_ |
| 26 |
|
| 27 |
#include <map> |
| 28 |
#include <cstring> |
| 29 |
#include <vector> |
| 30 |
#include <string> |
| 31 |
#include <limits> |
| 32 |
#include <Base/Persistence.h> |
| 33 |
|
| 34 |
#include "DynamicProperty.h" |
| 35 |
|
| 36 |
namespace Base { |
| 37 |
class Writer; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
namespace App |
| 42 |
{ |
| 43 |
class Property; |
| 44 |
class PropertyContainer; |
| 45 |
class DocumentObject; |
| 46 |
class Extension; |
| 47 |
|
| 48 |
// clang-format off |
| 49 |
|
| 50 |
/** |
| 51 |
* @brief Flags that define special behaviors for a property. |
| 52 |
* |
| 53 |
* These flags can be combined using bitwise OR to assign multiple attributes |
| 54 |
* to a property. |
| 55 |
*/ |
| 56 |
enum PropertyType |
| 57 |
{ |
| 58 |
/// No special property type. |
| 59 |
Prop_None = 0, |
| 60 |
/// The property is read-only in the editor. |
| 61 |
Prop_ReadOnly = 1, |
| 62 |
/// The property content won't be saved to file, but still saves name, type and status. |
| 63 |
Prop_Transient = 2, |
| 64 |
/// The property is hidden in the editor. |
| 65 |
Prop_Hidden = 4, |
| 66 |
/// A modified property doesn't touch its parent container. |
| 67 |
Prop_Output = 8, |
| 68 |
/// A modified property doesn't touch its container for recompute. |
| 69 |
Prop_NoRecompute = 16, |
| 70 |
/// The property won't be saved to file at all. |
| 71 |
Prop_NoPersist = 32, |
| 72 |
}; |
| 73 |
// clang-format on |
| 74 |
|
| 75 |
struct AppExport PropertyData |
| 76 |
{ |
| 77 |
|
| 78 |
/// @brief Struct to hold the property specification. |
| 79 |
struct PropertySpec |
| 80 |
{ |
| 81 |
/// The name of the property. |
| 82 |
const char * Name; |
| 83 |
/// The group name of the property. |
| 84 |
const char * Group; |
| 85 |
/// The documentation string of the property. |
| 86 |
const char * Docu; |
| 87 |
/// The offset of the property in the container. |
| 88 |
short Offset; |
| 89 |
/// The type of the property. |
| 90 |
short Type; |
| 91 |
|
| 92 |
/** |
| 93 |
* @brief Construct a PropertySpec. |
| 94 |
* |
| 95 |
* @param[in] name The name of the property. |
| 96 |
* @param[in] group The group name of the property. |
| 97 |
* @param[in] doc The documentation string of the property. |
| 98 |
* @param[in] offset The offset of the property in the container. |
| 99 |
* @param[in] type The type of the property. |
| 100 |
*/ |
| 101 |
inline PropertySpec(const char *name, const char *group, const char *doc, short offset, short type) |
| 102 |
:Name(name),Group(group),Docu(doc),Offset(offset),Type(type) |
| 103 |
{} |
| 104 |
}; |
| 105 |
|
| 106 |
//purpose of this struct is to be constructible from all acceptable container types and to |
| 107 |
//be able to return the offset to a property from the accepted containers. This allows you to use |
| 108 |
//one function implementation for multiple container types without losing all type safety by |
| 109 |
//accepting void* |
| 110 |
/** |
| 111 |
* @brief Struct that represents the base for offset calculation. |
| 112 |
* |
| 113 |
* This struct is used to calculate the offset of a property in a container. |
| 114 |
* It can be constructed from either a PropertyContainer or an Extension. |
| 115 |
*/ |
| 116 |
struct OffsetBase |
| 117 |
{ |
| 118 |
/** |
| 119 |
* @brief Construct an OffsetBase from a PropertyContainer. |
| 120 |
* |
| 121 |
* @param[in] container The PropertyContainer to construct from. |
| 122 |
*/ |
| 123 |
// Lint wants these marked explicit, but they are currently used implicitly in enough |
| 124 |
// places that I don't wnt to fix it. Instead we disable the Lint message. |
| 125 |
// NOLINTNEXTLINE(runtime/explicit) |
| 126 |
OffsetBase(const App::PropertyContainer* container) : m_container(container) {} |
| 127 |
/** |
| 128 |
* @brief Construct an OffsetBase from an Extension. |
| 129 |
* |
| 130 |
* @param[in] container The Extension to construct from. |
| 131 |
*/ |
| 132 |
// NOLINTNEXTLINE(runtime/explicit) |
| 133 |
OffsetBase(const App::Extension* container) : m_container(container) {} |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief Get the offset to a property. |
| 137 |
* |
| 138 |
* @param[in] prop The property to get the offset to. |
| 139 |
* @return The offset to the property relative to the base offset. |
| 140 |
*/ |
| 141 |
short int getOffsetTo(const App::Property* prop) const { |
| 142 |
auto *pt = (const char*)prop; |
| 143 |
auto *base = (const char *)m_container; |
| 144 |
if(pt<base || pt>base+std::numeric_limits<short>::max()) |
| 145 |
return -1; |
| 146 |
return (short) (pt-base); |
| 147 |
} |
| 148 |
/** |
| 149 |
* @brief Get the base offset in bytes. |
| 150 |
* |
| 151 |
* @return The base offset in bytes. |
| 152 |
*/ |
| 153 |
char* getOffset() const {return (char*) m_container;} |
| 154 |
|
| 155 |
private: |
| 156 |
const void* m_container; |
| 157 |
}; |
| 158 |
|
| 159 |
// clang-format off |
| 160 |
|
| 161 |
/** |
| 162 |
* @brief A multi index container for holding the property spec. |
| 163 |
* |
| 164 |
* The multi index has the following index: |
| 165 |
* - a sequence, to preserve creation order |
| 166 |
* - hash index on property name |
| 167 |
* - hash index on property pointer offset |
| 168 |
*/ |
| 169 |
mutable bmi::multi_index_container< |
| 170 |
PropertySpec, |
| 171 |
bmi::indexed_by< |
| 172 |
bmi::sequenced<>, |
| 173 |
bmi::hashed_unique< |
| 174 |
bmi::member<PropertySpec, const char*, &PropertySpec::Name>, |
| 175 |
CStringHasher, |
| 176 |
CStringHasher |
| 177 |
>, |
| 178 |
bmi::hashed_unique< |
| 179 |
bmi::member<PropertySpec, short, &PropertySpec::Offset> |
| 180 |
> |
| 181 |
> |
| 182 |
> propertyData; |
| 183 |
// clang-format on |
| 184 |
|
| 185 |
/// Whether the property data is merged with the parent. |
| 186 |
mutable bool parentMerged = false; |
| 187 |
|
| 188 |
/// The parent property data. |
| 189 |
const PropertyData* parentPropertyData; |
| 190 |
|
| 191 |
/** |
| 192 |
* @brief Add a property to the property data. |
| 193 |
* |
| 194 |
* @param[in] offsetBase The base offset to offset the property. |
| 195 |
* @param[in] PropName The name of the property. |
| 196 |
* @param[in] Prop The property to add. |
| 197 |
* @param[in] PropertyGroup The group name of the property. |
| 198 |
* @param[in] Type The type of the property. |
| 199 |
* @param[in] PropertyDocu The documentation string of the property. |
| 200 |
*/ |
| 201 |
void addProperty(OffsetBase offsetBase,const char* PropName, Property *Prop, const char* PropertyGroup= nullptr, PropertyType Type=Prop_None, const char* PropertyDocu=nullptr ); |
| 202 |
|
| 203 |
/** |
| 204 |
* @brief Find a property by its name. |
| 205 |
* |
| 206 |
* @param[in] offsetBase The base offset for the property. |
| 207 |
* @param[in] PropName The name of the property to find. |
| 208 |
* @return The property specification if found; `nullptr` otherwise. |
| 209 |
*/ |
| 210 |
const PropertySpec *findProperty(OffsetBase offsetBase,const char* PropName) const; |
| 211 |
|
| 212 |
/** |
| 213 |
* @brief Find a property by its pointer. |
| 214 |
* |
| 215 |
* @param[in] offsetBase The base offset for the property. |
| 216 |
* @param[in] prop The property to find. |
| 217 |
* @return The property specification if found; `nullptr` otherwise. |
| 218 |
*/ |
| 219 |
const PropertySpec *findProperty(OffsetBase offsetBase,const Property* prop) const; |
| 220 |
|
| 221 |
/** |
| 222 |
* @name PropertyData accessors |
| 223 |
* |
| 224 |
* @details These methods are used to access the property data based on: |
| 225 |
* - the offset base, |
| 226 |
* - either: |
| 227 |
* - a property pointer, or |
| 228 |
* - a property name. |
| 229 |
* @{ |
| 230 |
*/ |
| 231 |
const char* getName (OffsetBase offsetBase,const Property* prop) const; |
| 232 |
short getType (OffsetBase offsetBase,const Property* prop) const; |
| 233 |
short getType (OffsetBase offsetBase,const char* name) const; |
| 234 |
const char* getGroup (OffsetBase offsetBase,const char* name) const; |
| 235 |
const char* getGroup (OffsetBase offsetBase,const Property* prop) const; |
| 236 |
const char* getDocumentation(OffsetBase offsetBase,const char* name) const; |
| 237 |
const char* getDocumentation(OffsetBase offsetBase,const Property* prop) const; |
| 238 |
/// @} |
| 239 |
|
| 240 |
/** |
| 241 |
* @brief Get a property by its name. |
| 242 |
* |
| 243 |
* @param[in] offsetBase The base offset for the property. |
| 244 |
* @param[in] name The name of the property to find. |
| 245 |
* @return The property if found; `nullptr` otherwise. |
| 246 |
*/ |
| 247 |
Property *getPropertyByName(OffsetBase offsetBase,const char* name) const; |
| 248 |
|
| 249 |
/** |
| 250 |
* @brief Get a map of properties. |
| 251 |
* |
| 252 |
* @param[in] offsetBase The base offset for the property. |
| 253 |
* @param[out] Map A map of property names to properties. |
| 254 |
*/ |
| 255 |
void getPropertyMap(OffsetBase offsetBase,std::map<std::string,Property*> &Map) const; |
| 256 |
|
| 257 |
/** |
| 258 |
* @brief Get a list of properties. |
| 259 |
* |
| 260 |
* @param[in] offsetBase The base offset for the property. |
| 261 |
* @param[out] List A vector of properties. |
| 262 |
*/ |
| 263 |
void getPropertyList(OffsetBase offsetBase,std::vector<Property*> &List) const; |
| 264 |
|
| 265 |
/** |
| 266 |
* @brief Get a list of properties with their names. |
| 267 |
* |
| 268 |
* @param[in] offsetBase The base offset for the property. |
| 269 |
* @param[out] List A vector of pairs, where each pair contains the name and |
| 270 |
* the property. |
| 271 |
*/ |
| 272 |
void getPropertyNamedList(OffsetBase offsetBase, std::vector<std::pair<const char*,Property*> > &List) const; |
| 273 |
|
| 274 |
/** |
| 275 |
* @brief Visit each property in the PropertyData struct. |
| 276 |
* |
| 277 |
* @param[in] offsetBase The base offset for the property. |
| 278 |
* @param[in] visitor The function to apply to each property. |
| 279 |
* |
| 280 |
* @see PropertyContainer::visitProperties() |
| 281 |
*/ |
| 282 |
void visitProperties(OffsetBase offsetBase, const std::function<void(Property*)>& visitor) const; |
| 283 |
|
| 284 |
/** |
| 285 |
* @brief Merge the PropertyData structs. |
| 286 |
* |
| 287 |
* Merge two PropertyData structs. If `other` is `nullptr`, merge with the |
| 288 |
* parent PropertyData. |
| 289 |
* |
| 290 |
* @param[in] other (Optional) The other PropertyData to merge with; |
| 291 |
*/ |
| 292 |
void merge(PropertyData *other=nullptr) const; |
| 293 |
|
| 294 |
/** |
| 295 |
* @brief Split the PropertyData structs. |
| 296 |
* |
| 297 |
* This method splits the PropertyData structs. It is used to |
| 298 |
* restore the parent PropertyData after a merge. |
| 299 |
* |
| 300 |
* @param[in] other The other PropertyData to split with; this can be the parent PropertyData. |
| 301 |
*/ |
| 302 |
void split(PropertyData *other); |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
/** @brief %Base class of all classes with properties. |
| 307 |
* @ingroup PropertyFramework |
| 308 |
* |
| 309 |
* @details This base class for all classes that hold properties has |
| 310 |
* essentially two important children: Document and DocumentObject. Both |
| 311 |
* classes can hold properties and the shared functionality to make that happen |
| 312 |
* is in this class. |
| 313 |
* |
| 314 |
* For a more high-level overview see topic @ref PropertyFramework "Property Framework". |
| 315 |
*/ |
| 316 |
class AppExport PropertyContainer: public Base::Persistence |
| 317 |
{ |
| 318 |
|
| 319 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 320 |
|
| 321 |
public: |
| 322 |
/** |
| 323 |
* @brief Construct a property container. |
| 324 |
*/ |
| 325 |
PropertyContainer(); |
| 326 |
|
| 327 |
/** |
| 328 |
* @brief Destruct a property container. |
| 329 |
*/ |
| 330 |
~PropertyContainer() override; |
| 331 |
|
| 332 |
unsigned int getMemSize () const override; |
| 333 |
|
| 334 |
/** |
| 335 |
* @brief Get the full name of the property container. |
| 336 |
* |
| 337 |
* The full name typically also contains the name of the document. |
| 338 |
* |
| 339 |
* @return The full name of the property container. |
| 340 |
*/ |
| 341 |
virtual std::string getFullName() const {return {};} |
| 342 |
|
| 343 |
/** |
| 344 |
* @brief Find a property by its name. |
| 345 |
* |
| 346 |
* @param[in] name The name of the property to find. |
| 347 |
* @return The property if found or `nullptr` when not found. |
| 348 |
*/ |
| 349 |
virtual Property *getPropertyByName(const char* name) const; |
| 350 |
|
| 351 |
/** |
| 352 |
* @brief Get the name of a property. |
| 353 |
* |
| 354 |
* @param[in] prop The property to get the name for. |
| 355 |
* @return The name of the property. |
| 356 |
*/ |
| 357 |
virtual const char* getPropertyName(const Property* prop) const; |
| 358 |
|
| 359 |
/** |
| 360 |
* Get all properties of the property container. |
| 361 |
* |
| 362 |
* This includes the properties of the parent container. |
| 363 |
* |
| 364 |
* @param[out] map A map of property names to properties. |
| 365 |
*/ |
| 366 |
virtual void getPropertyMap(std::map<std::string,Property*> &map) const; |
| 367 |
|
| 368 |
/** |
| 369 |
* Get all properties of the property container. |
| 370 |
* |
| 371 |
* This includes the properties of the parent container. |
| 372 |
* |
| 373 |
* @param[out] List A vector of properties. |
| 374 |
*/ |
| 375 |
virtual void getPropertyList(std::vector<Property*> &List) const; |
| 376 |
|
| 377 |
/** |
| 378 |
* @brief Visit each property in the container. |
| 379 |
* |
| 380 |
* This method allows you to apply a function to each property in the |
| 381 |
* container. The visiting order is undefined. This method is necessary |
| 382 |
* because PropertyContainer has no begin and end methods and it is not |
| 383 |
* practical to implement these. What gets visited is undefined if the |
| 384 |
* collection of Properties is changed during this call. |
| 385 |
* |
| 386 |
* @param[in] visitor The function to apply to each property. |
| 387 |
*/ |
| 388 |
virtual void visitProperties(const std::function<void(Property*)>& visitor) const; |
| 389 |
|
| 390 |
/** |
| 391 |
* @brief Get a list of properties with their names. |
| 392 |
* |
| 393 |
* The list may contain duplicates and aliases. |
| 394 |
* |
| 395 |
* @param[out] list A vector of pairs, where each pair contains the name and |
| 396 |
* the property. |
| 397 |
*/ |
| 398 |
virtual void getPropertyNamedList(std::vector<std::pair<const char*,Property*> > &list) const; |
| 399 |
|
| 400 |
/** |
| 401 |
* @brief Set a status bit for all properties. |
| 402 |
* |
| 403 |
* This method sets a status bit for all properties in the container at once. |
| 404 |
* The status bits are defined in enum Property::Status. |
| 405 |
* |
| 406 |
* @param[in] bit The status bit to set. |
| 407 |
* @param[in] value The value to set the status bit to. |
| 408 |
*/ |
| 409 |
void setPropertyStatus(unsigned char bit,bool value); |
| 410 |
|
| 411 |
/** |
| 412 |
* @brief Get the type of a property given a property. |
| 413 |
* |
| 414 |
* This method returns the type as a bitmask of the PropertyType enum. |
| 415 |
* |
| 416 |
* @param[in] prop The property to get the type for. |
| 417 |
* @return The type as a bitmask of the PropertyType enum. |
| 418 |
*/ |
| 419 |
virtual short getPropertyType(const Property* prop) const; |
| 420 |
|
| 421 |
/** |
| 422 |
* @brief Get the type of a property given a property name. |
| 423 |
* |
| 424 |
* This method returns the type as a bitmask of the PropertyType enum. |
| 425 |
* |
| 426 |
* @param[in] name The name of the property to get the type for. |
| 427 |
* @return The type as a bitmask of the PropertyType enum. |
| 428 |
*/ |
| 429 |
virtual short getPropertyType(const char *name) const; |
| 430 |
|
| 431 |
/** |
| 432 |
* @brief Get the group of a property given a property. |
| 433 |
* |
| 434 |
* @param[in] prop The property to get the group for. |
| 435 |
* @return The group name of the property. |
| 436 |
*/ |
| 437 |
virtual const char* getPropertyGroup(const Property* prop) const; |
| 438 |
|
| 439 |
/** |
| 440 |
* @brief Get the group of a property given a property name. |
| 441 |
* |
| 442 |
* @param[in] name The name of the property to get the group for. |
| 443 |
* @return The group name of the property. |
| 444 |
*/ |
| 445 |
virtual const char* getPropertyGroup(const char *name) const; |
| 446 |
|
| 447 |
/** |
| 448 |
* @brief Get the documentation of a property given a property. |
| 449 |
* |
| 450 |
* @param[in] prop The property to get the documentation for. |
| 451 |
* @return The documentation string of the property. |
| 452 |
*/ |
| 453 |
virtual const char* getPropertyDocumentation(const Property* prop) const; |
| 454 |
|
| 455 |
/** |
| 456 |
* @brief Get the documentation of a property given a property name. |
| 457 |
* |
| 458 |
* @param[in] name The name of the property to get the documentation for. |
| 459 |
* @return The documentation string of the property. |
| 460 |
*/ |
| 461 |
virtual const char* getPropertyDocumentation(const char *name) const; |
| 462 |
|
| 463 |
/** |
| 464 |
* @brief Check if a property is read-only given a property. |
| 465 |
* |
| 466 |
* @param[in] prop The property to check. |
| 467 |
* @return `true` if the property is read-only; `false` otherwise. |
| 468 |
*/ |
| 469 |
bool isReadOnly(const Property* prop) const; |
| 470 |
|
| 471 |
/** |
| 472 |
* @brief Check if a property is read-only given a property name. |
| 473 |
* |
| 474 |
* @param[in] name The name of the property to check. |
| 475 |
* @return `true` if the property is read-only; `false` otherwise. |
| 476 |
*/ |
| 477 |
bool isReadOnly(const char *name) const; |
| 478 |
|
| 479 |
/** |
| 480 |
* @brief Check if a property is hidden given a property. |
| 481 |
* |
| 482 |
* @param[in] prop The property to check. |
| 483 |
* @return `true` if the property is hidden; `false` otherwise. |
| 484 |
*/ |
| 485 |
bool isHidden(const Property* prop) const; |
| 486 |
|
| 487 |
/** |
| 488 |
* @brief Check if a property is hidden given a property name. |
| 489 |
* |
| 490 |
* @param[in] name The name of the property to check. |
| 491 |
* @return `true` if the property is hidden; `false` otherwise. |
| 492 |
*/ |
| 493 |
bool isHidden(const char *name) const; |
| 494 |
|
| 495 |
/** |
| 496 |
* Add a property at runtime. |
| 497 |
* |
| 498 |
* Dynamic properties are properties that are not defined at compile-time |
| 499 |
* but can be added and removed during the lifetime of the object. |
| 500 |
* |
| 501 |
* @param[in] type The type name of the property (e.g., "App::PropertyFloat"). |
| 502 |
* @param[in] name Optional name of the property. If null, a default name may be generated. |
| 503 |
* @param[in] group Optional group name to which the property belongs |
| 504 |
* (used for UI or logical grouping). |
| 505 |
* @param[in] doc Optional documentation string describing the purpose of the property. |
| 506 |
* @param[in] attr Bitmask of property attributes, composed of values from the |
| 507 |
* PropertyType enum. |
| 508 |
* @param[in] ro If true, the property is marked as read-only. |
| 509 |
* @param[in] hidden If true, the property is hidden from the user interface. |
| 510 |
* @return A pointer to the newly added Property. |
| 511 |
* @throws Exception A runtime exception is thrown if the property cannot be added, such |
| 512 |
* as when the name is invalid. |
| 513 |
*/ |
| 514 |
virtual App::Property* addDynamicProperty( |
| 515 |
const char* type, const char* name=nullptr, |
| 516 |
const char* group=nullptr, const char* doc=nullptr, |
| 517 |
short attr=0, bool ro=false, bool hidden=false); |
| 518 |
|
| 519 |
/** |
| 520 |
* @brief Get the data of a dynamic property. |
| 521 |
* |
| 522 |
* This function retrieves the data associated with a dynamic property. |
| 523 |
* |
| 524 |
* @param[in] prop The property to get the data for. |
| 525 |
* @returns The data of the dynamic property. |
| 526 |
*/ |
| 527 |
DynamicProperty::PropData getDynamicPropertyData(const Property* prop) const { |
| 528 |
return dynamicProps.getDynamicPropertyData(prop); |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Change the group and documentation of a dynamic property. |
| 533 |
* |
| 534 |
* @param[in] prop The property to change. |
| 535 |
* @param[in] group The new group name for organizational purposes, (e.g., in UI panels). |
| 536 |
* @param[in] doc The new documentation string for this property. |
| 537 |
* @return `true` if the update was successful; `false` otherwise. |
| 538 |
*/ |
| 539 |
bool changeDynamicProperty(const Property *prop, const char *group, const char *doc) { |
| 540 |
return dynamicProps.changeDynamicProperty(prop,group,doc); |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* @brief Rename the dynamic property. |
| 545 |
* |
| 546 |
* @param[in] prop The property to rename. |
| 547 |
* @param[in] name The new name for the property. |
| 548 |
* |
| 549 |
* @return `true` if the property was renamed; `false` otherwise. |
| 550 |
* @throw Base::NameError If the new name is invalid or already exists. |
| 551 |
*/ |
| 552 |
virtual bool renameDynamicProperty(Property *prop, const char *name) { |
| 553 |
return dynamicProps.renameDynamicProperty(prop,name); |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* @brief Remove a dynamic property. |
| 558 |
* |
| 559 |
* @param[in] name The name of the property to remove. |
| 560 |
* @return `true` if the property was removed; `false` otherwise. |
| 561 |
*/ |
| 562 |
virtual bool removeDynamicProperty(const char* name) { |
| 563 |
return dynamicProps.removeDynamicProperty(name); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* @brief Get the names of all dynamic properties. |
| 568 |
* |
| 569 |
* @returns A vector of strings containing the names of all dynamic properties. |
| 570 |
*/ |
| 571 |
virtual std::vector<std::string> getDynamicPropertyNames() const { |
| 572 |
return dynamicProps.getDynamicPropertyNames(); |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* @brief Get a dynamic property. |
| 577 |
* |
| 578 |
* @param[in] name The name of the property. |
| 579 |
* @returns The property if found or `nullptr` when not found. |
| 580 |
*/ |
| 581 |
virtual App::Property *getDynamicPropertyByName(const char* name) const { |
| 582 |
return dynamicProps.getDynamicPropertyByName(name); |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* @brief Called when the status of a property is changed. |
| 587 |
* |
| 588 |
* This method is called when the status of a property changes. It can be |
| 589 |
* overridden by subclasses to implement custom behavior when a property |
| 590 |
* status changes. |
| 591 |
* |
| 592 |
* @param[in] prop The property whose status has changed. |
| 593 |
* @param[in] oldStatus The old status of the property as a bitmask of enum |
| 594 |
* Property::Status. |
| 595 |
*/ |
| 596 |
virtual void onPropertyStatusChanged(const Property &prop, unsigned long oldStatus); |
| 597 |
|
| 598 |
void Save (Base::Writer &writer) const override; |
| 599 |
void Restore(Base::XMLReader &reader) override; |
| 600 |
|
| 601 |
/** |
| 602 |
* @brief Prepare the properties for saving. |
| 603 |
* |
| 604 |
* All non-transient properties are prepared to be saved. |
| 605 |
* |
| 606 |
* @see Property::beforeSave() |
| 607 |
*/ |
| 608 |
virtual void beforeSave() const; |
| 609 |
|
| 610 |
/** |
| 611 |
* @brief Called when a property is edited by the user. |
| 612 |
* |
| 613 |
* Subclasses can override this method to implement custom behavior for |
| 614 |
* editing specific properties. |
| 615 |
* |
| 616 |
* @param[in] propName The name of the property to be edited. |
| 617 |
*/ |
| 618 |
virtual void editProperty([[maybe_unused]] const char* propName) {} |
| 619 |
|
| 620 |
/** |
| 621 |
* @brief Get the prefix for property names. |
| 622 |
* |
| 623 |
* @return The prefix for property names. |
| 624 |
*/ |
| 625 |
const char *getPropertyPrefix() const { |
| 626 |
return _propertyPrefix.c_str(); |
| 627 |
} |
| 628 |
|
| 629 |
/** |
| 630 |
* @brief Set the prefix for property names. |
| 631 |
* |
| 632 |
* @param[in] prefix The new prefix for property names. |
| 633 |
*/ |
| 634 |
void setPropertyPrefix(const char *prefix) { |
| 635 |
_propertyPrefix = prefix; |
| 636 |
} |
| 637 |
|
| 638 |
friend class Property; |
| 639 |
friend class DynamicProperty; |
| 640 |
|
| 641 |
|
| 642 |
protected: |
| 643 |
/** |
| 644 |
* @brief Called when a property is about to change. |
| 645 |
* |
| 646 |
* This method can be overridden by subclasses to implement custom behavior |
| 647 |
* when a property is about to change. This method is called in |
| 648 |
* Property::touch() right before onChanged() is called, typically in the |
| 649 |
* DocumentObject::execute() phase. |
| 650 |
* |
| 651 |
* @note This can be considered a low-level callback and is not widely |
| 652 |
* overridden. For preparing for a property change, use onBeforeChange(). |
| 653 |
* |
| 654 |
* @param[in] prop The property that is about to change. |
| 655 |
* @see Property::onBeforeChange() |
| 656 |
*/ |
| 657 |
virtual void onEarlyChange([[maybe_unused]] const Property* prop){} |
| 658 |
|
| 659 |
/** |
| 660 |
* @brief Called when a property has changed. |
| 661 |
* |
| 662 |
* This method can be overridden by subclasses to implement custom behavior |
| 663 |
* when a property has changed. |
| 664 |
* |
| 665 |
* @param[in] prop The property that has changed. |
| 666 |
*/ |
| 667 |
virtual void onChanged([[maybe_unused]] const Property* prop){} |
| 668 |
|
| 669 |
/** |
| 670 |
* @brief Called before a property is changed. |
| 671 |
* |
| 672 |
* This method can be overridden by subclasses to implement custom behavior |
| 673 |
* before a property has changed. It allows property containers to prepare |
| 674 |
* for a property change. |
| 675 |
* |
| 676 |
* @param[in] prop The property that is about to change. |
| 677 |
*/ |
| 678 |
virtual void onBeforeChange([[maybe_unused]] const Property* prop){} |
| 679 |
|
| 680 |
/** |
| 681 |
* @brief Get a pointer to the class-wide static property data. |
| 682 |
* |
| 683 |
* This method gives access to the class-wide static PropertyData shared by |
| 684 |
* all instances of the class. The macros `PROPERTY_HEADER` and |
| 685 |
* `PROPERTY_SOURCE` variants ensure that each subclass of %PropertyContainer |
| 686 |
* has its own static PropertyData instance, and that this method returns the |
| 687 |
* instance for that subclass. |
| 688 |
* |
| 689 |
* @returns A pointer to the static `PropertyData`. |
| 690 |
*/ |
| 691 |
static const PropertyData* getPropertyDataPtr(); |
| 692 |
|
| 693 |
/** |
| 694 |
* @brief Get a reference to the static property data for the dynamic type of this instance. |
| 695 |
* |
| 696 |
* This virtual method allows retrieval of the class-level static |
| 697 |
* PropertyData associated with the actual (dynamic) type of the object, even |
| 698 |
* when accessed via a base class pointer. The `PROPERTY_HEADER` and |
| 699 |
* `PROPERTY_SOURCE` macros ensure that each subclass defines its own static |
| 700 |
* PropertyData instance, and that this method correctly dispatches to return |
| 701 |
* it. |
| 702 |
* |
| 703 |
* @return A reference to the static `PropertyData` corresponding to the dynamic type. |
| 704 |
*/ |
| 705 |
virtual const PropertyData& getPropertyData() const; |
| 706 |
|
| 707 |
/** |
| 708 |
* @brief Handle a changed property name during restore. |
| 709 |
* |
| 710 |
* This method is called during restore to possibly fix reading of older |
| 711 |
* versions of this property container. This method is typically called if |
| 712 |
* the property on file has changed its name in more recent versions. |
| 713 |
* |
| 714 |
* The default implementation does nothing. |
| 715 |
* |
| 716 |
* @param[in,out] reader The XML stream to read from. |
| 717 |
* @param[in] typeName The name of the property type in the file. |
| 718 |
* @param[in] propName The name of the property in the file that does no |
| 719 |
* longer exist in the container. |
| 720 |
*/ |
| 721 |
virtual void handleChangedPropertyName(Base::XMLReader& reader, const char* typeName, const char* propName); |
| 722 |
|
| 723 |
/** |
| 724 |
* @brief Handle a changed property type during restore. |
| 725 |
* |
| 726 |
* This method is called during restore to possibly fix reading of older |
| 727 |
* versions of this property container. This method is typically called if |
| 728 |
* the property on file has changed its type in more recent versions. |
| 729 |
* |
| 730 |
* The default implementation does nothing. |
| 731 |
* |
| 732 |
* @param[in,out] reader The XML stream to read from. |
| 733 |
* @param[in] typeName The name of the property type in the file. |
| 734 |
* @param[in, out] prop The property that needs to be restored. Its type differs from `typeName`. |
| 735 |
*/ |
| 736 |
virtual void handleChangedPropertyType(Base::XMLReader &reader, const char * typeName, Property * prop); |
| 737 |
|
| 738 |
public: |
| 739 |
|
| 740 |
/// The copy constructor is deleted to prevent copying. |
| 741 |
PropertyContainer(const PropertyContainer&) = delete; |
| 742 |
|
| 743 |
/// The assignment operator is deleted to prevent assignment. |
| 744 |
PropertyContainer& operator = (const PropertyContainer&) = delete; |
| 745 |
|
| 746 |
protected: |
| 747 |
|
| 748 |
/// The container for dynamic properties. |
| 749 |
DynamicProperty dynamicProps; |
| 750 |
|
| 751 |
private: |
| 752 |
std::string _propertyPrefix; |
| 753 |
static PropertyData propertyData; |
| 754 |
}; |
| 755 |
|
| 756 |
// clang-format off |
| 757 |
/// Property define |
| 758 |
#define _ADD_PROPERTY(_name,_prop_, _defaultval_) \ |
| 759 |
do { \ |
| 760 |
this->_prop_.setValue _defaultval_;\ |
| 761 |
this->_prop_.setContainer(this); \ |
| 762 |
propertyData.addProperty(static_cast<App::PropertyContainer*>(this), _name, &this->_prop_); \ |
| 763 |
} while (0) |
| 764 |
|
| 765 |
#define ADD_PROPERTY(_prop_, _defaultval_) \ |
| 766 |
_ADD_PROPERTY(#_prop_, _prop_, _defaultval_) |
| 767 |
|
| 768 |
#define _ADD_PROPERTY_TYPE(_name,_prop_, _defaultval_, _group_,_type_,_Docu_) \ |
| 769 |
do { \ |
| 770 |
this->_prop_.setValue _defaultval_;\ |
| 771 |
this->_prop_.setContainer(this); \ |
| 772 |
propertyData.addProperty(static_cast<App::PropertyContainer*>(this), _name, &this->_prop_, (_group_),(_type_),(_Docu_)); \ |
| 773 |
} while (0) |
| 774 |
|
| 775 |
#define ADD_PROPERTY_TYPE(_prop_, _defaultval_, _group_,_type_,_Docu_) \ |
| 776 |
_ADD_PROPERTY_TYPE(#_prop_,_prop_,_defaultval_,_group_,_type_,_Docu_) |
| 777 |
|
| 778 |
|
| 779 |
#define PROPERTY_HEADER(_class_) \ |
| 780 |
TYPESYSTEM_HEADER(); \ |
| 781 |
protected: \ |
| 782 |
static const App::PropertyData * getPropertyDataPtr(void); \ |
| 783 |
virtual const App::PropertyData &getPropertyData(void) const; \ |
| 784 |
private: \ |
| 785 |
static App::PropertyData propertyData |
| 786 |
|
| 787 |
/// Like PROPERTY_HEADER, but with overridden methods declared as such |
| 788 |
#define PROPERTY_HEADER_WITH_OVERRIDE(_class_) \ |
| 789 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); \ |
| 790 |
public: \ |
| 791 |
static consteval const char* getClassName() {\ |
| 792 |
static_assert(sizeof(_class_) > 0, "Class is not complete"); \ |
| 793 |
\ |
| 794 |
constexpr const char* sClass = #_class_; \ |
| 795 |
constexpr std::string_view vClass {sClass}; \ |
| 796 |
static_assert(!vClass.empty(), "Class name must not be empty"); \ |
| 797 |
static_assert(std::is_base_of<App::PropertyContainer, _class_>::value, \ |
| 798 |
"Class must be derived from App::PropertyContainer"); \ |
| 799 |
\ |
| 800 |
constexpr bool isSubClassOfDocObj = std::is_base_of<App::DocumentObject, _class_>::value && \ |
| 801 |
!std::is_same<App::DocumentObject, _class_>::value; \ |
| 802 |
if constexpr (isSubClassOfDocObj) { \ |
| 803 |
constexpr auto pos = vClass.find("::"); \ |
| 804 |
static_assert(pos != std::string_view::npos, \ |
| 805 |
"Class name must be fully qualified for document object derived classes"); \ |
| 806 |
constexpr auto vNamespace = vClass.substr(0, pos); \ |
| 807 |
static_assert(!vNamespace.empty(), "Namespace must not be empty"); \ |
| 808 |
\ |
| 809 |
constexpr std::string_view filePath = __FILE__; \ |
| 810 |
constexpr std::string_view modPath = "/src/Mod/"; \ |
| 811 |
constexpr auto posAfterSrcMod = filePath.find(modPath); \ |
| 812 |
constexpr bool hasSrcModInPath = posAfterSrcMod != std::string_view::npos; \ |
| 813 |
if constexpr (hasSrcModInPath) { \ |
| 814 |
constexpr auto pathAfterSrcMod = filePath.substr(posAfterSrcMod + modPath.size()); \ |
| 815 |
constexpr bool isPathOk = pathAfterSrcMod.starts_with(vNamespace); \ |
| 816 |
static_assert( \ |
| 817 |
/* some compilers evaluate static_asserts inside ifs before checking it's a valid codepath */ \ |
| 818 |
!isSubClassOfDocObj || !hasSrcModInPath || \ |
| 819 |
/* allow `Path` until it's been properly renamed to CAM */ \ |
| 820 |
vNamespace == "Path" || isPathOk, \ |
| 821 |
"Classes in `src/Mod` needs to be in a directory with the same name as" \ |
| 822 |
" the namespace in order to load correctly"); \ |
| 823 |
} \ |
| 824 |
} \ |
| 825 |
return sClass; \ |
| 826 |
} \ |
| 827 |
protected: \ |
| 828 |
static const App::PropertyData * getPropertyDataPtr(void); \ |
| 829 |
const App::PropertyData &getPropertyData(void) const override; \ |
| 830 |
private: \ |
| 831 |
static App::PropertyData propertyData |
| 832 |
/// |
| 833 |
#define PROPERTY_SOURCE(_class_, _parentclass_) \ |
| 834 |
TYPESYSTEM_SOURCE_P(_class_)\ |
| 835 |
const App::PropertyData * _class_::getPropertyDataPtr(void){return &propertyData;} \ |
| 836 |
const App::PropertyData & _class_::getPropertyData(void) const{return propertyData;} \ |
| 837 |
App::PropertyData _class_::propertyData; \ |
| 838 |
void _class_::init(void){\ |
| 839 |
initSubclass(_class_::classTypeId, #_class_ , #_parentclass_, &(_class_::create) ); \ |
| 840 |
_class_::propertyData.parentPropertyData = _parentclass_::getPropertyDataPtr(); \ |
| 841 |
} |
| 842 |
|
| 843 |
#define PROPERTY_SOURCE_ABSTRACT(_class_, _parentclass_) \ |
| 844 |
TYPESYSTEM_SOURCE_ABSTRACT_P(_class_)\ |
| 845 |
const App::PropertyData * _class_::getPropertyDataPtr(void){return &propertyData;} \ |
| 846 |
const App::PropertyData & _class_::getPropertyData(void) const{return propertyData;} \ |
| 847 |
App::PropertyData _class_::propertyData; \ |
| 848 |
void _class_::init(void){\ |
| 849 |
initSubclass(_class_::classTypeId, #_class_ , #_parentclass_, &(_class_::create) ); \ |
| 850 |
_class_::propertyData.parentPropertyData = _parentclass_::getPropertyDataPtr(); \ |
| 851 |
} |
| 852 |
|
| 853 |
#define TYPESYSTEM_SOURCE_TEMPLATE(_class_) \ |
| 854 |
template<> Base::Type _class_::classTypeId = Base::Type::BadType; \ |
| 855 |
template<> Base::Type _class_::getClassTypeId(void) { return _class_::classTypeId; } \ |
| 856 |
template<> Base::Type _class_::getTypeId(void) const { return _class_::classTypeId; } \ |
| 857 |
template<> void * _class_::create(void){\ |
| 858 |
return new _class_ ();\ |
| 859 |
} |
| 860 |
|
| 861 |
#define PROPERTY_SOURCE_TEMPLATE(_class_, _parentclass_) \ |
| 862 |
TYPESYSTEM_SOURCE_TEMPLATE(_class_)\ |
| 863 |
template<> App::PropertyData _class_::propertyData = App::PropertyData(); \ |
| 864 |
template<> const App::PropertyData * _class_::getPropertyDataPtr(void){return &propertyData;} \ |
| 865 |
template<> const App::PropertyData & _class_::getPropertyData(void) const{return propertyData;} \ |
| 866 |
template<> void _class_::init(void){\ |
| 867 |
initSubclass(_class_::classTypeId, #_class_ , #_parentclass_, &(_class_::create) ); \ |
| 868 |
_class_::propertyData.parentPropertyData = _parentclass_::getPropertyDataPtr(); \ |
| 869 |
} |
| 870 |
// clang-format on |
| 871 |
|
| 872 |
} // namespace App |
| 873 |
|
| 874 |
#endif // SRC_APP_PROPERTYCONTAINER_H_ |