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 FlashString.
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 
49 #define DEFINE_FSTR_REF(name, ObjectType, object) const ObjectType& name PROGMEM = object.template as<ObjectType>();
50 
51 #define DEFINE_FSTR_REF_NAMED(name, ObjectType) DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name).object);
52 
56 #define FSTR_DATA_NAME(name) __fstr__##name
57 
82 #define FSTR_PTR(objref) static_cast<std::remove_reference<decltype(objref)>::type*>(&FSTR_DATA_NAME(objref).object)
83 
87 #define FSTR_CHECK_STRUCT(name) \
88  static_assert(std::is_pod<decltype(name)>::value, "FSTR structure not POD"); \
89  static_assert(offsetof(decltype(name), data) == sizeof(uint32_t), "FSTR structure alignment error");
90 
99 #define IMPORT_FSTR_OBJECT(name, ObjectType, file) \
100  IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
101  extern "C" __attribute__((visibility("hidden"))) const FSTR::ObjectBase FSTR_DATA_NAME(name); \
102  DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name));
103 
107 #define IMPORT_FSTR_OBJECT_LOCAL(name, ObjectType, file) \
108  IMPORT_FSTR_DATA(FSTR_DATA_NAME(name), file) \
109  extern "C" __attribute__((visibility("hidden"))) const FSTR::ObjectBase FSTR_DATA_NAME(name); \
110  static constexpr DEFINE_FSTR_REF(name, ObjectType, FSTR_DATA_NAME(name));
111 
112 namespace FSTR
113 {
120 template <class ObjectType, typename ElementType> class Object : public ObjectBase
121 {
122 public:
124 
129  {
130  invalidate();
131  }
132 
138  Object(const Object& obj)
139  {
140  copy(obj);
141  }
142 
143  Iterator begin() const
144  {
145  return Iterator(as<ObjectType>(), 0);
146  }
147 
148  Iterator end() const
149  {
150  return Iterator(as<ObjectType>(), length());
151  }
152 
156  static const ObjectType& empty()
157  {
158  return empty_.as<ObjectType>();
159  }
160 
164  FSTR_INLINE size_t length() const
165  {
166  return ObjectBase::length() / sizeof(ElementType);
167  }
168 
169  template <typename ValueType> int indexOf(const ValueType& value) const
170  {
171  auto len = length();
172  for(unsigned i = 0; i < len; ++i) {
173  if(as<ObjectType>().valueAt(i) == value) {
174  return i;
175  }
176  }
177 
178  return -1;
179  }
180 
181  FSTR_INLINE ElementType valueAt(unsigned index) const
182  {
183  if(index < length()) {
184  return readValue(data() + index);
185  } else {
186  return ElementType{0};
187  }
188  }
189 
193  FSTR_INLINE ElementType operator[](unsigned index) const
194  {
195  return valueAt(index);
196  }
197 
198  FSTR_INLINE size_t elementSize() const
199  {
200  return sizeof(ElementType);
201  }
202 
203  FSTR_INLINE const ElementType* data() const
204  {
205  return reinterpret_cast<const ElementType*>(ObjectBase::data());
206  }
207 
215  size_t read(size_t index, ElementType* buffer, size_t count) const
216  {
217  auto offset = index * sizeof(ElementType);
218  count *= sizeof(ElementType);
219  return ObjectBase::read(offset, buffer, count) / sizeof(ElementType);
220  }
221 
229  size_t readFlash(size_t index, ElementType* buffer, size_t count) const
230  {
231  auto offset = index * sizeof(ElementType);
232  count *= sizeof(ElementType);
233  return ObjectBase::readFlash(offset, buffer, count) / sizeof(ElementType);
234  }
235 };
236 
237 } // namespace FSTR
238 
size_t readFlash(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM, using flashread()
size_t readFlash(size_t index, ElementType *buffer, size_t count) const
Read content into RAM,using flashmem_read()
Definition: Object.hpp:229
Used when defining data structures.
Definition: ObjectBase.hpp:32
const uint8_t * data() const
Get a pointer to the flash data.
ObjectIterator< ObjectType, ElementType > Iterator
Definition: Object.hpp:123
ElementType valueAt(unsigned index) const
Definition: Object.hpp:181
size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:164
#define FSTR_INLINE
Definition: config.hpp:26
Base class template for all types.
Definition: Object.hpp:120
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:123
Object()
Creates a null object.
Definition: Object.hpp:128
Iterator begin() const
Definition: Object.hpp:143
size_t read(size_t index, ElementType *buffer, size_t count) const
Read content into RAM.
Definition: Object.hpp:215
void copy(const ObjectBase &obj)
Definition: ObjectBase.hpp:130
size_t read(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM.
Definition: ObjectBase.hpp:72
Definition: ObjectIterator.hpp:29
static const ObjectBase empty_
Definition: ObjectBase.hpp:120
size_t length() const
Get the length of the object data in bytes.
static const ObjectType & empty()
Return an empty object which evaluates to null.
Definition: Object.hpp:156
Definition: Array.hpp:107
int indexOf(const ValueType &value) const
Definition: Object.hpp:169
Iterator end() const
Definition: Object.hpp:148
ElementType operator[](unsigned index) const
Array operator[].
Definition: Object.hpp:193
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:55
Object(const Object &obj)
Copy constructor.
Definition: Object.hpp:138
const ElementType * data() const
Definition: Object.hpp:203
size_t elementSize() const
Definition: Object.hpp:198