// Copyright 2015-2016 Klemens D. Morgenstern // // 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 BOOST_DLL_DETAIL_IMPORT_MANGLED_HELPERS_HPP_ #define BOOST_DLL_DETAIL_IMPORT_MANGLED_HELPERS_HPP_ #include #include #include #include #include #ifdef BOOST_HAS_PRAGMA_ONCE # pragma once #endif namespace boost { namespace dll { namespace experimental { namespace detail { //the following could be done by fusion, though it's simple enough to just declare it here. template struct sequence {}; template struct push_front; template struct push_front> { typedef sequence type; }; template struct unqalified_is_same : boost::is_same< typename boost::remove_cv::type, typename boost::remove_cv::type > { }; /* ********************************** function sequence type traits ******************************/ //determine if it's a sequence of functions. template struct is_function_seq; //type-trait for function overloads template struct is_function_seq> : boost::conditional< boost::is_function::value, is_function_seq>, boost::false_type>::type {}; template struct is_function_seq> : boost::is_function { }; template<> struct is_function_seq> : boost::false_type { }; /* ********************************* Function Tuple *************************** */ //a tuple of plain functions. template struct function_tuple; template struct function_tuple : function_tuple { Return(*f_)(Args...); constexpr function_tuple(Return(* t)(Args...), T2* t2, Ts* ... ts) : function_tuple(t2, ts...) , f_(t) {} Return operator()(Args...args) const { return (*f_)(static_cast(args)...); } using function_tuple::operator(); }; template struct function_tuple { Return(*f_)(Args...); constexpr function_tuple(Return(* t)(Args...)) : f_(t) {} Return operator()(Args...args) const { return (*f_)(static_cast(args)...); } }; /* ********************************** MemFn sequence type traits ******************************/ template struct mem_fn_def { typedef Class class_type; typedef Func func_type; typedef typename boost::dll::detail::get_mem_fn_type::mem_fn mem_fn; }; template struct make_mem_fn_seq; // B: is T1 another version of T0? template struct make_mem_fn_seq_getter; template struct make_mem_fn_seq_getter { typedef mem_fn_def type; }; template struct make_mem_fn_seq_getter { typedef mem_fn_def type; }; template struct make_mem_fn_seq { typedef mem_fn_def mem_fn; typedef sequence type; }; template struct make_mem_fn_seq { typedef sequence<> type; }; template struct make_mem_fn_seq { /* Since we might have ovls, it might be : * Class, void(int), void(int, int) //--> just us class for both * Class, const Class, void(int)//--> ovl class. * */ static_assert(boost::is_object::value, ""); typedef typename make_mem_fn_seq_getter< unqalified_is_same::value, T0, T1, T2>::type mem_fn_type; typedef typename boost::conditional< unqalified_is_same::value, make_mem_fn_seq, make_mem_fn_seq> ::type next; typedef typename push_front::type type; }; /* Ok, this needs to be documented, so here's some pseudo-code: * * @code * * bool unqalified_is_same(lhs, rhs) * { * return remove_cv(lhs) == remove_cv(rhs); * } * * mem_fn make_mem_fn_seq_getter(b, cl, T2, T3) * { * if (b) //b means, that T2 is another version of cl, i.e. qualified * return get_mem_fn_type(T2, T3); * else //means that T2 is a function. * return get_mem_fn_type(cl, T2); * } * * sequence make_mem_fn_seq(type cl, type T2, type T3, types...) * { * mem_fn = make_mem_fn_seq_getter( * unqalified_is_same(cl, T2), cl, T2, T3); * * next = unqalified_is_same(cl, T2) ? * make_mem_fn_seq(T2, types...) //because: T2 is another version of cl, hence i use this. T3 was already consumed. * : * make_mem_fn_seq(Class, T3, types...) //because: T2 was a function, hence it is consumed and class remains unchanged. * ; * return push_front(mem_fn, next) ; * }; * @endcode */ template struct is_mem_fn_seq_impl { typedef typename boost::conditional< boost::is_function::value || boost::dll::experimental::detail::unqalified_is_same::value, typename is_mem_fn_seq_impl::type, boost::false_type>::type type; }; template struct is_mem_fn_seq_impl { typedef typename boost::conditional< boost::is_function::value && boost::is_object::value, boost::true_type, boost::false_type>::type type; }; template struct is_mem_fn_seq_impl { typedef typename boost::conditional< (boost::is_function::value || boost::dll::experimental::detail::unqalified_is_same::value) && boost::is_function::value, boost::true_type, boost::false_type>::type type; }; template struct is_mem_fn_seq : boost::false_type {}; //If only two arguments are provided at all. template struct is_mem_fn_seq> : boost::conditional< boost::is_object::value && boost::is_function::value, boost::true_type, boost::false_type>::type { }; template struct is_mem_fn_seq> : boost::conditional< boost::is_class::value && boost::is_function::value, typename is_mem_fn_seq_impl::type, boost::false_type>::type {}; /* ********************************** mem fn sequence tuple ******************************/ /* A tuple of member functions * Unlike for plain functions a sequence here might contain classes as well as functions. */ template struct mem_fn_tuple; template struct mem_fn_tuple, T2, Ts...> : mem_fn_tuple { typedef typename boost::dll::detail::get_mem_fn_type::mem_fn mem_fn; mem_fn f_; constexpr mem_fn_tuple(mem_fn f, typename T2::mem_fn t2, typename Ts::mem_fn ... ts) : mem_fn_tuple(t2, ts...) , f_(f) {} Return operator()(Class* const cl, Args...args) const { return (cl->*f_)(static_cast(args)...); } using mem_fn_tuple::operator(); }; template struct mem_fn_tuple> { typedef typename boost::dll::detail::get_mem_fn_type::mem_fn mem_fn; mem_fn f_; constexpr mem_fn_tuple(mem_fn f) : f_(f) {} Return operator()(Class * const cl, Args...args) const { return (cl->*f_)(static_cast(args)...); } }; }}}} #endif /* BOOST_DLL_DETAIL_IMPORT_MANGLED_HELPERS_HPP_ */