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  Object(const Object&) = delete;
107  Object(const Object&&) = delete;
108  Object& operator=(const Object&) = delete;
109  Object& operator=(const Object&&) = delete;
110 
111  Iterator begin() const
112  {
113  return Iterator(as<ObjectType>(), 0);
114  }
115 
116  Iterator end() const
117  {
118  return Iterator(as<ObjectType>(), length());
119  }
120 
124  static constexpr const ObjectType& empty()
125  {
126  return empty_.as<const ObjectType>();
127  }
128 
132  FSTR_INLINE constexpr size_t length() const
133  {
134  return ObjectBase::length() / sizeof(ElementType);
135  }
136 
137  template <typename ValueType> int indexOf(const ValueType& value) const
138  {
139  auto& self = as<ObjectType>();
140  auto dataptr = self.data();
141  auto len = self.length();
142  for(unsigned i = 0; i < len; ++i) {
143  if(self.unsafeValueAt(dataptr, i) == value) {
144  return int(i);
145  }
146  }
147 
148  return -1;
149  }
150 
151  FSTR_INLINE ElementType valueAt(unsigned index) const
152  {
153  return (index < length()) ? unsafeValueAt(data(), index) : ElementType{};
154  }
155 
159  FSTR_INLINE ElementType operator[](unsigned index) const
160  {
161  return valueAt(index);
162  }
163 
164  FSTR_INLINE size_t elementSize() const
165  {
166  return sizeof(ElementType);
167  }
168 
170  {
171  return reinterpret_cast<DataPtrType>(ObjectBase::data());
172  }
173 
181  size_t read(size_t index, ElementType* buffer, size_t count) const
182  {
183  auto offset = index * sizeof(ElementType);
184  count *= sizeof(ElementType);
185  return ObjectBase::read(offset, buffer, count) / sizeof(ElementType);
186  }
187 
195  size_t readFlash(size_t index, ElementType* buffer, size_t count) const
196  {
197  auto offset = index * sizeof(ElementType);
198  count *= sizeof(ElementType);
199  return ObjectBase::readFlash(offset, buffer, count) / sizeof(ElementType);
200  }
201 
202  FSTR_INLINE ElementType unsafeValueAt(const DataPtrType dataptr, unsigned index) const
203  {
204  return readValue(dataptr + index);
205  }
206 };
207 
208 } // namespace FSTR
209 
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:111
size_t readFlash(size_t index, ElementType *buffer, size_t count) const
Read content into RAM,using flashmem_read()
Definition: Object.hpp:195
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:181
Object & operator=(const Object &&)=delete
size_t elementSize() const
Definition: Object.hpp:164
ElementType operator[](unsigned index) const
Array operator[].
Definition: Object.hpp:159
Object(const Object &&)=delete
constexpr size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:132
ElementType valueAt(unsigned index) const
Definition: Object.hpp:151
DataPtrType data() const
Definition: Object.hpp:169
ObjectIterator< ObjectType, ElementType > Iterator
Definition: Object.hpp:104
Iterator end() const
Definition: Object.hpp:116
ElementType unsafeValueAt(const DataPtrType dataptr, unsigned index) const
Definition: Object.hpp:202
int indexOf(const ValueType &value) const
Definition: Object.hpp:137
Object(const Object &)=delete
static constexpr const ObjectType & empty()
Return an empty object which evaluates to null.
Definition: Object.hpp:124
Object & operator=(const Object &)=delete
#define FSTR_INLINE
Definition: config.hpp:28
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
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34