Object.hpp
Go to the documentation of this file.
1 /****
2  * Object.hpp - Definitions and macros common to all object types
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString 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  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Utility.hpp"
25 #include "ObjectBase.hpp"
26 #include "ObjectIterator.hpp"
27 
41 #define DECLARE_FSTR_OBJECT(name, ObjectType) extern const ObjectType& name;
42 
47 #define DEFINE_FSTR_OBJREF(name, object) const decltype(object)& name PROGMEM = object;
48 #define DEFINE_FSTR_REF(name) DEFINE_FSTR_OBJREF(name, FSTR_DATA_NAME(name).object)
49 #define DEFINE_FSTR_REF_LOCAL(name) static constexpr DEFINE_FSTR_REF(name)
55 #define FSTR_DATA_NAME(name) __fstr__##name
56 
62 #define FSTR_PTR(objref) (&objref)
63 
67 #define FSTR_CHECK_STRUCT(name) \
68  static_assert(std::is_standard_layout<decltype(name)>::value, "FSTR structure not Standard Layout"); \
69  static_assert(offsetof(decltype(name), data) == sizeof(uint32_t), "FSTR structure alignment error");
70 
79 #define IMPORT_FSTR_OBJECT(name, ObjectType, file) \
80  IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
81  extern "C" __attribute__((visibility("hidden"))) const ObjectType FSTR_DATA_NAME(name); \
82  DEFINE_FSTR_OBJREF(name, FSTR_DATA_NAME(name))
83 
87 #define IMPORT_FSTR_OBJECT_LOCAL(name, ObjectType, file) \
88  IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
89  extern "C" __attribute__((visibility("hidden"))) const ObjectType FSTR_DATA_NAME(name); \
90  static constexpr DEFINE_FSTR_OBJREF(name, FSTR_DATA_NAME(name))
91 
92 namespace FSTR
93 {
100 template <class ObjectType, typename ElementType> class Object : public ObjectBase
101 {
102 public:
103  using DataPtrType = const ElementType*;
105 
106 // This is precautionary to prevent misuse. No known fix for C++20
107 // NB. esp-quick-toolchain 10.3 reports 201703L for C++17,
108 // and 201709L for C++20 - standard says 202002L.
109 #if __cplusplus <= 201703L
110  Object(const Object&) = delete;
111  Object(const Object&&) = delete;
112  Object& operator=(const Object&) = delete;
113  Object& operator=(const Object&&) = delete;
114 #endif
115 
116  Iterator begin() const
117  {
118  return Iterator(as<ObjectType>(), 0);
119  }
120 
121  Iterator end() const
122  {
123  return Iterator(as<ObjectType>(), length());
124  }
125 
129  static constexpr const ObjectType& empty()
130  {
131  return empty_.as<const ObjectType>();
132  }
133 
137  FSTR_INLINE constexpr size_t length() const
138  {
139  return ObjectBase::length() / sizeof(ElementType);
140  }
141 
142  template <typename ValueType> int indexOf(const ValueType& value) const
143  {
144  auto& self = as<ObjectType>();
145  auto dataptr = self.data();
146  auto len = self.length();
147  for(unsigned i = 0; i < len; ++i) {
148  if(self.unsafeValueAt(dataptr, i) == value) {
149  return int(i);
150  }
151  }
152 
153  return -1;
154  }
155 
156  FSTR_INLINE ElementType valueAt(unsigned index) const
157  {
158  return (index < length()) ? unsafeValueAt(data(), index) : ElementType{};
159  }
160 
164  FSTR_INLINE ElementType operator[](unsigned index) const
165  {
166  return valueAt(index);
167  }
168 
169  FSTR_INLINE size_t elementSize() const
170  {
171  return sizeof(ElementType);
172  }
173 
175  {
176  return reinterpret_cast<DataPtrType>(ObjectBase::data());
177  }
178 
186  size_t read(size_t index, ElementType* buffer, size_t count) const
187  {
188  auto offset = index * sizeof(ElementType);
189  count *= sizeof(ElementType);
190  return ObjectBase::read(offset, buffer, count) / sizeof(ElementType);
191  }
192 
200  size_t readFlash(size_t index, ElementType* buffer, size_t count) const
201  {
202  auto offset = index * sizeof(ElementType);
203  count *= sizeof(ElementType);
204  return ObjectBase::readFlash(offset, buffer, count) / sizeof(ElementType);
205  }
206 
207  FSTR_INLINE ElementType unsafeValueAt(const DataPtrType dataptr, unsigned index) const
208  {
209  return readValue(dataptr + index);
210  }
211 };
212 
213 } // namespace FSTR
214 
Used when defining data structures.
Definition: ObjectBase.hpp:33
size_t read(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM.
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
const uint8_t * data() const
Get a pointer to the flash data.
static const ObjectBase empty_
Definition: ObjectBase.hpp:110
size_t readFlash(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM, using flashread()
Definition: ObjectIterator.hpp:29
Base class template for all types.
Definition: Object.hpp:101
Iterator begin() const
Definition: Object.hpp:116
size_t readFlash(size_t index, ElementType *buffer, size_t count) const
Read content into RAM,using flashmem_read()
Definition: Object.hpp:200
const ElementType * DataPtrType
Definition: Object.hpp:103
size_t read(size_t index, ElementType *buffer, size_t count) const
Read content into RAM.
Definition: Object.hpp:186
Object & operator=(const Object &&)=delete
size_t elementSize() const
Definition: Object.hpp:169
ElementType operator[](unsigned index) const
Array operator[].
Definition: Object.hpp:164
Object(const Object &&)=delete
constexpr size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:137
ElementType valueAt(unsigned index) const
Definition: Object.hpp:156
DataPtrType data() const
Definition: Object.hpp:174
ObjectIterator< ObjectType, ElementType > Iterator
Definition: Object.hpp:104
Iterator end() const
Definition: Object.hpp:121
ElementType unsafeValueAt(const DataPtrType dataptr, unsigned index) const
Definition: Object.hpp:207
int indexOf(const ValueType &value) const
Definition: Object.hpp:142
Object(const Object &)=delete
static constexpr const ObjectType & empty()
Return an empty object which evaluates to null.
Definition: Object.hpp:129
Object & operator=(const Object &)=delete
#define FSTR_INLINE
Definition: config.hpp:28
ObjectType
Definition: ObjectInfo.h:32
Definition: Array.hpp:118
std::enable_if< sizeof(T)==1, T >::type readValue(const T *ptr)
Read a typed value from flash memory ensuring correct alignment of access.
Definition: Utility.hpp:134