From 3dcc2f3cdcbc2d718d7d68d4b06a70173683a8ba Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 8 Aug 2014 19:26:40 -0400 Subject: [PATCH] Add macros for delegating constructors --- fit/detail/delegate.h | 37 +++++++++++++++++++ .../remove_rvalue_reference.h} | 23 +++++++++--- fit/fix.h | 2 +- fit/lazy.h | 2 +- fit/on.h | 2 +- test/on.cpp | 16 ++++---- 6 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 fit/detail/delegate.h rename fit/{enable_if_convertible.h => detail/remove_rvalue_reference.h} (52%) diff --git a/fit/detail/delegate.h b/fit/detail/delegate.h new file mode 100644 index 0000000..d392c96 --- /dev/null +++ b/fit/detail/delegate.h @@ -0,0 +1,37 @@ +/*============================================================================= + Copyright (c) 2012 Paul Fultz II + delgate.h + 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 FIT_GUARD_FUNCTION_DELGATE_H +#define FIT_GUARD_FUNCTION_DELGATE_H + +#include +#include + +#define FIT_ENABLE_IF_CONVERTIBLE(...) \ + typename std::enable_if::value, int>::type = 0 + +#define FIT_ENABLE_IF_CONVERTIBLE_UNPACK(...) \ + typename std::enable_if::value..., int>::type = 0 + +#define FIT_ENABLE_IF_CONSTRUCTIBLE(...) \ + typename std::enable_if::value, int>::type = 0 + +#define FIT_DELGATE_CONSTRUCTOR(C, T, var) \ + template \ + constexpr C(FitXs&&... fit_xs) : var(std::forward(fit_xs)...) {} + +// TODO: For compilers that support inheriting constructors replace with +// `using Base::Base;` +#define FIT_INHERIT_CONSTRUCTOR(Derived, Base) FIT_DELGATE_CONSTRUCTOR(Derived, Base, Base) + +namespace fit { +namespace detail { + +} +} + +#endif \ No newline at end of file diff --git a/fit/enable_if_convertible.h b/fit/detail/remove_rvalue_reference.h similarity index 52% rename from fit/enable_if_convertible.h rename to fit/detail/remove_rvalue_reference.h index 919740c..78cd7e0 100644 --- a/fit/enable_if_convertible.h +++ b/fit/detail/remove_rvalue_reference.h @@ -1,16 +1,27 @@ /*============================================================================= Copyright (c) 2014 Paul Fultz II - enable_if_convertible.h + remove_rvalue_reference.h 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 FIT_GUARD_FUNCTION_ENABLE_IF_CONVERTIBLE_H -#define FIT_GUARD_FUNCTION_ENABLE_IF_CONVERTIBLE_H +#ifndef FIT_GUARD_FUNCTION_REMOVE_RVALUE_REFERENCE_H +#define FIT_GUARD_FUNCTION_REMOVE_RVALUE_REFERENCE_H -#include +namespace fit { namespace detail { -#define FIT_ENABLE_IF_CONVERTIBLE(T, U) \ - typename std::enable_if::value, int>::type = 0 +template +struct remove_rvalue_reference +{ + typedef T type; +}; + +template +struct remove_rvalue_reference +{ + typedef T type; +}; + +}} #endif \ No newline at end of file diff --git a/fit/fix.h b/fit/fix.h index 5772a86..885a019 100644 --- a/fit/fix.h +++ b/fit/fix.h @@ -32,7 +32,7 @@ #include #include -#include +#include #ifndef FIT_FIX_HAS_CONSTEXPR #define FIT_FIX_HAS_CONSTEXPR 0 diff --git a/fit/lazy.h b/fit/lazy.h index 91487f7..1967dee 100644 --- a/fit/lazy.h +++ b/fit/lazy.h @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/fit/on.h b/fit/on.h index b756f74..ea02837 100644 --- a/fit/on.h +++ b/fit/on.h @@ -41,7 +41,7 @@ #include #include -#include +#include #include namespace fit { diff --git a/test/on.cpp b/test/on.cpp index a0751a6..4e8d6ab 100644 --- a/test/on.cpp +++ b/test/on.cpp @@ -4,20 +4,20 @@ struct foo { - constexpr foo(int x) : x(x) - {} - int x; + constexpr foo(int x) : x(x) + {} + int x; }; struct select_x { - template - constexpr auto operator()(T&& x) const FIT_RETURNS(x.x); + template + constexpr auto operator()(T&& x) const FIT_RETURNS(x.x); }; FIT_TEST_CASE() { - constexpr auto add = fit::_ + fit::_; - static_assert(fit::on(select_x(), fit::_ + fit::_)(foo(1), foo(2)) == 3, "Constexpr projection failed"); - FIT_TEST_CHECK(fit::on(std::mem_fn(&foo::x), fit::_ + fit::_)(foo(1), foo(2)) == 3); + constexpr auto add = fit::_ + fit::_; + static_assert(fit::on(select_x(), fit::_ + fit::_)(foo(1), foo(2)) == 3, "Constexpr projection failed"); + FIT_TEST_CHECK(fit::on(std::mem_fn(&foo::x), fit::_ + fit::_)(foo(1), foo(2)) == 3); }