SensESP 3.4.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
nullable.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_TYPES_NULLABLE_H_
2#define SENSESP_SRC_SENSESP_TYPES_NULLABLE_H_
3
4#include "ArduinoJson.h"
5
6namespace sensesp {
7
20template <typename T>
21class Nullable {
22 public:
23 Nullable() : value_{} {}
24 Nullable(T value) : value_{value} {}
26 value_ = value;
27 return *this;
28 }
30 value_ = other.value_;
31 return *this;
32 }
33 operator T() const {
34 return value_;
35 }
36
37 bool is_valid() const {
38 return value_ != invalid_value_;
39 }
40
41 T* ptr() {
42 return &value_;
43 }
44
45 static T invalid() {
46 return invalid_value_;
47 }
48
49 T value() const {
50 return value_;
51 }
52
53 private:
54 T value_;
55 static T invalid_value_;
56};
57
79template <>
80class Nullable<bool> {
81 public:
82 Nullable() : value_{false}, valid_{false} {}
83 Nullable(bool value) : value_{value}, valid_{true} {}
84 Nullable(const Nullable<bool>& other) = default;
86 value_ = value;
87 valid_ = true;
88 return *this;
89 }
90 Nullable<bool>& operator=(const Nullable<bool>& other) = default;
91 operator bool() const {
92 return value_;
93 }
94
95 bool is_valid() const {
96 return valid_;
97 }
98
99 // Mirrors the primary template's write-makes-valid behavior: handing out
100 // mutable storage marks the value valid (it cannot clear validity).
101 bool* ptr() {
102 valid_ = true;
103 return &value_;
104 }
105
107 return Nullable<bool>();
108 }
109
110 bool value() const {
111 return value_;
112 }
113
114 private:
115 bool value_;
116 bool valid_;
117};
118
123
124template <typename T>
125void convertFromJson(JsonVariantConst src, Nullable<T> &dst) {
126 if (src.isNull()) {
127 dst = Nullable<T>::invalid();
128 } else {
129 dst = src.as<T>();
130 }
131}
132
133template <typename T>
134void convertToJson(const Nullable<T> &src, JsonVariant dst) {
135 if (src.is_valid()) {
136 dst.set(src.value());
137 } else {
138 dst.clear();
139 }
140}
141
142} // namespace sensesp
143
144#endif // SENSESP_SRC_SENSESP_TYPES_NULLABLE_H_
Nullable< bool > & operator=(const Nullable< bool > &other)=default
Nullable(const Nullable< bool > &other)=default
Nullable< bool > & operator=(bool value)
Definition nullable.h:85
bool is_valid() const
Definition nullable.h:95
static Nullable< bool > invalid()
Definition nullable.h:106
Template class that supports a special invalid magic value for a type.
Definition nullable.h:21
Nullable< T > & operator=(T &value)
Definition nullable.h:25
Nullable< T > & operator=(const Nullable< T > &other)
Definition nullable.h:29
T value() const
Definition nullable.h:49
static T invalid()
Definition nullable.h:45
Nullable(T value)
Definition nullable.h:24
bool is_valid() const
Definition nullable.h:37
Nullable< bool > NullableBool
Definition nullable.h:122
Nullable< int > NullableInt
Definition nullable.h:119
void convertToJson(const Nullable< T > &src, JsonVariant dst)
Definition nullable.h:134
Nullable< double > NullableDouble
Definition nullable.h:121
void convertFromJson(JsonVariantConst src, Nullable< T > &dst)
Definition nullable.h:125
Nullable< float > NullableFloat
Definition nullable.h:120