Move optional.h to webrtc/api/
We use Optional in our public API, so its header should be in
webrtc/api/.
BUG=webrtc:8205
Review-Url: https://codereview.webrtc.org/3011943002
Cr-Commit-Position: refs/heads/master@{#19693}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
index 2e3d77a..86a63e4 100644
--- a/webrtc/api/BUILD.gn
+++ b/webrtc/api/BUILD.gn
@@ -207,6 +207,17 @@
]
}
+rtc_source_set("optional") {
+ sources = [
+ "optional.cc",
+ "optional.h",
+ ]
+ deps = [
+ ":array_view",
+ "../rtc_base:rtc_base_approved",
+ ]
+}
+
rtc_source_set("libjingle_peerconnection_test_api") {
testonly = true
sources = [
@@ -266,6 +277,7 @@
}
sources = [
"array_view_unittest.cc",
+ "optional_unittest.cc",
"ortc/mediadescription_unittest.cc",
"ortc/sessiondescription_unittest.cc",
"rtcerror_unittest.cc",
@@ -280,6 +292,7 @@
deps = [
":array_view",
":libjingle_peerconnection_api",
+ ":optional",
":ortc_api",
"../rtc_base:rtc_base_approved",
"../rtc_base:rtc_base_tests_utils",
diff --git a/webrtc/api/audio_codecs/BUILD.gn b/webrtc/api/audio_codecs/BUILD.gn
index 29238e6..04a5238 100644
--- a/webrtc/api/audio_codecs/BUILD.gn
+++ b/webrtc/api/audio_codecs/BUILD.gn
@@ -27,6 +27,7 @@
]
deps = [
"..:array_view",
+ "..:optional",
"../..:webrtc_common",
"../../rtc_base:rtc_base_approved",
]
diff --git a/webrtc/api/audio_codecs/L16/BUILD.gn b/webrtc/api/audio_codecs/L16/BUILD.gn
index db61713..8f06a8f 100644
--- a/webrtc/api/audio_codecs/L16/BUILD.gn
+++ b/webrtc/api/audio_codecs/L16/BUILD.gn
@@ -19,6 +19,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:pcm16b",
"../../../rtc_base:rtc_base_approved",
@@ -32,6 +33,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:pcm16b",
"../../../rtc_base:rtc_base_approved",
diff --git a/webrtc/api/audio_codecs/L16/audio_decoder_L16.h b/webrtc/api/audio_codecs/L16/audio_decoder_L16.h
index 478da16..39b389a 100644
--- a/webrtc/api/audio_codecs/L16/audio_decoder_L16.h
+++ b/webrtc/api/audio_codecs/L16/audio_decoder_L16.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/L16/audio_encoder_L16.h b/webrtc/api/audio_codecs/L16/audio_encoder_L16.h
index e2e4019..3ac0423 100644
--- a/webrtc/api/audio_codecs/L16/audio_encoder_L16.h
+++ b/webrtc/api/audio_codecs/L16/audio_encoder_L16.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_encoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/audio_decoder.h b/webrtc/api/audio_codecs/audio_decoder.h
index 0d15e6d..2d850fd 100644
--- a/webrtc/api/audio_codecs/audio_decoder.h
+++ b/webrtc/api/audio_codecs/audio_decoder.h
@@ -15,9 +15,9 @@
#include <vector>
#include "webrtc/api/array_view.h"
+#include "webrtc/api/optional.h"
#include "webrtc/rtc_base/buffer.h"
#include "webrtc/rtc_base/constructormagic.h"
-#include "webrtc/rtc_base/optional.h"
#include "webrtc/typedefs.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/audio_encoder.h b/webrtc/api/audio_codecs/audio_encoder.h
index 37a03d0..7406d7d 100644
--- a/webrtc/api/audio_codecs/audio_encoder.h
+++ b/webrtc/api/audio_codecs/audio_encoder.h
@@ -17,9 +17,9 @@
#include <vector>
#include "webrtc/api/array_view.h"
+#include "webrtc/api/optional.h"
#include "webrtc/rtc_base/buffer.h"
#include "webrtc/rtc_base/deprecation.h"
-#include "webrtc/rtc_base/optional.h"
#include "webrtc/typedefs.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/audio_format.h b/webrtc/api/audio_codecs/audio_format.h
index c442972..ab825bb 100644
--- a/webrtc/api/audio_codecs/audio_format.h
+++ b/webrtc/api/audio_codecs/audio_format.h
@@ -16,7 +16,7 @@
#include <string>
#include <utility>
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/g711/BUILD.gn b/webrtc/api/audio_codecs/g711/BUILD.gn
index fbf9248..aa86490 100644
--- a/webrtc/api/audio_codecs/g711/BUILD.gn
+++ b/webrtc/api/audio_codecs/g711/BUILD.gn
@@ -19,6 +19,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:g711",
"../../../rtc_base:rtc_base_approved",
@@ -32,6 +33,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:g711",
"../../../rtc_base:rtc_base_approved",
diff --git a/webrtc/api/audio_codecs/g711/audio_decoder_g711.h b/webrtc/api/audio_codecs/g711/audio_decoder_g711.h
index b9650c9..2824c32 100644
--- a/webrtc/api/audio_codecs/g711/audio_decoder_g711.h
+++ b/webrtc/api/audio_codecs/g711/audio_decoder_g711.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/g711/audio_encoder_g711.h b/webrtc/api/audio_codecs/g711/audio_encoder_g711.h
index a22650d..5750100 100644
--- a/webrtc/api/audio_codecs/g711/audio_encoder_g711.h
+++ b/webrtc/api/audio_codecs/g711/audio_encoder_g711.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_encoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/g722/BUILD.gn b/webrtc/api/audio_codecs/g722/BUILD.gn
index f3108e7..5af7e5c 100644
--- a/webrtc/api/audio_codecs/g722/BUILD.gn
+++ b/webrtc/api/audio_codecs/g722/BUILD.gn
@@ -26,6 +26,7 @@
deps = [
":audio_encoder_g722_config",
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:g722",
"../../../rtc_base:rtc_base_approved",
@@ -39,6 +40,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:g722",
"../../../rtc_base:rtc_base_approved",
diff --git a/webrtc/api/audio_codecs/g722/audio_decoder_g722.h b/webrtc/api/audio_codecs/g722/audio_decoder_g722.h
index 9b063c2..cf0dfa9 100644
--- a/webrtc/api/audio_codecs/g722/audio_decoder_g722.h
+++ b/webrtc/api/audio_codecs/g722/audio_decoder_g722.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/g722/audio_encoder_g722.h b/webrtc/api/audio_codecs/g722/audio_encoder_g722.h
index 458a6c5..37cb968 100644
--- a/webrtc/api/audio_codecs/g722/audio_encoder_g722.h
+++ b/webrtc/api/audio_codecs/g722/audio_encoder_g722.h
@@ -17,7 +17,7 @@
#include "webrtc/api/audio_codecs/audio_encoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
#include "webrtc/api/audio_codecs/g722/audio_encoder_g722_config.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/ilbc/BUILD.gn b/webrtc/api/audio_codecs/ilbc/BUILD.gn
index ab9681a..0f5f80d 100644
--- a/webrtc/api/audio_codecs/ilbc/BUILD.gn
+++ b/webrtc/api/audio_codecs/ilbc/BUILD.gn
@@ -26,6 +26,7 @@
deps = [
":audio_encoder_ilbc_config",
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:ilbc",
"../../../rtc_base:rtc_base_approved",
@@ -39,6 +40,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:ilbc",
"../../../rtc_base:rtc_base_approved",
diff --git a/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h b/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h
index 65a67e2..343308d 100644
--- a/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h
+++ b/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h
index 6f7a9f6..a30d109 100644
--- a/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h
+++ b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h
@@ -17,7 +17,7 @@
#include "webrtc/api/audio_codecs/audio_encoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
#include "webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc_config.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/isac/BUILD.gn b/webrtc/api/audio_codecs/isac/BUILD.gn
index 3340e28..b22eb95 100644
--- a/webrtc/api/audio_codecs/isac/BUILD.gn
+++ b/webrtc/api/audio_codecs/isac/BUILD.gn
@@ -19,6 +19,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:isac_fix",
"../../../rtc_base:rtc_base_approved",
@@ -32,6 +33,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:isac_fix",
"../../../rtc_base:rtc_base_approved",
@@ -45,6 +47,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:isac",
"../../../rtc_base:rtc_base_approved",
@@ -58,6 +61,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:isac",
"../../../rtc_base:rtc_base_approved",
diff --git a/webrtc/api/audio_codecs/isac/audio_decoder_isac_fix.h b/webrtc/api/audio_codecs/isac/audio_decoder_isac_fix.h
index 71e7cc4..7662002 100644
--- a/webrtc/api/audio_codecs/isac/audio_decoder_isac_fix.h
+++ b/webrtc/api/audio_codecs/isac/audio_decoder_isac_fix.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h b/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h
index c0dc880..f0232e9 100644
--- a/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h
+++ b/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/isac/audio_encoder_isac_fix.h b/webrtc/api/audio_codecs/isac/audio_encoder_isac_fix.h
index 5f0e8fe..ce4a1d0 100644
--- a/webrtc/api/audio_codecs/isac/audio_encoder_isac_fix.h
+++ b/webrtc/api/audio_codecs/isac/audio_encoder_isac_fix.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_encoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h b/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h
index 35bc94b..244e59b 100644
--- a/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h
+++ b/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_encoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/opus/BUILD.gn b/webrtc/api/audio_codecs/opus/BUILD.gn
index 658d151..faa5956 100644
--- a/webrtc/api/audio_codecs/opus/BUILD.gn
+++ b/webrtc/api/audio_codecs/opus/BUILD.gn
@@ -18,6 +18,7 @@
"audio_encoder_opus_config.h",
]
deps = [
+ "../..:optional",
"../../../rtc_base:rtc_base_approved",
]
defines = []
@@ -51,6 +52,7 @@
]
deps = [
"..:audio_codecs_api",
+ "../..:optional",
"../../..:webrtc_common",
"../../../modules/audio_coding:webrtc_opus",
"../../../rtc_base:rtc_base_approved",
diff --git a/webrtc/api/audio_codecs/opus/audio_decoder_opus.h b/webrtc/api/audio_codecs/opus/audio_decoder_opus.h
index 6a9d6da..272503d 100644
--- a/webrtc/api/audio_codecs/opus/audio_decoder_opus.h
+++ b/webrtc/api/audio_codecs/opus/audio_decoder_opus.h
@@ -16,7 +16,7 @@
#include "webrtc/api/audio_codecs/audio_decoder.h"
#include "webrtc/api/audio_codecs/audio_format.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/audio_codecs/opus/audio_encoder_opus_config.h b/webrtc/api/audio_codecs/opus/audio_encoder_opus_config.h
index c3fe17b..d07f784 100644
--- a/webrtc/api/audio_codecs/opus/audio_encoder_opus_config.h
+++ b/webrtc/api/audio_codecs/opus/audio_encoder_opus_config.h
@@ -15,7 +15,7 @@
#include <vector>
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/mediaconstraintsinterface.h b/webrtc/api/mediaconstraintsinterface.h
index 72fdd97..eb89b3e 100644
--- a/webrtc/api/mediaconstraintsinterface.h
+++ b/webrtc/api/mediaconstraintsinterface.h
@@ -23,8 +23,8 @@
#include <string>
#include <vector>
+#include "webrtc/api/optional.h"
#include "webrtc/api/peerconnectioninterface.h"
-#include "webrtc/rtc_base/optional.h"
namespace webrtc {
diff --git a/webrtc/api/mediastreaminterface.h b/webrtc/api/mediastreaminterface.h
index aaf5b62..5d65447 100644
--- a/webrtc/api/mediastreaminterface.h
+++ b/webrtc/api/mediastreaminterface.h
@@ -22,8 +22,8 @@
#include <string>
#include <vector>
+#include "webrtc/api/optional.h"
#include "webrtc/api/video/video_frame.h"
-#include "webrtc/rtc_base/optional.h"
// TODO(zhihuang): Remove unrelated headers once downstream applications stop
// relying on them; they were previously transitively included by
// mediachannel.h, which is no longer a dependency of this file.
diff --git a/webrtc/api/optional.cc b/webrtc/api/optional.cc
new file mode 100644
index 0000000..ec86966
--- /dev/null
+++ b/webrtc/api/optional.cc
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016 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.
+ */
+
+#include "webrtc/api/optional.h"
+
+namespace rtc {
+namespace optional_internal {
+
+#if RTC_HAS_ASAN
+
+void* FunctionThatDoesNothingImpl(void* x) {
+ return x;
+}
+
+#endif
+
+} // namespace optional_internal
+} // namespace rtc
diff --git a/webrtc/api/optional.h b/webrtc/api/optional.h
new file mode 100644
index 0000000..de1ec76
--- /dev/null
+++ b/webrtc/api/optional.h
@@ -0,0 +1,407 @@
+/*
+ * 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_API_OPTIONAL_H_
+#define WEBRTC_API_OPTIONAL_H_
+
+#include <algorithm>
+#include <memory>
+#include <utility>
+
+#ifdef UNIT_TEST
+#include <iomanip>
+#include <ostream>
+#endif // UNIT_TEST
+
+#include "webrtc/api/array_view.h"
+#include "webrtc/rtc_base/checks.h"
+#include "webrtc/rtc_base/sanitizer.h"
+
+namespace rtc {
+
+namespace optional_internal {
+
+#if RTC_HAS_ASAN
+
+// This is a non-inlined function. The optimizer can't see inside it. It
+// prevents the compiler from generating optimized code that reads value_ even
+// if it is unset. Although safe, this causes memory sanitizers to complain.
+void* FunctionThatDoesNothingImpl(void*);
+
+template <typename T>
+inline T* FunctionThatDoesNothing(T* x) {
+ return reinterpret_cast<T*>(
+ FunctionThatDoesNothingImpl(reinterpret_cast<void*>(x)));
+}
+
+#else
+
+template <typename T>
+inline T* FunctionThatDoesNothing(T* x) {
+ return x;
+}
+
+#endif
+
+} // namespace optional_internal
+
+// Simple std::optional-wannabe. It either contains a T or not.
+//
+// A moved-from Optional<T> may only be destroyed, and assigned to if T allows
+// being assigned to after having been moved from. Specifically, you may not
+// assume that it just doesn't contain a value anymore.
+//
+// Examples of good places to use Optional:
+//
+// - As a class or struct member, when the member doesn't always have a value:
+// struct Prisoner {
+// std::string name;
+// Optional<int> cell_number; // Empty if not currently incarcerated.
+// };
+//
+// - As a return value for functions that may fail to return a value on all
+// allowed inputs. For example, a function that searches an array might
+// return an Optional<size_t> (the index where it found the element, or
+// nothing if it didn't find it); and a function that parses numbers might
+// return Optional<double> (the parsed number, or nothing if parsing failed).
+//
+// Examples of bad places to use Optional:
+//
+// - As a return value for functions that may fail because of disallowed
+// inputs. For example, a string length function should not return
+// Optional<size_t> so that it can return nothing in case the caller passed
+// it a null pointer; the function should probably use RTC_[D]CHECK instead,
+// and return plain size_t.
+//
+// - As a return value for functions that may fail to return a value on all
+// allowed inputs, but need to tell the caller what went wrong. Returning
+// Optional<double> when parsing a single number as in the example above
+// might make sense, but any larger parse job is probably going to need to
+// tell the caller what the problem was, not just that there was one.
+//
+// - As a non-mutable function argument. When you want to pass a value of a
+// type T that can fail to be there, const T* is almost always both fastest
+// and cleanest. (If you're *sure* that the the caller will always already
+// have an Optional<T>, const Optional<T>& is slightly faster than const T*,
+// but this is a micro-optimization. In general, stick to const T*.)
+//
+// 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 Optional final {
+ public:
+ // Construct an empty Optional.
+ Optional() : has_value_(false), empty_('\0') { PoisonValue(); }
+
+ // Construct an Optional that contains a value.
+ explicit Optional(const T& value) : has_value_(true) {
+ new (&value_) T(value);
+ }
+ explicit Optional(T&& value) : has_value_(true) {
+ new (&value_) T(std::move(value));
+ }
+
+ // Copy constructor: copies the value from m if it has one.
+ Optional(const Optional& m) : has_value_(m.has_value_) {
+ if (has_value_)
+ new (&value_) T(m.value_);
+ else
+ PoisonValue();
+ }
+
+ // Move constructor: if m has a value, moves the value from m, leaving m
+ // still in a state where it has a value, but a moved-from one (the
+ // properties of which depends on T; the only general guarantee is that we
+ // can destroy m).
+ Optional(Optional&& m) : has_value_(m.has_value_) {
+ if (has_value_)
+ new (&value_) T(std::move(m.value_));
+ else
+ PoisonValue();
+ }
+
+ ~Optional() {
+ if (has_value_)
+ value_.~T();
+ else
+ UnpoisonValue();
+ }
+
+ // Copy assignment. Uses T's copy assignment if both sides have a value, T's
+ // copy constructor if only the right-hand side has a value.
+ Optional& operator=(const Optional& m) {
+ if (m.has_value_) {
+ if (has_value_) {
+ value_ = m.value_; // T's copy assignment.
+ } else {
+ UnpoisonValue();
+ new (&value_) T(m.value_); // T's copy constructor.
+ has_value_ = true;
+ }
+ } else {
+ reset();
+ }
+ return *this;
+ }
+
+ // Move assignment. Uses T's move assignment if both sides have a value, T's
+ // move constructor if only the right-hand side has a value. The state of m
+ // after it's been moved from is as for the move constructor.
+ Optional& operator=(Optional&& m) {
+ if (m.has_value_) {
+ if (has_value_) {
+ value_ = std::move(m.value_); // T's move assignment.
+ } else {
+ UnpoisonValue();
+ new (&value_) T(std::move(m.value_)); // T's move constructor.
+ has_value_ = true;
+ }
+ } else {
+ reset();
+ }
+ return *this;
+ }
+
+ // Swap the values if both m1 and m2 have values; move the value if only one
+ // of them has one.
+ friend void swap(Optional& m1, Optional& m2) {
+ if (m1.has_value_) {
+ if (m2.has_value_) {
+ // Both have values: swap.
+ using std::swap;
+ swap(m1.value_, m2.value_);
+ } else {
+ // Only m1 has a value: move it to m2.
+ m2.UnpoisonValue();
+ new (&m2.value_) T(std::move(m1.value_));
+ m1.value_.~T(); // Destroy the moved-from value.
+ m1.has_value_ = false;
+ m2.has_value_ = true;
+ m1.PoisonValue();
+ }
+ } else if (m2.has_value_) {
+ // Only m2 has a value: move it to m1.
+ m1.UnpoisonValue();
+ new (&m1.value_) T(std::move(m2.value_));
+ m2.value_.~T(); // Destroy the moved-from value.
+ m1.has_value_ = true;
+ m2.has_value_ = false;
+ m2.PoisonValue();
+ }
+ }
+
+ // Destroy any contained value. Has no effect if we have no value.
+ void reset() {
+ if (!has_value_)
+ return;
+ value_.~T();
+ has_value_ = false;
+ PoisonValue();
+ }
+
+ template <class... Args>
+ void emplace(Args&&... args) {
+ if (has_value_)
+ value_.~T();
+ else
+ UnpoisonValue();
+ new (&value_) T(std::forward<Args>(args)...);
+ has_value_ = true;
+ }
+
+ // Conversion to bool to test if we have a value.
+ explicit operator bool() const { return has_value_; }
+ bool has_value() 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_;
+ }
+ const T& value() const {
+ RTC_DCHECK(has_value_);
+ return value_;
+ }
+ T& value() {
+ RTC_DCHECK(has_value_);
+ return value_;
+ }
+
+ // Dereference with a default value in case we don't have a value.
+ const T& value_or(const T& default_val) const {
+ // The no-op call prevents the compiler from generating optimized code that
+ // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is
+ // not completely inlined; see its declaration.).
+ return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_)
+ : default_val;
+ }
+
+ // Dereference and move value.
+ T MoveValue() {
+ RTC_DCHECK(has_value_);
+ return std::move(value_);
+ }
+
+ // Equality tests. Two Optionals are equal if they contain equivalent values,
+ // or if they're both empty.
+ friend bool operator==(const Optional& m1, const Optional& m2) {
+ return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_
+ : m1.has_value_ == m2.has_value_;
+ }
+ friend bool operator==(const Optional& opt, const T& value) {
+ return opt.has_value_ && opt.value_ == value;
+ }
+ friend bool operator==(const T& value, const Optional& opt) {
+ return opt.has_value_ && value == opt.value_;
+ }
+
+ friend bool operator!=(const Optional& m1, const Optional& m2) {
+ return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
+ : m1.has_value_ != m2.has_value_;
+ }
+ friend bool operator!=(const Optional& opt, const T& value) {
+ return !opt.has_value_ || opt.value_ != value;
+ }
+ friend bool operator!=(const T& value, const Optional& opt) {
+ return !opt.has_value_ || value != opt.value_;
+ }
+
+ private:
+ // Tell sanitizers that value_ shouldn't be touched.
+ void PoisonValue() {
+ rtc::AsanPoison(rtc::MakeArrayView(&value_, 1));
+ rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1));
+ }
+
+ // Tell sanitizers that value_ is OK to touch again.
+ void UnpoisonValue() { rtc::AsanUnpoison(rtc::MakeArrayView(&value_, 1)); }
+
+ bool has_value_; // True iff value_ contains a live value.
+ union {
+ // empty_ exists only to make it possible to initialize the union, even when
+ // it doesn't contain any data. If the union goes uninitialized, it may
+ // trigger compiler warnings.
+ char empty_;
+ // By placing value_ in a union, we get to manage its construction and
+ // destruction manually: the Optional constructors won't automatically
+ // construct it, and the Optional destructor won't automatically destroy
+ // it. Basically, this just allocates a properly sized and aligned block of
+ // memory in which we can manually put a T with placement new.
+ T value_;
+ };
+};
+
+#ifdef UNIT_TEST
+namespace optional_internal {
+
+// Checks if there's a valid PrintTo(const T&, std::ostream*) call for T.
+template <typename T>
+struct HasPrintTo {
+ private:
+ struct No {};
+
+ template <typename T2>
+ static auto Test(const T2& obj)
+ -> decltype(PrintTo(obj, std::declval<std::ostream*>()));
+
+ template <typename>
+ static No Test(...);
+
+ public:
+ static constexpr bool value =
+ !std::is_same<decltype(Test<T>(std::declval<const T&>())), No>::value;
+};
+
+// Checks if there's a valid operator<<(std::ostream&, const T&) call for T.
+template <typename T>
+struct HasOstreamOperator {
+ private:
+ struct No {};
+
+ template <typename T2>
+ static auto Test(const T2& obj)
+ -> decltype(std::declval<std::ostream&>() << obj);
+
+ template <typename>
+ static No Test(...);
+
+ public:
+ static constexpr bool value =
+ !std::is_same<decltype(Test<T>(std::declval<const T&>())), No>::value;
+};
+
+// Prefer using PrintTo to print the object.
+template <typename T>
+typename std::enable_if<HasPrintTo<T>::value, void>::type OptionalPrintToHelper(
+ const T& value,
+ std::ostream* os) {
+ PrintTo(value, os);
+}
+
+// Fall back to operator<<(std::ostream&, ...) if it exists.
+template <typename T>
+typename std::enable_if<HasOstreamOperator<T>::value && !HasPrintTo<T>::value,
+ void>::type
+OptionalPrintToHelper(const T& value, std::ostream* os) {
+ *os << value;
+}
+
+inline void OptionalPrintObjectBytes(const unsigned char* bytes,
+ size_t size,
+ std::ostream* os) {
+ *os << "<optional with " << size << "-byte object [";
+ for (size_t i = 0; i != size; ++i) {
+ *os << (i == 0 ? "" : ((i & 1) ? "-" : " "));
+ *os << std::hex << std::setw(2) << std::setfill('0')
+ << static_cast<int>(bytes[i]);
+ }
+ *os << "]>";
+}
+
+// As a final back-up, just print the contents of the objcets byte-wise.
+template <typename T>
+typename std::enable_if<!HasOstreamOperator<T>::value && !HasPrintTo<T>::value,
+ void>::type
+OptionalPrintToHelper(const T& value, std::ostream* os) {
+ OptionalPrintObjectBytes(reinterpret_cast<const unsigned char*>(&value),
+ sizeof(value), os);
+}
+
+} // namespace optional_internal
+
+// PrintTo is used by gtest to print out the results of tests. We want to ensure
+// the object contained in an Optional can be printed out if it's set, while
+// avoiding touching the object's storage if it is undefined.
+template <typename T>
+void PrintTo(const rtc::Optional<T>& opt, std::ostream* os) {
+ if (opt) {
+ optional_internal::OptionalPrintToHelper(*opt, os);
+ } else {
+ *os << "<empty optional>";
+ }
+}
+
+#endif // UNIT_TEST
+
+} // namespace rtc
+
+#endif // WEBRTC_API_OPTIONAL_H_
diff --git a/webrtc/api/optional_unittest.cc b/webrtc/api/optional_unittest.cc
new file mode 100644
index 0000000..f8f9cf3
--- /dev/null
+++ b/webrtc/api/optional_unittest.cc
@@ -0,0 +1,829 @@
+/*
+ * 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.
+ */
+
+#include <memory>
+#include <sstream>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "webrtc/api/optional.h"
+#include "webrtc/rtc_base/gunit.h"
+
+namespace rtc {
+
+namespace {
+
+struct MyUnprintableType {
+ int value;
+};
+
+struct MyPrintableType {
+ int value;
+};
+
+struct MyOstreamPrintableType {
+ int value;
+};
+
+void PrintTo(const MyPrintableType& mpt, std::ostream* os) {
+ *os << "The value is " << mpt.value;
+}
+
+std::ostream& operator<<(std::ostream& os, const MyPrintableType& mpt) {
+ os << mpt.value;
+ return os;
+}
+
+std::ostream& operator<<(std::ostream& os, const MyOstreamPrintableType& mpt) {
+ os << mpt.value;
+ return os;
+}
+
+// Class whose instances logs various method calls (constructor, destructor,
+// etc.). Each instance has a unique ID (a simple global sequence number) and
+// an origin ID. When a copy is made, the new object gets a fresh ID but copies
+// the origin ID from the original. When a new Logger is created from scratch,
+// it gets a fresh ID, and the origin ID is the same as the ID (default
+// constructor) or given as an argument (explicit constructor).
+class Logger {
+ public:
+ Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); }
+ explicit Logger(int origin) : id_(g_next_id++), origin_(origin) {
+ Log("explicit constructor");
+ }
+ Logger(int origin, const Logger& pass_by_ref, Logger pass_by_value)
+ : id_(g_next_id++), origin_(origin) {
+ Log("multi parameter constructor");
+ }
+ Logger(const Logger& other) : id_(g_next_id++), origin_(other.origin_) {
+ LogFrom("copy constructor", other);
+ }
+ Logger(Logger&& other) : id_(g_next_id++), origin_(other.origin_) {
+ LogFrom("move constructor", other);
+ }
+ ~Logger() { Log("destructor"); }
+ Logger& operator=(const Logger& other) {
+ origin_ = other.origin_;
+ LogFrom("operator= copy", other);
+ return *this;
+ }
+ Logger& operator=(Logger&& other) {
+ origin_ = other.origin_;
+ LogFrom("operator= move", other);
+ return *this;
+ }
+ friend void swap(Logger& a, Logger& b) {
+ using std::swap;
+ swap(a.origin_, b.origin_);
+ Log2("swap", a, b);
+ }
+ friend bool operator==(const Logger& a, const Logger& b) {
+ Log2("operator==", a, b);
+ return a.origin_ == b.origin_;
+ }
+ friend bool operator!=(const Logger& a, const Logger& b) {
+ Log2("operator!=", a, b);
+ return a.origin_ != b.origin_;
+ }
+ void Foo() { Log("Foo()"); }
+ void Foo() const { Log("Foo() const"); }
+ static std::unique_ptr<std::vector<std::string>> Setup() {
+ std::unique_ptr<std::vector<std::string>> s(new std::vector<std::string>);
+ g_log = s.get();
+ g_next_id = 0;
+ return s;
+ }
+
+ private:
+ int id_;
+ int origin_;
+ static std::vector<std::string>* g_log;
+ static int g_next_id;
+ void Log(const char* msg) const {
+ std::ostringstream oss;
+ oss << id_ << ':' << origin_ << ". " << msg;
+ g_log->push_back(oss.str());
+ }
+ void LogFrom(const char* msg, const Logger& other) const {
+ std::ostringstream oss;
+ oss << id_ << ':' << origin_ << ". " << msg << " (from " << other.id_ << ':'
+ << other.origin_ << ")";
+ g_log->push_back(oss.str());
+ }
+ static void Log2(const char* msg, const Logger& a, const Logger& b) {
+ std::ostringstream oss;
+ oss << msg << ' ' << a.id_ << ':' << a.origin_ << ", " << b.id_ << ':'
+ << b.origin_;
+ g_log->push_back(oss.str());
+ }
+};
+
+std::vector<std::string>* Logger::g_log = nullptr;
+int Logger::g_next_id = 0;
+
+// Append all the other args to the vector pointed to by the first arg.
+template <typename T>
+void VectorAppend(std::vector<T>* v) {}
+template <typename T, typename... Ts>
+void VectorAppend(std::vector<T>* v, const T& e, Ts... es) {
+ v->push_back(e);
+ VectorAppend(v, es...);
+}
+
+// Create a vector of strings. Because we're not allowed to use
+// std::initializer_list.
+template <typename... Ts>
+std::vector<std::string> V(Ts... es) {
+ std::vector<std::string> strings;
+ VectorAppend(&strings, static_cast<std::string>(es)...);
+ return strings;
+}
+
+} // namespace
+
+TEST(OptionalTest, TestConstructDefault) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ EXPECT_FALSE(x);
+ EXPECT_FALSE(x.has_value());
+ }
+ EXPECT_EQ(V(), *log);
+}
+
+TEST(OptionalTest, TestConstructCopyEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ EXPECT_FALSE(x);
+ EXPECT_FALSE(x.has_value());
+ auto y = x;
+ EXPECT_FALSE(y);
+ EXPECT_FALSE(y.has_value());
+ }
+ EXPECT_EQ(V(), *log);
+}
+
+TEST(OptionalTest, TestConstructCopyFull) {
+ auto log = Logger::Setup();
+ {
+ Logger a;
+ Optional<Logger> x(a);
+ EXPECT_TRUE(x);
+ EXPECT_TRUE(x.has_value());
+ log->push_back("---");
+ auto y = x;
+ EXPECT_TRUE(y);
+ EXPECT_TRUE(y.has_value());
+ log->push_back("---");
+ }
+ EXPECT_EQ(V("0:0. default constructor", "1:0. copy constructor (from 0:0)",
+ "---", "2:0. copy constructor (from 1:0)", "---",
+ "2:0. destructor", "1:0. destructor", "0:0. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestConstructMoveEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ EXPECT_FALSE(x);
+ EXPECT_FALSE(x.has_value());
+ auto y = std::move(x);
+ EXPECT_FALSE(y);
+ EXPECT_FALSE(y.has_value());
+ }
+ EXPECT_EQ(V(), *log);
+}
+
+TEST(OptionalTest, TestConstructMoveFull) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ EXPECT_TRUE(x);
+ EXPECT_TRUE(x.has_value());
+ log->push_back("---");
+ auto y = std::move(x);
+ EXPECT_TRUE(x);
+ EXPECT_TRUE(x.has_value());
+ EXPECT_TRUE(y);
+ EXPECT_TRUE(y.has_value());
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "---", "2:17. move constructor (from 1:17)", "---",
+ "2:17. destructor", "1:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestCopyAssignToEmptyFromEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x, y;
+ x = y;
+ }
+ EXPECT_EQ(V(), *log);
+}
+
+TEST(OptionalTest, TestCopyAssignToFullFromEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Optional<Logger> y;
+ log->push_back("---");
+ x = y;
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "---", "1:17. destructor", "---"),
+ *log);
+}
+
+TEST(OptionalTest, TestCopyAssignToEmptyFromFull) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Optional<Logger> y(Logger(17));
+ log->push_back("---");
+ x = y;
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "---", "2:17. copy constructor (from 1:17)", "---",
+ "1:17. destructor", "2:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestCopyAssignToFullFromFull) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Optional<Logger> y(Logger(42));
+ log->push_back("---");
+ x = y;
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "2:42. explicit constructor",
+ "3:42. move constructor (from 2:42)", "2:42. destructor", "---",
+ "1:42. operator= copy (from 3:42)", "---", "3:42. destructor",
+ "1:42. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestCopyAssignToEmptyFromT) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Logger y(17);
+ log->push_back("---");
+ x = Optional<Logger>(y);
+ log->push_back("---");
+ }
+ EXPECT_EQ(V("0:17. explicit constructor", "---",
+ "1:17. copy constructor (from 0:17)",
+ "2:17. move constructor (from 1:17)", "1:17. destructor", "---",
+ "0:17. destructor", "2:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestCopyAssignToFullFromT) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Logger y(42);
+ log->push_back("---");
+ x = Optional<Logger>(y);
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "2:42. explicit constructor", "---",
+ "3:42. copy constructor (from 2:42)",
+ "1:42. operator= move (from 3:42)", "3:42. destructor", "---",
+ "2:42. destructor", "1:42. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestMoveAssignToEmptyFromEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x, y;
+ x = std::move(y);
+ }
+ EXPECT_EQ(V(), *log);
+}
+
+TEST(OptionalTest, TestMoveAssignToFullFromEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Optional<Logger> y;
+ log->push_back("---");
+ x = std::move(y);
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "---", "1:17. destructor", "---"),
+ *log);
+}
+
+TEST(OptionalTest, TestMoveAssignToEmptyFromFull) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Optional<Logger> y(Logger(17));
+ log->push_back("---");
+ x = std::move(y);
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "---", "2:17. move constructor (from 1:17)", "---",
+ "1:17. destructor", "2:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestMoveAssignToFullFromFull) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Optional<Logger> y(Logger(42));
+ log->push_back("---");
+ x = std::move(y);
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "2:42. explicit constructor",
+ "3:42. move constructor (from 2:42)", "2:42. destructor", "---",
+ "1:42. operator= move (from 3:42)", "---", "3:42. destructor",
+ "1:42. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestMoveAssignToEmptyFromT) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Logger y(17);
+ log->push_back("---");
+ x = Optional<Logger>(std::move(y));
+ log->push_back("---");
+ }
+ EXPECT_EQ(V("0:17. explicit constructor", "---",
+ "1:17. move constructor (from 0:17)",
+ "2:17. move constructor (from 1:17)", "1:17. destructor", "---",
+ "0:17. destructor", "2:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestMoveAssignToFullFromT) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Logger y(42);
+ log->push_back("---");
+ x = Optional<Logger>(std::move(y));
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "2:42. explicit constructor", "---",
+ "3:42. move constructor (from 2:42)",
+ "1:42. operator= move (from 3:42)", "3:42. destructor", "---",
+ "2:42. destructor", "1:42. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestResetEmpty) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ x.reset();
+ }
+ EXPECT_EQ(V(), *log);
+}
+
+TEST(OptionalTest, TestResetFull) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ log->push_back("---");
+ x.reset();
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
+ "0:17. destructor", "---", "1:17. destructor", "---"),
+ *log);
+}
+
+TEST(OptionalTest, TestEmplaceEmptyWithExplicit) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ log->push_back("---");
+ x.emplace(42);
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("---",
+ "0:42. explicit constructor",
+ "---",
+ "0:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceEmptyWithMultipleParameters) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Logger ref(21);
+ Logger value(35);
+ log->push_back("---");
+ x.emplace(42, ref, std::move(value));
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:21. explicit constructor",
+ "1:35. explicit constructor",
+ "---",
+ "2:35. move constructor (from 1:35)",
+ "3:42. multi parameter constructor",
+ "2:35. destructor",
+ "---",
+ "1:35. destructor",
+ "0:21. destructor",
+ "3:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceEmptyWithCopy) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Logger y(42);
+ log->push_back("---");
+ x.emplace(y);
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:42. explicit constructor",
+ "---",
+ "1:42. copy constructor (from 0:42)",
+ "---",
+ "0:42. destructor",
+ "1:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceEmptyWithMove) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x;
+ Logger y(42);
+ log->push_back("---");
+ x.emplace(std::move(y));
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:42. explicit constructor",
+ "---",
+ "1:42. move constructor (from 0:42)",
+ "---",
+ "0:42. destructor",
+ "1:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceFullWithExplicit) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ log->push_back("---");
+ x.emplace(42);
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(
+ V("0:17. explicit constructor",
+ "1:17. move constructor (from 0:17)",
+ "0:17. destructor",
+ "---",
+ "1:17. destructor",
+ "2:42. explicit constructor",
+ "---",
+ "2:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceFullWithMultipleParameters) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Logger ref(21);
+ Logger value(35);
+ log->push_back("---");
+ x.emplace(42, ref, std::move(value));
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:17. explicit constructor",
+ "1:17. move constructor (from 0:17)",
+ "0:17. destructor",
+ "2:21. explicit constructor",
+ "3:35. explicit constructor",
+ "---",
+ "1:17. destructor",
+ "4:35. move constructor (from 3:35)",
+ "5:42. multi parameter constructor",
+ "4:35. destructor",
+ "---",
+ "3:35. destructor",
+ "2:21. destructor",
+ "5:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceFullWithCopy) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Logger y(42);
+ log->push_back("---");
+ x.emplace(y);
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:17. explicit constructor",
+ "1:17. move constructor (from 0:17)",
+ "0:17. destructor",
+ "2:42. explicit constructor",
+ "---",
+ "1:17. destructor",
+ "3:42. copy constructor (from 2:42)",
+ "---",
+ "2:42. destructor",
+ "3:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestEmplaceFullWithMove) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(17));
+ Logger y(42);
+ log->push_back("---");
+ x.emplace(std::move(y));
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:17. explicit constructor",
+ "1:17. move constructor (from 0:17)",
+ "0:17. destructor",
+ "2:42. explicit constructor",
+ "---",
+ "1:17. destructor",
+ "3:42. move constructor (from 2:42)",
+ "---",
+ "2:42. destructor",
+ "3:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestDereference) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(42));
+ const auto& y = x;
+ log->push_back("---");
+ x->Foo();
+ y->Foo();
+ std::move(x)->Foo();
+ std::move(y)->Foo();
+ log->push_back("---");
+ (*x).Foo();
+ (*y).Foo();
+ (*std::move(x)).Foo();
+ (*std::move(y)).Foo();
+ log->push_back("---");
+ x.value().Foo();
+ y.value().Foo();
+ std::move(x).value().Foo();
+ std::move(y).value().Foo();
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:42. explicit constructor",
+ "1:42. move constructor (from 0:42)",
+ "0:42. destructor",
+ "---",
+ "1:42. Foo()",
+ "1:42. Foo() const",
+ "1:42. Foo()",
+ "1:42. Foo() const",
+ "---",
+ "1:42. Foo()",
+ "1:42. Foo() const",
+ "1:42. Foo()",
+ "1:42. Foo() const",
+ "---",
+ "1:42. Foo()",
+ "1:42. Foo() const",
+ "1:42. Foo()",
+ "1:42. Foo() const",
+ "---",
+ "1:42. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestDereferenceWithDefault) {
+ auto log = Logger::Setup();
+ {
+ const Logger a(17), b(42);
+ Optional<Logger> x(a);
+ Optional<Logger> y;
+ log->push_back("-1-");
+ EXPECT_EQ(a, x.value_or(Logger(42)));
+ log->push_back("-2-");
+ EXPECT_EQ(b, y.value_or(Logger(42)));
+ log->push_back("-3-");
+ EXPECT_EQ(a, Optional<Logger>(Logger(17)).value_or(b));
+ log->push_back("-4-");
+ EXPECT_EQ(b, Optional<Logger>().value_or(b));
+ log->push_back("-5-");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:42. explicit constructor",
+ "2:17. copy constructor (from 0:17)", "-1-",
+ "3:42. explicit constructor", "operator== 0:17, 2:17",
+ "3:42. destructor", "-2-", "4:42. explicit constructor",
+ "operator== 1:42, 4:42", "4:42. destructor", "-3-",
+ "5:17. explicit constructor", "6:17. move constructor (from 5:17)",
+ "operator== 0:17, 6:17", "6:17. destructor", "5:17. destructor", "-4-",
+ "operator== 1:42, 1:42", "-5-", "2:17. destructor", "1:42. destructor",
+ "0:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestEquality) {
+ auto log = Logger::Setup();
+ {
+ Logger a(17), b(42);
+ Optional<Logger> ma1(a), ma2(a), mb(b), me1, me2;
+ log->push_back("---");
+ EXPECT_EQ(ma1, ma1);
+ EXPECT_EQ(ma1, ma2);
+ EXPECT_NE(ma1, mb);
+ EXPECT_NE(ma1, me1);
+ EXPECT_EQ(me1, me1);
+ EXPECT_EQ(me1, me2);
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:17. explicit constructor", "1:42. explicit constructor",
+ "2:17. copy constructor (from 0:17)",
+ "3:17. copy constructor (from 0:17)",
+ "4:42. copy constructor (from 1:42)", "---", "operator== 2:17, 2:17",
+ "operator== 2:17, 3:17", "operator!= 2:17, 4:42", "---",
+ "4:42. destructor", "3:17. destructor", "2:17. destructor",
+ "1:42. destructor", "0:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestEqualityWithObject) {
+ auto log = Logger::Setup();
+ {
+ Logger a(17), b(42);
+ Optional<Logger> ma(a), me;
+ // Using operator== and operator!= explicetly instead of EXPECT_EQ/EXPECT_NE
+ // macros because those operators are under test.
+ log->push_back("---");
+
+ EXPECT_TRUE(ma == a);
+ EXPECT_TRUE(a == ma);
+ EXPECT_FALSE(ma == b);
+ EXPECT_FALSE(b == ma);
+ EXPECT_FALSE(me == a);
+ EXPECT_FALSE(a == me);
+
+ EXPECT_FALSE(ma != a);
+ EXPECT_FALSE(a != ma);
+ EXPECT_TRUE(ma != b);
+ EXPECT_TRUE(b != ma);
+ EXPECT_TRUE(me != a);
+ EXPECT_TRUE(a != me);
+
+ log->push_back("---");
+ }
+ // clang-format off
+ EXPECT_EQ(V("0:17. explicit constructor",
+ "1:42. explicit constructor",
+ "2:17. copy constructor (from 0:17)",
+ "---",
+ "operator== 2:17, 0:17",
+ "operator== 0:17, 2:17",
+ "operator== 2:17, 1:42",
+ "operator== 1:42, 2:17",
+ // No operator should be called when comparing to empty.
+ "operator!= 2:17, 0:17",
+ "operator!= 0:17, 2:17",
+ "operator!= 2:17, 1:42",
+ "operator!= 1:42, 2:17",
+ // No operator should be called when comparing to empty.
+ "---",
+ "2:17. destructor",
+ "1:42. destructor",
+ "0:17. destructor"),
+ *log);
+ // clang-format on
+}
+
+TEST(OptionalTest, TestSwap) {
+ auto log = Logger::Setup();
+ {
+ Logger a(17), b(42);
+ Optional<Logger> x1(a), x2(b), y1(a), y2, z1, z2;
+ log->push_back("---");
+ swap(x1, x2); // Swap full <-> full.
+ swap(y1, y2); // Swap full <-> empty.
+ swap(z1, z2); // Swap empty <-> empty.
+ log->push_back("---");
+ }
+ EXPECT_EQ(V("0:17. explicit constructor", "1:42. explicit constructor",
+ "2:17. copy constructor (from 0:17)",
+ "3:42. copy constructor (from 1:42)",
+ "4:17. copy constructor (from 0:17)", "---", "swap 2:42, 3:17",
+ "5:17. move constructor (from 4:17)", "4:17. destructor", "---",
+ "5:17. destructor", "3:17. destructor", "2:42. destructor",
+ "1:42. destructor", "0:17. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestMoveValue) {
+ auto log = Logger::Setup();
+ {
+ Optional<Logger> x(Logger(42));
+ log->push_back("---");
+ Logger moved = x.MoveValue();
+ log->push_back("---");
+ }
+ EXPECT_EQ(
+ V("0:42. explicit constructor", "1:42. move constructor (from 0:42)",
+ "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---",
+ "2:42. destructor", "1:42. destructor"),
+ *log);
+}
+
+TEST(OptionalTest, TestPrintTo) {
+ constexpr char kEmptyOptionalMessage[] = "<empty optional>";
+ const Optional<MyUnprintableType> empty_unprintable;
+ const Optional<MyPrintableType> empty_printable;
+ const Optional<MyOstreamPrintableType> empty_ostream_printable;
+ EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable));
+ EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable));
+ EXPECT_EQ(kEmptyOptionalMessage,
+ ::testing::PrintToString(empty_ostream_printable));
+ EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>({1})));
+ EXPECT_NE("1", ::testing::PrintToString(Optional<MyPrintableType>({1})));
+ EXPECT_EQ("The value is 1",
+ ::testing::PrintToString(Optional<MyPrintableType>({1})));
+ EXPECT_EQ("1",
+ ::testing::PrintToString(Optional<MyOstreamPrintableType>({1})));
+}
+
+void UnusedFunctionWorkaround() {
+ // These are here to ensure we don't get warnings about ostream and PrintTo
+ // for MyPrintableType never getting called.
+ const MyPrintableType dont_warn{17};
+ const MyOstreamPrintableType dont_warn2{18};
+ std::stringstream sstr;
+ sstr << dont_warn;
+ PrintTo(dont_warn, &sstr);
+ sstr << dont_warn2;
+}
+
+} // namespace rtc
diff --git a/webrtc/api/ortc/mediadescription.h b/webrtc/api/ortc/mediadescription.h
index efee662..d638dfc 100644
--- a/webrtc/api/ortc/mediadescription.h
+++ b/webrtc/api/ortc/mediadescription.h
@@ -15,8 +15,8 @@
#include <utility>
#include <vector>
+#include "webrtc/api/optional.h"
#include "webrtc/media/base/cryptoparams.h"
-#include "webrtc/rtc_base/optional.h"
namespace webrtc {
diff --git a/webrtc/api/ortc/rtptransportinterface.h b/webrtc/api/ortc/rtptransportinterface.h
index 165daad..1fdc12c 100644
--- a/webrtc/api/ortc/rtptransportinterface.h
+++ b/webrtc/api/ortc/rtptransportinterface.h
@@ -13,10 +13,10 @@
#include <string>
+#include "webrtc/api/optional.h"
#include "webrtc/api/ortc/packettransportinterface.h"
#include "webrtc/api/rtcerror.h"
#include "webrtc/common_types.h"
-#include "webrtc/rtc_base/optional.h"
namespace webrtc {
diff --git a/webrtc/api/rtpparameters.h b/webrtc/api/rtpparameters.h
index 46f7139..ce61b15 100644
--- a/webrtc/api/rtpparameters.h
+++ b/webrtc/api/rtpparameters.h
@@ -16,7 +16,7 @@
#include <vector>
#include "webrtc/api/mediatypes.h"
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
namespace webrtc {
diff --git a/webrtc/api/video_codecs/BUILD.gn b/webrtc/api/video_codecs/BUILD.gn
index 7fad43d..be7194e 100644
--- a/webrtc/api/video_codecs/BUILD.gn
+++ b/webrtc/api/video_codecs/BUILD.gn
@@ -20,6 +20,7 @@
]
deps = [
+ "..:optional",
"..:video_frame_api",
"../..:webrtc_common",
"../../common_video",
diff --git a/webrtc/api/video_codecs/video_encoder.h b/webrtc/api/video_codecs/video_encoder.h
index 7a319d7..4ec6e45 100644
--- a/webrtc/api/video_codecs/video_encoder.h
+++ b/webrtc/api/video_codecs/video_encoder.h
@@ -15,11 +15,11 @@
#include <string>
#include <vector>
+#include "webrtc/api/optional.h"
#include "webrtc/api/video/video_frame.h"
#include "webrtc/common_types.h"
#include "webrtc/common_video/include/video_frame.h"
#include "webrtc/rtc_base/checks.h"
-#include "webrtc/rtc_base/optional.h"
#include "webrtc/typedefs.h"
namespace webrtc {