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 
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:
123  using DataPtrType = const ElementType*;
125 
130  {
131 #ifndef ARCH_HOST
132  // Illegal on real flash object
133  assert(!isFlashPtr(this));
134 #endif
135  }
136 
143  Object(const Object& obj) : ObjectBase{obj.isCopy() ? obj.flashLength_ : uint32_t(&obj) | copyBit}
144  {
145  }
146 
147  Iterator begin() const
148  {
149  return Iterator(as<ObjectType>(), 0);
150  }
151 
152  Iterator end() const
153  {
154  return Iterator(as<ObjectType>(), length());
155  }
156 
160  static constexpr const ObjectType& empty()
161  {
162  return empty_.as<const ObjectType>();
163  }
164 
168  FSTR_INLINE constexpr const size_t length() const
169  {
170  return ObjectBase::length() / sizeof(ElementType);
171  }
172 
173  template <typename ValueType> int indexOf(const ValueType& value) const
174  {
175  auto& self = as<ObjectType>();
176  auto dataptr = self.data();
177  auto len = self.length();
178  for(unsigned i = 0; i < len; ++i) {
179  if(self.unsafeValueAt(dataptr, i) == value) {
180  return i;
181  }
182  }
183 
184  return -1;
185  }
186 
187  FSTR_INLINE ElementType valueAt(unsigned index) const
188  {
189  return (index < length()) ? unsafeValueAt(data(), index) : ElementType{};
190  }
191 
195  FSTR_INLINE ElementType operator[](unsigned index) const
196  {
197  return valueAt(index);
198  }
199 
200  FSTR_INLINE size_t elementSize() const
201  {
202  return sizeof(ElementType);
203  }
204 
206  {
207  return reinterpret_cast<DataPtrType>(ObjectBase::data());
208  }
209 
217  size_t read(size_t index, ElementType* buffer, size_t count) const
218  {
219  auto offset = index * sizeof(ElementType);
220  count *= sizeof(ElementType);
221  return ObjectBase::read(offset, buffer, count) / sizeof(ElementType);
222  }
223 
231  size_t readFlash(size_t index, ElementType* buffer, size_t count) const
232  {
233  auto offset = index * sizeof(ElementType);
234  count *= sizeof(ElementType);
235  return ObjectBase::readFlash(offset, buffer, count) / sizeof(ElementType);
236  }
237 
238  FSTR_INLINE ElementType unsafeValueAt(const DataPtrType dataptr, unsigned index) const
239  {
240  return readValue(dataptr + index);
241  }
242 };
243 
244 } // namespace FSTR
245 
#define isFlashPtr(ptr)
Simple check to determine if a pointer refers to flash memory.
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:24
Used when defining data structures.
Definition: ObjectBase.hpp:33
constexpr FSTR_NOINLINE const size_t length() const
Get the length of the object data in bytes.
Definition: ObjectBase.hpp:38
size_t read(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM.
const uint32_t flashLength_
Definition: ObjectBase.hpp:117
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:66
static constexpr uint32_t copyBit
Set to indicate copy.
Definition: ObjectBase.hpp:122
constexpr const bool isCopy() const
Definition: ObjectBase.hpp:101
const uint8_t * data() const
Get a pointer to the flash data.
static constexpr uint32_t lengthInvalid
Indicates null string in a copy.
Definition: ObjectBase.hpp:123
static const ObjectBase empty_
Definition: ObjectBase.hpp:121
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:121
Iterator begin() const
Definition: Object.hpp:147
size_t readFlash(size_t index, ElementType *buffer, size_t count) const
Read content into RAM,using flashmem_read()
Definition: Object.hpp:231
const ElementType * DataPtrType
Definition: Object.hpp:123
constexpr const size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:168
size_t read(size_t index, ElementType *buffer, size_t count) const
Read content into RAM.
Definition: Object.hpp:217
size_t elementSize() const
Definition: Object.hpp:200
ElementType operator[](unsigned index) const
Array operator[].
Definition: Object.hpp:195
Object()
Creates a null object.
Definition: Object.hpp:129
ElementType valueAt(unsigned index) const
Definition: Object.hpp:187
DataPtrType data() const
Definition: Object.hpp:205
Object(const Object &obj)
Copy constructor.
Definition: Object.hpp:143
ObjectIterator< ObjectType, ElementType > Iterator
Definition: Object.hpp:124
Iterator end() const
Definition: Object.hpp:152
ElementType unsafeValueAt(const DataPtrType dataptr, unsigned index) const
Definition: Object.hpp:238
int indexOf(const ValueType &value) const
Definition: Object.hpp:173
static constexpr const ObjectType & empty()
Return an empty object which evaluates to null.
Definition: Object.hpp:160
#define FSTR_INLINE
Definition: config.hpp:28
Definition: Array.hpp:108
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:126
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34