Chromium Code Reviews| Index: webrtc/base/maybe.h |
| diff --git a/webrtc/base/maybe.h b/webrtc/base/maybe.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0fc1914ce285c6f307306e6f035d4d2c4d6bbee6 |
| --- /dev/null |
| +++ b/webrtc/base/maybe.h |
| @@ -0,0 +1,114 @@ |
| +/* |
| + * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#ifndef WEBRTC_BASE_MAYBE_H_ |
| +#define WEBRTC_BASE_MAYBE_H_ |
| + |
| +#include <algorithm> |
| +#include <utility> |
| + |
| +#include "webrtc/base/checks.h" |
| + |
| +namespace rtc { |
| + |
| +// Simple std::experimental::optional-wannabe. It either contains a T or not. |
| +// In order to keep the implementation simple and portable, this implementation |
| +// actually contains a (default-constructed) T even when it supposedly doesn't |
| +// contain a value; use e.g. rtc::scoped_ptr<T> instead if that's too |
| +// expensive. |
| +// |
| +// 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
|
| +// assume that it just doesn't contain a value anymore and can be reused. |
| +// |
| +// TODO(kwiberg): Get rid of this class when the standard library has |
| +// std::optional (and we're allowed to use it). |
| +template <typename T> |
| +class Maybe final { |
| + public: |
| + // Construct an empty Maybe. |
| + Maybe() : has_value_(false) {} |
| + |
| + // Construct a Maybe that contains a value. |
| + 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.
|
| + explicit Maybe(T&& val) : value_(static_cast<T&&>(val)), has_value_(true) {} |
| + |
| + // Copy and move constructors. |
| + // 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,
|
| + Maybe(const Maybe&) = default; |
| + Maybe(Maybe&& m) |
| + : value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} |
| + |
| + // Assignment. Note that we allow assignment from either Maybe<T> or plain T. |
| + // TODO(kwiberg): =default the move assignment op when MSVC supports it. |
| + Maybe& operator=(const Maybe&) = default; |
| + Maybe& operator=(Maybe&& m) { |
| + value_ = static_cast<T&&>(m.value_); |
| + has_value_ = m.has_value_; |
| + return *this; |
| + } |
| + 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
|
| + value_ = val; |
| + has_value_ = true; |
| + return *this; |
| + } |
| + Maybe& operator=(T&& val) { |
| + value_ = static_cast<T&&>(val); |
| + has_value_ = true; |
| + return *this; |
| + } |
| + |
| + 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
|
| + using std::swap; |
| + swap(m1.value_, m2.value_); |
| + swap(m1.has_value_, m2.has_value_); |
| + } |
| + |
| + // Conversion to bool to test if we have a value. |
| + explicit operator bool() const { return has_value_; } |
| + |
| + // Dereferencing. Only allowed if we have a value. |
| + const T* operator->() const { |
| + RTC_DCHECK(has_value_); |
| + return &value_; |
| + } |
| + T* operator->() { |
| + RTC_DCHECK(has_value_); |
| + return &value_; |
| + } |
| + const T& operator*() const { |
| + RTC_DCHECK(has_value_); |
| + return value_; |
| + } |
| + T& operator*() { |
| + RTC_DCHECK(has_value_); |
| + return value_; |
| + } |
|
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
|
| + |
| + // Equality tests. Two Maybes are equal if they contain equivalent values, or |
| + // if they're both empty. |
| + friend bool operator==(const Maybe& m1, const Maybe& m2) { |
| + return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ |
| + : m1.has_value_ == m2.has_value_; |
| + } |
| + friend bool operator!=(const Maybe& m1, const Maybe& m2) { |
| + 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
|
| + : m1.has_value_ != m2.has_value_; |
| + } |
| + |
| + private: |
| + // Invariant: Unless *this has been moved from, value_ is default-initialized |
| + // (or copied or moved from a default-initialized T) if !has_value_. |
| + T value_; |
| + bool has_value_; |
| +}; |
| + |
| +} // namespace rtc |
| + |
| +#endif // WEBRTC_BASE_MAYBE_H_ |