PropertyInfo.h
Go to the documentation of this file.
1 
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(UInt64, 8) \
42  XX(Number, 4) \
43  XX(String, sizeof(StringId)) \
44  XX(Object, sizeof(ObjectInfo*)) \
45  XX(Alias, 0)
46 
47 namespace ConfigDB
48 {
49 DEFINE_FSTR_LOCAL(fstr_empty, "")
50 
51 enum class PropertyType : uint32_t {
52 #define XX(name, ...) name,
54 #undef XX
55 };
56 
60 using StringId = uint16_t;
61 
62 struct ObjectInfo;
63 
64 inline uint8_t getPropertySize(PropertyType type)
65 {
66  switch(type) {
67 #define XX(tag, size) \
68  case PropertyType::tag: \
69  return size;
71 #undef XX
72  }
73  return 0;
74 }
75 
76 struct EnumInfo {
78  FSTR::ObjectBase values; //< Possible values
79 
80  uint8_t getItemSize() const
81  {
82  return (type == PropertyType::String) ? sizeof(FSTR::String*) : getPropertySize(type);
83  }
84 
85  unsigned length() const
86  {
87  return values.length() / getItemSize();
88  }
89 
90  String getString(uint8_t index) const;
91  int find(const char* value, unsigned length) const;
92 
93  template <typename T> const FSTR::Array<T>& getArray() const
94  {
95  return values.as<FSTR::Array<T>>();
96  }
97 
99  {
101  }
102 
103  template <typename T> T getValue(uint8_t index) const
104  {
105  return getArray<T>()[index];
106  }
107 };
108 
112 struct PropertyInfo {
113  template <typename T, typename U = T> struct RangeTemplate {
116 
117  U clip(U value) const
118  {
119  return TRange(U(minimum), U(maximum)).clip(value);
120  }
121  };
122  template <typename T> struct RangePtrTemplate {
123  const T* minimum;
124  const T* maximum;
125 
126  T clip(T value) const
127  {
128  if(minimum || maximum) {
129  return TRange(minimum ? *minimum : 0, maximum ? *maximum : 0).clip(value);
130  }
131  return value;
132  }
133  };
134  // Variant property information depends on type
135  union Variant {
144  };
145 
148  uint32_t offset;
150 
151  static const PropertyInfo empty;
152 
153  explicit operator bool() const
154  {
155  return this != &empty;
156  }
157 
158  bool isStringType() const
159  {
160  if(type == PropertyType::String) {
161  return true;
162  }
163  if(type == PropertyType::Enum && variant.enuminfo->type == PropertyType::String) {
164  return true;
165  }
166  return false;
167  }
168 
172  uint8_t getSize() const
173  {
174  return getPropertySize(type);
175  }
176 
180  int findObject(const char* name, unsigned length) const;
181 
185  int findProperty(const char* name, unsigned length) const;
186 
190  const PropertyInfo& getObject(unsigned index) const;
191 };
192 
193 } // namespace ConfigDB
194 
#define CONFIGDB_PROPERTY_TYPE_MAP(XX)
Property types with storage size.
Definition: PropertyInfo.h:31
String toString(ConfigDB::PropertyType type)
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:51
uint8_t getPropertySize(PropertyType type)
Definition: PropertyInfo.h:64
uint16_t StringId
Defines contained string data using index into string pool.
Definition: PropertyInfo.h:60
Definition: PropertyInfo.h:76
const FSTR::Array< T > & getArray() const
Definition: PropertyInfo.h:93
const FSTR::Vector< FSTR::String > & getStrings() const
Definition: PropertyInfo.h:98
unsigned length() const
Definition: PropertyInfo.h:85
PropertyType type
The actual store type for this enum (String, etc.)
Definition: PropertyInfo.h:77
int find(const char *value, unsigned length) const
T getValue(uint8_t index) const
Definition: PropertyInfo.h:103
String getString(uint8_t index) const
FSTR::ObjectBase values
Definition: PropertyInfo.h:78
uint8_t getItemSize() const
Definition: PropertyInfo.h:80
Definition: ObjectInfo.h:38
Definition: PropertyInfo.h:122
T clip(T value) const
Definition: PropertyInfo.h:126
const T * minimum
Definition: PropertyInfo.h:123
const T * maximum
Definition: PropertyInfo.h:124
Definition: PropertyInfo.h:113
T maximum
Definition: PropertyInfo.h:115
T minimum
Definition: PropertyInfo.h:114
U clip(U value) const
Definition: PropertyInfo.h:117
Property metadata.
Definition: PropertyInfo.h:112
static const PropertyInfo empty
Definition: PropertyInfo.h:151
int findObject(const char *name, unsigned length) const
Find named object information.
bool isStringType() const
Definition: PropertyInfo.h:158
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:148
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:172
Variant variant
Definition: PropertyInfo.h:149
PropertyType type
Definition: PropertyInfo.h:146
const FlashString & name
Definition: PropertyInfo.h:147
Manage a range of numbers between specified limits.
Definition: Range.h:21
T clip(T value) const
Clip values to within the range.
Definition: Range.h:99
Definition: PropertyInfo.h:135
RangePtrTemplate< int64_t > int64
Definition: PropertyInfo.h:142
const ObjectInfo * object
Definition: PropertyInfo.h:137
RangePtrTemplate< uint64_t > uint64
Definition: PropertyInfo.h:143
const EnumInfo * enuminfo
Definition: PropertyInfo.h:138
RangeTemplate< int32_t > int32
Definition: PropertyInfo.h:139
RangeTemplate< uint32_t > uint32
Definition: PropertyInfo.h:140
const FlashString * defaultString
Definition: PropertyInfo.h:136
RangeTemplate< const_number_t, number_t > number
Definition: PropertyInfo.h:141