Union.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "Object.h"
23 
24 namespace ConfigDB
25 {
29 class Union : public Object
30 {
31 public:
32  using Tag = uint8_t;
33 
34  using Object::Object;
35 
39  Tag getTag() const
40  {
41  return getPropertyData(0)->uint8;
42  }
43 
47  void setTag(Tag tag)
48  {
49  assert(tag < typeinfo().objectCount);
50  disposeArrays();
51  memset(getDataPtr(), 0, typeinfo().structSize);
52  getPropertyData(0)->uint8 = tag;
54  }
55 
59  void clear()
60  {
61  disposeArrays();
62  memset(getDataPtr(), 0, typeinfo().structSize);
63  getPropertyData(0)->uint8 = 0;
64  getObject(0).clear();
65  }
66 
67  unsigned getObjectCount() const
68  {
69  return 1;
70  }
71 
72  Object getObject([[maybe_unused]] unsigned index)
73  {
74  assert(index == 0);
75  return Object(*this, getTag());
76  }
77 
78  unsigned getPropertyCount() const
79  {
80  return 0;
81  }
82 
83  PropertyConst getProperty(unsigned) const
84  {
85  return {};
86  }
87 
89  {
90  return {};
91  }
92 
96  template <typename Item> const Item as(Tag tag) const
97  {
98  assert(getTag() == tag);
99  if(getTag() != tag) {
100  return {};
101  }
102  return Item(*parent, typeinfo().getObject(tag), dataRef + propinfo().offset);
103  }
104 
108  template <typename Item> Item as(Tag tag)
109  {
110  assert(getTag() == tag);
111  if(getTag() != tag) {
112  return {};
113  }
114  return Item(*parent, typeinfo().getObject(tag), dataRef + propinfo().offset);
115  }
116 
120  template <typename Item> Item to(Tag tag)
121  {
122  setTag(tag);
123  return Item(*parent, typeinfo().getObject(tag), dataRef + propinfo().offset);
124  }
125 };
126 
131 template <class ClassType> class UnionTemplate : public Union
132 {
133 public:
134  using Union::Union;
135 };
136 
142 template <class UpdaterType, class ClassType> class UnionUpdaterTemplate : public ClassType
143 {
144 public:
145  using ClassType::ClassType;
146 
147  explicit operator bool() const
148  {
149  return Object::operator bool() && this->isWriteable();
150  }
151 };
152 
153 } // namespace ConfigDB
An object can contain other objects, properties and arrays.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:37
Object()
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:39
Object * parent
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:299
const ObjectInfo & typeinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:232
const PropertyInfo & propinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:227
PropertyData * getPropertyData(unsigned index)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:237
uint16_t dataRef
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:300
void resetToDefaults()
Does a 'clear' followed by 'loadArrayDefaults'.
void * getDataPtr()
void clear()
Reset contents to defaults (except arrays, which are cleared)
Manages a key/value pair stored in an object, or a simple array value.
Definition: Property.h:32
Definition: Property.h:69
Used by code generator.
Definition: Union.h:132
Used by code generator.
Definition: Union.h:143
Variant object, can contain one of a selection of objects types.
Definition: Union.h:30
Item as(Tag tag)
Used by code generator to obtain a writeable reference to the contained object.
Definition: Union.h:108
PropertyConst getProperty(unsigned) const
Definition: Union.h:83
Object()
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:39
Object getObject([[maybe_unused]] unsigned index)
Definition: Union.h:72
uint8_t Tag
Definition: Union.h:32
void setTag(Tag tag)
Set the current tag and reset content to object default.
Definition: Union.h:47
void clear()
Reset tag to default and clear whatever object that corresponds to.
Definition: Union.h:59
const Item as(Tag tag) const
Used by code generator to obtain a read-only reference to the contained object.
Definition: Union.h:96
Item to(Tag tag)
Used by code generator to set the tag and return a direct reference to the contained object.
Definition: Union.h:120
unsigned getObjectCount() const
Definition: Union.h:67
unsigned getPropertyCount() const
Definition: Union.h:78
Property getProperty(unsigned)
Definition: Union.h:88
Tag getTag() const
Get the current tag which identifies the stored type.
Definition: Union.h:39
Definition: Array.h:26
uint8_t uint8
Definition: PropertyData.h:45