/*============================================================================= Copyright (c) 2014 Paul Fultz II pack.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_PACK_H #define FIT_GUARD_FUNCTION_PACK_H /// pack /// ==== /// /// Description /// ----------- /// /// The `pack` function returns a higher order function object that takes a /// function that will be passed the initial elements. The function object is /// a sequence that can be unpacked with `unpack_adaptor` as well. Also, /// `pack_join` can be used to join multiple packs together. /// /// Synopsis /// -------- /// /// // Capture lvalues by reference and rvalues by value. /// template /// constexpr auto pack(Ts&&... xs); /// /// // Capture lvalues by reference and rvalue reference by reference /// template /// constexpr auto pack_forward(Ts&&... xs); /// /// // Decay everything before capturing /// template /// constexpr auto pack_decay(Ts&&... xs); /// /// // Join multiple packs together /// template /// constexpr auto pack_join(Ts&&... xs); /// /// Semantics /// --------- /// /// assert(pack(xs...)(f) == f(xs...)); /// assert(unpack(f)(pack(xs...)) == f(xs...)); /// /// assert(pack_join(pack(xs...), pack(ys...)) == pack(xs..., ys...)); /// /// /// Example /// ------- /// /// struct sum /// { /// template /// T operator()(T x, U y) const /// { /// return x+y; /// } /// }; /// /// int r = pack(3, 2)(sum()); /// assert(r == 5); /// #include #include #include #include #include #include #include #include #ifndef FIT_HAS_RVALUE_THIS #define FIT_HAS_RVALUE_THIS 1 #endif #ifndef FIT_PACK_HAS_EBO #ifdef __clang__ #define FIT_PACK_HAS_EBO 1 #else #define FIT_PACK_HAS_EBO 0 #endif #endif namespace fit { namespace detail { template struct pack_tag {}; #if FIT_PACK_HAS_EBO template struct pack_holder : std::conditional, alias > {}; #else template struct pack_holder : std::conditional< FIT_IS_EMPTY(T) && FIT_IS_LITERAL(T) && is_default_constructible::value, alias_static, alias > {}; #endif template struct pack_base; template struct is_copyable : std::integral_constant::type>::value || #else std::is_copy_constructible::value || #endif std::is_reference::value )> {}; template::value && !std::is_lvalue_reference::value , int>::type = 0> constexpr T pack_get(X&& x, Ts&&... xs) { return static_cast(alias_value(FIT_FORWARD(X)(x), xs...)); } template::value , int>::type = 0> constexpr T pack_get(X&& x, Ts&&... xs) { return alias_value(x, xs...); } template::value , int>::type = 0> constexpr auto pack_get(X&& x, Ts&&... xs) FIT_RETURNS ( alias_value(FIT_FORWARD(X)(x), xs...) ); #if (defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7) || defined(_MSC_VER) template struct pack_holder_base : Ts::type... { template::type> constexpr pack_holder_base(Xs&&... xs) : Ts::type(FIT_FORWARD(Xs)(xs))... {} #ifndef _MSC_VER // FIT_INHERIT_DEFAULT(pack_holder_base, typename std::remove_cv::type>::type...) FIT_INHERIT_DEFAULT(pack_holder_base, typename Ts::type...) #endif }; template struct pack_holder_base : T::type { typedef typename T::type base; FIT_INHERIT_CONSTRUCTOR(pack_holder_base, base); }; template struct pack_holder_builder { template struct apply : pack_holder, Ts...>> {}; }; template struct pack_base, Ts...> : pack_holder_base::template apply...> { typedef pack_holder_base::template apply...> base; template constexpr pack_base(X1&& x1, X2&& x2, Xs&&... xs) : base(FIT_FORWARD(X1)(x1), FIT_FORWARD(X2)(x2), FIT_FORWARD(Xs)(xs)...) {} template::value), int>::type = 0> constexpr pack_base(X1&& x1) : base(FIT_FORWARD(X1)(x1)) {} // FIT_INHERIT_DEFAULT(pack_base, typename std::remove_cv::type>::type...); FIT_INHERIT_DEFAULT(pack_base, Ts...); FIT_RETURNS_CLASS(pack_base); template constexpr auto operator()(F&& f) const FIT_RETURNS ( f(pack_get, Ts...>>(*FIT_CONST_THIS, f)...) ); typedef std::integral_constant fit_function_param_limit; template struct apply : F::template apply {}; }; template struct pack_base, T> : pack_holder_base, T>>> { typedef pack_holder_base, T>>> base; template::value), int>::type = 0> constexpr pack_base(X1&& x1) : base(FIT_FORWARD(X1)(x1)) {} FIT_INHERIT_DEFAULT(pack_base, T); FIT_RETURNS_CLASS(pack_base); template constexpr auto operator()(F&& f) const FIT_RETURNS ( f(pack_get, T>>(*FIT_CONST_THIS, f)) ); typedef std::integral_constant fit_function_param_limit; template struct apply : F::template apply {}; }; #else template struct pack_base, Ts...> : pack_holder, Ts...>>::type... { // FIT_INHERIT_DEFAULT(pack_base, typename std::remove_cv::type>::type...); FIT_INHERIT_DEFAULT(pack_base, Ts...); template, Ts...>>::type)> constexpr pack_base(Xs&&... xs) : pack_holder, Ts...>>::type(FIT_FORWARD(Xs)(xs))... {} template constexpr auto operator()(F&& f) const FIT_RETURNS ( f(pack_get, Ts...>>(*this, f)...) ); typedef std::integral_constant fit_function_param_limit; template struct apply : F::template apply {}; }; #endif template<> struct pack_base > { template constexpr auto operator()(F&& f) const FIT_RETURNS (f()); typedef std::integral_constant fit_function_param_limit; template struct apply : F::template apply<> {}; }; #define FIT_DETAIL_UNPACK_PACK_BASE(ref, move) \ template \ constexpr auto unpack_pack_base(F&& f, pack_base, Ts...> ref x) \ FIT_RETURNS(f(alias_value, Ts...>, Ts>(move(x), f)...)) FIT_UNARY_PERFECT_FOREACH(FIT_DETAIL_UNPACK_PACK_BASE) template struct pack_join_base; // TODO: Extend to join more than two packs at a time template struct pack_join_base, Ts1...>, pack_base, Ts2...>> { static constexpr long total_size = sizeof...(Ts1) + sizeof...(Ts2); typedef pack_base::type, Ts1..., Ts2...> result_type; template static constexpr result_type call(P1&& p1, P2&& p2) { return result_type( pack_get, Ts1...>>(FIT_FORWARD(P1)(p1))..., pack_get, Ts2...>>(FIT_FORWARD(P2)(p2))...); } }; template struct pack_join_result : pack_join_base< typename std::remove_cv::type>::type, typename std::remove_cv::type>::type > {}; struct pack_f { template constexpr auto operator()(Ts&&... xs) const FIT_RETURNS ( pack_base::type, typename remove_rvalue_reference::type...>(FIT_FORWARD(Ts)(xs)...) ); }; struct pack_forward_f { template constexpr auto operator()(Ts&&... xs) const FIT_RETURNS ( pack_base::type, Ts&&...>(FIT_FORWARD(Ts)(xs)...) ); }; struct pack_decay_f { template constexpr auto operator()(Ts&&... xs) const FIT_RETURNS ( pack_f()(decay(FIT_FORWARD(Ts)(xs))...) ); }; template constexpr typename pack_join_result::result_type make_pack_join_dual(P1&& p1, P2&& p2) { return pack_join_result::call(FIT_FORWARD(P1)(p1), FIT_FORWARD(P2)(p2)); } // Manually compute join return type to make older gcc happy template struct join_type; template struct join_type { typedef T type; }; template struct join_type { typedef typename pack_join_result::type>::result_type type; }; template constexpr P1 make_pack_join(P1&& p1) { return FIT_FORWARD(P1)(p1); } template constexpr typename join_type::type make_pack_join(P1&& p1, Ps&&... ps) { return make_pack_join_dual(FIT_FORWARD(P1)(p1), make_pack_join(FIT_FORWARD(Ps)(ps)...)); } struct pack_join_f { template constexpr auto operator()(Ps&&... ps) const FIT_RETURNS ( make_pack_join(FIT_FORWARD(Ps)(ps)...) ); }; } FIT_DECLARE_STATIC_VAR(pack, detail::pack_f); FIT_DECLARE_STATIC_VAR(pack_forward, detail::pack_forward_f); FIT_DECLARE_STATIC_VAR(pack_decay, detail::pack_decay_f); FIT_DECLARE_STATIC_VAR(pack_join, detail::pack_join_f); } // namespace fit #endif