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_INLINE constexpr size_t length() const
39  {
40  return flashLength_ & ~lengthInvalid;
41  }
42 
47  FSTR_INLINE constexpr size_t size() const
48  {
49  return ALIGNUP4(length());
50  }
51 
52  bool operator==(const ObjectBase& other) const;
53 
60  template <class ObjectType> FSTR_INLINE constexpr const ObjectType& as() const
61  {
62  return *static_cast<const ObjectType*>(this);
63  }
64 
68  const uint8_t* data() const;
69 
77  size_t read(size_t offset, void* buffer, size_t count) const;
78 
93  size_t readFlash(size_t offset, void* buffer, size_t count) const;
94 
99  FSTR_INLINE constexpr bool isNull() const
100  {
101  return flashLength_ == lengthInvalid;
102  }
103 
104  /* Member data must be public for initialisation to work but DO NOT ACCESS DIRECTLY !! */
105 
106  const uint32_t flashLength_;
107  // const uint8_t data[]
108 
109 protected:
110  static const ObjectBase empty_;
111  static constexpr uint32_t lengthInvalid = 0x80000000U;
112 };
113 
114 } // namespace FSTR
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.
const uint32_t flashLength_
Definition: ObjectBase.hpp:106
constexpr const ObjectType & as() const
Cast to a different object type.
Definition: ObjectBase.hpp:60
bool operator==(const ObjectBase &other) const
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 constexpr uint32_t lengthInvalid
Indicates null string.
Definition: ObjectBase.hpp:111
static const ObjectBase empty_
Definition: ObjectBase.hpp:110
constexpr size_t size() const
Get the object data size in bytes.
Definition: ObjectBase.hpp:47
constexpr bool isNull() const
Indicates an invalid String, used for return value from lookups, etc.
Definition: ObjectBase.hpp:99
size_t readFlash(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM, using flashread()
#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:118
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34