Rename move_helper.hpp to ref_arg.hpp and use template alias

Shortens the call sites a lot: `typename ref_arg<Ts>::type` -> `ref_arg_t<Ts>`
MSVC 2017 seems to have issues with std::conditional_t so use the C++11
variant here.
This commit is contained in:
Alexander Grund 2022-02-07 16:23:23 +01:00
parent baaaa15489
commit f3dc82f305
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
8 changed files with 39 additions and 43 deletions

View file

@ -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 <boost/version.hpp>

View file

@ -24,7 +24,7 @@ namespace mock { namespace detail {
class default_matcher : public matcher_base<Args...>
{
private:
bool operator()(typename ref_arg<Args>::type...) override { return true; }
bool operator()(ref_arg_t<Args>...) 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<std::size_t... I>
bool is_valid_impl(std::index_sequence<I...>, typename ref_arg<Args>::type... t)
bool is_valid_impl(std::index_sequence<I...>, ref_arg_t<Args>... t)
{
using expander = bool[];
bool result = true;
(void)expander{ result &= std::get<I>(matchers_)(std::forward<Args>(t))... };
(void)expander{ result &= std::get<I>(matchers_)(static_cast<ref_arg_t<Args>>(t))... };
return result;
}
bool operator()(typename ref_arg<Args>::type... t) override
bool operator()(ref_arg_t<Args>... t) override
{
return is_valid_impl(std::make_index_sequence<sizeof...(Args)>{}, std::forward<Args>(t)...);
return is_valid_impl(std::make_index_sequence<sizeof...(Args)>{}, static_cast<ref_arg_t<Args>>(t)...);
}
template<std::size_t... I>
void serialize_impl(std::index_sequence<I...>, 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<Args>::type... t) override { return f_(std::forward<Args>(t)...); }
bool operator()(ref_arg_t<Args>... t) override { return f_(static_cast<ref_arg_t<Args>>(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<Args>::type... t) const
bool is_valid(ref_arg_t<Args>... t) const
{
return !invocation_->exhausted() && (*matcher_)(std::forward<Args>(t)...);
return !invocation_->exhausted() && (*matcher_)(static_cast<ref_arg_t<Args>>(t)...);
}
bool invoke() const

View file

@ -54,7 +54,7 @@ namespace mock { namespace detail {
}
expectation_type expect() { return impl_->expect(); }
R operator()(Ts... args) const { return (*impl_)(std::forward<Ts>(args)...); }
R operator()(Ts... args) const { return (*impl_)(static_cast<ref_arg_t<Ts>>(args)...); }
friend std::ostream& operator<<(std::ostream& s, const function& f) { return s << *f.impl_; }

View file

@ -249,7 +249,7 @@ namespace mock { namespace detail {
valid_ = false;
for(const auto& expectation : expectations_)
{
if(expectation.is_valid(std::forward<Args>(args)...))
if(expectation.is_valid(static_cast<ref_arg_t<Args>>(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>(args)...);
return expectation.functor()(static_cast<ref_arg_t<Args>>(args)...);
return expectation.trigger();
}
}

View file

@ -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 <ostream>
namespace mock { namespace detail {
template<typename... Ts>
template<typename... Args>
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<Ts>::type...) = 0;
virtual bool operator()(ref_arg_t<Args>...) = 0;
friend std::ostream& operator<<(std::ostream& s, const matcher_base& m)
{

View file

@ -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 <type_traits>
namespace mock { namespace detail {
template<typename T>
using ref_arg = std::conditional<std::is_reference<T>::value, T, std::add_rvalue_reference_t<T>>;
}} // namespace mock::detail
#endif // MOCK_MOVE_HELPER_HPP_INCLUDED

View file

@ -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 <type_traits>
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<typename T>
using ref_arg_t = typename std::conditional<std::is_reference<T>::value, T, std::add_rvalue_reference_t<T>>::type;
}} // namespace mock::detail
#endif // MOCK_REF_ARG_HPP_INCLUDED

View file

@ -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 <cstring>
#include <functional>
@ -51,10 +51,7 @@ class matcher<Actual, mock::constraint<Constraint>>
{
public:
explicit matcher(const constraint<Constraint>& c) : c_(c.c_) {}
bool operator()(typename detail::ref_arg<Actual>::type actual)
{
return c_(std::forward<typename detail::ref_arg<Actual>::type>(actual));
}
bool operator()(detail::ref_arg_t<Actual> actual) { return c_(static_cast<detail::ref_arg_t<Actual>>(actual)); }
friend std::ostream& operator<<(std::ostream& s, const matcher& m) { return s << mock::format(m.c_); }
private:
@ -66,10 +63,7 @@ class matcher<Actual, Functor, std::enable_if_t<detail::is_functor<Functor, Actu
{
public:
explicit matcher(const Functor& f) : c_(f) {}
bool operator()(typename detail::ref_arg<Actual>::type actual)
{
return c_(std::forward<typename detail::ref_arg<Actual>::type>(actual));
}
bool operator()(detail::ref_arg_t<Actual> actual) { return c_(static_cast<detail::ref_arg_t<Actual>>(actual)); }
friend std::ostream& operator<<(std::ostream& s, const matcher& m) { return s << mock::format(m.c_); }
private: