This is an automated email from the ASF dual-hosted git repository. mzhu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mesos.git
commit 0dc24dba9afdca1948eea5cad05f861162dc8dd6 Author: Meng Zhu <[email protected]> AuthorDate: Wed Feb 27 08:53:51 2019 -0800 Added star operator for `Result`, `Try` and `Option`. Review: https://reviews.apache.org/r/70067 --- 3rdparty/stout/include/stout/option.hpp | 6 ++++++ 3rdparty/stout/include/stout/result.hpp | 5 +++++ 3rdparty/stout/include/stout/try.hpp | 6 ++++++ 3rdparty/stout/tests/option_tests.cpp | 35 +++++++++++++++++++++++++++++++++ 3rdparty/stout/tests/result_tests.cpp | 35 +++++++++++++++++++++++++++++++++ 3rdparty/stout/tests/try_tests.cpp | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+) diff --git a/3rdparty/stout/include/stout/option.hpp b/3rdparty/stout/include/stout/option.hpp index 8feed01..79e9fee 100644 --- a/3rdparty/stout/include/stout/option.hpp +++ b/3rdparty/stout/include/stout/option.hpp @@ -18,6 +18,7 @@ #include <algorithm> #include <functional> #include <type_traits> +#include <utility> #include <boost/functional/hash.hpp> @@ -123,6 +124,11 @@ public: const T* operator->() const { return &get(); } T* operator->() { return &get(); } + const T& operator*() const& { return get(); } + T& operator*() & { return get(); } + const T&& operator*() const&& { return std::move(*this).get(); } + T&& operator*() && { return std::move(*this).get(); } + template <typename U> T getOrElse(U&& u) const & { diff --git a/3rdparty/stout/include/stout/result.hpp b/3rdparty/stout/include/stout/result.hpp index 9c7a025..d5dbf6c 100644 --- a/3rdparty/stout/include/stout/result.hpp +++ b/3rdparty/stout/include/stout/result.hpp @@ -121,6 +121,11 @@ public: const T* operator->() const { return &get(); } T* operator->() { return &get(); } + const T& operator*() const& { return get(); } + T& operator*() & { return get(); } + const T&& operator*() const&& { return std::move(*this).get(); } + T&& operator*() && { return std::move(*this).get(); } + const std::string& error() const { assert(isError()); return data.error(); } private: diff --git a/3rdparty/stout/include/stout/try.hpp b/3rdparty/stout/include/stout/try.hpp index 2882d06..3aedf22 100644 --- a/3rdparty/stout/include/stout/try.hpp +++ b/3rdparty/stout/include/stout/try.hpp @@ -17,6 +17,7 @@ #include <iostream> #include <string> +#include <utility> #include <stout/abort.hpp> #include <stout/error.hpp> @@ -84,6 +85,11 @@ public: const T* operator->() const { return &get(); } T* operator->() { return &get(); } + const T& operator*() const& { return get(); } + T& operator*() & { return get(); } + const T&& operator*() const&& { return std::move(*this).get(); } + T&& operator*() && { return std::move(*this).get(); } + // NOTE: This function is intended to return the error of type `E`. // However, we return a `std::string` if `E` == `Error` since that's what it // used to return, and it's the only data that `Error` holds anyway. diff --git a/3rdparty/stout/tests/option_tests.cpp b/3rdparty/stout/tests/option_tests.cpp index 815d40e..746eeaa 100644 --- a/3rdparty/stout/tests/option_tests.cpp +++ b/3rdparty/stout/tests/option_tests.cpp @@ -128,6 +128,41 @@ TEST(OptionTest, ArrowOperator) } +TEST(OptionTest, StarOperator) +{ + // A test class with a `moved` flag where we can verify if an object + // has been moved. + struct Foo + { + bool moved = false; + string s; + + Foo(const string& s) { this->s = s; }; + + Foo(Foo&& that) + { + s = std::move(that.s); + that.moved = true; + }; + + Foo& operator=(Foo&& that) + { + s = std::move(that.s); + that.moved = true; + + return *this; + }; + }; + + Option<Foo> foo("hello"); + EXPECT_EQ("hello", (*foo).s); + + Option<Foo> bar(*std::move(foo)); + EXPECT_EQ("hello", (*bar).s); + EXPECT_TRUE(foo->moved); +} + + struct NonCopyable { NonCopyable() = default; diff --git a/3rdparty/stout/tests/result_tests.cpp b/3rdparty/stout/tests/result_tests.cpp index 1750e6b..5833fea 100644 --- a/3rdparty/stout/tests/result_tests.cpp +++ b/3rdparty/stout/tests/result_tests.cpp @@ -57,3 +57,38 @@ TEST(ResultTest, ArrowOperator) s->clear(); EXPECT_TRUE(s->empty()); } + + +TEST(ResultTest, StarOperator) +{ + // A test class with a `moved` flag where we can verify if an object + // has been moved. + struct Foo + { + bool moved = false; + string s; + + Foo(const string& s) { this->s = s; }; + + Foo(Foo&& that) + { + s = std::move(that.s); + that.moved = true; + }; + + Foo& operator=(Foo&& that) + { + s = std::move(that.s); + that.moved = true; + + return *this; + }; + }; + + Result<Foo> foo("hello"); + EXPECT_EQ("hello", (*foo).s); + + Result<Foo> bar(*std::move(foo)); + EXPECT_EQ("hello", (*bar).s); + EXPECT_TRUE(foo->moved); +} diff --git a/3rdparty/stout/tests/try_tests.cpp b/3rdparty/stout/tests/try_tests.cpp index 99b7b2a..b3d9895 100644 --- a/3rdparty/stout/tests/try_tests.cpp +++ b/3rdparty/stout/tests/try_tests.cpp @@ -26,3 +26,38 @@ TEST(TryTest, ArrowOperator) s->clear(); EXPECT_TRUE(s->empty()); } + + +TEST(TryTest, StarOperator) +{ + // A test class with a `moved` flag where we can verify if an object + // has been moved. + struct Foo + { + bool moved = false; + string s; + + Foo(const string& s) { this->s = s; }; + + Foo(Foo&& that) + { + s = std::move(that.s); + that.moved = true; + }; + + Foo& operator=(Foo&& that) + { + s = std::move(that.s); + that.moved = true; + + return *this; + }; + }; + + Try<Foo> foo("hello"); + EXPECT_EQ("hello", (*foo).s); + + Try<Foo> bar(*std::move(foo)); + EXPECT_EQ("hello", (*bar).s); + EXPECT_TRUE(foo->moved); +}
