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 | |
mgraczyk
2015/10/20 02:44:44
Why? I can't do this?
Maybe<T> x{10};
func(std::m
kwiberg-webrtc
2015/10/20 08:43:20
Yeah, that's actually the case here too. I'll fix
| |
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. | |
39 explicit Maybe(const T& val) : value_(val), has_value_(true) {} | |
mgraczyk
2015/10/20 02:44:44
These constructors are not explicit for std::exper
kwiberg-webrtc
2015/10/20 08:43:20
I want it to work, but it means more style guide v
Andrew MacDonald
2015/10/20 18:38:04
Personally, I'd prefer the implicit conversions. A
mgraczyk
2015/10/20 19:10:23
+1
kwiberg-webrtc
2015/10/20 21:11:59
Swell. I'll restore the implicit conversions then.
| |
40 explicit Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {} | |
41 | |
42 // Copy and move constructors. | |
43 // TODO(kwiberg): =default the move constructor when MSVC supports it. | |
Andrew MacDonald
2015/10/20 01:54:30
Ugh :(
kwiberg-webrtc
2015/10/20 08:43:20
Yeah. C++11 says you're allowed to =default them,
| |
44 Maybe(const Maybe&) = default; | |
45 Maybe(Maybe&& m) | |
46 : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} | |
47 | |
48 // Assignment. Note that we allow assignment from either Maybe<T> or plain T. | |
49 // TODO(kwiberg): =default the move assignment op when MSVC supports it. | |
50 Maybe& operator=(const Maybe&) = default; | |
51 Maybe& operator=(Maybe&& m) { | |
52 value_ = static_cast<T&&>(m.value_); | |
53 has_value_ = m.has_value_; | |
54 return *this; | |
55 } | |
56 Maybe& operator=(const T& val) { | |
mgraczyk
2015/10/20 02:44:44
std::optional does not have this overload.
http:/
kwiberg-webrtc
2015/10/20 08:43:20
And since it will implicitly turn T into std::opti
Andrew MacDonald
2015/10/20 18:38:04
So if you remove explicit from Maybe's correspondi
mgraczyk
2015/10/20 19:10:23
Yes I believe this is not needed.
kwiberg-webrtc
2015/10/20 21:11:59
I'll try it, and see if the test detects any chang
kwiberg-webrtc
2015/10/20 21:26:00
As expected, it works, but results in a temporary
mgraczyk
2015/10/20 22:16:19
Yes but I would be surprised if the temporary were
kwiberg-webrtc
2015/10/20 22:57:17
Yes, the extra temp is created even with optimizat
| |
57 value_ = val; | |
58 has_value_ = true; | |
59 return *this; | |
60 } | |
61 Maybe& operator=(T&& val) { | |
62 value_ = static_cast<T&&>(val); | |
63 has_value_ = true; | |
64 return *this; | |
65 } | |
66 | |
67 friend void swap(Maybe& m1, Maybe& m2) { | |
mgraczyk
2015/10/20 02:44:44
This trick might not work with some versions of th
kwiberg-webrtc
2015/10/20 08:43:20
This page says the standard library is supposed to
mgraczyk
2015/10/20 22:16:19
The alternative would be to use a non-friend, non-
kwiberg-webrtc
2015/10/20 22:57:18
I see. And yes, the unit test for swap proves that
mgraczyk
2015/10/21 00:57:18
I can't think of any. From the SO answers I believ
| |
68 using std::swap; | |
69 swap(m1.value_, m2.value_); | |
70 swap(m1.has_value_, m2.has_value_); | |
71 } | |
72 | |
73 // Conversion to bool to test if we have a value. | |
74 explicit operator bool() const { return has_value_; } | |
75 | |
76 // Dereferencing. Only allowed if we have a value. | |
77 const T* operator->() const { | |
78 RTC_DCHECK(has_value_); | |
79 return &value_; | |
80 } | |
81 T* operator->() { | |
82 RTC_DCHECK(has_value_); | |
83 return &value_; | |
84 } | |
85 const T& operator*() const { | |
86 RTC_DCHECK(has_value_); | |
87 return value_; | |
88 } | |
89 T& operator*() { | |
90 RTC_DCHECK(has_value_); | |
91 return value_; | |
92 } | |
mgraczyk
2015/10/20 02:44:44
It would probably be worthwhile to include value_o
kwiberg-webrtc
2015/10/20 08:43:20
Sounds reasonable. But I don't understand why std:
kwiberg-webrtc
2015/10/20 13:35:30
I ended up doing only const T& value_or(const T&)
mgraczyk
2015/10/20 19:10:23
Yes, without std::move() and library support your
kwiberg-webrtc
2015/10/20 21:11:59
Well, you can have rvalues without std::move. Cons
mgraczyk
2015/10/20 22:16:19
I see and I agree that it is best to keep it simpl
| |
93 | |
94 // Equality tests. Two Maybes are equal if they contain equivalent values, or | |
95 // if they're both empty. | |
96 friend bool operator==(const Maybe& m1, const Maybe& m2) { | |
97 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ | |
98 : m1.has_value_ == m2.has_value_; | |
99 } | |
100 friend bool operator!=(const Maybe& m1, const Maybe& m2) { | |
101 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ | |
Andrew MacDonald
2015/10/20 01:54:30
return !(m1 == m2)
is more typical?
kwiberg-webrtc
2015/10/20 08:43:20
Yes. But, if I do that, then Maybe<T>::operator!=
Andrew MacDonald
2015/10/20 18:38:04
No, I see why you did it this way now. Thanks for
| |
102 : m1.has_value_ != m2.has_value_; | |
103 } | |
104 | |
105 private: | |
106 // Invariant: Unless *this has been moved from, value_ is default-initialized | |
107 // (or copied or moved from a default-initialized T) if !has_value_. | |
108 T value_; | |
109 bool has_value_; | |
110 }; | |
111 | |
112 } // namespace rtc | |
113 | |
114 #endif // WEBRTC_BASE_MAYBE_H_ | |
OLD | NEW |