Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: webrtc/base/maybe.h

Issue 1413763003: Introduce rtc::Maybe<T>, which either contains a T or not. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Make constructors implicit again Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/base_tests.gyp ('k') | webrtc/base/maybe_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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_MAYBE_H_
12 #define WEBRTC_BASE_MAYBE_H_
13
14 #include <algorithm>
15 #include <utility>
16
17 #include "webrtc/base/checks.h"
18
19 namespace 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 Maybe<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 // TODO(kwiberg): Get rid of this class when the standard library has
32 // std::optional (and we're allowed to use it).
33 template <typename T>
34 class Maybe final {
35 public:
36 // Construct an empty Maybe.
37 Maybe() : has_value_(false) {}
38
39 // Construct a Maybe that contains a value. Note: These are non-explicit, so
40 // that a T will implicitly convert to Maybe<T>.
41 Maybe(const T& val) : value_(val), has_value_(true) {}
42 Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {}
43
44 // Copy and move constructors.
45 // TODO(kwiberg): =default the move constructor when MSVC supports it.
46 Maybe(const Maybe&) = default;
47 Maybe(Maybe&& m)
48 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {}
49
50 // Assignment. Note that we allow assignment from either Maybe<T> or plain T.
51 // TODO(kwiberg): =default the move assignment op when MSVC supports it.
52 Maybe& operator=(const Maybe&) = default;
53 Maybe& operator=(Maybe&& m) {
54 value_ = static_cast<T&&>(m.value_);
55 has_value_ = m.has_value_;
56 return *this;
57 }
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
69 friend void swap(Maybe& m1, Maybe& m2) {
70 using std::swap;
71 swap(m1.value_, m2.value_);
72 swap(m1.has_value_, m2.has_value_);
73 }
74
75 // Conversion to bool to test if we have a value.
76 explicit operator bool() const { return has_value_; }
77
78 // Dereferencing. Only allowed if we have a value.
79 const T* operator->() const {
80 RTC_DCHECK(has_value_);
81 return &value_;
82 }
83 T* operator->() {
84 RTC_DCHECK(has_value_);
85 return &value_;
86 }
87 const T& operator*() const {
88 RTC_DCHECK(has_value_);
89 return value_;
90 }
91 T& operator*() {
92 RTC_DCHECK(has_value_);
93 return value_;
94 }
95
96 // Dereference with a default value in case we don't have a value.
97 const T& value_or(const T& default_val) const {
98 return has_value_ ? value_ : default_val;
99 }
100
101 // Equality tests. Two Maybes are equal if they contain equivalent values, or
102 // if they're both empty.
103 friend bool operator==(const Maybe& m1, const Maybe& m2) {
104 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_
105 : m1.has_value_ == m2.has_value_;
106 }
107 friend bool operator!=(const Maybe& m1, const Maybe& m2) {
108 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
109 : m1.has_value_ != m2.has_value_;
110 }
111
112 private:
113 // Invariant: Unless *this has been moved from, value_ is default-initialized
114 // (or copied or moved from a default-initialized T) if !has_value_.
115 T value_;
116 bool has_value_;
117 };
118
119 } // namespace rtc
120
121 #endif // WEBRTC_BASE_MAYBE_H_
OLDNEW
« no previous file with comments | « webrtc/base/base_tests.gyp ('k') | webrtc/base/maybe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698