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

Unified 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: MSVC lacks support for C++11 =default for move construction and assignment 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/base_tests.gyp ('k') | webrtc/base/maybe_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/maybe.h
diff --git a/webrtc/base/maybe.h b/webrtc/base/maybe.h
new file mode 100644
index 0000000000000000000000000000000000000000..766ae30ac831f586e555e50188d8840549f9bae8
--- /dev/null
+++ b/webrtc/base/maybe.h
@@ -0,0 +1,115 @@
+/*
+ * 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
+// 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. Note: These are non-explicit, so
+ // that a T will implicitly convert to Maybe<T>.
+ Maybe(const T& val) : value_(val), has_value_(true) {}
+ 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.
+ 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) {
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*.
+ 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) {
+ 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_;
+ }
+
+ // 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_
+ : 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_
« 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