diff --git a/BUILD.gn b/BUILD.gn index 81266ebb81..f4f81fe41f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -390,6 +390,7 @@ rtc_source_set("webrtc_common") { deps = [ ":typedefs", "api:array_view", + "api:optional", "api/video:video_bitrate_allocation", "rtc_base:checks", "rtc_base:deprecation", diff --git a/api/BUILD.gn b/api/BUILD.gn index b6caeaa687..55d126acb5 100644 --- a/api/BUILD.gn +++ b/api/BUILD.gn @@ -256,9 +256,9 @@ rtc_source_set("simulated_network_api") { "test/simulated_network.h", ] deps = [ + ":optional", "../rtc_base:criticalsection", "../rtc_base:rtc_base", - "//third_party/abseil-cpp/absl/types:optional", ] } @@ -285,6 +285,18 @@ rtc_source_set("array_view") { ] } +# TODO(bugs.webrtc.org/9078): Deprecated, replaced by absl/types:optional. +# Delete after webrtc and downstreams users are updated. +rtc_source_set("optional") { + visibility = [ "*" ] + sources = [ + "optional.h", + ] + deps = [ + "//third_party/abseil-cpp/absl/types:optional", + ] +} + rtc_source_set("refcountedbase") { visibility = [ "*" ] sources = [ @@ -444,6 +456,10 @@ if (rtc_include_tests) { sources = [ "array_view_unittest.cc", + + # TODO(bugs.webrtc.org/8821): Remove optional_unittests when webrtc starts + # running absl unittest on each commit. + "optional_unittest.cc", "ortc/mediadescription_unittest.cc", "ortc/sessiondescription_unittest.cc", "rtcerror_unittest.cc", @@ -459,6 +475,7 @@ if (rtc_include_tests) { ":array_view", ":libjingle_peerconnection_api", ":libjingle_peerconnection_test_api", + ":optional", ":ortc_api", "../rtc_base:checks", "../rtc_base:rtc_base_approved", diff --git a/api/optional.h b/api/optional.h new file mode 100644 index 0000000000..eada13fa9b --- /dev/null +++ b/api/optional.h @@ -0,0 +1,27 @@ +/* + * 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. + */ + +// TODO(bugs.webrtc.org/9078): Use absl::optional directly. +#ifndef API_OPTIONAL_H_ +#define API_OPTIONAL_H_ + +#include "absl/types/optional.h" + +namespace rtc { + +using absl::nullopt_t; +using absl::nullopt; + +template +using Optional = absl::optional; + +} // namespace rtc + +#endif // API_OPTIONAL_H_ diff --git a/api/optional_unittest.cc b/api/optional_unittest.cc new file mode 100644 index 0000000000..f56dc6d7ac --- /dev/null +++ b/api/optional_unittest.cc @@ -0,0 +1,906 @@ +/* + * 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. + */ + +// TODO(bugs.webrtc.org/8821): Delete this file when absl unittests run on +// webrtc bots. + +#include +#include +#include +#include +#include + +#include "api/optional.h" +#include "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> Setup() { + std::unique_ptr> s(new std::vector); + g_log = s.get(); + g_next_id = 0; + return s; + } + + private: + int id_; + int origin_; + static std::vector* 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* 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 +void VectorAppend(std::vector* v) {} +template +void VectorAppend(std::vector* 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 +std::vector V(Ts... es) { + std::vector strings; + VectorAppend(&strings, static_cast(es)...); + return strings; +} + +} // namespace + +TEST(OptionalTest, TestConstructDefault) { + auto log = Logger::Setup(); + { + Optional x; + EXPECT_FALSE(x); + EXPECT_FALSE(x.has_value()); + } + EXPECT_EQ(V(), *log); +} + +TEST(OptionalTest, TestConstructNullopt) { + auto log = Logger::Setup(); + { + Optional x(nullopt); + EXPECT_FALSE(x); + EXPECT_FALSE(x.has_value()); + } + EXPECT_EQ(V(), *log); +} + +TEST(OptionalTest, TestConstructCopyEmpty) { + auto log = Logger::Setup(); + { + Optional 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 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 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 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 x, y; + x = y; + } + EXPECT_EQ(V(), *log); +} + +TEST(OptionalTest, TestCopyAssignToFullFromEmpty) { + auto log = Logger::Setup(); + { + Optional x(Logger(17)); + Optional 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, TestCopyAssignToFullFromNullopt) { + auto log = Logger::Setup(); + { + Optional x(Logger(17)); + log->push_back("---"); + x = nullopt; + log->push_back("---"); + EXPECT_FALSE(x); + } + EXPECT_EQ( + V("0:17. explicit constructor", "1:17. move constructor (from 0:17)", + "0:17. destructor", "---", "1:17. destructor", "---"), + *log); +} + +TEST(OptionalTest, TestCopyAssignToFullFromEmptyBraces) { + auto log = Logger::Setup(); + { + Optional x(Logger(17)); + log->push_back("---"); + x = {}; + log->push_back("---"); + EXPECT_FALSE(x); + } + 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 x; + Optional 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 x(Logger(17)); + Optional 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 x; + Logger y(17); + log->push_back("---"); + x = Optional(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 x(Logger(17)); + Logger y(42); + log->push_back("---"); + x = Optional(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 x, y; + x = std::move(y); + } + EXPECT_EQ(V(), *log); +} + +TEST(OptionalTest, TestMoveAssignToFullFromEmpty) { + auto log = Logger::Setup(); + { + Optional x(Logger(17)); + Optional 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 x; + Optional 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 x(Logger(17)); + Optional 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 x; + Logger y(17); + log->push_back("---"); + x = Optional(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 x(Logger(17)); + Logger y(42); + log->push_back("---"); + x = Optional(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 x; + x.reset(); + } + EXPECT_EQ(V(), *log); +} + +TEST(OptionalTest, TestResetFull) { + auto log = Logger::Setup(); + { + Optional 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 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 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 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 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 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 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 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 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 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 x(a); + Optional y; + EXPECT_EQ(a, x.value_or(Logger(42))); + EXPECT_EQ(b, y.value_or(Logger(42))); + EXPECT_EQ(a, Optional(Logger(17)).value_or(b)); + EXPECT_EQ(b, Optional().value_or(b)); + // Can't expect exact list of constructors and destructors because it is + // compiler-dependent. i.e. msvc produce different output than clang. Calls + // above are subject to copy elision that allow to change behavior. +} + +TEST(OptionalTest, TestEquality) { + auto log = Logger::Setup(); + { + Logger a(17), b(42); + Optional 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, TestEqualityWithNullopt) { + auto log = Logger::Setup(); + { + Logger a(17); + Optional ma(a), me; + // Using operator== and operator!= explicitly instead of EXPECT_EQ/EXPECT_NE + // macros because those operators are under test. + log->push_back("---"); + + EXPECT_FALSE(ma == nullopt); + EXPECT_FALSE(nullopt == ma); + EXPECT_TRUE(me == nullopt); + EXPECT_TRUE(nullopt == me); + + EXPECT_TRUE(ma != nullopt); + EXPECT_TRUE(nullopt != ma); + EXPECT_FALSE(me != nullopt); + EXPECT_FALSE(nullopt != me); + + log->push_back("---"); + } + // clang-format off + EXPECT_EQ(V("0:17. explicit constructor", + "1:17. copy constructor (from 0:17)", + "---", + // No operators should be called when comparing to empty. + "---", + "1:17. destructor", + "0:17. destructor"), + *log); + // clang-format on +} + +TEST(OptionalTest, TestEqualityWithObject) { + auto log = Logger::Setup(); + { + Logger a(17), b(42); + Optional ma(a), me; + // Using operator== and operator!= explicitly 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 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 x(Logger(42)); + log->push_back("---"); + Logger moved = std::move(x.value()); + 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); +} + +// Nice printing available only when GTEST aware ABSL is present +#ifdef GTEST_HAS_ABSL +#define MaybeTestPrintTo TestPrintTo +#define MaybeTestUnprintablePrintTo TestUnprintablePrintTo +#else +#define MaybeTestPrintTo DISABLED_TestPrintTo +#define MaybeTestUnprintablePrintTo DISABLED_TestUnprintablePrintTo +#endif +TEST(OptionalTest, MaybeTestPrintTo) { + constexpr char kEmptyOptionalMessage[] = "(nullopt)"; + const Optional empty_unprintable; + const Optional empty_printable; + const Optional 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({1}))); + EXPECT_NE("(1)", ::testing::PrintToString(Optional({1}))); + EXPECT_EQ("(The value is 1)", + ::testing::PrintToString(Optional({1}))); + EXPECT_EQ("(1)", + ::testing::PrintToString(Optional({1}))); +} + +TEST(OptionalTest, MaybeTestUnprintablePrintTo) { + struct UnprintableType { + uint8_t value[5]; + }; + Optional opt({0xa1, 0xb2, 0xc3, 0xd4, 0xe5}); + EXPECT_EQ("(5-byte object )", ::testing::PrintToString(opt)); +} + +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/api/test/simulated_network.h b/api/test/simulated_network.h index 4ddc4377d1..0006be2bc2 100644 --- a/api/test/simulated_network.h +++ b/api/test/simulated_network.h @@ -17,7 +17,7 @@ #include #include -#include "absl/types/optional.h" +#include "api/optional.h" #include "rtc_base/criticalsection.h" #include "rtc_base/random.h" #include "rtc_base/thread_annotations.h" diff --git a/common_types.h b/common_types.h index 919a947962..4a76050963 100644 --- a/common_types.h +++ b/common_types.h @@ -17,6 +17,7 @@ #include #include "api/array_view.h" +#include "api/optional.h" // TODO(sprang): Remove this include when all usage includes it directly. #include "api/video/video_bitrate_allocation.h" #include "rtc_base/checks.h" diff --git a/media/base/vp9_profile.cc b/media/base/vp9_profile.cc index a28a912762..3ced430e5f 100644 --- a/media/base/vp9_profile.cc +++ b/media/base/vp9_profile.cc @@ -54,8 +54,8 @@ absl::optional ParseSdpForVP9Profile( bool IsSameVP9Profile(const SdpVideoFormat::Parameters& params1, const SdpVideoFormat::Parameters& params2) { - const absl::optional profile = ParseSdpForVP9Profile(params1); - const absl::optional other_profile = + const rtc::Optional profile = ParseSdpForVP9Profile(params1); + const rtc::Optional other_profile = ParseSdpForVP9Profile(params2); return profile && other_profile && profile == other_profile; } diff --git a/modules/audio_coding/neteq/tools/neteq_input.cc b/modules/audio_coding/neteq/tools/neteq_input.cc index fed3eccea7..12a75fcf41 100644 --- a/modules/audio_coding/neteq/tools/neteq_input.cc +++ b/modules/audio_coding/neteq/tools/neteq_input.cc @@ -34,12 +34,12 @@ TimeLimitedNetEqInput::TimeLimitedNetEqInput(std::unique_ptr input, start_time_ms_(input_->NextEventTime()), duration_ms_(duration_ms) {} -absl::optional TimeLimitedNetEqInput::NextPacketTime() const { - return ended_ ? absl::nullopt : input_->NextPacketTime(); +rtc::Optional TimeLimitedNetEqInput::NextPacketTime() const { + return ended_ ? rtc::Optional() : input_->NextPacketTime(); } -absl::optional TimeLimitedNetEqInput::NextOutputEventTime() const { - return ended_ ? absl::nullopt : input_->NextOutputEventTime(); +rtc::Optional TimeLimitedNetEqInput::NextOutputEventTime() const { + return ended_ ? rtc::Optional() : input_->NextOutputEventTime(); } std::unique_ptr TimeLimitedNetEqInput::PopPacket() { @@ -62,8 +62,8 @@ bool TimeLimitedNetEqInput::ended() const { return ended_ || input_->ended(); } -absl::optional TimeLimitedNetEqInput::NextHeader() const { - return ended_ ? absl::nullopt : input_->NextHeader(); +rtc::Optional TimeLimitedNetEqInput::NextHeader() const { + return ended_ ? rtc::Optional() : input_->NextHeader(); } void TimeLimitedNetEqInput::MaybeSetEnded() { diff --git a/modules/audio_coding/neteq/tools/neteq_input.h b/modules/audio_coding/neteq/tools/neteq_input.h index 16915853d1..a13a86eb97 100644 --- a/modules/audio_coding/neteq/tools/neteq_input.h +++ b/modules/audio_coding/neteq/tools/neteq_input.h @@ -84,18 +84,18 @@ class NetEqInput { class TimeLimitedNetEqInput : public NetEqInput { public: TimeLimitedNetEqInput(std::unique_ptr input, int64_t duration_ms); - absl::optional NextPacketTime() const override; - absl::optional NextOutputEventTime() const override; + rtc::Optional NextPacketTime() const override; + rtc::Optional NextOutputEventTime() const override; std::unique_ptr PopPacket() override; void AdvanceOutputEvent() override; bool ended() const override; - absl::optional NextHeader() const override; + rtc::Optional NextHeader() const override; private: void MaybeSetEnded(); std::unique_ptr input_; - const absl::optional start_time_ms_; + const rtc::Optional start_time_ms_; const int64_t duration_ms_; bool ended_ = false; }; diff --git a/pc/test/framegeneratorcapturervideotracksource.h b/pc/test/framegeneratorcapturervideotracksource.h index 77e78e0c98..b406e3bfba 100644 --- a/pc/test/framegeneratorcapturervideotracksource.h +++ b/pc/test/framegeneratorcapturervideotracksource.h @@ -42,8 +42,8 @@ class FrameGeneratorCapturerVideoTrackSource : public VideoTrackSource { FrameGeneratorCapturerVideoTrackSource(Config config, Clock* clock) : VideoTrackSource(false /* remote */) { video_capturer_.reset(test::FrameGeneratorCapturer::Create( - config.width, config.height, absl::nullopt, - config.num_squares_generated, config.frames_per_second, clock)); + config.width, config.height, rtc::nullopt, config.num_squares_generated, + config.frames_per_second, clock)); } ~FrameGeneratorCapturerVideoTrackSource() = default;