| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 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 APP_PROPERTY_H |
| 25 |
#define APP_PROPERTY_H |
| 26 |
|
| 27 |
#include <Base/Exception.h> |
| 28 |
#include <Base/Persistence.h> |
| 29 |
#include <boost/any.hpp> |
| 30 |
#include <boost/signals2.hpp> |
| 31 |
#include <bitset> |
| 32 |
#include <string> |
| 33 |
#include <FCGlobal.h> |
| 34 |
|
| 35 |
#include "ElementNamingUtils.h" |
| 36 |
namespace Py |
| 37 |
{ |
| 38 |
class Object; |
| 39 |
} |
| 40 |
|
| 41 |
namespace App |
| 42 |
{ |
| 43 |
|
| 44 |
class PropertyContainer; |
| 45 |
class ObjectIdentifier; |
| 46 |
|
| 47 |
/** |
| 48 |
* @brief %Base class of all properties. |
| 49 |
* @ingroup PropertyFramework |
| 50 |
* |
| 51 |
* This is the base class of all properties. Properties are objects that are |
| 52 |
* used in documents or document objects that maintain all kinds of information |
| 53 |
* about the document or document objects. Examples are properties of |
| 54 |
* features, such as the length, but properties can also keep track of shapes. |
| 55 |
* They are also used to gain access to the document object in the console. |
| 56 |
* |
| 57 |
* This abstract base class defines all methods shared by all possible |
| 58 |
* properties. It is also possible to create user-defined properties. |
| 59 |
* |
| 60 |
* For a more high-level overview see topic @ref PropertyFramework "Property Framework". |
| 61 |
*/ |
| 62 |
class AppExport Property: public Base::Persistence |
| 63 |
{ |
| 64 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 65 |
|
| 66 |
public: |
| 67 |
|
| 68 |
/** |
| 69 |
* @brief Defines the position in the status bitmask. |
| 70 |
*/ |
| 71 |
enum Status |
| 72 |
{ |
| 73 |
/// Whether a property is touched. |
| 74 |
Touched = 0, |
| 75 |
/// Whether a property can be modified. |
| 76 |
Immutable = 1, |
| 77 |
/// Whether a property is read-only for the property editor. |
| 78 |
ReadOnly = 2, |
| 79 |
/// Whether the property is hidden in the property editor. |
| 80 |
Hidden = 3, |
| 81 |
/// Whether a property is saved in the document. |
| 82 |
Transient = 4, |
| 83 |
/// To turn ON PropertyMaterial edit. |
| 84 |
MaterialEdit = 5, |
| 85 |
/// To turn OFF PropertyMaterialList edit. |
| 86 |
NoMaterialListEdit = 6, |
| 87 |
/// Whether a property is an output property. |
| 88 |
Output = 7, |
| 89 |
/// Whether a dynamic property can be removed. |
| 90 |
LockDynamic = 8, |
| 91 |
/// Prevents causing `Gui::Document::setModified()`. |
| 92 |
NoModify = 9, |
| 93 |
/// Whether to allow change in a partial document. |
| 94 |
PartialTrigger = 10, |
| 95 |
/// Whether to prevent to touch the owner for a recompute on property change. |
| 96 |
NoRecompute = 11, |
| 97 |
/// Whether a floating point number should be saved as single precision. |
| 98 |
Single = 12, |
| 99 |
/// For PropertyLists, whether the order of the elements is |
| 100 |
/// relevant for the container using it. |
| 101 |
Ordered = 13, |
| 102 |
/// In case of expression binding, whether the expression on |
| 103 |
/// restore and touch the object on value change. |
| 104 |
EvalOnRestore = 14, |
| 105 |
/// For internal use to avoid recursive signaling. |
| 106 |
Busy = 15, |
| 107 |
/// Whether the linked object should be copied on change of the property. |
| 108 |
CopyOnChange = 16, |
| 109 |
/// Whether the property editor should create a button for user defined editing. |
| 110 |
UserEdit = 17, |
| 111 |
/// Do not propagate changes of the property to its container |
| 112 |
DisableNotify = 18, |
| 113 |
|
| 114 |
// The following bits are corresponding to PropertyType set when the |
| 115 |
// property added. These types are meant to be static, and cannot be |
| 116 |
// changed in runtime. It is mirrored here to save the linear search |
| 117 |
// required in PropertyContainer::getPropertyType() |
| 118 |
|
| 119 |
/// Mark the beginning of enum PropertyType bits. |
| 120 |
PropStaticBegin = 21, |
| 121 |
/// Whether the property is dynamically added. |
| 122 |
PropDynamic = 21, |
| 123 |
/// Corresponds to Prop_NoPersist. |
| 124 |
PropNoPersist = 22, |
| 125 |
/// Corresponds to Prop_NoRecompute. |
| 126 |
PropNoRecompute = 23, |
| 127 |
/// Corresponds to Prop_ReadOnly. |
| 128 |
PropReadOnly = 24, |
| 129 |
/// Corresponds to Prop_Transient. |
| 130 |
PropTransient = 25, |
| 131 |
/// Corresponds to Prop_Hidden. |
| 132 |
PropHidden = 26, |
| 133 |
/// Corresponds to Prop_Output. |
| 134 |
PropOutput = 27, |
| 135 |
/// Mark the end of enum PropertyType bits. |
| 136 |
PropStaticEnd = 28, |
| 137 |
|
| 138 |
/// User defined status bit. |
| 139 |
User1 = 28, |
| 140 |
/// User defined status bit. |
| 141 |
User2 = 29, |
| 142 |
/// User defined status bit. |
| 143 |
User3 = 30, |
| 144 |
/// User defined status bit. |
| 145 |
User4 = 31 |
| 146 |
}; |
| 147 |
|
| 148 |
/// Construct a property. |
| 149 |
Property(); |
| 150 |
/// Destruct a property. |
| 151 |
~Property() override; |
| 152 |
|
| 153 |
/** |
| 154 |
* @brief Safely delete a dynamic property. |
| 155 |
* |
| 156 |
* @param[inout] p The property to delete. |
| 157 |
*/ |
| 158 |
static void destroy(Property* p); |
| 159 |
|
| 160 |
unsigned int getMemSize() const override |
| 161 |
{ |
| 162 |
// you have to implement this method in all property classes! |
| 163 |
return sizeof(father) + sizeof(StatusBits); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @brief Get the name of this property in the belonging container. |
| 168 |
* |
| 169 |
* With \ref hasName() it can be checked beforehand if a valid name is set. |
| 170 |
* |
| 171 |
* @return The name of the property or if no name is set, an empty string. |
| 172 |
*/ |
| 173 |
const char* getName() const; |
| 174 |
|
| 175 |
/** |
| 176 |
* @brief Check whether the property has a name set. |
| 177 |
* |
| 178 |
* If no name is set then \ref getName() will return an empty string |
| 179 |
* |
| 180 |
* @return True if a name is set, false otherwise. |
| 181 |
*/ |
| 182 |
bool hasName() const; |
| 183 |
|
| 184 |
/** |
| 185 |
* @brief Check whether the passed name is valid. |
| 186 |
* |
| 187 |
* If a name is null or an empty string it is considered invalid, and valid |
| 188 |
* otherwise. |
| 189 |
* |
| 190 |
* @param[in] name The name to check. |
| 191 |
* @return True if the name is valid, false otherwise. |
| 192 |
*/ |
| 193 |
static bool isValidName(const char* name); |
| 194 |
|
| 195 |
/** |
| 196 |
* @brief Get the name of the property and its container. |
| 197 |
* |
| 198 |
* The container name is separated from the property name by a dot. If the |
| 199 |
* property has no name, then a question mark is returned. If the |
| 200 |
* container has no name, then a question mark is returned for the |
| 201 |
* container named. |
| 202 |
*/ |
| 203 |
std::string getFullName() const; |
| 204 |
|
| 205 |
/** |
| 206 |
* @brief Get the class name of the associated property editor item. |
| 207 |
* |
| 208 |
* @return The class name of the property editor item or an empty string if |
| 209 |
* not defined. |
| 210 |
*/ |
| 211 |
virtual const char* getEditorName() const |
| 212 |
{ |
| 213 |
return ""; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @brief Get the type of the property in the container. |
| 218 |
* |
| 219 |
* The type is expressed as a bitmask of enum PropertyType. |
| 220 |
* |
| 221 |
* @return The type of the property as a bitmask of enum PropertyType. |
| 222 |
*/ |
| 223 |
short getType() const; |
| 224 |
|
| 225 |
/** |
| 226 |
* @brief Get the group of this property. |
| 227 |
* |
| 228 |
* @return The group of the property or a nullptr if not defined. |
| 229 |
*/ |
| 230 |
const char* getGroup() const; |
| 231 |
|
| 232 |
/** |
| 233 |
* @brief Get the documentation of this property. |
| 234 |
* |
| 235 |
* @return The documentation of the property or a nullptr if not defined. |
| 236 |
*/ |
| 237 |
const char* getDocumentation() const; |
| 238 |
|
| 239 |
/** |
| 240 |
* @brief Set the container of this property. |
| 241 |
* |
| 242 |
* This is called by the framework to set the father (container). |
| 243 |
* |
| 244 |
* @param[in] father The container of this property. |
| 245 |
*/ |
| 246 |
void setContainer(PropertyContainer* father); |
| 247 |
|
| 248 |
/** |
| 249 |
* @brief Get the container of this property. |
| 250 |
* |
| 251 |
* Get a pointer to the PropertyContainer derived class to which the |
| 252 |
* property belongs. |
| 253 |
* |
| 254 |
* @return A pointer to the PropertyContainer derived class. |
| 255 |
*/ |
| 256 |
PropertyContainer* getContainer() const |
| 257 |
{ |
| 258 |
return father; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @brief Set the value of the property identified by the path. |
| 263 |
* |
| 264 |
* This function sets the value of the property identified by the path. It |
| 265 |
* is meant to be overridden for subclasses in which the `path` is |
| 266 |
* typically ignored. The default implementation redirects setting a value |
| 267 |
* to the the `path` ObjectIdentifier. |
| 268 |
* |
| 269 |
* @param[in] path The path to the property. |
| 270 |
* @param[in] value The value to set. |
| 271 |
*/ |
| 272 |
virtual void setPathValue(const App::ObjectIdentifier& path, const boost::any& value); |
| 273 |
|
| 274 |
/** |
| 275 |
* @brief Get the value of the property identified by the path. |
| 276 |
* |
| 277 |
* This function gets the value of the property identified by the path. It |
| 278 |
* is meant to be overridden for subclasses in which the `path` is |
| 279 |
* typically ignored. The default implementation makes use of the `path` |
| 280 |
* ObjectIdentifier to get the value of the property. |
| 281 |
* |
| 282 |
* @param[in] path The path to the property. |
| 283 |
* @return The value of the property. |
| 284 |
*/ |
| 285 |
virtual const boost::any getPathValue(const App::ObjectIdentifier& path) const; |
| 286 |
|
| 287 |
/** |
| 288 |
* @brief Get the Python value of the property identified by the path. |
| 289 |
* |
| 290 |
* This function gets the Python value of the property identified by the |
| 291 |
* path. It is meant to be overridden for subclasses. This default |
| 292 |
* implementation return `false`. |
| 293 |
* |
| 294 |
* @param[in] path The path to the property. |
| 295 |
* @param[out] value The Python value of the property. |
| 296 |
* @return True if the value was successfully retrieved, false otherwise. |
| 297 |
*/ |
| 298 |
virtual bool getPyPathValue([[maybe_unused]] const App::ObjectIdentifier& path, |
| 299 |
[[maybe_unused]] Py::Object& value) const |
| 300 |
{ |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* @brief Convert an object identifier to a canonical representation. |
| 306 |
* |
| 307 |
* Convert an object identifier to a canonical representation of the object |
| 308 |
* identifier. |
| 309 |
* |
| 310 |
* @param[in] path The object identifier to convert. |
| 311 |
* @return An object identifier that represents the canonical path. |
| 312 |
*/ |
| 313 |
virtual App::ObjectIdentifier canonicalPath(const App::ObjectIdentifier& path) const; |
| 314 |
|
| 315 |
/** |
| 316 |
* @brief Get valid paths for this property. |
| 317 |
* |
| 318 |
* This function is used by auto completer. |
| 319 |
* |
| 320 |
* @param[out] paths A vector of object identifiers with the valid paths |
| 321 |
* for this property. |
| 322 |
*/ |
| 323 |
virtual void getPaths(std::vector<App::ObjectIdentifier>& paths) const; |
| 324 |
|
| 325 |
/** @brief Callback for after document restore. |
| 326 |
* |
| 327 |
* This function is called at the beginning of Document::afterRestore(). |
| 328 |
* It is called without dependency sorting, because some link property |
| 329 |
* types can only reconstruct the linking information inside this function. |
| 330 |
* Typical use cases include: |
| 331 |
* |
| 332 |
* - **PropertyLinkSub** |
| 333 |
* Parses and restores sub-object references (including |
| 334 |
* those that point into external documents), applying any necessary |
| 335 |
* name-mapping during import. |
| 336 |
* |
| 337 |
* - **PropertyExpressionEngine** |
| 338 |
* Re-parses expressions after restore so that it can handle document |
| 339 |
* name adjustment. It internally relies on PropertyXLink to store the |
| 340 |
* external document path for external linking. When the external |
| 341 |
* document is restored, its internal name may change due to name |
| 342 |
* conflict with existing documents. With this callback it can now auto |
| 343 |
* adjust external references without any problem. |
| 344 |
*/ |
| 345 |
virtual void afterRestore() {} |
| 346 |
|
| 347 |
/** @brief Callback for after document restore. |
| 348 |
* |
| 349 |
* This function is called by Document::restore() after finished calling |
| 350 |
* Property::afterRestore() on all properties of objects. By then, the |
| 351 |
* object dependency information is assumed ready. So, unlike |
| 352 |
* Property::afterRestore(), this function is called on objects with |
| 353 |
* dependency order. |
| 354 |
* |
| 355 |
* It is called before calling DocumentObject::onDocumentRestored(). |
| 356 |
*/ |
| 357 |
virtual void onContainerRestored() {} |
| 358 |
|
| 359 |
/** Property status handling |
| 360 |
*/ |
| 361 |
//@{ |
| 362 |
/// This method sets whether notification will be propagated on changing |
| 363 |
/// the value of the property. The old value of the setting is returned. |
| 364 |
bool enableNotify(bool on); |
| 365 |
/// This method returns whether notification of changes to the property value |
| 366 |
/// are propagated to the container. |
| 367 |
bool isNotifyEnabled() const; |
| 368 |
/// Set the property touched |
| 369 |
void touch(); |
| 370 |
|
| 371 |
/** |
| 372 |
* @brief Test if this property is touched. |
| 373 |
* |
| 374 |
* @return True if the property is touched, false otherwise. |
| 375 |
*/ |
| 376 |
inline bool isTouched() const |
| 377 |
{ |
| 378 |
return StatusBits.test(Touched); |
| 379 |
} |
| 380 |
|
| 381 |
/// Reset this property as being touched. |
| 382 |
inline void purgeTouched() |
| 383 |
{ |
| 384 |
StatusBits.reset(Touched); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* @brief Get the status of this property. |
| 389 |
* |
| 390 |
* @return The status of the property as a bitmask of enum Status. |
| 391 |
*/ |
| 392 |
inline unsigned long getStatus() const |
| 393 |
{ |
| 394 |
return StatusBits.to_ulong(); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* @brief Test if the property has a specific status. |
| 399 |
* |
| 400 |
* @param[in] pos The status to test. |
| 401 |
* @return True if the property has the specified status, false otherwise. |
| 402 |
*/ |
| 403 |
inline bool testStatus(Status pos) const |
| 404 |
{ |
| 405 |
return StatusBits.test(static_cast<size_t>(pos)); |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* @brief Set the status of the property. |
| 410 |
* |
| 411 |
* Sets the status of the property given a specific position. |
| 412 |
* |
| 413 |
* @param[in] pos The status to set. |
| 414 |
* @param[in] on The value to set the status to. |
| 415 |
*/ |
| 416 |
void setStatus(Status pos, bool on); |
| 417 |
|
| 418 |
/** |
| 419 |
* @brief Set the status of the property. |
| 420 |
* |
| 421 |
* Sets the status of the property given a specific bitmask. |
| 422 |
* |
| 423 |
* @param[in] status The status to set as a bitmask. |
| 424 |
*/ |
| 425 |
void setStatusValue(unsigned long status); |
| 426 |
|
| 427 |
/** |
| 428 |
* @brief Set the property read only. |
| 429 |
* |
| 430 |
* This sets property editable/grayed out in property editor. |
| 431 |
* @param[in] readOnly True to set the property as read-only, false otherwise. |
| 432 |
*/ |
| 433 |
void setReadOnly(bool readOnly); |
| 434 |
|
| 435 |
/** |
| 436 |
* @brief Check if the property is read-only. |
| 437 |
* |
| 438 |
* @return True if the property is read-only, false otherwise. |
| 439 |
*/ |
| 440 |
inline bool isReadOnly() const |
| 441 |
{ |
| 442 |
return testStatus(App::Property::ReadOnly); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* @brief Set the precision of floating point properties. |
| 447 |
* |
| 448 |
* This sets the precision of properties using floating point |
| 449 |
* numbers to single precision. The default is double precision. |
| 450 |
* |
| 451 |
* @param[in] single True to set single precision, false for double precision. |
| 452 |
*/ |
| 453 |
void setSinglePrecision(bool single) |
| 454 |
{ |
| 455 |
setStatus(App::Property::Single, single); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* @brief Gets precision of floating point properties. |
| 460 |
* |
| 461 |
* @return True if single precision is set, false for double precision. |
| 462 |
*/ |
| 463 |
inline bool isSinglePrecision() const |
| 464 |
{ |
| 465 |
return testStatus(App::Property::Single); |
| 466 |
} |
| 467 |
/// @} |
| 468 |
|
| 469 |
/** |
| 470 |
* @brief Returns a new copy of the property. |
| 471 |
* |
| 472 |
* The copy is mainly used for Undo/Redo and transactions. |
| 473 |
* |
| 474 |
* @return A new copy of the property. |
| 475 |
*/ |
| 476 |
virtual Property* Copy() const = 0; |
| 477 |
|
| 478 |
/** |
| 479 |
* @brief Pastes the value from a property. |
| 480 |
* |
| 481 |
* This function pastes the value from a property into this property. It |
| 482 |
* is mainly used for Undo/Redo and transactions. |
| 483 |
* |
| 484 |
* @param[in] from The property to paste from. |
| 485 |
*/ |
| 486 |
virtual void Paste(const Property& from) = 0; |
| 487 |
|
| 488 |
/** |
| 489 |
* @brief Callback for when a child property has changed value. |
| 490 |
* |
| 491 |
* @param[in] prop The child property that has changed value. |
| 492 |
*/ |
| 493 |
virtual void hasSetChildValue([[maybe_unused]] Property& prop) {} |
| 494 |
|
| 495 |
/** |
| 496 |
* @brief Callback for when a child property is about to change value. |
| 497 |
* |
| 498 |
* It is called before a child property changes value. |
| 499 |
* |
| 500 |
*@param[in] prop The child property that is about to change value. |
| 501 |
*/ |
| 502 |
virtual void aboutToSetChildValue([[maybe_unused]] Property& prop) {} |
| 503 |
|
| 504 |
/** |
| 505 |
* @brief Compare if this property has the same content as the given one. |
| 506 |
* |
| 507 |
* @param[in] other The property to compare with. |
| 508 |
* @return True if the properties are the same, false otherwise. |
| 509 |
*/ |
| 510 |
virtual bool isSame(const Property& other) const; |
| 511 |
|
| 512 |
/** |
| 513 |
* @brief Return a unique ID for the property. |
| 514 |
* |
| 515 |
* The ID of a property is generated from a monotonically increasing |
| 516 |
* internal counter. The intention of the ID is to be used as a key for |
| 517 |
* mapping, instead of using the raw pointer. This prevent the runtime |
| 518 |
* allocator to reuse just deleted memory as a result of using raw |
| 519 |
* pointers, something that is challenging to debug. |
| 520 |
*/ |
| 521 |
int64_t getID() const |
| 522 |
{ |
| 523 |
return _id; |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* @brief Callback for when the property is about to be saved. |
| 528 |
* |
| 529 |
* This method is called before saving the property. It can be overridden |
| 530 |
* by subclasses to implement custom behavior before saving. |
| 531 |
* |
| 532 |
* @see PropertyContainer::beforeSave() |
| 533 |
*/ |
| 534 |
virtual void beforeSave() const {} |
| 535 |
|
| 536 |
friend class PropertyContainer; |
| 537 |
friend struct PropertyData; |
| 538 |
friend class DynamicProperty; |
| 539 |
|
| 540 |
protected: |
| 541 |
/** @brief %Status bits of the property. |
| 542 |
* |
| 543 |
* The first 8 bits are used for the base system the rest can be used in |
| 544 |
* descendent classes to mark special statuses on the objects. |
| 545 |
* The bits and their meaning are listed below: |
| 546 |
* - **0**: object is marked as 'touched' |
| 547 |
* - **1**: object is marked as 'immutable' |
| 548 |
* - **2**: object is marked as 'read-only' (for property editor) |
| 549 |
* - **3**: object is marked as 'hidden' (for property editor) |
| 550 |
* |
| 551 |
* @see Property::Status that defines the position of the status bits. |
| 552 |
*/ |
| 553 |
std::bitset<32> StatusBits; |
| 554 |
|
| 555 |
protected: |
| 556 |
|
| 557 |
/** |
| 558 |
* @brief Callback for when the value of the property has changed. |
| 559 |
* |
| 560 |
* This is called by all setValue() methods after the value has changed. |
| 561 |
*/ |
| 562 |
virtual void hasSetValue(); |
| 563 |
|
| 564 |
/** |
| 565 |
* @brief Callback for when the value of the property is about to change. |
| 566 |
* |
| 567 |
* This is called by all setValue() methods before the value has changed. |
| 568 |
*/ |
| 569 |
virtual void aboutToSetValue(); |
| 570 |
|
| 571 |
/** |
| 572 |
* @brief Verify a path for the current property. |
| 573 |
* |
| 574 |
* @param[in] path The path to verify. |
| 575 |
*/ |
| 576 |
virtual void verifyPath(const App::ObjectIdentifier& path) const; |
| 577 |
|
| 578 |
/** |
| 579 |
* @brief Return a file name suitable for saving this property. |
| 580 |
* |
| 581 |
* @param[in] postfix The postfix to append to the file name. |
| 582 |
* @param[in] prefix The prefix to prepend to the file name. |
| 583 |
* @return The file name for saving the property. |
| 584 |
*/ |
| 585 |
std::string getFileName(const char* postfix = nullptr, const char* prefix = nullptr) const; |
| 586 |
|
| 587 |
public: |
| 588 |
/** |
| 589 |
* @brief The copy constructor is deleted to prevent copying. |
| 590 |
*/ |
| 591 |
Property(const Property&) = delete; |
| 592 |
|
| 593 |
/** |
| 594 |
* @brief The assignment operator is deleted to prevent assignment. |
| 595 |
*/ |
| 596 |
Property& operator=(const Property&) = delete; |
| 597 |
|
| 598 |
private: |
| 599 |
// Sync status with Property_Type |
| 600 |
void syncType(unsigned type); |
| 601 |
|
| 602 |
private: |
| 603 |
PropertyContainer* father {nullptr}; |
| 604 |
const char* myName {nullptr}; |
| 605 |
int64_t _id; |
| 606 |
|
| 607 |
public: |
| 608 |
/// Signal emitted when the property value has changed. |
| 609 |
boost::signals2::signal<void(const App::Property&)> signalChanged; |
| 610 |
}; |
| 611 |
|
| 612 |
|
| 613 |
/** |
| 614 |
* @brief A template class to inhibit nested calls for setting values. |
| 615 |
* |
| 616 |
* A template class that is used to inhibit multiple nested calls to |
| 617 |
* aboutToSetValue/hasSetValue for properties, and only invoke it on change and |
| 618 |
* on the last time it is needed. This is useful in cases where you want to |
| 619 |
* change multiple values in a property "atomically", using possibly multiple |
| 620 |
* primitive functions that normally would trigger aboutToSetValue/hasSetValue |
| 621 |
* calls on their own. |
| 622 |
* |
| 623 |
* To use, inherit privately from the AtomicPropertyChangeInterface class, |
| 624 |
* using your class name as the template argument. In all cases in which you |
| 625 |
* would normally call aboutToSetValue/hasSetValue before and after a change, |
| 626 |
* create an AtomicPropertyChange object. |
| 627 |
* |
| 628 |
* The default constructor assumes you are about to change the property and |
| 629 |
* will call property's Property::aboutToSetValue() if the property has not |
| 630 |
* been marked as changed before by any other AtomicPropertyChange instances in |
| 631 |
* current call stack. You can pass `false` as the a second argument to the |
| 632 |
* constructor, and manually call AtomicPropertyChange::aboutToChange() before |
| 633 |
* actual change, which enables you to prevent unnecessary property copy for |
| 634 |
* undo/redo when there are actual changes. AtomicPropertyChange will |
| 635 |
* guarantee calling Property::hasSetValue() when the last instance in the |
| 636 |
* current call stack is destroyed. |
| 637 |
* |
| 638 |
* One thing to take note is that, because C++ does not allow throwing |
| 639 |
* exception in destructor, any exception thrown when calling property's |
| 640 |
* Property::hasSetValue() will be caught and swallowed. To allow exception |
| 641 |
* propagation, you can manually call AtomicPropertyChange::tryInvoke(). If the |
| 642 |
* condition is satisfied, it will call Property::hasSetValue() that allows |
| 643 |
* exception propagation. |
| 644 |
*/ |
| 645 |
template<class P> |
| 646 |
class AtomicPropertyChangeInterface |
| 647 |
{ |
| 648 |
protected: |
| 649 |
AtomicPropertyChangeInterface() = default; |
| 650 |
|
| 651 |
public: |
| 652 |
/** |
| 653 |
* @brief A class that captures property changes atomically. |
| 654 |
* |
| 655 |
* This class is used to capture multiple property changes atomically. It |
| 656 |
* automatically marks the property as changed and calls the |
| 657 |
* Property::aboutToSetValue() method when the object is constructed. It |
| 658 |
* also automatically calls the Property::hasSetValue() method when the |
| 659 |
* object is destructed, if the property has been marked as changed. This |
| 660 |
* ensures that Property::aboutToSetValue() and Property::hasSetValue() are |
| 661 |
* only called once for multiple property changes. |
| 662 |
* |
| 663 |
* @see AtomicPropertyChangeInterface |
| 664 |
*/ |
| 665 |
class AtomicPropertyChange |
| 666 |
{ |
| 667 |
public: |
| 668 |
/** |
| 669 |
* @brief Construct an AtomicPropertyChange object. |
| 670 |
* |
| 671 |
* @param prop: the property |
| 672 |
* @param markChange: If true, marks the property as changed if it |
| 673 |
* hasn't been marked before, and calls its |
| 674 |
* aboutToSetValue(). |
| 675 |
*/ |
| 676 |
explicit AtomicPropertyChange(P& prop, bool markChange = true) |
| 677 |
: mProp(prop) |
| 678 |
{ |
| 679 |
mProp.signalCounter++; |
| 680 |
if (markChange) { |
| 681 |
aboutToChange(); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* @brief Mark the property as changed |
| 687 |
* |
| 688 |
* It will mark the property as changed only if it has been marked |
| 689 |
* before, and only then will it call the property's aboutToSetValue(). |
| 690 |
*/ |
| 691 |
void aboutToChange() |
| 692 |
{ |
| 693 |
if (!mProp.hasChanged) { |
| 694 |
mProp.hasChanged = true; |
| 695 |
mProp.aboutToSetValue(); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* @brief Destruct an AtomicPropertyChange object. |
| 701 |
* |
| 702 |
* If the property is marked as changed, and this is the last instance |
| 703 |
* of the class in current call stack, it will call property's |
| 704 |
* hasSetValue() |
| 705 |
*/ |
| 706 |
~AtomicPropertyChange() |
| 707 |
{ |
| 708 |
// Signal counter == 1? meaning we are the last one. Invoke |
| 709 |
// hasSetValue() before decrease counter to prevent recursive call |
| 710 |
// triggered by another AtomicPropertyChange created inside |
| 711 |
// hasSetValue(), as it has now been changed to a virtual function. |
| 712 |
if (mProp.signalCounter == 1 && mProp.hasChanged) { |
| 713 |
// Must make sure to not throw in a destructor |
| 714 |
try { |
| 715 |
mProp.hasSetValue(); |
| 716 |
} |
| 717 |
catch (Base::Exception& e) { |
| 718 |
e.reportException(); |
| 719 |
} |
| 720 |
catch (...) { |
| 721 |
} |
| 722 |
mProp.hasChanged = false; |
| 723 |
} |
| 724 |
if (mProp.signalCounter > 0) { |
| 725 |
mProp.signalCounter--; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
/** |
| 730 |
* @brief Check and invoke property's hasSetValue(). |
| 731 |
* |
| 732 |
* Check if this is the last instance and the property has been marked |
| 733 |
* as changed. If so, invoke property's hasSetValue(). |
| 734 |
*/ |
| 735 |
// Destructor cannot throw. So we provide this function to allow error |
| 736 |
// propagation. |
| 737 |
void tryInvoke() |
| 738 |
{ |
| 739 |
if (mProp.signalCounter == 1 && mProp.hasChanged) { |
| 740 |
mProp.hasSetValue(); |
| 741 |
if (mProp.signalCounter > 0) { |
| 742 |
--mProp.signalCounter; |
| 743 |
} |
| 744 |
mProp.hasChanged = false; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
private: |
| 749 |
P& mProp; /**< Referenced to property we work on */ |
| 750 |
}; |
| 751 |
|
| 752 |
protected: |
| 753 |
/// Counter for invoking transaction start/stop. |
| 754 |
int signalCounter {0}; |
| 755 |
/// Flag to indicate if the property has been changed. |
| 756 |
bool hasChanged {false}; |
| 757 |
}; |
| 758 |
|
| 759 |
|
| 760 |
/** |
| 761 |
* @brief Base class for list-like properties. |
| 762 |
* |
| 763 |
* This class is not derived from Property so that we can have more that one |
| 764 |
* base class for list-like properties. |
| 765 |
* |
| 766 |
* @see PropertyList |
| 767 |
* @see PropertyLinkListBase |
| 768 |
*/ |
| 769 |
class AppExport PropertyListsBase |
| 770 |
{ |
| 771 |
public: |
| 772 |
|
| 773 |
/** |
| 774 |
* @brief Set the size of the property list. |
| 775 |
* |
| 776 |
*@param newSize The new size of the property list. |
| 777 |
*/ |
| 778 |
virtual void setSize(int newSize) = 0; |
| 779 |
|
| 780 |
/** |
| 781 |
* @brief Get the size of the property list. |
| 782 |
* |
| 783 |
* @return The size of the property list. |
| 784 |
*/ |
| 785 |
virtual int getSize() const = 0; |
| 786 |
|
| 787 |
/** |
| 788 |
* @brief Get the list of touched elements. |
| 789 |
* |
| 790 |
* @return A set of touched elements. |
| 791 |
*/ |
| 792 |
const std::set<int>& getTouchList() const |
| 793 |
{ |
| 794 |
return _touchList; |
| 795 |
} |
| 796 |
|
| 797 |
/// Clear the list of touched elements. |
| 798 |
void clearTouchList() |
| 799 |
{ |
| 800 |
_touchList.clear(); |
| 801 |
} |
| 802 |
|
| 803 |
protected: |
| 804 |
/** |
| 805 |
* @brief Set the values of the property list with Python values. |
| 806 |
* |
| 807 |
* This method is used to set the values of the property list with values |
| 808 |
* represented as Python objects. If `indices` is empty, all values are |
| 809 |
* set. |
| 810 |
* |
| 811 |
* @param[in] vals The new values for the property list as Python values. |
| 812 |
* @param[in] indices The indices of the values to set. |
| 813 |
*/ |
| 814 |
virtual void setPyValues(const std::vector<PyObject*>& vals, const std::vector<int>& indices) |
| 815 |
{ |
| 816 |
(void)vals; |
| 817 |
(void)indices; |
| 818 |
throw Base::NotImplementedError("not implemented"); |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* @brief Set the values of the property list with a Python object. |
| 823 |
* |
| 824 |
* The Python object is expected to be a dictionary or something that looks |
| 825 |
* like a sequence of values, for example a list or tuple, or an iterable. |
| 826 |
* |
| 827 |
* @param[in] pyObj The Python object to set the values from. |
| 828 |
*/ |
| 829 |
void _setPyObject(PyObject* pyObj); |
| 830 |
|
| 831 |
protected: |
| 832 |
/// The list of touched elements. |
| 833 |
std::set<int> _touchList; |
| 834 |
}; |
| 835 |
|
| 836 |
/** |
| 837 |
* @brief The base class of all property lists. |
| 838 |
* |
| 839 |
* The PropertyLists class is the base class for properties that can contain |
| 840 |
* multiple values, not only a single value. All property types that may |
| 841 |
* contain more than one value inherit from this class. |
| 842 |
*/ |
| 843 |
class AppExport PropertyLists: public Property, public PropertyListsBase |
| 844 |
{ |
| 845 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 846 |
|
| 847 |
public: |
| 848 |
/** |
| 849 |
* @brief Set the values of the property list with a Python object. |
| 850 |
* |
| 851 |
* The Python object is expected to be a dictionary or something that looks |
| 852 |
* like a sequence of values, for example a list or tuple, or an iterable. |
| 853 |
* |
| 854 |
* @param[in] obj The Python object to set the values from. |
| 855 |
*/ |
| 856 |
void setPyObject(PyObject* obj) override |
| 857 |
{ |
| 858 |
_setPyObject(obj); |
| 859 |
} |
| 860 |
|
| 861 |
/** |
| 862 |
* @brief Set the order of the elements to be relevant. |
| 863 |
* |
| 864 |
* If the order of the elements in the list is relevant, certain |
| 865 |
* operations, such as restoring, must ensure that the order is kept |
| 866 |
* despite potential errors. |
| 867 |
* |
| 868 |
* @param[in] on True to set the order as relevant, false otherwise. |
| 869 |
*/ |
| 870 |
inline void setOrderRelevant(bool on) |
| 871 |
{ |
| 872 |
this->setStatus(Status::Ordered, on); |
| 873 |
} |
| 874 |
|
| 875 |
/** |
| 876 |
* @brief Check if the order of the elements is relevant. |
| 877 |
* |
| 878 |
* @return True if the order is relevant, false otherwise. |
| 879 |
*/ |
| 880 |
inline bool isOrderRelevant() const |
| 881 |
{ |
| 882 |
return this->testStatus(Status::Ordered); |
| 883 |
} |
| 884 |
}; |
| 885 |
|
| 886 |
/** |
| 887 |
* @brief Helper class to implement PropertyLists. |
| 888 |
* |
| 889 |
* This class combines property storage (via a standard container ListT) with |
| 890 |
* the change-notification interface in AtomicPropertyChangeInterface. |
| 891 |
* |
| 892 |
* @tparam T The type of individual property values. |
| 893 |
* @tparam ListT The container type for holding values (defaults to @c std::vector<T>). |
| 894 |
* @tparam ParentT The base class providing core property-list behavior |
| 895 |
* (defaults to PropertyLists). |
| 896 |
*/ |
| 897 |
template<class T, class ListT = std::vector<T>, class ParentT = PropertyLists> |
| 898 |
class PropertyListsT: public ParentT, |
| 899 |
public AtomicPropertyChangeInterface<PropertyListsT<T, ListT, ParentT>> |
| 900 |
{ |
| 901 |
public: |
| 902 |
/// Alias for a reference to a const element in the list. |
| 903 |
using const_reference = typename ListT::const_reference; |
| 904 |
|
| 905 |
/// The underlying container type. |
| 906 |
using list_type = ListT; |
| 907 |
|
| 908 |
/// The base class type. |
| 909 |
using parent_type = ParentT; |
| 910 |
|
| 911 |
/** |
| 912 |
* @brief Helper type for performing atomic property changes. |
| 913 |
* |
| 914 |
* This is defined by the AtomicPropertyChangeInterface class. |
| 915 |
*/ |
| 916 |
using atomic_change = typename AtomicPropertyChangeInterface< |
| 917 |
PropertyListsT<T, ListT, ParentT>>::AtomicPropertyChange; |
| 918 |
|
| 919 |
/** |
| 920 |
* @brief Grant atomic_change access to private internals. |
| 921 |
* |
| 922 |
* Allows atomic_change operations to modify the internal state directly. |
| 923 |
*/ |
| 924 |
friend atomic_change; |
| 925 |
|
| 926 |
/** |
| 927 |
* @brief Resize the property list, filling new slots with a given value. |
| 928 |
* |
| 929 |
* @param[in] newSize The desired total number of elements. |
| 930 |
* @param[in] def The single element value to use for all newly-added slots. |
| 931 |
*/ |
| 932 |
virtual void setSize(int newSize, const_reference def) |
| 933 |
{ |
| 934 |
_lValueList.resize(newSize, def); |
| 935 |
} |
| 936 |
|
| 937 |
/** |
| 938 |
* @brief Resize the property list, value-initializing any new elements. |
| 939 |
* |
| 940 |
* Adjusts the container to hold exactly @p newSize elements. |
| 941 |
* - If @p newSize is less than the current size, the container is shrunk. |
| 942 |
* - If @p newSize is greater, new elements are default-constructed |
| 943 |
* |
| 944 |
* @param[in] newSize The desired total number of elements. |
| 945 |
*/ |
| 946 |
void setSize(int newSize) override |
| 947 |
{ |
| 948 |
_lValueList.resize(newSize); |
| 949 |
} |
| 950 |
|
| 951 |
/** |
| 952 |
* @brief Get the size of the list. |
| 953 |
* |
| 954 |
* @return The size of the list. |
| 955 |
*/ |
| 956 |
int getSize() const override |
| 957 |
{ |
| 958 |
return static_cast<int>(_lValueList.size()); |
| 959 |
} |
| 960 |
|
| 961 |
/** |
| 962 |
* @brief Set a property list to a given value. |
| 963 |
* |
| 964 |
* Clears any existing values and makes the list contain exactly one |
| 965 |
* element initialized to @p value. |
| 966 |
* |
| 967 |
* @param[in] value The value to assign. |
| 968 |
*/ |
| 969 |
void setValue(const_reference value) |
| 970 |
{ |
| 971 |
ListT vals; |
| 972 |
vals.resize(1, value); |
| 973 |
setValues(vals); |
| 974 |
} |
| 975 |
|
| 976 |
/** |
| 977 |
* @brief Replace the entire list of values. |
| 978 |
* |
| 979 |
* Clears and assigns @p newValues to the internal list, notifying |
| 980 |
* observers of the change atomically. |
| 981 |
* |
| 982 |
* @param[in] newValues The new container of values (defaults to empty list). |
| 983 |
*/ |
| 984 |
virtual void setValues(const ListT& newValues = ListT()) |
| 985 |
{ |
| 986 |
atomic_change guard(*this); |
| 987 |
this->_touchList.clear(); |
| 988 |
this->_lValueList = newValues; |
| 989 |
guard.tryInvoke(); |
| 990 |
} |
| 991 |
|
| 992 |
/** |
| 993 |
* @brief Alias for setValues(). |
| 994 |
* |
| 995 |
* @param[in] newValues The new container of values (defaults to empty list). |
| 996 |
*/ |
| 997 |
void setValue(const ListT& newValues = ListT()) |
| 998 |
{ |
| 999 |
setValues(newValues); |
| 1000 |
} |
| 1001 |
|
| 1002 |
/** |
| 1003 |
* @brief Retrieve the underlying list of values. |
| 1004 |
* |
| 1005 |
* @return A const reference to the internal container. |
| 1006 |
*/ |
| 1007 |
const ListT& getValues() const |
| 1008 |
{ |
| 1009 |
return _lValueList; |
| 1010 |
} |
| 1011 |
|
| 1012 |
/** |
| 1013 |
* @brief Alias for getValues(). |
| 1014 |
* |
| 1015 |
* @return A const reference to the internal container. |
| 1016 |
*/ |
| 1017 |
const ListT& getValue() const |
| 1018 |
{ |
| 1019 |
return getValues(); |
| 1020 |
} |
| 1021 |
|
| 1022 |
/** |
| 1023 |
* @brief Retrieve an element by index. |
| 1024 |
* |
| 1025 |
* @param[in] idx The index of the element to retrieve. |
| 1026 |
* @return The element at position @p idx. |
| 1027 |
* |
| 1028 |
* @note No bounds-check is performed. |
| 1029 |
*/ |
| 1030 |
const_reference operator[](int idx) const |
| 1031 |
{ |
| 1032 |
return _lValueList[idx]; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/** |
| 1036 |
* @brief Compare two Property instances for equivalence. |
| 1037 |
* |
| 1038 |
* @param[in] other The other Property to compare against. |
| 1039 |
* @return True if both are of the same type and contain equal values. |
| 1040 |
*/ |
| 1041 |
bool isSame(const Property& other) const override |
| 1042 |
{ |
| 1043 |
if (&other == this) { |
| 1044 |
return true; |
| 1045 |
} |
| 1046 |
return this->getTypeId() == other.getTypeId() |
| 1047 |
&& this->getValue() == static_cast<decltype(this)>(&other)->getValue(); |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** |
| 1051 |
* @brief Set the property value from a Python object. |
| 1052 |
* |
| 1053 |
* Attempts to extract a value via getPyValue() and assign it; |
| 1054 |
* on failure, falls back to the ParentT implementation. |
| 1055 |
* |
| 1056 |
* @param[in] value A PyObject pointer representing the new value. |
| 1057 |
*/ |
| 1058 |
void setPyObject(PyObject* value) override |
| 1059 |
{ |
| 1060 |
try { |
| 1061 |
setValue(getPyValue(value)); |
| 1062 |
return; |
| 1063 |
} |
| 1064 |
catch (...) { |
| 1065 |
} |
| 1066 |
parent_type::setPyObject(value); |
| 1067 |
} |
| 1068 |
|
| 1069 |
/** |
| 1070 |
* @brief Set a value at a specific index, growing the list if needed. |
| 1071 |
* |
| 1072 |
* If @p index is -1 or equal to the current size, the list grows by one. |
| 1073 |
* Otherwise, the existing element at @p index is replaced. |
| 1074 |
* Observers are notified of the change atomically. |
| 1075 |
* |
| 1076 |
* @param[in] index The position at which to set @p value (-1 for append). |
| 1077 |
* @param[in] value The new element value. |
| 1078 |
* @throw Base::RuntimeError if @p index is out of bounds (< -1 or > size). |
| 1079 |
*/ |
| 1080 |
virtual void set1Value(int index, const_reference value) |
| 1081 |
{ |
| 1082 |
int size = getSize(); |
| 1083 |
if (index < -1 || index > size) { |
| 1084 |
throw Base::RuntimeError("index out of bound"); |
| 1085 |
} |
| 1086 |
|
| 1087 |
atomic_change guard(*this); |
| 1088 |
if (index == -1 || index == size) { |
| 1089 |
index = size; |
| 1090 |
setSize(index + 1, value); |
| 1091 |
} |
| 1092 |
else { |
| 1093 |
_lValueList[index] = value; |
| 1094 |
} |
| 1095 |
this->_touchList.insert(index); |
| 1096 |
guard.tryInvoke(); |
| 1097 |
} |
| 1098 |
|
| 1099 |
protected: |
| 1100 |
void setPyValues(const std::vector<PyObject*>& vals, const std::vector<int>& indices) override |
| 1101 |
{ |
| 1102 |
if (indices.empty()) { |
| 1103 |
ListT values {}; |
| 1104 |
values.reserve(vals.size()); |
| 1105 |
for (auto* valsContent : vals) { |
| 1106 |
values.push_back(getPyValue(valsContent)); |
| 1107 |
} |
| 1108 |
setValues(std::move(values)); |
| 1109 |
return; |
| 1110 |
} |
| 1111 |
assert(vals.size() == indices.size()); |
| 1112 |
atomic_change guard(*this); |
| 1113 |
int i {0}; |
| 1114 |
for (auto index : indices) { |
| 1115 |
set1Value(index, getPyValue(vals[i])); |
| 1116 |
i++; |
| 1117 |
} |
| 1118 |
guard.tryInvoke(); |
| 1119 |
} |
| 1120 |
|
| 1121 |
/** |
| 1122 |
* @brief Convert a Python object to a value of type T. |
| 1123 |
* |
| 1124 |
* @param[in] item The Python object to convert. |
| 1125 |
* |
| 1126 |
* @return The converted value of type T. |
| 1127 |
*/ |
| 1128 |
virtual T getPyValue(PyObject* item) const = 0; |
| 1129 |
|
| 1130 |
protected: |
| 1131 |
ListT _lValueList; |
| 1132 |
}; |
| 1133 |
|
| 1134 |
} // namespace App |
| 1135 |
|
| 1136 |
#endif // APP_PROPERTY_H |