19
19
#pragma once
20
20
21
21
#include < app/util/attribute-storage-null-handling.h>
22
- #include < lib/core/Optional.h>
23
-
22
+ #include < optional>
24
23
#include < type_traits>
24
+ #include < utility>
25
25
26
26
namespace chip {
27
27
namespace app {
@@ -37,31 +37,40 @@ inline constexpr auto NullNullable = NullOptional;
37
37
* things.
38
38
*/
39
39
template <typename T>
40
- struct Nullable : protected Optional <T>
40
+ struct Nullable : protected std ::optional <T>
41
41
{
42
+
42
43
//
43
44
// The following 'using' statement is needed to make visible
44
45
// all constructors of the base class within this derived class.
45
46
//
46
- using Optional<T>::Optional;
47
+ using std::optional<T>::optional;
48
+ using std::optional<T>::operator *;
49
+ using std::optional<T>::operator ->;
47
50
48
- // Pull in APIs that make sense on Nullable with the same names as on
49
- // Optional.
50
- using Optional<T>::Value;
51
- using Optional<T>::ValueOr;
51
+ Nullable (NullOptionalType) : std::optional<T>(std::nullopt) {}
52
52
53
53
// Some consumers need an easy way to determine our underlying type.
54
54
using UnderlyingType = T;
55
55
56
- constexpr void SetNull () { Optional <T>::ClearValue (); }
57
- constexpr bool IsNull () const { return !Optional <T>::HasValue (); }
56
+ constexpr void SetNull () { std::optional <T>::reset (); }
57
+ constexpr bool IsNull () const { return !std::optional <T>::has_value (); }
58
58
59
59
template <class ... Args>
60
60
constexpr T & SetNonNull (Args &&... args)
61
61
{
62
- return Optional<T>::Emplace (std::forward<Args>(args)...);
62
+ return std::optional<T>::emplace (std::forward<Args>(args)...);
63
+ }
64
+
65
+ template <typename ... Args>
66
+ constexpr auto ValueOr (Args &&... args) const
67
+ {
68
+ return std::optional<T>::value_or (std::forward<Args>(args)...);
63
69
}
64
70
71
+ inline constexpr const T & Value () const { return std::optional<T>::value (); }
72
+ inline T & Value () { return std::optional<T>::value (); }
73
+
65
74
// For integer types, being nullable involves a range restriction.
66
75
template <
67
76
typename U = std::decay_t <T>,
@@ -96,22 +105,26 @@ struct Nullable : protected Optional<T>
96
105
// The only fabric-scoped objects in the spec are commands, events and structs inside lists, and none of those can be nullable.
97
106
static constexpr bool kIsFabricScoped = false ;
98
107
99
- bool operator ==(const Nullable & other) const { return Optional<T>::operator ==(other); }
100
- bool operator !=(const Nullable & other) const { return Optional<T>::operator !=(other); }
101
- bool operator ==(const T & other) const { return Optional<T>::operator ==(other); }
102
- bool operator !=(const T & other) const { return Optional<T>::operator !=(other); }
108
+ inline bool operator ==(const T & other) const { return static_cast <const std::optional<T> &>(*this ) == other; }
109
+ inline bool operator !=(const T & other) const { return !(*this == other); }
110
+
111
+ inline bool operator ==(const Nullable<T> & other) const
112
+ {
113
+ return static_cast <const std::optional<T> &>(*this ) == static_cast <const std::optional<T> &>(other);
114
+ }
115
+ inline bool operator !=(const Nullable<T> & other) const { return !(*this == other); }
103
116
};
104
117
105
118
template <class T >
106
119
constexpr Nullable<std::decay_t <T>> MakeNullable (T && value)
107
120
{
108
- return Nullable<std::decay_t <T>>(InPlace , std::forward<T>(value));
121
+ return Nullable<std::decay_t <T>>(std::in_place , std::forward<T>(value));
109
122
}
110
123
111
124
template <class T , class ... Args>
112
125
constexpr Nullable<T> MakeNullable (Args &&... args)
113
126
{
114
- return Nullable<T>(InPlace , std::forward<Args>(args)...);
127
+ return Nullable<T>(std::in_place , std::forward<Args>(args)...);
115
128
}
116
129
117
130
} // namespace DataModel
0 commit comments