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> typename std::enable_if<!std::is_class<T>::value, KeyType>::type key() const
66  {
67  // Ensure access is aligned for 1/2 byte keys
68  return readValue<KeyType>(&key_);
69  }
70 
74  template <typename T = KeyType>
75  typename std::enable_if<std::is_same<T, String>::value, const KeyType&>::type key() const
76  {
77  return (key_ == nullptr) ? String::empty() : *key_;
78  }
79 
83  const ContentType& content() const
84  {
85  return (content_ == nullptr) ? ContentType::empty() : *content_;
86  }
87 
88  operator const ContentType&() const
89  {
90  return content();
91  }
92 
93  /* WString support */
94 
95  explicit operator WString() const
96  {
97  return WString(content());
98  }
99 
100  /* Print support */
101 
102  size_t printTo(Print& p) const
103  {
104  size_t count = 0;
105 
106  if(*this) {
107  count += print(p, key());
108  count += p.print(_F(" => "));
109  count += print(p, content());
110  } else {
111  count += p.print(_F("(invalid)"));
112  }
113 
114  return count;
115  }
116 
117  /* Private member data */
118 
119  KeyStoreType key_;
121 };
122 
123 } // 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:83
std::enable_if<!std::is_class< T >::value, KeyType >::type key() const
Get the key (non-class key types)
Definition: MapPair.hpp:65
size_t printTo(Print &p) const
Definition: MapPair.hpp:102
std::enable_if< std::is_same< T, String >::value, const KeyType & >::type key() const
Get the key (String key type)
Definition: MapPair.hpp:75
KeyStoreType key_
Definition: MapPair.hpp:119
const ContentType * content_
Definition: MapPair.hpp:120
void IfHelper() const
Definition: MapPair.hpp:42
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:160
Provides formatted output to stream.
Definition: Print.h:37
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:97
#define _F(str)
Definition: FakePgmSpace.h:97
Definition: WebConstants.h:72
Definition: Array.hpp:108
::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