FlashStringRefAdapter.hpp
Go to the documentation of this file.
1 // ArduinoJson - arduinojson.org
2 // Copyright Benoit Blanchon 2014-2019
3 // MIT License
4 //
5 // Sming FlashString adapter mikee47 April 2019 <mike@sillyhouse.net>
6 
7 #pragma once
8 
9 #include <ArduinoJson/Strings/IsWriteableString.hpp>
10 
11 namespace ARDUINOJSON_NAMESPACE
12 {
14 {
15 public:
16  explicit FlashStringRefAdapter(const FlashString& str) : str(str)
17  {
18  }
19 
20  bool equals(const char* expected) const
21  {
22  return str.equals(expected);
23  }
24 
25  bool isNull() const
26  {
27  return str.isNull();
28  }
29 
30  char* save(MemoryPool* pool) const
31  {
32  size_t n = str.size();
33  char* dup = pool->allocFrozenString(n);
34  if(dup) {
35  str.read(0, dup, n);
36  }
37  return dup;
38  }
39 
40  const char* data() const
41  {
42  // Cannot access directly using a char*
43  return nullptr;
44  }
45 
46  size_t size() const
47  {
48  return str.length();
49  }
50 
51  bool isStatic() const
52  {
53  // Whilst our value won't change, it cannot be accessed using a regular char*
54  return false;
55  }
56 
57 private:
58  const FlashString& str;
59 };
60 
62 {
63  return FlashStringRefAdapter(str);
64 }
65 
66 template <> struct IsString<FlashString> : true_type {
67 };
68 
69 template <> struct IsWriteableString<FlashString> : false_type {
70 };
71 
72 } // namespace ARDUINOJSON_NAMESPACE
bool isStatic() const
Definition: FlashStringRefAdapter.hpp:51
Definition: FlashStringRefAdapter.hpp:13
FlashStringRefAdapter(const FlashString &str)
Definition: FlashStringRefAdapter.hpp:16
size_t length() const
Get the length of the content in elements.
Definition: Object.hpp:164
FlashStringRefAdapter adaptString(const FlashString &str)
Definition: FlashStringRefAdapter.hpp:61
bool equals(const char *expected) const
Definition: FlashStringRefAdapter.hpp:20
bool equals(const char *cstr, size_t len=0) const
Check for equality with a C-string.
size_t read(size_t index, ElementType *buffer, size_t count) const
Read content into RAM.
Definition: Object.hpp:215
bool isNull() const
Definition: FlashStringRefAdapter.hpp:25
bool isNull() const
Indicates an invalid String, used for return value from lookups, etc.
Definition: ObjectBase.hpp:109
describes a counted string stored in flash memory
Definition: String.hpp:173
size_t size() const
Get the number of bytes used to store the String.
Definition: String.hpp:180
char * save(MemoryPool *pool) const
Definition: FlashStringRefAdapter.hpp:30
const char * data() const
Definition: FlashStringRefAdapter.hpp:40
Definition: FlashStringReader.hpp:9
size_t size() const
Definition: FlashStringRefAdapter.hpp:46