PropertyInfo.h
Go to the documentation of this file.
1 /****
2  * ConfigDB/PropertyInfo.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 <WString.h>
23 #include <Data/Range.h>
24 #include "Number.h"
25 #include <FlashString/Array.hpp>
26 #include <FlashString/Vector.hpp>
27 
31 #define CONFIGDB_PROPERTY_TYPE_MAP(XX) \
32  XX(Boolean, 1) \
33  XX(Int8, 1) \
34  XX(Int16, 2) \
35  XX(Int32, 4) \
36  XX(Int64, 8) \
37  XX(Enum, 1) \
38  XX(UInt8, 1) \
39  XX(UInt16, 2) \
40  XX(UInt32, 4) \
41  XX(Number, 4) \
42  XX(String, sizeof(StringId)) \
43  XX(Object, sizeof(ObjectInfo*)) \
44  XX(Alias, 0)
45 
46 namespace ConfigDB
47 {
48 DEFINE_FSTR_LOCAL(fstr_empty, "")
49 
50 enum class PropertyType : uint32_t {
51 #define XX(name, ...) name,
53 #undef XX
54 };
55 
59 using StringId = uint16_t;
60 
61 struct ObjectInfo;
62 
63 inline uint8_t getPropertySize(PropertyType type)
64 {
65  switch(type) {
66 #define XX(tag, size) \
67  case PropertyType::tag: \
68  return size;
70 #undef XX
71  }
72  return 0;
73 }
74 
75 struct EnumInfo {
77  FSTR::ObjectBase values; //< Possible values
78 
79  uint8_t getItemSize() const
80  {
81  return (type == PropertyType::String) ? sizeof(FSTR::String*) : getPropertySize(type);
82  }
83 
84  unsigned length() const
85  {
86  return values.length() / getItemSize();
87  }
88 
89  String getString(uint8_t index) const;
90  int find(const char* value, unsigned length) const;
91 
92  template <typename T> const FSTR::Array<T>& getArray() const
93  {
94  return values.as<FSTR::Array<T>>();
95  }
96 
98  {
100  }
101 
102  template <typename T> T getValue(uint8_t index) const
103  {
104  return getArray<T>()[index];
105  }
106 };
107 
111 template <typename T> class EnumRange : public TRange<T>
112 {
113 public:
114  using TRange<T>::TRange;
115 
116  T random() const
117  {
118  TRange<uint8_t> range{uint8_t(this->min), uint8_t(this->max)};
119  return T(range.random());
120  }
121 };
122 
126 struct PropertyInfo {
135 
136  // Variant property information depends on type
137  union Variant {
142  const RangeInt8* int8;
149  };
150 
153  uint32_t offset;
155 
156  static const PropertyInfo empty;
157 
158  explicit operator bool() const
159  {
160  return this != &empty;
161  }
162 
163  bool isStringType() const
164  {
165  if(type == PropertyType::String) {
166  return true;
167  }
168  if(type == PropertyType::Enum && variant.enuminfo->type == PropertyType::String) {
169  return true;
170  }
171  return false;
172  }
173 
177  uint8_t getSize() const
178  {
179  return getPropertySize(type);
180  }
181 
185  int findObject(const char* name, unsigned length) const;
186 
190  int findProperty(const char* name, unsigned length) const;
191 
195  const PropertyInfo& getObject(unsigned index) const;
196 };
197 
198 } // namespace ConfigDB
199 
#define CONFIGDB_PROPERTY_TYPE_MAP(XX)
Property types with storage size.
Definition: PropertyInfo.h:31
String toString(ConfigDB::PropertyType type)
Support random() for ranges of enumerated types.
Definition: PropertyInfo.h:112
T random() const
Definition: PropertyInfo.h:116
Class to access an array of integral values stored in flash.
Definition: Array.hpp:124
Used when defining data structures.
Definition: ObjectBase.hpp:33
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:60
constexpr size_t length() const
Get the length of the object data in bytes.
Definition: ObjectBase.hpp:38
describes a counted string stored in flash memory
Definition: String.hpp:174
Class to access a Vector of objects stored in flash.
Definition: Vector.hpp:110
The String class.
Definition: WString.h:133
#define DEFINE_FSTR_LOCAL(name, str)
Like DEFINE_FSTR except reference is declared static constexpr.
Definition: String.hpp:84
XX(name, extensionStart, mime)
Definition: Array.h:26
PropertyType
Definition: PropertyInfo.h:50
uint8_t getPropertySize(PropertyType type)
Definition: PropertyInfo.h:63
uint16_t StringId
Defines contained string data using index into string pool.
Definition: PropertyInfo.h:59
Definition: PropertyInfo.h:75
const FSTR::Array< T > & getArray() const
Definition: PropertyInfo.h:92
const FSTR::Vector< FSTR::String > & getStrings() const
Definition: PropertyInfo.h:97
unsigned length() const
Definition: PropertyInfo.h:84
PropertyType type
The actual store type for this enum (String, etc.)
Definition: PropertyInfo.h:76
int find(const char *value, unsigned length) const
T getValue(uint8_t index) const
Definition: PropertyInfo.h:102
String getString(uint8_t index) const
FSTR::ObjectBase values
Definition: PropertyInfo.h:77
uint8_t getItemSize() const
Definition: PropertyInfo.h:79
Definition: ObjectInfo.h:38
Property metadata.
Definition: PropertyInfo.h:126
static const PropertyInfo empty
Definition: PropertyInfo.h:156
int findObject(const char *name, unsigned length) const
Find named object information.
bool isStringType() const
Definition: PropertyInfo.h:163
int findProperty(const char *name, unsigned length) const
Find named object information.
uint32_t offset
Location of property data in parent object, OR Alias property index.
Definition: PropertyInfo.h:153
const PropertyInfo & getObject(unsigned index) const
Get child object by index.
uint8_t getSize() const
Get number of bytes required to store this property value within a structure.
Definition: PropertyInfo.h:177
Variant variant
Definition: PropertyInfo.h:154
PropertyType type
Definition: PropertyInfo.h:151
const FlashString & name
Definition: PropertyInfo.h:152
Manage a range of numbers between specified limits.
Definition: Range.h:22
T min
Definition: Range.h:23
T max
Definition: Range.h:24
Definition: PropertyInfo.h:137
const RangeNumber * number
Definition: PropertyInfo.h:141
const RangeUInt16 * uint16
Definition: PropertyInfo.h:147
const ObjectInfo * object
Definition: PropertyInfo.h:139
const EnumInfo * enuminfo
Definition: PropertyInfo.h:140
const RangeInt8 * int8
Definition: PropertyInfo.h:142
const RangeInt16 * int16
Definition: PropertyInfo.h:143
const RangeInt64 * int64
Definition: PropertyInfo.h:145
const RangeInt32 * int32
Definition: PropertyInfo.h:144
const FlashString * defaultString
Definition: PropertyInfo.h:138
const RangeUInt8 * uint8
Definition: PropertyInfo.h:146
const RangeUInt32 * uint32
Definition: PropertyInfo.h:148