forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNullable.h
135 lines (115 loc) · 4.34 KB
/
Nullable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <app/util/attribute-storage-null-handling.h>
#include <optional>
#include <type_traits>
#include <utility>
namespace chip {
namespace app {
namespace DataModel {
/**
* NullNullable is an alias for NullOptional, for better readability.
*/
inline constexpr auto NullNullable = NullOptional;
/*
* Dedicated type for nullable things, to differentiate them from optional
* things.
*/
template <typename T>
struct Nullable : protected std::optional<T>
{
//
// The following 'using' statement is needed to make visible
// all constructors of the base class within this derived class.
//
using std::optional<T>::optional;
using std::optional<T>::operator*;
using std::optional<T>::operator->;
Nullable(NullOptionalType) : std::optional<T>(std::nullopt) {}
// Some consumers need an easy way to determine our underlying type.
using UnderlyingType = T;
constexpr void SetNull() { std::optional<T>::reset(); }
constexpr bool IsNull() const { return !std::optional<T>::has_value(); }
template <class... Args>
constexpr T & SetNonNull(Args &&... args)
{
return std::optional<T>::emplace(std::forward<Args>(args)...);
}
template <typename... Args>
constexpr auto ValueOr(Args &&... args) const
{
return std::optional<T>::value_or(std::forward<Args>(args)...);
}
constexpr const T & Value() const { return **this; }
T & Value() { return **this; }
// For integer types, being nullable involves a range restriction.
template <
typename U = std::decay_t<T>,
typename std::enable_if_t<(std::is_integral<U>::value && !std::is_same<U, bool>::value) || std::is_enum<U>::value, int> = 0>
constexpr bool ExistingValueInEncodableRange() const
{
return NumericAttributeTraits<T>::CanRepresentValue(/* isNullable = */ true, Value());
}
// For all other types, all values are valid.
template <typename U = std::decay_t<T>,
typename std::enable_if_t<(!std::is_integral<U>::value || std::is_same<U, bool>::value) && !std::is_enum<U>::value,
int> = 0>
constexpr bool ExistingValueInEncodableRange() const
{
return true;
}
// Set the nullable to the `other` nullable, returning true if something actually changed.
// This can be used to determine if changes occurred on assignment, so that reporting can be triggered
// only on actual changes.
constexpr bool Update(const Nullable<T> & other)
{
bool changed = *this != other;
if (changed)
{
*this = other;
}
return changed;
}
// The only fabric-scoped objects in the spec are commands, events and structs inside lists, and none of those can be nullable.
static constexpr bool kIsFabricScoped = false;
bool operator==(const Nullable<T> & other) const
{
if (!std::optional<T>::has_value())
{
return !other.has_value();
}
return other.has_value() && (*other == **this);
}
bool operator!=(const Nullable<T> & other) const { return !(*this == other); }
bool operator==(const T & other) const { return std::optional<T>::has_value() && (**this == other); }
bool operator!=(const T & other) const { return !(*this == other); }
};
template <class T>
constexpr Nullable<std::decay_t<T>> MakeNullable(T && value)
{
return Nullable<std::decay_t<T>>(std::in_place, std::forward<T>(value));
}
template <class T, class... Args>
constexpr Nullable<T> MakeNullable(Args &&... args)
{
return Nullable<T>(std::in_place, std::forward<Args>(args)...);
}
} // namespace DataModel
} // namespace app
} // namespace chip