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 this library.
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  FSTR_NOINLINE constexpr const size_t length() const
39  {
40  if(isNull()) {
41  return 0;
42  }
43  if(isCopy()) {
44  return reinterpret_cast<const ObjectBase*>(flashLength_ & ~copyBit)->length();
45  }
46  return flashLength_;
47  }
48 
53  FSTR_INLINE constexpr const size_t size() const
54  {
55  return ALIGNUP4(length());
56  }
57 
58  bool operator==(const ObjectBase& other) const;
59 
66  template <class ObjectType> FSTR_INLINE constexpr const ObjectType& as() const
67  {
68  return *static_cast<const ObjectType*>(this);
69  }
70 
74  const uint8_t* data() const;
75 
83  size_t read(size_t offset, void* buffer, size_t count) const;
84 
99  size_t readFlash(size_t offset, void* buffer, size_t count) const;
100 
101  FSTR_INLINE constexpr const bool isCopy() const
102  {
103  return (flashLength_ & copyBit) != 0;
104  }
105 
110  FSTR_INLINE constexpr const bool isNull() const
111  {
112  return flashLength_ == lengthInvalid;
113  }
114 
115  /* Member data must be public for initialisation to work but DO NOT ACCESS DIRECTLY !! */
116 
117  const uint32_t flashLength_;
118  // const uint8_t data[]
119 
120 protected:
121  static const ObjectBase empty_;
122  static constexpr uint32_t copyBit = 0x80000000U;
123  static constexpr uint32_t lengthInvalid = copyBit | 0;
124 };
125 
126 } // namespace FSTR
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 bool isNull() const
Indicates an invalid String, used for return value from lookups, etc.
Definition: ObjectBase.hpp:110
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:66
bool operator==(const ObjectBase &other) const
static constexpr uint32_t copyBit
Set to indicate copy.
Definition: ObjectBase.hpp:122
constexpr const size_t size() const
Get the object data size in bytes.
Definition: ObjectBase.hpp:53
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()
#define FSTR_NOINLINE
Definition: config.hpp:29
#define FSTR_INLINE
Definition: config.hpp:28
#define ALIGNUP4(n)
Align a size up to the nearest word boundary.
Definition: FakePgmSpace.h:39
Definition: Array.hpp:108
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34