OpenLexocad  27.0
PropertyContainer.h
Go to the documentation of this file.
1 #pragma once
2 #pragma warning(disable : 4251)
3 
4 #include <Base/Persistance.h>
5 #include <Core/PropertyMacros.h>
6 #include <Core/PropertyValue.h>
7 #include <Core/Variant.h>
8 #include <Core/core_defines2.h>
9 #include <Geom/BrepData.h>
10 
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <unordered_map>
15 
16 #define USE_PROPERTYMAP
17 
18 static const char* PropertyName = "Property";
19 
20 class PropertyTree;
22 class CA_Writer;
23 class Base::Reader;
24 class QFont;
25 class Draw::DrawStyle;
26 class Draw::Arrowheads;
27 class MD5;
28 class Draw::Texture2Transform;
29 
30 
31 
32 namespace Base
33 {
34 class Color;
35 }
36 
37 namespace Core
38 {
39 class FieldData
40 {
41 public:
42  FieldData(const Core::FieldData* /*fd*/){};
43 };
44 } // namespace Core
45 
46 namespace App
47 {
48 class ElementTool;
49 }
50 
51 
52 
53 #define MUTABLE_CONTAINER(_class_) \
54 public: \
55  static bool isMutableStatic() { return true; } \
56 \
57 public: \
58  virtual bool isMutable() const { return true; }
59 
60 #define IMMUTABLE_CONTAINER(_class_) \
61 public: \
62  friend class Core::PropertyBundle<_class_>; \
63 \
64 public: \
65  static bool isMutableStatic() { return false; } \
66 \
67 public: \
68  virtual bool isMutable() const { return false; } \
69 \
70 public: \
71  Core::PropertyBundle<_class_> getPropertyBundle() const \
72  { \
73  Core::PropertyBundle<_class_> dsBundle; \
74  dsBundle.setPropertyValues(this); \
75  return dsBundle; \
76  }
77 
78 
79 namespace Core
80 {
81 class CoreDocument;
82 class Placement;
83 class DocObject;
84 class ObjectExecutor;
85 class Property;
86 class DbgInfo;
87 class PropertyUser;
88 
90 {
91 public:
92  LxFieldData(void);
93  LxFieldData(const LxFieldData* fd);
94  void addField(void* base, char* fieldname, void* fieldptr);
95  std::unordered_map<std::string, ptrdiff_t> m_fieldmap;
96  bool fieldsAdded = false;
97 };
98 
99 
100 
102 {
104  LX_NODE_HEADER();
106 
107 public:
108  friend class PostInitClass;
109  friend class CoreDocument;
110  friend class ObjectExecutor;
111  friend class ::App::ElementTool;
112 
113  enum class CheckStatus
114  {
115  IsNew,
116  IsUpdated
117  };
118 
120  // //
121  // --------------------- BEGIN API --------------------- //
122  // //
123  // ATTENTION: DO NOT CHANGE ANY SIGNATURES IN THE API ! //
124  // //
126 
127  PropertyContainer(void);
128  // Copy constructor
130  virtual ~PropertyContainer(void) = default;
131 
134  bool addProperty(Core::Property* p, const std::string& name);
136  Core::Property* addProperty(Base::Type t, const std::string& name, const Core::Variant& defaultValue);
138  Core::PropertyUser* addPropertyUser(const std::string& name, const Core::Variant& value);
140  void removeProperty(Core::Property* p, std::string pName);
142  void removeAllProperties();
144  inline Core::Property* getPropertyByName(const std::string& name) const;
146  inline Core::Property* getPropertyByName(const Base::String& name) const;
147 
149  template <typename T>
150  T* getPropertyByName(const std::string& name) const
151  {
152  Core::Property* p = getPropertyByName(name);
153 
154  if (p && p->isDerivedFrom<T>())
155  {
156  return static_cast<T*>(p);
157  }
158 
159  return nullptr;
160  }
161 
162 
164  void getPropertyMap(Core::PropertyMap&) const;
166  std::vector<std::pair<Core::Property*, std::string> > getPropertiesAndNameByType(Base::Type type) const;
167  std::vector<Core::Property*> getPropertiesByType(Base::Type type) const;
168  std::vector<Core::Property*> getPropertiesByTypes(const std::vector<Base::Type>& type) const;
170  bool setPropertyFromVariant(const std::string& name, const Core::Variant& value);
171 
172  bool setProperty(const std::string& name, double value);
173  bool setProperty(const std::string& name, int value);
174  bool setProperty(const std::string& name, const Geom::Vec& value);
175  bool setProperty(const std::string& name, const std::list<Geom::Vec>& value);
176  bool setProperty(const std::string& name, const Geom::Trsf& value);
177  bool setProperty(const std::string& name, const std::string& value);
178  bool setProperty(const std::string& name, const char* value);
179  bool setProperty(const std::string& name, const Base::String& value);
180  bool setProperty(const std::string& name, const Geom::Pnt& value);
181  bool setProperty(const std::string& name, const Geom::Pnt2d& value);
182  bool setProperty(const std::string& name, const std::vector<Geom::Pnt>& value);
183  bool setProperty(const std::string& name, const std::vector<Geom::Pnt2d>& value);
184  // bool setProperty(const std::string& name, const std::list<std::string>& value);
185  // bool setProperty(const std::string& name, const Core::Placement& value);
186  bool setProperty(const std::string& name, const MD5& value);
187  bool setProperty(const std::string& name, bool value);
188  bool setProperty(const std::string& name, Core::DocObject* value);
189  // without this method, method setProperty(const std::string& name, bool value) is called for const Core::DocObject
190  bool setProperty(const std::string& name, const Core::DocObject* value);
191  bool setProperty(const std::string& name, const boost::unordered_set<Core::DocObject*>& value);
192  bool setProperty(const std::string& name, const std::list<Core::DocObject*>& value);
193  bool setProperty(const std::string& name, const std::vector<int>& value);
194  bool setProperty(const std::string& name, const QFont& value);
195  bool setProperty(const std::string& name, const Draw::DrawStyle& value);
196  bool setProperty(const std::string& name, const Draw::Arrowheads& value);
197  bool setProperty(const std::string& name, const Geom::Dir& value);
198  bool setProperty(const std::string& name, const Geom::Dir2d& value);
199  bool setProperty(const std::string& name, const Base::Color& value);
200  bool setProperty(const std::string& name, const Geom::Ax1& value);
201  bool setProperty(const std::string& name, const Geom::Ax2& value);
202  // bool setProperty(const std::string& name, const gp_Ax22d& value);
203  bool setProperty(const std::string& name, const Draw::Texture2Transform& value);
204  bool setProperty(const std::string& name, const std::list<std::list<Geom::Pnt> >& value);
205  bool setProperty(const std::string& name, const std::list<double>& value);
206  bool setProperty(const std::string& name, pBrepData& value);
207 
208 
209 
215  virtual bool onBeforeChange(Core::Property* p, const Core::Variant& newValue = Core::Variant());
217  virtual void onChanged(Core::Property* p);
219  virtual void onDeleted();
221  virtual void onNew();
223  virtual void save(Base::AbstractWriter& writer, Base::PersistanceVersion& version);
225  virtual void restore(Base::AbstractXMLReader& reader, Base::PersistanceVersion& version);
228  bool enableNotify(const bool enable);
230  bool isNotifyEnabled(void) const;
232  void touch();
233 
236  bool isDeleted() const;
238  bool isStatusDeleted() const;
240  bool isMarkedForDelete() const;
242  bool isMarkedForDeleteFinal() const;
244  bool isDeletedFinal() const;
246  bool isNew() const;
248  bool isUpdated() const;
250  bool isValid() const;
252  bool hasErrors() const;
254  void setData(const std::string& key, void* data);
256  void* getData(const std::string& key, bool* ok = nullptr) const;
258  void removeData(const std::string& key);
260  bool hasSameValuesAs(const PropertyContainer* container) const;
262  void setPropertyValues(const PropertyContainer* other);
264  template <typename T>
265  T getPropertyValueByName(const std::string& propertyName, bool* ok = 0) const
266  {
267  Core::Property* p = getPropertyByName(propertyName);
268  if (p)
269  return p->getVariant().getValue<T>(ok);
270  else
271  return Core::Variant().getValue<T>(ok);
272  }
273 
276  virtual void ensureIntegrity();
281  void integrityMaintained();
283  void integrityLost();
285  bool checkIntegrity();
286 
287 
288 protected:
290  void addPropertiesFrom(const Core::PropertyContainer* other);
291 
293  // //
294  // ---------------------- END API ---------------------- //
295  // //
297 
298 
299 #ifndef LXAPI // INTERFACES BELOW ARE -NOT- PART OF THE LEXOCAD API
300 
301 public:
303  Core::PropertyValueMap getPropertyValueMap() const;
305  std::map<std::string, Core::Variant> getKeyValueMap() const;
307  bool setPropertyFromKeyValue(const std::string& propertyName, const std::string& key, const Core::Variant& value);
309  std::set<Core::Property*> getUpdatedProperties() const;
311  void setUpdated();
312  void setUpdated(const Core::Property* p);
314  void setHasErrors();
316  void setNew();
318  void setValid();
320  void setDeleted();
322  void setMarkedForDelete();
324  void setMarkedForDeleteFinal();
326  void setDeleteFinal();
327 
328  unsigned int getPropertyChangeStatus() const;
330  CheckStatus getCheckStatus() const;
332  void setCheckStatus(CheckStatus status);
334  void setAllPropertiesValid();
336  void setPropertyValues(const Core::PropertyValueMap& other);
338  std::vector<Core::Property*> getPropertiesOrdered() const;
339 
341  void __setCheckIsNew__();
342  virtual bool check_lx(char* f, char* n);
343 
344 
345 
346  bool isRestored = false;
347 
348 #ifdef USE_PROPERTYMAP
349 
351  bool rollBack();
352 
354  void saveLastValidPropertyMap();
355 
357  const Core::PropertyValueMap& getLastValidPropertyMap() const;
358 #endif
359 
360 
361 protected:
362  enum class Status
363  {
364  New = 1,
365  Updated = 2,
366  Valid = 3,
367  MarkedForDelete = 4,
368  MarkedForDeleteFinal,
369  Deleted,
370  DeletedFinal,
371  Error
372  };
373 
374  virtual void setStatus(Status status);
375  Status getStatus() const;
376 
377  bool checkForAlreadyExistingProperty(Core::Property* property);
378 
380  virtual void restoreProperty(Core::Property* property,
381  const Base::String& name,
382  Base::AbstractXMLReader& reader,
384 
385  unsigned int _changedProperties;
386 
387 private:
389  void _setPropertyValues(const Core::PropertyValueMap& pmap, bool isInit = false);
391  void _setPropertyValues(const Core::PropertyContainer* other, bool isInit = false);
392  void getPropertyMapSetNotify(bool v);
393 
394  std::unordered_map<std::string, Core::Property*> _userPropertyMap;
395 #ifdef USE_PROPERTYMAP
396  std::unordered_map<std::string, PropertyValue> _lastValidPropertyMap;
397 #endif
398  std::unordered_map<std::string, void*> _data;
399  Status _status = Status::New;
400  CheckStatus _checkstatus = CheckStatus::IsNew;
401  bool _notify = true;
402 
406  bool _integrity = true;
407 #endif
408 };
409 
410 
411 
413 {
414 public:
416  ~PostInitClass();
418 };
419 
420 } // namespace Core
unsigned int _changedProperties
Definition: PropertyContainer.h:385
T * getPropertyByName(const std::string &name) const
Returns the property of type T with name 'name'. Returns 'nullptr' on failure.
Definition: PropertyContainer.h:150
#define CORE_EXPORT
Definition: core_defines2.h:10
CoreDocument()
Definition: Color.h:47
Defines a non-persistent 3D Cartesian point.
Definition: Pnt.h:42
Definition: PropertyContainer.h:89
Definition: md5.h:71
Definition: PropertyContainer.h:101
Core::PropertyText name
Definition: CoreDocument.h:143
Definition: Dir2d.h:38
CheckStatus
Definition: PropertyContainer.h:113
std::shared_ptr< Geom::BrepData > pBrepData
Definition: BrepData.h:34
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:29
Definition: Persistance.h:20
LX_CORE_EXPORT Version & version
Definition: Type.h:56
Definition: CoreDocument.h:197
Definition: Variant.h:90
Definition: Writer.h:209
T getValue(bool *ok) const
Definition: Variant.h:320
Definition: Ax2.h:69
void restoreProperty(Core::Property *property, const Base::String &name, Base::AbstractXMLReader &reader, Base::PersistanceVersion &version) override
Persistance class and root of the type system.
Definition: Persistance.h:27
std::unordered_map< std::string, Core::Property * > PropertyMap
Definition: PropertyValue.h:28
Definition: AbstractXMLReader.h:10
Definition: Trsf.h:58
Definition: Dir.h:46
std::unordered_map< std::string, ptrdiff_t > m_fieldmap
Definition: PropertyContainer.h:95
std::unordered_map< std::string, PropertyValue > PropertyValueMap
Definition: PropertyValue.h:29
Definition: Base.h:19
PostInitClass(Core::PropertyContainer *p)
Definition: PropertyContainer.h:415
bool isDerivedFrom() const
Definition: Base.h:114
Definition: Writer.h:27
Definition: Ax1.h:52
Defines a non-persistent vector in 3D space.
Definition: Vec.h:45
#define MUTABLE_CONTAINER(_class_)
Definition: PropertyContainer.h:53
T getPropertyValueByName(const std::string &propertyName, bool *ok=0) const
Returns the property value T with this name. If ok=true T holds a valid value.
Definition: PropertyContainer.h:265
Definition: PropertyContainer.h:412
FieldData(const Core::FieldData *)
Definition: PropertyContainer.h:42
Definition: PropertyContainer.h:39
LX_NODE_HEADER()
Definition: Property.h:81
Status
Definition: PropertyContainer.h:362
Core::PropertyContainer * container
Definition: PropertyContainer.h:417
Core::PropertyUser is a class that can hold properties defined by the user. Its value member is a Cor...
Definition: PropertyUser.h:17
Definition: AbstractXMLReader.h:8
virtual Core::Variant getVariant(void) const =0
Defines a non-persistent 2D cartesian point.
Definition: Pnt2d.h:33
TYPESYSTEM_HEADER()
Definition: DocObject.h:28