blob: f25086569cbad3cb413488c7bdced19e6cf12816 [file] [log] [blame]
Karl Wibergbe579832015-11-10 22:34:18 +01001/*
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>
15#include <utility>
16
17#include "webrtc/base/checks.h"
18
19namespace rtc {
20
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
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
25// expensive.
26//
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
29// assume that it just doesn't contain a value anymore.
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//
59// TODO(kwiberg): Get rid of this class when the standard library has
60// std::optional (and we're allowed to use it).
61template <typename T>
62class Optional final {
63 public:
64 // Construct an empty Optional.
65 Optional() : has_value_(false) {}
66
67 // Construct an Optional that contains a value.
68 explicit Optional(const T& val) : value_(val), has_value_(true) {}
kwibergcea7c2f2016-01-07 05:52:04 -080069 explicit Optional(T&& val) : value_(std::move(val)), has_value_(true) {}
Karl Wibergbe579832015-11-10 22:34:18 +010070
71 // Copy and move constructors.
Karl Wibergbe579832015-11-10 22:34:18 +010072 Optional(const Optional&) = default;
kwiberg6ca0a312016-04-15 05:24:56 -070073 Optional(Optional&&) = default;
Karl Wibergbe579832015-11-10 22:34:18 +010074
75 // Assignment.
Karl Wibergbe579832015-11-10 22:34:18 +010076 Optional& operator=(const Optional&) = default;
kwiberg6ca0a312016-04-15 05:24:56 -070077 Optional& operator=(Optional&&) = default;
Karl Wibergbe579832015-11-10 22:34:18 +010078
79 friend void swap(Optional& m1, Optional& m2) {
80 using std::swap;
81 swap(m1.value_, m2.value_);
82 swap(m1.has_value_, m2.has_value_);
83 }
84
85 // Conversion to bool to test if we have a value.
86 explicit operator bool() const { return has_value_; }
87
88 // Dereferencing. Only allowed if we have a value.
89 const T* operator->() const {
90 RTC_DCHECK(has_value_);
91 return &value_;
92 }
93 T* operator->() {
94 RTC_DCHECK(has_value_);
95 return &value_;
96 }
97 const T& operator*() const {
98 RTC_DCHECK(has_value_);
99 return value_;
100 }
101 T& operator*() {
102 RTC_DCHECK(has_value_);
103 return value_;
104 }
105
106 // Dereference with a default value in case we don't have a value.
107 const T& value_or(const T& default_val) const {
108 return has_value_ ? value_ : default_val;
109 }
110
111 // Equality tests. Two Optionals are equal if they contain equivalent values,
112 // or
113 // if they're both empty.
114 friend bool operator==(const Optional& m1, const Optional& m2) {
115 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_
116 : m1.has_value_ == m2.has_value_;
117 }
118 friend bool operator!=(const Optional& m1, const Optional& m2) {
119 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
120 : m1.has_value_ != m2.has_value_;
121 }
122
123 private:
124 // Invariant: Unless *this has been moved from, value_ is default-initialized
125 // (or copied or moved from a default-initialized T) if !has_value_.
126 T value_;
127 bool has_value_;
128};
129
130} // namespace rtc
131
132#endif // WEBRTC_BASE_OPTIONAL_H_