OLD | NEW |
---|---|
(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. Specifically, you may not | |
28 // assume that it just doesn't contain a value anymore and can be reused. | |
29 // | |
30 // TODO(kwiberg): Get rid of this class when the standard library has | |
31 // std::optional (and we're allowed to use it). | |
32 template <typename T> | |
33 class Maybe final { | |
34 public: | |
35 // Construct an empty Maybe. | |
36 Maybe() : has_value_(false) {} | |
37 | |
38 // Construct a Maybe that contains a value. Note: These are non-explicit, so | |
39 // that a T will implicitly convert to Maybe<T>. | |
40 Maybe(const T& val) : value_(val), has_value_(true) {} | |
41 Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {} | |
42 | |
43 // Copy and move constructors. | |
44 // TODO(kwiberg): =default the move constructor when MSVC supports it. | |
45 Maybe(const Maybe&) = default; | |
46 Maybe(Maybe&& m) | |
47 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} | |
48 | |
49 // Assignment. Note that we allow assignment from either Maybe<T> or plain T. | |
50 // TODO(kwiberg): =default the move assignment op when MSVC supports it. | |
51 Maybe& operator=(const Maybe&) = default; | |
52 Maybe& operator=(Maybe&& m) { | |
53 value_ = static_cast<T&&>(m.value_); | |
54 has_value_ = m.has_value_; | |
55 return *this; | |
56 } | |
57 Maybe& operator=(const T& val) { | |
the sun
2015/10/19 12:43:41
if (this != &val) { ...
kwiberg-webrtc
2015/10/19 13:00:06
Not possible. this is a Maybe<T>*, &val is a T*.
| |
58 value_ = val; | |
59 has_value_ = true; | |
60 return *this; | |
61 } | |
62 Maybe& operator=(T&& val) { | |
63 value_ = static_cast<T&&>(val); | |
64 has_value_ = true; | |
65 return *this; | |
66 } | |
67 | |
68 friend void swap(Maybe& m1, Maybe& m2) { | |
69 using std::swap; | |
70 swap(m1.value_, m2.value_); | |
71 swap(m1.has_value_, m2.has_value_); | |
72 } | |
73 | |
74 // Conversion to bool to test if we have a value. | |
75 explicit operator bool() const { return has_value_; } | |
76 | |
77 // Dereferencing. Only allowed if we have a value. | |
78 const T* operator->() const { | |
79 RTC_DCHECK(has_value_); | |
80 return &value_; | |
81 } | |
82 T* operator->() { | |
83 RTC_DCHECK(has_value_); | |
84 return &value_; | |
85 } | |
86 const T& operator*() const { | |
87 RTC_DCHECK(has_value_); | |
88 return value_; | |
89 } | |
90 T& operator*() { | |
91 RTC_DCHECK(has_value_); | |
92 return value_; | |
93 } | |
94 | |
95 // Equality tests. Two Maybes are equal if they contain equivalent values, or | |
96 // if they're both empty. | |
97 friend bool operator==(const Maybe& m1, const Maybe& m2) { | |
98 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ | |
99 : m1.has_value_ == m2.has_value_; | |
100 } | |
101 friend bool operator!=(const Maybe& m1, const Maybe& m2) { | |
102 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ | |
103 : m1.has_value_ != m2.has_value_; | |
104 } | |
105 | |
106 private: | |
107 // Invariant: Unless *this has been moved from, value_ is default-initialized | |
108 // (or copied or moved from a default-initialized T) if !has_value_. | |
109 T value_; | |
110 bool has_value_; | |
111 }; | |
112 | |
113 } // namespace rtc | |
114 | |
115 #endif // WEBRTC_BASE_MAYBE_H_ | |
OLD | NEW |