// - lambda_traits.hpp --- Boost Lambda Library ---------------------------- // // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) // // 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) // // For more information, see www.boost.org // ------------------------------------------------------------------------- #ifndef BOOST_LAMBDA_LAMBDA_TRAITS_HPP #define BOOST_LAMBDA_LAMBDA_TRAITS_HPP #include "boost/type_traits/transform_traits.hpp" #include "boost/type_traits/cv_traits.hpp" #include "boost/type_traits/function_traits.hpp" #include "boost/type_traits/object_traits.hpp" #include "boost/tuple/tuple.hpp" namespace boost { namespace lambda { // -- if construct ------------------------------------------------ // Proposed by Krzysztof Czarnecki and Ulrich Eisenecker namespace detail { template struct IF { typedef Then RET; }; template struct IF { typedef Else RET; }; // An if construct that doesn't instantiate the non-matching template: // Called as: // IF_type::type // The matching template must define the typeded 'type' // I.e. A::type if condition is true, B::type if condition is false // Idea from Vesa Karvonen (from C&E as well I guess) template struct IF_type_ { typedef typename T::type type; }; template struct IF_type { typedef typename IF_type_::RET >::type type; }; // helper that can be used to give typedef T to some type template struct identity_mapping { typedef T type; }; // An if construct for finding an integral constant 'value' // Does not instantiate the non-matching branch // Called as IF_value::value // If condition is true A::value must be defined, otherwise B::value template struct IF_value_ { BOOST_STATIC_CONSTANT(int, value = T::value); }; template struct IF_value { BOOST_STATIC_CONSTANT(int, value = (IF_value_::RET>::value)); }; // -------------------------------------------------------------- // removes reference from other than function types: template class remove_reference_if_valid { typedef typename boost::remove_reference::type plainT; public: typedef typename IF< boost::is_function::value, T, plainT >::RET type; }; template struct remove_reference_and_cv { typedef typename boost::remove_cv< typename boost::remove_reference::type >::type type; }; // returns a reference to the element of tuple T template struct tuple_element_as_reference { typedef typename boost::tuples::access_traits< typename boost::tuples::element::type >::non_const_type type; }; // returns the cv and reverence stripped type of a tuple element template struct tuple_element_stripped { typedef typename remove_reference_and_cv< typename boost::tuples::element::type >::type type; }; // is_lambda_functor ------------------------------------------------- template struct is_lambda_functor_ { BOOST_STATIC_CONSTANT(bool, value = false); }; template struct is_lambda_functor_ > { BOOST_STATIC_CONSTANT(bool, value = true); }; } // end detail template struct is_lambda_functor { BOOST_STATIC_CONSTANT(bool, value = detail::is_lambda_functor_< typename detail::remove_reference_and_cv::type >::value); }; namespace detail { // -- parameter_traits_ --------------------------------------------- // An internal parameter type traits class that respects // the reference_wrapper class. // The conversions performed are: // references -> compile_time_error // T1 -> T2, // reference_wrapper -> T& // const array -> ref to const array // array -> ref to array // function -> ref to function // ------------------------------------------------------------------------ template struct parameter_traits_ { typedef T2 type; }; // Do not instantiate with reference types template struct parameter_traits_ { typedef typename generate_error:: parameter_traits_class_instantiated_with_reference_type type; }; // Arrays can't be stored as plain types; convert them to references template struct parameter_traits_ { typedef T (&type)[n]; }; template struct parameter_traits_ { typedef const T (&type)[n]; }; template struct parameter_traits_ { typedef volatile T (&type)[n]; }; template struct parameter_traits_ { typedef const volatile T (&type)[n]; }; template struct parameter_traits_, Any >{ typedef T& type; }; template struct parameter_traits_, Any >{ typedef T& type; }; template struct parameter_traits_, Any >{ typedef T& type; }; template struct parameter_traits_, Any >{ typedef T& type; }; template struct parameter_traits_ { typedef void type; }; template struct parameter_traits_, Any > { typedef lambda_functor type; }; template struct parameter_traits_, Any > { typedef lambda_functor type; }; // Are the volatile versions needed? template struct parameter_traits_, Any > { typedef lambda_functor type; }; template struct parameter_traits_, Any > { typedef lambda_functor type; }; } // end namespace detail // ------------------------------------------------------------------------ // traits classes for lambda expressions (bind functions, operators ...) // must be instantiated with non-reference types // The default is const plain type ------------------------- // const T -> const T, // T -> const T, // references -> compile_time_error // reference_wrapper -> T& // array -> const ref array template struct const_copy_argument { typedef typename detail::parameter_traits_< T, typename detail::IF::value, T&, const T>::RET >::type type; }; // T may be a function type. Without the IF test, const would be added // to a function type, which is illegal. // all arrays are converted to const. // This traits template is used for 'const T&' parameter passing // and thus the knowledge of the potential // non-constness of an actual argument is lost. template struct const_copy_argument { typedef const T (&type)[n]; }; template struct const_copy_argument { typedef const volatile T (&type)[n]; }; template struct const_copy_argument {}; // do not instantiate with references // typedef typename detail::generate_error::references_not_allowed type; template<> struct const_copy_argument { typedef void type; }; // Does the same as const_copy_argument, but passes references through as such template struct bound_argument_conversion { typedef typename const_copy_argument::type type; }; template struct bound_argument_conversion { typedef T& type; }; // The default is non-const reference ------------------------- // const T -> const T&, // T -> T&, // references -> compile_time_error // reference_wrapper -> T& template struct reference_argument { typedef typename detail::parameter_traits_::type type; }; template struct reference_argument { typedef typename detail::generate_error::references_not_allowed type; }; template struct reference_argument > { typedef lambda_functor type; }; template struct reference_argument > { typedef lambda_functor type; }; // Are the volatile versions needed? template struct reference_argument > { typedef lambda_functor type; }; template struct reference_argument > { typedef lambda_functor type; }; template<> struct reference_argument { typedef void type; }; namespace detail { // Array to pointer conversion template struct array_to_pointer { typedef T type; }; template struct array_to_pointer { typedef const T* type; }; template struct array_to_pointer { typedef T* type; }; template struct array_to_pointer { typedef const T* type; }; template struct array_to_pointer { typedef T* type; }; // --------------------------------------------------------------------------- // The call_traits for bind // Respects the reference_wrapper class. // These templates are used outside of bind functions as well. // the bind_tuple_mapper provides a shorter notation for default // bound argument storing semantics, if all arguments are treated // uniformly. // from template foo(const T& t) : bind_traits::type // from template foo(T& t) : bind_traits::type // Conversions: // T -> const T, // cv T -> cv T, // T& -> T& // reference_wrapper -> T& // const reference_wrapper -> T& // array -> const ref array // make bound arguments const, this is a deliberate design choice, the // purpose is to prevent side effects to bound arguments that are stored // as copies template struct bind_traits { typedef const T type; }; template struct bind_traits { typedef T& type; }; // null_types are an exception, we always want to store them as non const // so that other templates can assume that null_type is always without const template<> struct bind_traits { typedef null_type type; }; // the bind_tuple_mapper, bind_type_generators may // introduce const to null_type template<> struct bind_traits { typedef null_type type; }; // Arrays can't be stored as plain types; convert them to references. // All arrays are converted to const. This is because bind takes its // parameters as const T& and thus the knowledge of the potential // non-constness of actual argument is lost. template struct bind_traits { typedef const T (&type)[n]; }; template struct bind_traits { typedef const T (&type)[n]; }; template struct bind_traits { typedef const volatile T (&type)[n]; }; template struct bind_traits { typedef const volatile T (&type)[n]; }; template struct bind_traits { typedef R(&type)(); }; template struct bind_traits { typedef R(&type)(Arg1); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3, Arg4); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8); }; template struct bind_traits { typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9); }; template struct bind_traits >{ typedef T& type; }; template struct bind_traits >{ typedef T& type; }; template<> struct bind_traits { typedef void type; }; template < class T0 = null_type, class T1 = null_type, class T2 = null_type, class T3 = null_type, class T4 = null_type, class T5 = null_type, class T6 = null_type, class T7 = null_type, class T8 = null_type, class T9 = null_type > struct bind_tuple_mapper { typedef tuple::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type, typename bind_traits::type> type; }; // bind_traits, except map const T& -> const T // this is needed e.g. in currying. Const reference arguments can // refer to temporaries, so it is not safe to store them as references. template struct remove_const_reference { typedef typename bind_traits::type type; }; template struct remove_const_reference { typedef const T type; }; // maps the bind argument types to the resulting lambda functor type template < class T0 = null_type, class T1 = null_type, class T2 = null_type, class T3 = null_type, class T4 = null_type, class T5 = null_type, class T6 = null_type, class T7 = null_type, class T8 = null_type, class T9 = null_type > class bind_type_generator { typedef typename detail::bind_tuple_mapper< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 >::type args_t; BOOST_STATIC_CONSTANT(int, nof_elems = boost::tuples::length::value); typedef action< nof_elems, function_action > action_type; public: typedef lambda_functor< lambda_functor_base< action_type, args_t > > type; }; } // detail template inline const T& make_const(const T& t) { return t; } } // end of namespace lambda } // end of namespace boost #endif // BOOST_LAMBDA_TRAITS_HPP