aboutsummaryrefslogtreecommitdiff
path: root/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/nullable.h
blob: 56892693b5af7f571785450a7498e4cfe4f777ec (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIB_FIT_NULLABLE_H_
#define LIB_FIT_NULLABLE_H_

#include <assert.h>
#include <lib/stdcompat/optional.h>

#include <type_traits>
#include <utility>

#include "pw_assert/assert.h"

namespace fit {

// Determines whether a type can be compared with nullptr.
template <typename T, typename Comparable = bool>
struct is_comparable_with_null : public std::false_type {};
template <typename T>
struct is_comparable_with_null<T, decltype(std::declval<const T&>() == nullptr)>
    : public std::true_type {};

// Suppress the warning when the compiler can see that a nullable value is
// never equal to nullptr.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress"
template <typename T, std::enable_if_t<is_comparable_with_null<T>::value, bool> = true>
constexpr inline bool is_null(T&& value) {
  return std::forward<T>(value) == nullptr;
}
#pragma GCC diagnostic pop

template <typename T, std::enable_if_t<!is_comparable_with_null<T>::value, bool> = false>
constexpr inline bool is_null(T&&) {
  return false;
}

// Determines whether a type can be initialized, assigned, and compared
// with nullptr.
template <typename T>
struct is_nullable
    : public std::integral_constant<bool, std::is_constructible_v<T, decltype(nullptr)> &&
                                              std::is_assignable_v<T&, decltype(nullptr)> &&
                                              is_comparable_with_null<T>::value> {};
template <>
struct is_nullable<void> : public std::false_type {};

// Holds a value or nullptr.
//
// This class is similar to |std::optional<T>| except that it uses less
// storage when the value type can be initialized, assigned, and compared
// with nullptr.
//
// For example:
// - sizeof(fit::nullable<void*>) == sizeof(void*)
// - sizeof(std::optional<void*>) == sizeof(struct { bool; void*; })
// - sizeof(fit::nullable<int>) == sizeof(struct { bool; int; })
// - sizeof(std::optional<int>) == sizeof(struct { bool; int; })
//
// TODO(fxbug.dev/4681): fit::nullable does not precisely mirror
// cpp17::optional. This should be corrected to avoid surprises when switching
// between the types.
template <typename T, bool = (is_nullable<T>::value && std::is_constructible_v<T, T&&> &&
                              std::is_assignable_v<T&, T&&>)>
class nullable final {
 public:
  using value_type = T;

  ~nullable() = default;
  constexpr nullable() = default;

  explicit constexpr nullable(decltype(nullptr)) {}
  explicit constexpr nullable(T value) : opt_(std::move(value)) {}

  constexpr nullable(const nullable& other) = default;
  constexpr nullable& operator=(const nullable& other) = default;

  constexpr nullable(nullable&& other) = default;
  constexpr nullable& operator=(nullable&& other) = default;

  constexpr T& value() & { return opt_.value(); }
  constexpr const T& value() const& { return opt_.value(); }
  constexpr T&& value() && { return std::move(opt_.value()); }
  constexpr const T&& value() const&& { return std::move(opt_.value()); }

  template <typename U = T>
  constexpr T value_or(U&& default_value) const {
    return opt_.value_or(std::forward<U>(default_value));
  }

  constexpr T* operator->() { return &*opt_; }
  constexpr const T* operator->() const { return &*opt_; }
  constexpr T& operator*() { return *opt_; }
  constexpr const T& operator*() const { return *opt_; }

  constexpr bool has_value() const { return opt_.has_value(); }
  explicit constexpr operator bool() const { return has_value(); }

  constexpr nullable& operator=(decltype(nullptr)) {
    reset();
    return *this;
  }

  constexpr nullable& operator=(T value) {
    opt_ = std::move(value);
    return *this;
  }

  constexpr void reset() { opt_.reset(); }

  constexpr void swap(nullable& other) { opt_.swap(other.opt_); }

 private:
  cpp17::optional<T> opt_;
};

template <typename T>
class nullable<T, true> final {
 public:
  using value_type = T;

  constexpr nullable() : value_(nullptr) {}
  explicit constexpr nullable(decltype(nullptr)) : value_(nullptr) {}
  explicit constexpr nullable(T value) : value_(std::move(value)) {}
  constexpr nullable(const nullable& other) = default;
  constexpr nullable(nullable&& other) : value_(std::move(other.value_)) {}
  ~nullable() = default;

  constexpr T& value() & {
    if (has_value()) {
      return value_;
    }
    PW_ASSERT(false);
  }
  constexpr const T& value() const& {
    if (has_value()) {
      return value_;
    }
    PW_ASSERT(false);
  }
  constexpr T&& value() && {
    if (has_value()) {
      return std::move(value_);
    }
    PW_ASSERT(false);
  }
  constexpr const T&& value() const&& {
    if (has_value()) {
      return std::move(value_);
    }
    PW_ASSERT(false);
  }

  template <typename U = T>
  constexpr T value_or(U&& default_value) const {
    return has_value() ? value_ : static_cast<T>(std::forward<U>(default_value));
  }

  constexpr T* operator->() { return &value_; }
  constexpr const T* operator->() const { return &value_; }
  constexpr T& operator*() { return value_; }
  constexpr const T& operator*() const { return value_; }

  constexpr bool has_value() const { return !(value_ == nullptr); }
  explicit constexpr operator bool() const { return has_value(); }

  constexpr nullable& operator=(const nullable& other) = default;
  constexpr nullable& operator=(nullable&& other) {
    value_ = std::move(other.value_);
    return *this;
  }

  constexpr nullable& operator=(decltype(nullptr)) {
    reset();
    return *this;
  }

  constexpr nullable& operator=(T value) {
    value_ = std::move(value);
    return *this;
  }

  constexpr void reset() { value_ = nullptr; }

  constexpr void swap(nullable& other) {
    using std::swap;
    swap(value_, other.value_);
  }

 private:
  T value_;
};

template <typename T>
void swap(nullable<T>& a, nullable<T>& b) {
  a.swap(b);
}

template <typename T>
constexpr bool operator==(const nullable<T>& lhs, decltype(nullptr)) {
  return !lhs.has_value();
}
template <typename T>
constexpr bool operator!=(const nullable<T>& lhs, decltype(nullptr)) {
  return lhs.has_value();
}

template <typename T>
constexpr bool operator==(decltype(nullptr), const nullable<T>& rhs) {
  return !rhs.has_value();
}
template <typename T>
constexpr bool operator!=(decltype(nullptr), const nullable<T>& rhs) {
  return rhs.has_value();
}

template <typename T, typename U>
constexpr bool operator==(const nullable<T>& lhs, const nullable<U>& rhs) {
  return (lhs.has_value() == rhs.has_value()) && (!lhs.has_value() || *lhs == *rhs);
}
template <typename T, typename U>
constexpr bool operator!=(const nullable<T>& lhs, const nullable<U>& rhs) {
  return (lhs.has_value() != rhs.has_value()) || (lhs.has_value() && *lhs != *rhs);
}

template <typename T, typename U>
constexpr bool operator==(const nullable<T>& lhs, const U& rhs) {
  return (lhs.has_value() != is_null(rhs)) && (!lhs.has_value() || *lhs == rhs);
}
template <typename T, typename U>
constexpr bool operator!=(const nullable<T>& lhs, const U& rhs) {
  return (lhs.has_value() == is_null(rhs)) || (lhs.has_value() && *lhs != rhs);
}

template <typename T, typename U>
constexpr bool operator==(const T& lhs, const nullable<U>& rhs) {
  return (is_null(lhs) != rhs.has_value()) && (!rhs.has_value() || lhs == *rhs);
}
template <typename T, typename U>
constexpr bool operator!=(const T& lhs, const nullable<U>& rhs) {
  return (is_null(lhs) == rhs.has_value()) || (rhs.has_value() && lhs != *rhs);
}

}  // namespace fit

#endif  // LIB_FIT_NULLABLE_H_