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