| 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 18 matching lines...) Expand all Loading... |
| 29 // assume that it just doesn't contain a value anymore. | 29 // assume that it just doesn't contain a value anymore. |
| 30 // | 30 // |
| 31 // TODO(kwiberg): Get rid of this class when the standard library has | 31 // TODO(kwiberg): Get rid of this class when the standard library has |
| 32 // std::optional (and we're allowed to use it). | 32 // std::optional (and we're allowed to use it). |
| 33 template <typename T> | 33 template <typename T> |
| 34 class Maybe final { | 34 class Maybe final { |
| 35 public: | 35 public: |
| 36 // Construct an empty Maybe. | 36 // Construct an empty Maybe. |
| 37 Maybe() : has_value_(false) {} | 37 Maybe() : has_value_(false) {} |
| 38 | 38 |
| 39 // Construct a Maybe that contains a value. Note: These are non-explicit, so | 39 // Construct a Maybe that contains a value. |
| 40 // that a T will implicitly convert to Maybe<T>. | 40 explicit Maybe(const T& val) : value_(val), has_value_(true) {} |
| 41 Maybe(const T& val) : value_(val), has_value_(true) {} | 41 explicit Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {} |
| 42 Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {} | |
| 43 | 42 |
| 44 // Copy and move constructors. | 43 // Copy and move constructors. |
| 45 // TODO(kwiberg): =default the move constructor when MSVC supports it. | 44 // TODO(kwiberg): =default the move constructor when MSVC supports it. |
| 46 Maybe(const Maybe&) = default; | 45 Maybe(const Maybe&) = default; |
| 47 Maybe(Maybe&& m) | 46 Maybe(Maybe&& m) |
| 48 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} | 47 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} |
| 49 | 48 |
| 50 // Assignment. Note that we allow assignment from either Maybe<T> or plain T. | 49 // Assignment. |
| 51 // TODO(kwiberg): =default the move assignment op when MSVC supports it. | 50 // TODO(kwiberg): =default the move assignment op when MSVC supports it. |
| 52 Maybe& operator=(const Maybe&) = default; | 51 Maybe& operator=(const Maybe&) = default; |
| 53 Maybe& operator=(Maybe&& m) { | 52 Maybe& operator=(Maybe&& m) { |
| 54 value_ = static_cast<T&&>(m.value_); | 53 value_ = static_cast<T&&>(m.value_); |
| 55 has_value_ = m.has_value_; | 54 has_value_ = m.has_value_; |
| 56 return *this; | 55 return *this; |
| 57 } | 56 } |
| 58 Maybe& operator=(const T& val) { | |
| 59 value_ = val; | |
| 60 has_value_ = true; | |
| 61 return *this; | |
| 62 } | |
| 63 Maybe& operator=(T&& val) { | |
| 64 value_ = static_cast<T&&>(val); | |
| 65 has_value_ = true; | |
| 66 return *this; | |
| 67 } | |
| 68 | 57 |
| 69 friend void swap(Maybe& m1, Maybe& m2) { | 58 friend void swap(Maybe& m1, Maybe& m2) { |
| 70 using std::swap; | 59 using std::swap; |
| 71 swap(m1.value_, m2.value_); | 60 swap(m1.value_, m2.value_); |
| 72 swap(m1.has_value_, m2.has_value_); | 61 swap(m1.has_value_, m2.has_value_); |
| 73 } | 62 } |
| 74 | 63 |
| 75 // Conversion to bool to test if we have a value. | 64 // Conversion to bool to test if we have a value. |
| 76 explicit operator bool() const { return has_value_; } | 65 explicit operator bool() const { return has_value_; } |
| 77 | 66 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 private: | 101 private: |
| 113 // Invariant: Unless *this has been moved from, value_ is default-initialized | 102 // Invariant: Unless *this has been moved from, value_ is default-initialized |
| 114 // (or copied or moved from a default-initialized T) if !has_value_. | 103 // (or copied or moved from a default-initialized T) if !has_value_. |
| 115 T value_; | 104 T value_; |
| 116 bool has_value_; | 105 bool has_value_; |
| 117 }; | 106 }; |
| 118 | 107 |
| 119 } // namespace rtc | 108 } // namespace rtc |
| 120 | 109 |
| 121 #endif // WEBRTC_BASE_MAYBE_H_ | 110 #endif // WEBRTC_BASE_MAYBE_H_ |
| OLD | NEW |