From f3dc82f3054474df666382a73adde5e77313441e Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Mon, 7 Feb 2022 16:23:23 +0100 Subject: [PATCH] Rename move_helper.hpp to ref_arg.hpp and use template alias Shortens the call sites a lot: `typename ref_arg::type` -> `ref_arg_t` MSVC 2017 seems to have issues with std::conditional_t so use the C++11 variant here. --- include/turtle/constraints.hpp | 1 - include/turtle/detail/expectation.hpp | 16 ++++++++-------- include/turtle/detail/function.hpp | 2 +- include/turtle/detail/function_impl.hpp | 4 ++-- include/turtle/detail/matcher_base.hpp | 6 +++--- include/turtle/detail/move_helper.hpp | 19 ------------------- include/turtle/detail/ref_arg.hpp | 22 ++++++++++++++++++++++ include/turtle/matcher.hpp | 12 +++--------- 8 files changed, 39 insertions(+), 43 deletions(-) delete mode 100644 include/turtle/detail/move_helper.hpp create mode 100644 include/turtle/detail/ref_arg.hpp diff --git a/include/turtle/constraints.hpp b/include/turtle/constraints.hpp index 14e5458..572b7d9 100644 --- a/include/turtle/constraints.hpp +++ b/include/turtle/constraints.hpp @@ -11,7 +11,6 @@ #include "config.hpp" #include "constraint.hpp" -#include "detail/move_helper.hpp" #include "detail/void_t.hpp" #include "unwrap_reference.hpp" #include diff --git a/include/turtle/detail/expectation.hpp b/include/turtle/detail/expectation.hpp index cd107dc..18b9bb2 100644 --- a/include/turtle/detail/expectation.hpp +++ b/include/turtle/detail/expectation.hpp @@ -24,7 +24,7 @@ namespace mock { namespace detail { class default_matcher : public matcher_base { private: - bool operator()(typename ref_arg::type...) override { return true; } + bool operator()(ref_arg_t...) override { return true; } void serialize(std::ostream& s) const override { constexpr auto arity = sizeof...(Args); @@ -51,16 +51,16 @@ namespace mock { namespace detail { private: template - bool is_valid_impl(std::index_sequence, typename ref_arg::type... t) + bool is_valid_impl(std::index_sequence, ref_arg_t... t) { using expander = bool[]; bool result = true; - (void)expander{ result &= std::get(matchers_)(std::forward(t))... }; + (void)expander{ result &= std::get(matchers_)(static_cast>(t))... }; return result; } - bool operator()(typename ref_arg::type... t) override + bool operator()(ref_arg_t... t) override { - return is_valid_impl(std::make_index_sequence{}, std::forward(t)...); + return is_valid_impl(std::make_index_sequence{}, static_cast>(t)...); } template void serialize_impl(std::index_sequence, std::ostream& s) const @@ -87,7 +87,7 @@ namespace mock { namespace detail { multi_matcher(const F& f) : f_(f) {} private: - bool operator()(typename ref_arg::type... t) override { return f_(std::forward(t)...); } + bool operator()(ref_arg_t... t) override { return f_(static_cast>(t)...); } void serialize(std::ostream& s) const override { s << mock::format(f_); } private: @@ -143,9 +143,9 @@ namespace mock { namespace detail { bool verify() const { return invocation_->verify(); } - bool is_valid(typename ref_arg::type... t) const + bool is_valid(ref_arg_t... t) const { - return !invocation_->exhausted() && (*matcher_)(std::forward(t)...); + return !invocation_->exhausted() && (*matcher_)(static_cast>(t)...); } bool invoke() const diff --git a/include/turtle/detail/function.hpp b/include/turtle/detail/function.hpp index 288990a..e4e8c64 100644 --- a/include/turtle/detail/function.hpp +++ b/include/turtle/detail/function.hpp @@ -54,7 +54,7 @@ namespace mock { namespace detail { } expectation_type expect() { return impl_->expect(); } - R operator()(Ts... args) const { return (*impl_)(std::forward(args)...); } + R operator()(Ts... args) const { return (*impl_)(static_cast>(args)...); } friend std::ostream& operator<<(std::ostream& s, const function& f) { return s << *f.impl_; } diff --git a/include/turtle/detail/function_impl.hpp b/include/turtle/detail/function_impl.hpp index 1c0bea4..a396718 100644 --- a/include/turtle/detail/function_impl.hpp +++ b/include/turtle/detail/function_impl.hpp @@ -249,7 +249,7 @@ namespace mock { namespace detail { valid_ = false; for(const auto& expectation : expectations_) { - if(expectation.is_valid(std::forward(args)...)) + if(expectation.is_valid(static_cast>(args)...)) { if(!expectation.invoke()) { @@ -266,7 +266,7 @@ namespace mock { namespace detail { valid_ = true; error_type::call(MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line()); if(expectation.functor()) - return expectation.functor()(std::forward(args)...); + return expectation.functor()(static_cast>(args)...); return expectation.trigger(); } } diff --git a/include/turtle/detail/matcher_base.hpp b/include/turtle/detail/matcher_base.hpp index de16e22..abb1fb9 100644 --- a/include/turtle/detail/matcher_base.hpp +++ b/include/turtle/detail/matcher_base.hpp @@ -9,11 +9,11 @@ #ifndef MOCK_MATCHER_BASE_HPP_INCLUDED #define MOCK_MATCHER_BASE_HPP_INCLUDED -#include "move_helper.hpp" +#include "ref_arg.hpp" #include namespace mock { namespace detail { - template + template class matcher_base { public: @@ -22,7 +22,7 @@ namespace mock { namespace detail { matcher_base& operator=(const matcher_base&) = delete; virtual ~matcher_base() = default; - virtual bool operator()(typename ref_arg::type...) = 0; + virtual bool operator()(ref_arg_t...) = 0; friend std::ostream& operator<<(std::ostream& s, const matcher_base& m) { diff --git a/include/turtle/detail/move_helper.hpp b/include/turtle/detail/move_helper.hpp deleted file mode 100644 index 18e303a..0000000 --- a/include/turtle/detail/move_helper.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// http://turtle.sourceforge.net -// -// Copyright Mathieu Champlon 2018 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#ifndef MOCK_MOVE_HELPER_HPP_INCLUDED -#define MOCK_MOVE_HELPER_HPP_INCLUDED - -#include - -namespace mock { namespace detail { - template - using ref_arg = std::conditional::value, T, std::add_rvalue_reference_t>; -}} // namespace mock::detail - -#endif // MOCK_MOVE_HELPER_HPP_INCLUDED diff --git a/include/turtle/detail/ref_arg.hpp b/include/turtle/detail/ref_arg.hpp new file mode 100644 index 0000000..abd755f --- /dev/null +++ b/include/turtle/detail/ref_arg.hpp @@ -0,0 +1,22 @@ +// http://turtle.sourceforge.net +// +// Copyright Mathieu Champlon 2018 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef MOCK_REF_ARG_HPP_INCLUDED +#define MOCK_REF_ARG_HPP_INCLUDED + +#include + +namespace mock { namespace detail { + /// Make T a reference type: + /// If T is already a reference type, return T, else return an rvalue reference to T + /// Useful to pass along arguments keeping the ability to modify them (e.g. move from them) + template + using ref_arg_t = typename std::conditional::value, T, std::add_rvalue_reference_t>::type; +}} // namespace mock::detail + +#endif // MOCK_REF_ARG_HPP_INCLUDED diff --git a/include/turtle/matcher.hpp b/include/turtle/matcher.hpp index 249500f..7854d77 100644 --- a/include/turtle/matcher.hpp +++ b/include/turtle/matcher.hpp @@ -12,7 +12,7 @@ #include "config.hpp" #include "constraints.hpp" #include "detail/is_functor.hpp" -#include "detail/move_helper.hpp" +#include "detail/ref_arg.hpp" #include "log.hpp" #include #include @@ -51,10 +51,7 @@ class matcher> { public: explicit matcher(const constraint& c) : c_(c.c_) {} - bool operator()(typename detail::ref_arg::type actual) - { - return c_(std::forward::type>(actual)); - } + bool operator()(detail::ref_arg_t actual) { return c_(static_cast>(actual)); } friend std::ostream& operator<<(std::ostream& s, const matcher& m) { return s << mock::format(m.c_); } private: @@ -66,10 +63,7 @@ class matcher::type actual) - { - return c_(std::forward::type>(actual)); - } + bool operator()(detail::ref_arg_t actual) { return c_(static_cast>(actual)); } friend std::ostream& operator<<(std::ostream& s, const matcher& m) { return s << mock::format(m.c_); } private: