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  // NOLINTNEXTLINE
45  return reinterpret_cast<const ObjectBase*>(flashLength_ & ~copyBit)->length();
46  }
47  return flashLength_;
48  }
49 
54  FSTR_INLINE constexpr const size_t size() const
55  {
56  return ALIGNUP4(length());
57  }
58 
59  bool operator==(const ObjectBase& other) const;
60 
67  template <class ObjectType> FSTR_INLINE constexpr const ObjectType& as() const
68  {
69  return *static_cast<const ObjectType*>(this);
70  }
71 
75  const uint8_t* data() const;
76 
84  size_t read(size_t offset, void* buffer, size_t count) const;
85 
100  size_t readFlash(size_t offset, void* buffer, size_t count) const;
101 
102  FSTR_INLINE constexpr const bool isCopy() const
103  {
104  return (flashLength_ & copyBit) != 0;
105  }
106 
111  FSTR_INLINE constexpr const bool isNull() const
112  {
113  return flashLength_ == lengthInvalid;
114  }
115 
116  /* Member data must be public for initialisation to work but DO NOT ACCESS DIRECTLY !! */
117 
118  const uint32_t flashLength_;
119  // const uint8_t data[]
120 
121 protected:
122  static const ObjectBase empty_;
123  static constexpr uint32_t copyBit = 0x80000000U;
124  static constexpr uint32_t lengthInvalid = copyBit | 0;
125 };
126 
127 } // 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:118
constexpr const bool isNull() const
Indicates an invalid String, used for return value from lookups, etc.
Definition: ObjectBase.hpp:111
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:67
bool operator==(const ObjectBase &other) const
static constexpr uint32_t copyBit
Set to indicate copy.
Definition: ObjectBase.hpp:123
constexpr const size_t size() const
Get the object data size in bytes.
Definition: ObjectBase.hpp:54
constexpr const bool isCopy() const
Definition: ObjectBase.hpp:102
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:124
static const ObjectBase empty_
Definition: ObjectBase.hpp:122
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