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 f412473b7a04778dce0485f6926e5eba9323c551 Author: Meng Zhu <[email protected]> AuthorDate: Mon Mar 4 13:54:22 2019 -0800 Added rvalue overload for `Result::get()`. Review: https://reviews.apache.org/r/70123 --- 3rdparty/stout/include/stout/result.hpp | 39 +++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/3rdparty/stout/include/stout/result.hpp b/3rdparty/stout/include/stout/result.hpp index c1387ae..9c7a025 100644 --- a/3rdparty/stout/include/stout/result.hpp +++ b/3rdparty/stout/include/stout/result.hpp @@ -17,6 +17,7 @@ #include <iostream> #include <string> +#include <utility> #include <stout/abort.hpp> #include <stout/error.hpp> @@ -112,31 +113,35 @@ public: bool isNone() const { return data.isSome() && data->isNone(); } bool isError() const { return data.isError(); } - const T& get() const + T& get() & { return get(*this); } + const T& get() const& { return get(*this); } + T&& get() && { return get(std::move(*this)); } + const T&& get() const&& { return get(std::move(*this)); } + + const T* operator->() const { return &get(); } + T* operator->() { return &get(); } + + const std::string& error() const { assert(isError()); return data.error(); } + +private: + // This is made static to decouple us from the `const` qualifier of `this`. + template <typename Self> + static auto get(Self&& self) + -> decltype(std::forward<Self>(self).data.get().get()) { - if (!isSome()) { + if (!self.isSome()) { std::string errorMessage = "Result::get() but state == "; - if (isError()) { - errorMessage += "ERROR: " + data.error(); - } else if (isNone()) { + if (self.isError()) { + errorMessage += "ERROR: " + self.data.error(); + } else if (self.isNone()) { errorMessage += "NONE"; } ABORT(errorMessage); } - return data->get(); - } - - T& get() - { - return const_cast<T&>(static_cast<const Result&>(*this).get()); + // NOLINTNEXTLINE(mesos-redundant-get) + return std::forward<Self>(self).data.get().get(); } - const T* operator->() const { return &get(); } - T* operator->() { return &get(); } - - const std::string& error() const { assert(isError()); return data.error(); } - -private: // We leverage Try<Option<T>> to avoid dynamic allocation of T. This // means we can take advantage of all the RAII features of 'Try' and // makes the implementation of this class much simpler!
