ObjectBase.hpp
Go to the documentation of this file.
1 /****
2  * ObjectBase.hpp - POD base class type for defining data structures
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: Nov 2019 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "config.hpp"
25 
26 namespace FSTR
27 {
33 {
34 public:
38  size_t length() const;
39 
44  FSTR_INLINE size_t size() const
45  {
46  return ALIGNUP(length());
47  }
48 
55  template <class ObjectType> FSTR_INLINE constexpr const ObjectType& as() const
56  {
57  return *static_cast<const ObjectType*>(this);
58  }
59 
63  const uint8_t* data() const;
64 
72  size_t read(size_t offset, void* buffer, size_t count) const
73  {
74  auto len = length();
75  if(offset >= len) {
76  return 0;
77  }
78 
79  count = std::min(len - offset, count);
80  memcpy_P(buffer, data() + offset, count);
81  return count;
82  }
83 
98  size_t readFlash(size_t offset, void* buffer, size_t count) const;
99 
100  FSTR_INLINE bool isCopy() const
101  {
102  return (flashLength_ & copyBit) != 0;
103  }
104 
109  FSTR_INLINE bool isNull() const
110  {
111  return flashLength_ == lengthInvalid;
112  }
113 
114  /* Member data must be public for initialisation to work but DO NOT ACCESS DIRECTLY !! */
115 
116  uint32_t flashLength_;
117  // const uint8_t data[]
118 
119 protected:
120  static const ObjectBase empty_;
121 
122  /*
123  * @brief Called by Object<> default constructor
124  */
125  void invalidate();
126 
127  /*
128  * @brief Make a 'copy' of this object by taking a reference to the real one
129  */
130  void copy(const ObjectBase& obj)
131  {
132  if(obj.isCopy()) {
133  flashLength_ = obj.flashLength_;
134  } else {
135  flashLength_ = reinterpret_cast<uint32_t>(&obj) | copyBit;
136  }
137  }
138 
139 private:
140  static constexpr uint32_t copyBit = 0x80000000U;
141  static constexpr uint32_t lengthInvalid = copyBit | 0;
142 };
143 
144 } // namespace FSTR
#define ALIGNUP(_n)
Align a size up to the nearest word boundary.
Definition: FakePgmSpace.h:44
size_t readFlash(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM, using flashread()
Used when defining data structures.
Definition: ObjectBase.hpp:32
const uint8_t * data() const
Get a pointer to the flash data.
bool isCopy() const
Definition: ObjectBase.hpp:100
uint32_t flashLength_
Definition: ObjectBase.hpp:116
#define FSTR_INLINE
Definition: config.hpp:26
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
void * memcpy_P(void *dest, const void *src_P, size_t length)
bool isNull() const
Indicates an invalid String, used for return value from lookups, etc.
Definition: ObjectBase.hpp:109
static const ObjectBase empty_
Definition: ObjectBase.hpp:120
size_t length() const
Get the length of the object data in bytes.
Definition: Array.hpp:107
size_t size() const
Get the object data size in bytes.
Definition: ObjectBase.hpp:44
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:55