| 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_MAYBE_H_ | 11 #ifndef WEBRTC_BASE_OPTIONAL_H_ |
| 12 #define WEBRTC_BASE_MAYBE_H_ | 12 #define WEBRTC_BASE_OPTIONAL_H_ |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 18 | 18 |
| 19 namespace rtc { | 19 namespace rtc { |
| 20 | 20 |
| 21 // Simple std::experimental::optional-wannabe. It either contains a T or not. | 21 // Simple std::experimental::optional-wannabe. It either contains a T or not. |
| 22 // In order to keep the implementation simple and portable, this implementation | 22 // In order to keep the implementation simple and portable, this implementation |
| 23 // actually contains a (default-constructed) T even when it supposedly doesn't | 23 // actually contains a (default-constructed) T even when it supposedly doesn't |
| 24 // contain a value; use e.g. rtc::scoped_ptr<T> instead if that's too | 24 // contain a value; use e.g. rtc::scoped_ptr<T> instead if that's too |
| 25 // expensive. | 25 // expensive. |
| 26 // | 26 // |
| 27 // A moved-from Maybe<T> may only be destroyed, and assigned to if T allows | 27 // A moved-from Optional<T> may only be destroyed, and assigned to if T allows |
| 28 // being assigned to after having been moved from. Specifically, you may not | 28 // being assigned to after having been moved from. Specifically, you may not |
| 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 // Examples of good places to use Optional: |
| 32 // |
| 33 // - As a class or struct member, when the member doesn't always have a value: |
| 34 // struct Prisoner { |
| 35 // std::string name; |
| 36 // Optional<int> cell_number; // Empty if not currently incarcerated. |
| 37 // }; |
| 38 // |
| 39 // - As a return value for functions that may fail to return a value on all |
| 40 // allowed inputs. For example, a function that searches an array might |
| 41 // return an Optional<size_t> (the index where it found the element, or |
| 42 // nothing if it didn't find it); and a function that parses numbers might |
| 43 // return Optional<double> (the parsed number, or nothing if parsing failed). |
| 44 // |
| 45 // Examples of bad places to use Optional: |
| 46 // |
| 47 // - As a return value for functions that may fail because of disallowed |
| 48 // inputs. For example, a string length function should not return |
| 49 // Optional<size_t> so that it can return nothing in case the caller passed |
| 50 // it a null pointer; the function should probably use RTC_[D]CHECK instead, |
| 51 // and return plain size_t. |
| 52 // |
| 53 // - As a return value for functions that may fail to return a value on all |
| 54 // allowed inputs, but need to tell the caller what went wrong. Returning |
| 55 // Optional<double> when parsing a single number as in the example above |
| 56 // might make sense, but any larger parse job is probably going to need to |
| 57 // tell the caller what the problem was, not just that there was one. |
| 58 // |
| 31 // TODO(kwiberg): Get rid of this class when the standard library has | 59 // TODO(kwiberg): Get rid of this class when the standard library has |
| 32 // std::optional (and we're allowed to use it). | 60 // std::optional (and we're allowed to use it). |
| 33 template <typename T> | 61 template <typename T> |
| 34 class Maybe final { | 62 class Optional final { |
| 35 public: | 63 public: |
| 36 // Construct an empty Maybe. | 64 // Construct an empty Optional. |
| 37 Maybe() : has_value_(false) {} | 65 Optional() : has_value_(false) {} |
| 38 | 66 |
| 39 // Construct a Maybe that contains a value. | 67 // Construct an Optional that contains a value. |
| 40 explicit Maybe(const T& val) : value_(val), has_value_(true) {} | 68 explicit Optional(const T& val) : value_(val), has_value_(true) {} |
| 41 explicit Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {} | 69 explicit Optional(T&& val) |
| 70 : value_(static_cast<T&&>(val)), has_value_(true) {} |
| 42 | 71 |
| 43 // Copy and move constructors. | 72 // Copy and move constructors. |
| 44 // TODO(kwiberg): =default the move constructor when MSVC supports it. | 73 // TODO(kwiberg): =default the move constructor when MSVC supports it. |
| 45 Maybe(const Maybe&) = default; | 74 Optional(const Optional&) = default; |
| 46 Maybe(Maybe&& m) | 75 Optional(Optional&& m) |
| 47 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} | 76 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} |
| 48 | 77 |
| 49 // Assignment. | 78 // Assignment. |
| 50 // TODO(kwiberg): =default the move assignment op when MSVC supports it. | 79 // TODO(kwiberg): =default the move assignment op when MSVC supports it. |
| 51 Maybe& operator=(const Maybe&) = default; | 80 Optional& operator=(const Optional&) = default; |
| 52 Maybe& operator=(Maybe&& m) { | 81 Optional& operator=(Optional&& m) { |
| 53 value_ = static_cast<T&&>(m.value_); | 82 value_ = static_cast<T&&>(m.value_); |
| 54 has_value_ = m.has_value_; | 83 has_value_ = m.has_value_; |
| 55 return *this; | 84 return *this; |
| 56 } | 85 } |
| 57 | 86 |
| 58 friend void swap(Maybe& m1, Maybe& m2) { | 87 friend void swap(Optional& m1, Optional& m2) { |
| 59 using std::swap; | 88 using std::swap; |
| 60 swap(m1.value_, m2.value_); | 89 swap(m1.value_, m2.value_); |
| 61 swap(m1.has_value_, m2.has_value_); | 90 swap(m1.has_value_, m2.has_value_); |
| 62 } | 91 } |
| 63 | 92 |
| 64 // Conversion to bool to test if we have a value. | 93 // Conversion to bool to test if we have a value. |
| 65 explicit operator bool() const { return has_value_; } | 94 explicit operator bool() const { return has_value_; } |
| 66 | 95 |
| 67 // Dereferencing. Only allowed if we have a value. | 96 // Dereferencing. Only allowed if we have a value. |
| 68 const T* operator->() const { | 97 const T* operator->() const { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 80 T& operator*() { | 109 T& operator*() { |
| 81 RTC_DCHECK(has_value_); | 110 RTC_DCHECK(has_value_); |
| 82 return value_; | 111 return value_; |
| 83 } | 112 } |
| 84 | 113 |
| 85 // Dereference with a default value in case we don't have a value. | 114 // Dereference with a default value in case we don't have a value. |
| 86 const T& value_or(const T& default_val) const { | 115 const T& value_or(const T& default_val) const { |
| 87 return has_value_ ? value_ : default_val; | 116 return has_value_ ? value_ : default_val; |
| 88 } | 117 } |
| 89 | 118 |
| 90 // Equality tests. Two Maybes are equal if they contain equivalent values, or | 119 // Equality tests. Two Optionals are equal if they contain equivalent values, |
| 120 // or |
| 91 // if they're both empty. | 121 // if they're both empty. |
| 92 friend bool operator==(const Maybe& m1, const Maybe& m2) { | 122 friend bool operator==(const Optional& m1, const Optional& m2) { |
| 93 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ | 123 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ |
| 94 : m1.has_value_ == m2.has_value_; | 124 : m1.has_value_ == m2.has_value_; |
| 95 } | 125 } |
| 96 friend bool operator!=(const Maybe& m1, const Maybe& m2) { | 126 friend bool operator!=(const Optional& m1, const Optional& m2) { |
| 97 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ | 127 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ |
| 98 : m1.has_value_ != m2.has_value_; | 128 : m1.has_value_ != m2.has_value_; |
| 99 } | 129 } |
| 100 | 130 |
| 101 private: | 131 private: |
| 102 // Invariant: Unless *this has been moved from, value_ is default-initialized | 132 // Invariant: Unless *this has been moved from, value_ is default-initialized |
| 103 // (or copied or moved from a default-initialized T) if !has_value_. | 133 // (or copied or moved from a default-initialized T) if !has_value_. |
| 104 T value_; | 134 T value_; |
| 105 bool has_value_; | 135 bool has_value_; |
| 106 }; | 136 }; |
| 107 | 137 |
| 108 } // namespace rtc | 138 } // namespace rtc |
| 109 | 139 |
| 110 #endif // WEBRTC_BASE_MAYBE_H_ | 140 #endif // WEBRTC_BASE_OPTIONAL_H_ |
| OLD | NEW |