Chromium Code Reviews| 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 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 // | 55 // |
| 56 // 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 |
| 57 // std::optional (and we're allowed to use it). | 57 // std::optional (and we're allowed to use it). |
| 58 template <typename T> | 58 template <typename T> |
| 59 class Optional final { | 59 class Optional final { |
| 60 public: | 60 public: |
| 61 // Construct an empty Optional. | 61 // Construct an empty Optional. |
| 62 Optional() : has_value_(false) {} | 62 Optional() : has_value_(false) {} |
| 63 | 63 |
| 64 // Construct an Optional that contains a value. | 64 // Construct an Optional that contains a value. |
| 65 explicit Optional(const T& value) : has_value_(true) { | 65 Optional(const T& value) : has_value_(true) { |
|
tommi
2016/06/16 16:08:22
this goes against the style guide though. Is requ
kwiberg-webrtc
2016/06/16 17:26:42
It's very convenient to have T convert implicitly
| |
| 66 new (&value_) T(value); | 66 new (&value_) T(value); |
| 67 } | 67 } |
| 68 explicit Optional(T&& value) : has_value_(true) { | 68 Optional(T&& value) : has_value_(true) { |
| 69 new (&value_) T(std::move(value)); | 69 new (&value_) T(std::move(value)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Copy constructor: copies the value from m if it has one. | 72 // Copy constructor: copies the value from m if it has one. |
| 73 Optional(const Optional& m) : has_value_(m.has_value_) { | 73 Optional(const Optional& m) : has_value_(m.has_value_) { |
| 74 if (has_value_) | 74 if (has_value_) |
| 75 new (&value_) T(m.value_); | 75 new (&value_) T(m.value_); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Move constructor: if m has a value, moves the value from m, leaving m | 78 // Move constructor: if m has a value, moves the value from m, leaving m |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 // if they're both empty. | 179 // if they're both empty. |
| 180 friend bool operator==(const Optional& m1, const Optional& m2) { | 180 friend bool operator==(const Optional& m1, const Optional& m2) { |
| 181 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ | 181 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ |
| 182 : m1.has_value_ == m2.has_value_; | 182 : m1.has_value_ == m2.has_value_; |
| 183 } | 183 } |
| 184 friend bool operator!=(const Optional& m1, const Optional& m2) { | 184 friend bool operator!=(const Optional& m1, const Optional& m2) { |
| 185 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ | 185 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ |
| 186 : m1.has_value_ != m2.has_value_; | 186 : m1.has_value_ != m2.has_value_; |
| 187 } | 187 } |
| 188 | 188 |
| 189 friend bool operator==(const Optional& opt, const T& val) { | |
| 190 return opt.has_value_ && opt.value_ == val; | |
| 191 } | |
| 192 | |
| 193 friend bool operator==(const T& val, const Optional& opt) { | |
| 194 return opt == val; | |
| 195 } | |
| 196 | |
| 197 friend bool operator!=(const Optional& opt, const T& val) { | |
| 198 return !(opt == val); | |
| 199 } | |
| 200 | |
| 201 friend bool operator!=(const T& val, const Optional& opt) { | |
| 202 return !(opt == val); | |
| 203 } | |
| 204 | |
| 189 private: | 205 private: |
| 190 bool has_value_; // True iff value_ contains a live value. | 206 bool has_value_; // True iff value_ contains a live value. |
| 191 union { | 207 union { |
| 192 // By placing value_ in a union, we get to manage its construction and | 208 // By placing value_ in a union, we get to manage its construction and |
| 193 // destruction manually: the Optional constructors won't automatically | 209 // destruction manually: the Optional constructors won't automatically |
| 194 // construct it, and the Optional destructor won't automatically destroy | 210 // construct it, and the Optional destructor won't automatically destroy |
| 195 // it. Basically, this just allocates a properly sized and aligned block of | 211 // 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. | 212 // memory in which we can manually put a T with placement new. |
| 197 T value_; | 213 T value_; |
| 198 }; | 214 }; |
| 199 }; | 215 }; |
| 200 | 216 |
| 201 } // namespace rtc | 217 } // namespace rtc |
| 202 | 218 |
| 203 #endif // WEBRTC_BASE_OPTIONAL_H_ | 219 #endif // WEBRTC_BASE_OPTIONAL_H_ |
| OLD | NEW |