Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_OPTIONAL_H_ |
| 12 | #define WEBRTC_BASE_OPTIONAL_H_ |
| 13 | |
| 14 | #include <algorithm> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 15 | #include <memory> |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 18 | #include "webrtc/base/array_view.h" |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 19 | #include "webrtc/base/checks.h" |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 20 | #include "webrtc/base/sanitizer.h" |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 24 | namespace optional_internal { |
| 25 | |
| 26 | #if RTC_HAS_ASAN |
| 27 | |
| 28 | // This is a non-inlined function. The optimizer can't see inside it. |
| 29 | void* FunctionThatDoesNothingImpl(void*); |
| 30 | |
| 31 | template <typename T> |
| 32 | inline T* FunctionThatDoesNothing(T* x) { |
| 33 | return reinterpret_cast<T*>( |
| 34 | FunctionThatDoesNothingImpl(reinterpret_cast<void*>(x))); |
| 35 | } |
| 36 | |
| 37 | #else |
| 38 | |
| 39 | template <typename T> |
| 40 | inline T* FunctionThatDoesNothing(T* x) { return x; } |
| 41 | |
| 42 | #endif |
| 43 | |
| 44 | } // namespace optional_internal |
| 45 | |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 46 | // Simple std::optional-wannabe. It either contains a T or not. |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 47 | // |
| 48 | // A moved-from Optional<T> may only be destroyed, and assigned to if T allows |
| 49 | // being assigned to after having been moved from. Specifically, you may not |
| 50 | // assume that it just doesn't contain a value anymore. |
| 51 | // |
| 52 | // Examples of good places to use Optional: |
| 53 | // |
| 54 | // - As a class or struct member, when the member doesn't always have a value: |
| 55 | // struct Prisoner { |
| 56 | // std::string name; |
| 57 | // Optional<int> cell_number; // Empty if not currently incarcerated. |
| 58 | // }; |
| 59 | // |
| 60 | // - As a return value for functions that may fail to return a value on all |
| 61 | // allowed inputs. For example, a function that searches an array might |
| 62 | // return an Optional<size_t> (the index where it found the element, or |
| 63 | // nothing if it didn't find it); and a function that parses numbers might |
| 64 | // return Optional<double> (the parsed number, or nothing if parsing failed). |
| 65 | // |
| 66 | // Examples of bad places to use Optional: |
| 67 | // |
| 68 | // - As a return value for functions that may fail because of disallowed |
| 69 | // inputs. For example, a string length function should not return |
| 70 | // Optional<size_t> so that it can return nothing in case the caller passed |
| 71 | // it a null pointer; the function should probably use RTC_[D]CHECK instead, |
| 72 | // and return plain size_t. |
| 73 | // |
| 74 | // - As a return value for functions that may fail to return a value on all |
| 75 | // allowed inputs, but need to tell the caller what went wrong. Returning |
| 76 | // Optional<double> when parsing a single number as in the example above |
| 77 | // might make sense, but any larger parse job is probably going to need to |
| 78 | // tell the caller what the problem was, not just that there was one. |
| 79 | // |
| 80 | // TODO(kwiberg): Get rid of this class when the standard library has |
| 81 | // std::optional (and we're allowed to use it). |
| 82 | template <typename T> |
| 83 | class Optional final { |
| 84 | public: |
| 85 | // Construct an empty Optional. |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 86 | Optional() : has_value_(false), empty_('\0') { |
| 87 | PoisonValue(); |
| 88 | } |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 89 | |
| 90 | // Construct an Optional that contains a value. |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 91 | explicit Optional(const T& value) : has_value_(true) { |
| 92 | new (&value_) T(value); |
| 93 | } |
| 94 | explicit Optional(T&& value) : has_value_(true) { |
| 95 | new (&value_) T(std::move(value)); |
| 96 | } |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 97 | |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 98 | // Copy constructor: copies the value from m if it has one. |
| 99 | Optional(const Optional& m) : has_value_(m.has_value_) { |
| 100 | if (has_value_) |
| 101 | new (&value_) T(m.value_); |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 102 | else |
| 103 | PoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 104 | } |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 105 | |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 106 | // Move constructor: if m has a value, moves the value from m, leaving m |
| 107 | // still in a state where it has a value, but a moved-from one (the |
| 108 | // properties of which depends on T; the only general guarantee is that we |
| 109 | // can destroy m). |
| 110 | Optional(Optional&& m) : has_value_(m.has_value_) { |
| 111 | if (has_value_) |
| 112 | new (&value_) T(std::move(m.value_)); |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 113 | else |
| 114 | PoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 115 | } |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 116 | |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 117 | ~Optional() { |
| 118 | if (has_value_) |
| 119 | value_.~T(); |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 120 | else |
| 121 | UnpoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Copy assignment. Uses T's copy assignment if both sides have a value, T's |
| 125 | // copy constructor if only the right-hand side has a value. |
| 126 | Optional& operator=(const Optional& m) { |
| 127 | if (m.has_value_) { |
| 128 | if (has_value_) { |
| 129 | value_ = m.value_; // T's copy assignment. |
| 130 | } else { |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 131 | UnpoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 132 | new (&value_) T(m.value_); // T's copy constructor. |
| 133 | has_value_ = true; |
| 134 | } |
danilchap | c4fd23c | 2016-10-17 07:16:54 -0700 | [diff] [blame^] | 135 | } else { |
| 136 | reset(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 137 | } |
| 138 | return *this; |
| 139 | } |
| 140 | |
| 141 | // Move assignment. Uses T's move assignment if both sides have a value, T's |
| 142 | // move constructor if only the right-hand side has a value. The state of m |
| 143 | // after it's been moved from is as for the move constructor. |
| 144 | Optional& operator=(Optional&& m) { |
| 145 | if (m.has_value_) { |
| 146 | if (has_value_) { |
| 147 | value_ = std::move(m.value_); // T's move assignment. |
| 148 | } else { |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 149 | UnpoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 150 | new (&value_) T(std::move(m.value_)); // T's move constructor. |
| 151 | has_value_ = true; |
| 152 | } |
danilchap | c4fd23c | 2016-10-17 07:16:54 -0700 | [diff] [blame^] | 153 | } else { |
| 154 | reset(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 155 | } |
| 156 | return *this; |
| 157 | } |
| 158 | |
| 159 | // Swap the values if both m1 and m2 have values; move the value if only one |
| 160 | // of them has one. |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 161 | friend void swap(Optional& m1, Optional& m2) { |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 162 | if (m1.has_value_) { |
| 163 | if (m2.has_value_) { |
| 164 | // Both have values: swap. |
| 165 | using std::swap; |
| 166 | swap(m1.value_, m2.value_); |
| 167 | } else { |
| 168 | // Only m1 has a value: move it to m2. |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 169 | m2.UnpoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 170 | new (&m2.value_) T(std::move(m1.value_)); |
| 171 | m1.value_.~T(); // Destroy the moved-from value. |
| 172 | m1.has_value_ = false; |
| 173 | m2.has_value_ = true; |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 174 | m1.PoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 175 | } |
| 176 | } else if (m2.has_value_) { |
| 177 | // Only m2 has a value: move it to m1. |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 178 | m1.UnpoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 179 | new (&m1.value_) T(std::move(m2.value_)); |
| 180 | m2.value_.~T(); // Destroy the moved-from value. |
| 181 | m1.has_value_ = true; |
| 182 | m2.has_value_ = false; |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 183 | m2.PoisonValue(); |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 184 | } |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 185 | } |
| 186 | |
danilchap | c4fd23c | 2016-10-17 07:16:54 -0700 | [diff] [blame^] | 187 | // Destroy any contained value. Has no effect if we have no value. |
| 188 | void reset() { |
| 189 | if (!has_value_) |
| 190 | return; |
| 191 | value_.~T(); |
| 192 | has_value_ = false; |
| 193 | PoisonValue(); |
| 194 | } |
| 195 | |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 196 | // Conversion to bool to test if we have a value. |
| 197 | explicit operator bool() const { return has_value_; } |
| 198 | |
| 199 | // Dereferencing. Only allowed if we have a value. |
| 200 | const T* operator->() const { |
| 201 | RTC_DCHECK(has_value_); |
| 202 | return &value_; |
| 203 | } |
| 204 | T* operator->() { |
| 205 | RTC_DCHECK(has_value_); |
| 206 | return &value_; |
| 207 | } |
| 208 | const T& operator*() const { |
| 209 | RTC_DCHECK(has_value_); |
| 210 | return value_; |
| 211 | } |
| 212 | T& operator*() { |
| 213 | RTC_DCHECK(has_value_); |
| 214 | return value_; |
| 215 | } |
| 216 | |
| 217 | // Dereference with a default value in case we don't have a value. |
| 218 | const T& value_or(const T& default_val) const { |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 219 | // The no-op call prevents the compiler from generating optimized code that |
| 220 | // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is |
| 221 | // not completely inlined; see its declaration.). |
| 222 | return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_) |
| 223 | : default_val; |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Equality tests. Two Optionals are equal if they contain equivalent values, |
| 227 | // or |
| 228 | // if they're both empty. |
| 229 | friend bool operator==(const Optional& m1, const Optional& m2) { |
| 230 | return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ |
| 231 | : m1.has_value_ == m2.has_value_; |
| 232 | } |
| 233 | friend bool operator!=(const Optional& m1, const Optional& m2) { |
| 234 | return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ |
| 235 | : m1.has_value_ != m2.has_value_; |
| 236 | } |
| 237 | |
| 238 | private: |
kwiberg | 2b11fd2 | 2016-09-09 03:35:24 -0700 | [diff] [blame] | 239 | // Tell sanitizers that value_ shouldn't be touched. |
| 240 | void PoisonValue() { |
| 241 | rtc::AsanPoison(rtc::MakeArrayView(&value_, 1)); |
| 242 | rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1)); |
| 243 | } |
| 244 | |
| 245 | // Tell sanitizers that value_ is OK to touch again. |
| 246 | void UnpoisonValue() { |
| 247 | rtc::AsanUnpoison(rtc::MakeArrayView(&value_, 1)); |
| 248 | } |
| 249 | |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 250 | bool has_value_; // True iff value_ contains a live value. |
| 251 | union { |
ossu | ea41694 | 2016-06-30 02:14:54 -0700 | [diff] [blame] | 252 | // empty_ exists only to make it possible to initialize the union, even when |
| 253 | // it doesn't contain any data. If the union goes uninitialized, it may |
| 254 | // trigger compiler warnings. |
| 255 | char empty_; |
kwiberg | d040480 | 2016-05-09 06:06:05 -0700 | [diff] [blame] | 256 | // By placing value_ in a union, we get to manage its construction and |
| 257 | // destruction manually: the Optional constructors won't automatically |
| 258 | // construct it, and the Optional destructor won't automatically destroy |
| 259 | // it. Basically, this just allocates a properly sized and aligned block of |
| 260 | // memory in which we can manually put a T with placement new. |
| 261 | T value_; |
| 262 | }; |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | } // namespace rtc |
| 266 | |
| 267 | #endif // WEBRTC_BASE_OPTIONAL_H_ |