Union.h
Go to the documentation of this file.
1 /****
2  * ConfigDB/Union.h
3  *
4  * Copyright 2024 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the ConfigDB Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Object.h"
23 
24 namespace ConfigDB
25 {
31 class Union : public Object
32 {
33 public:
37  using Tag = uint8_t;
38 
39  using Object::Object;
40 
44  Tag getTag() const
45  {
46  return getPropertyData(0)->uint8;
47  }
48 
52  void setTag(Tag tag)
53  {
54  if(!writeCheck()) {
55  return;
56  }
57  assert(tag < typeinfo().objectCount);
58  disposeArrays();
59  memset(getDataPtr(), 0, typeinfo().structSize);
60  getPropertyData(0)->uint8 = tag;
62  }
63 
67  void clear()
68  {
69  if(!writeCheck()) {
70  return;
71  }
72  disposeArrays();
73  memset(getDataPtr(), 0, typeinfo().structSize);
74  getPropertyData(0)->uint8 = 0;
75  getObject(0).clear();
76  }
77 
78  unsigned getObjectCount() const
79  {
80  return 1;
81  }
82 
83  Object getObject([[maybe_unused]] unsigned index)
84  {
85  assert(index == 0);
86  return Object(*this, getTag());
87  }
88 
89  unsigned getPropertyCount() const
90  {
91  return 0;
92  }
93 
94  PropertyConst getProperty(unsigned) const
95  {
96  return {};
97  }
98 
100  {
101  return {};
102  }
103 
109  template <typename Item> const Item as(Tag tag) const
110  {
111  assert(getTag() == tag);
112  if(getTag() != tag) {
113  return {};
114  }
115  return Item(*parent, typeinfo().getObject(tag), dataRef + propinfo().offset);
116  }
117 
123  template <typename Item> Item as(Tag tag)
124  {
125  assert(getTag() == tag);
126  if(getTag() != tag) {
127  return {};
128  }
129  return Item(*parent, typeinfo().getObject(tag), dataRef + propinfo().offset);
130  }
131 
137  template <typename Item> Item to(Tag tag)
138  {
139  setTag(tag);
140  return Item(*parent, typeinfo().getObject(tag), dataRef + propinfo().offset);
141  }
142 };
143 
148 template <class ClassType> class UnionTemplate : public Union
149 {
150 public:
151  using Union::Union;
152 };
153 
159 template <class UpdaterType, class ClassType> class UnionUpdaterTemplate : public ClassType
160 {
161 public:
162  using ClassType::ClassType;
163 
164  explicit operator bool() const
165  {
166  return Object::operator bool() && this->isWriteable();
167  }
168 };
169 
170 } // 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:301
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:302
void resetToDefaults()
Does a 'clear' followed by 'loadArrayDefaults'.
void * getDataPtr()
void clear()
Reset contents to defaults (except arrays, which are cleared)
bool writeCheck() const
Read-only access to a key/value pair stored in an object, or a simple array value.
Definition: Property.h:32
Adds write access for a key/value pair stored in an object, or a simple array value.
Definition: Property.h:80
Used by code generator.
Definition: Union.h:149
Used by code generator.
Definition: Union.h:160
Variant object, can contain one of a selection of object types A union contains one private property,...
Definition: Union.h:32
Item as(Tag tag)
Used by code generator to obtain a writeable reference to the contained object.
Definition: Union.h:123
PropertyConst getProperty(unsigned) const
Definition: Union.h:94
Object()
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:39
Object getObject([[maybe_unused]] unsigned index)
Definition: Union.h:83
uint8_t Tag
A zero-based index which identifies the stored object type.
Definition: Union.h:37
void setTag(Tag tag)
Set the current tag and reset content to object default.
Definition: Union.h:52
void clear()
Reset tag to default and clear whatever object that corresponds to.
Definition: Union.h:67
const Item as(Tag tag) const
Used by code generator to obtain a read-only reference to the contained object.
Definition: Union.h:109
Item to(Tag tag)
Used by code generator to set the tag and return a direct reference to the contained object.
Definition: Union.h:137
unsigned getObjectCount() const
Definition: Union.h:78
unsigned getPropertyCount() const
Definition: Union.h:89
Property getProperty(unsigned)
Definition: Union.h:99
Tag getTag() const
Get the current tag which identifies the stored type.
Definition: Union.h:44
Definition: Array.h:26
uint8_t uint8
Definition: PropertyData.h:45