MapPair.hpp
Go to the documentation of this file.
1 /****
2  * MapPair.hpp - Defines the MapPair class template
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 "String.hpp"
25 #include "Print.hpp"
26 #include <WString.h>
27 
28 namespace FSTR
29 {
36 template <typename KeyType, class ContentType> class MapPair
37 {
38  typedef typename std::conditional<std::is_same<KeyType, String>::value, const KeyType*, KeyType>::type KeyStoreType;
39 
40 public:
41  typedef void (MapPair::*IfHelperType)() const;
42  void IfHelper() const
43  {
44  }
45 
49  operator IfHelperType() const
50  {
51  return content_ ? &MapPair::IfHelper : 0;
52  }
53 
57  static const MapPair empty()
58  {
59  return MapPair{KeyStoreType(0), nullptr};
60  }
61 
65  template <typename T = KeyType>
66  FSTR_ALIGN32 typename std::enable_if<!std::is_class<T>::value, KeyType>::type key() const
67  {
68  // Ensure access is aligned for 1/2 byte keys
69  return readValue<KeyType>(&key_);
70  }
71 
75  template <typename T = KeyType>
76  FSTR_ALIGN32 typename std::enable_if<std::is_same<T, String>::value, const KeyType&>::type key() const
77  {
78  return (key_ == nullptr) ? String::empty() : *key_;
79  }
80 
84  const ContentType& content() const
85  {
86  return (content_ == nullptr) ? ContentType::empty() : *content_;
87  }
88 
89  operator const ContentType&() const
90  {
91  return content();
92  }
93 
94  /* WString support */
95 
96  explicit operator WString() const
97  {
98  return WString(content());
99  }
100 
101  /* Print support */
102 
103  size_t printTo(Print& p) const
104  {
105  size_t count = 0;
106 
107  if(*this) {
108  count += print(p, key());
109  count += p.print(_F(" => "));
110  count += print(p, content());
111  } else {
112  count += p.print(_F("(invalid)"));
113  }
114 
115  return count;
116  }
117 
118  /* Private member data */
119 
120  KeyStoreType key_;
122 };
123 
124 } // namespace FSTR
describes a pair mapping key => data for a specified key type
Definition: MapPair.hpp:37
void(MapPair::* IfHelperType)() const
Definition: MapPair.hpp:41
const ContentType & content() const
Accessor to get a reference to the content.
Definition: MapPair.hpp:84
size_t printTo(Print &p) const
Definition: MapPair.hpp:103
KeyStoreType key_
Definition: MapPair.hpp:120
FSTR_ALIGN32 std::enable_if<!std::is_class< T >::value, KeyType >::type key() const
Get the key (non-class key types)
Definition: MapPair.hpp:66
const ContentType * content_
Definition: MapPair.hpp:121
void IfHelper() const
Definition: MapPair.hpp:42
FSTR_ALIGN32 std::enable_if< std::is_same< T, String >::value, const KeyType & >::type key() const
Get the key (String key type)
Definition: MapPair.hpp:76
static const MapPair empty()
Get an empty Pair object, identifies as invalid when lookup fails.
Definition: MapPair.hpp:57
static constexpr const String & empty()
Return an empty object which evaluates to null.
Definition: Object.hpp:124
Provides formatted output to stream.
Definition: Print.h:37
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:103
#define FSTR_ALIGN32
Definition: config.hpp:32
#define _F(str)
Definition: FakePgmSpace.h:97
Definition: WebConstants.h:72
Definition: Array.hpp:118
::String WString
A Wiring String.
Definition: String.hpp:168
std::enable_if< std::is_class< ObjectType >::value, size_t >::type print(Print &p, const ObjectType &object)
Print an object.
Definition: Print.hpp:40