| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_OPTIONAL_H_ | 11 #ifndef WEBRTC_BASE_OPTIONAL_H_ |
| 12 #define WEBRTC_BASE_OPTIONAL_H_ | 12 #define WEBRTC_BASE_OPTIONAL_H_ |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 #include <memory> | 15 #include <memory> |
| 16 #include <utility> | 16 #include <utility> |
| 17 | 17 |
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 | 19 |
| 20 namespace rtc { | 20 namespace rtc { |
| 21 | 21 |
| 22 // Simple std::experimental::optional-wannabe. It either contains a T or not. | 22 // Simple std::optional-wannabe. It either contains a T or not. |
| 23 // In order to keep the implementation simple and portable, this implementation | |
| 24 // actually contains a (default-constructed) T even when it supposedly doesn't | |
| 25 // contain a value; use e.g. std::unique_ptr<T> instead if that's too | |
| 26 // expensive. | |
| 27 // | 23 // |
| 28 // A moved-from Optional<T> may only be destroyed, and assigned to if T allows | 24 // A moved-from Optional<T> may only be destroyed, and assigned to if T allows |
| 29 // being assigned to after having been moved from. Specifically, you may not | 25 // being assigned to after having been moved from. Specifically, you may not |
| 30 // assume that it just doesn't contain a value anymore. | 26 // assume that it just doesn't contain a value anymore. |
| 31 // | 27 // |
| 32 // Examples of good places to use Optional: | 28 // Examples of good places to use Optional: |
| 33 // | 29 // |
| 34 // - As a class or struct member, when the member doesn't always have a value: | 30 // - As a class or struct member, when the member doesn't always have a value: |
| 35 // struct Prisoner { | 31 // struct Prisoner { |
| 36 // std::string name; | 32 // std::string name; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 59 // | 55 // |
| 60 // TODO(kwiberg): Get rid of this class when the standard library has | 56 // TODO(kwiberg): Get rid of this class when the standard library has |
| 61 // std::optional (and we're allowed to use it). | 57 // std::optional (and we're allowed to use it). |
| 62 template <typename T> | 58 template <typename T> |
| 63 class Optional final { | 59 class Optional final { |
| 64 public: | 60 public: |
| 65 // Construct an empty Optional. | 61 // Construct an empty Optional. |
| 66 Optional() : has_value_(false) {} | 62 Optional() : has_value_(false) {} |
| 67 | 63 |
| 68 // Construct an Optional that contains a value. | 64 // Construct an Optional that contains a value. |
| 69 explicit Optional(const T& val) : value_(val), has_value_(true) {} | 65 explicit Optional(const T& value) : has_value_(true) { |
| 70 explicit Optional(T&& val) : value_(std::move(val)), has_value_(true) {} | 66 new (&value_) T(value); |
| 67 } |
| 68 explicit Optional(T&& value) : has_value_(true) { |
| 69 new (&value_) T(std::move(value)); |
| 70 } |
| 71 | 71 |
| 72 // Copy and move constructors. | 72 // Copy constructor: copies the value from m if it has one. |
| 73 Optional(const Optional&) = default; | 73 Optional(const Optional& m) : has_value_(m.has_value_) { |
| 74 Optional(Optional&&) = default; | 74 if (has_value_) |
| 75 new (&value_) T(m.value_); |
| 76 } |
| 75 | 77 |
| 76 // Assignment. | 78 // Move constructor: if m has a value, moves the value from m, leaving m |
| 77 Optional& operator=(const Optional&) = default; | 79 // still in a state where it has a value, but a moved-from one (the |
| 78 Optional& operator=(Optional&&) = default; | 80 // properties of which depends on T; the only general guarantee is that we |
| 81 // can destroy m). |
| 82 Optional(Optional&& m) : has_value_(m.has_value_) { |
| 83 if (has_value_) |
| 84 new (&value_) T(std::move(m.value_)); |
| 85 } |
| 79 | 86 |
| 87 ~Optional() { |
| 88 if (has_value_) |
| 89 value_.~T(); |
| 90 } |
| 91 |
| 92 // Copy assignment. Uses T's copy assignment if both sides have a value, T's |
| 93 // copy constructor if only the right-hand side has a value. |
| 94 Optional& operator=(const Optional& m) { |
| 95 if (m.has_value_) { |
| 96 if (has_value_) { |
| 97 value_ = m.value_; // T's copy assignment. |
| 98 } else { |
| 99 new (&value_) T(m.value_); // T's copy constructor. |
| 100 has_value_ = true; |
| 101 } |
| 102 } else if (has_value_) { |
| 103 value_.~T(); |
| 104 has_value_ = false; |
| 105 } |
| 106 return *this; |
| 107 } |
| 108 |
| 109 // Move assignment. Uses T's move assignment if both sides have a value, T's |
| 110 // move constructor if only the right-hand side has a value. The state of m |
| 111 // after it's been moved from is as for the move constructor. |
| 112 Optional& operator=(Optional&& m) { |
| 113 if (m.has_value_) { |
| 114 if (has_value_) { |
| 115 value_ = std::move(m.value_); // T's move assignment. |
| 116 } else { |
| 117 new (&value_) T(std::move(m.value_)); // T's move constructor. |
| 118 has_value_ = true; |
| 119 } |
| 120 } else if (has_value_) { |
| 121 value_.~T(); |
| 122 has_value_ = false; |
| 123 } |
| 124 return *this; |
| 125 } |
| 126 |
| 127 // Swap the values if both m1 and m2 have values; move the value if only one |
| 128 // of them has one. |
| 80 friend void swap(Optional& m1, Optional& m2) { | 129 friend void swap(Optional& m1, Optional& m2) { |
| 81 using std::swap; | 130 if (m1.has_value_) { |
| 82 swap(m1.value_, m2.value_); | 131 if (m2.has_value_) { |
| 83 swap(m1.has_value_, m2.has_value_); | 132 // Both have values: swap. |
| 133 using std::swap; |
| 134 swap(m1.value_, m2.value_); |
| 135 } else { |
| 136 // Only m1 has a value: move it to m2. |
| 137 new (&m2.value_) T(std::move(m1.value_)); |
| 138 m1.value_.~T(); // Destroy the moved-from value. |
| 139 m1.has_value_ = false; |
| 140 m2.has_value_ = true; |
| 141 } |
| 142 } else if (m2.has_value_) { |
| 143 // Only m2 has a value: move it to m1. |
| 144 new (&m1.value_) T(std::move(m2.value_)); |
| 145 m2.value_.~T(); // Destroy the moved-from value. |
| 146 m1.has_value_ = true; |
| 147 m2.has_value_ = false; |
| 148 } |
| 84 } | 149 } |
| 85 | 150 |
| 86 // Conversion to bool to test if we have a value. | 151 // Conversion to bool to test if we have a value. |
| 87 explicit operator bool() const { return has_value_; } | 152 explicit operator bool() const { return has_value_; } |
| 88 | 153 |
| 89 // Dereferencing. Only allowed if we have a value. | 154 // Dereferencing. Only allowed if we have a value. |
| 90 const T* operator->() const { | 155 const T* operator->() const { |
| 91 RTC_DCHECK(has_value_); | 156 RTC_DCHECK(has_value_); |
| 92 return &value_; | 157 return &value_; |
| 93 } | 158 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 115 friend bool operator==(const Optional& m1, const Optional& m2) { | 180 friend bool operator==(const Optional& m1, const Optional& m2) { |
| 116 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ | 181 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ |
| 117 : m1.has_value_ == m2.has_value_; | 182 : m1.has_value_ == m2.has_value_; |
| 118 } | 183 } |
| 119 friend bool operator!=(const Optional& m1, const Optional& m2) { | 184 friend bool operator!=(const Optional& m1, const Optional& m2) { |
| 120 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ | 185 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ |
| 121 : m1.has_value_ != m2.has_value_; | 186 : m1.has_value_ != m2.has_value_; |
| 122 } | 187 } |
| 123 | 188 |
| 124 private: | 189 private: |
| 125 // Invariant: Unless *this has been moved from, value_ is default-initialized | 190 bool has_value_; // True iff value_ contains a live value. |
| 126 // (or copied or moved from a default-initialized T) if !has_value_. | 191 union { |
| 127 T value_; | 192 // By placing value_ in a union, we get to manage its construction and |
| 128 bool has_value_; | 193 // destruction manually: the Optional constructors won't automatically |
| 194 // construct it, and the Optional destructor won't automatically destroy |
| 195 // it. Basically, this just allocates a properly sized and aligned block of |
| 196 // memory in which we can manually put a T with placement new. |
| 197 T value_; |
| 198 }; |
| 129 }; | 199 }; |
| 130 | 200 |
| 131 } // namespace rtc | 201 } // namespace rtc |
| 132 | 202 |
| 133 #endif // WEBRTC_BASE_OPTIONAL_H_ | 203 #endif // WEBRTC_BASE_OPTIONAL_H_ |
| OLD | NEW |