Home | History | Annotate | Download | only in type_traits
      1 
      2 //  Copyright 2000 John Maddock (john (at) johnmaddock.co.uk)
      3 //  Use, modification and distribution are subject to the Boost Software License,
      4 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
      5 //  http://www.boost.org/LICENSE_1_0.txt).
      6 //
      7 //  See http://www.boost.org/libs/type_traits for most recent version including documentation.
      8 
      9 #ifndef BOOST_TT_FUNCTION_TRAITS_HPP_INCLUDED
     10 #define BOOST_TT_FUNCTION_TRAITS_HPP_INCLUDED
     11 
     12 #include <boost/config.hpp>
     13 #include <boost/type_traits/is_function.hpp>
     14 #include <boost/type_traits/add_pointer.hpp>
     15 
     16 namespace boost {
     17 
     18 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
     19 namespace detail {
     20 
     21 template<typename Function> struct function_traits_helper;
     22 
     23 template<typename R>
     24 struct function_traits_helper<R (*)(void)>
     25 {
     26   BOOST_STATIC_CONSTANT(unsigned, arity = 0);
     27   typedef R result_type;
     28 };
     29 
     30 template<typename R, typename T1>
     31 struct function_traits_helper<R (*)(T1)>
     32 {
     33   BOOST_STATIC_CONSTANT(unsigned, arity = 1);
     34   typedef R result_type;
     35   typedef T1 arg1_type;
     36   typedef T1 argument_type;
     37 };
     38 
     39 template<typename R, typename T1, typename T2>
     40 struct function_traits_helper<R (*)(T1, T2)>
     41 {
     42   BOOST_STATIC_CONSTANT(unsigned, arity = 2);
     43   typedef R result_type;
     44   typedef T1 arg1_type;
     45   typedef T2 arg2_type;
     46   typedef T1 first_argument_type;
     47   typedef T2 second_argument_type;
     48 };
     49 
     50 template<typename R, typename T1, typename T2, typename T3>
     51 struct function_traits_helper<R (*)(T1, T2, T3)>
     52 {
     53   BOOST_STATIC_CONSTANT(unsigned, arity = 3);
     54   typedef R result_type;
     55   typedef T1 arg1_type;
     56   typedef T2 arg2_type;
     57   typedef T3 arg3_type;
     58 };
     59 
     60 template<typename R, typename T1, typename T2, typename T3, typename T4>
     61 struct function_traits_helper<R (*)(T1, T2, T3, T4)>
     62 {
     63   BOOST_STATIC_CONSTANT(unsigned, arity = 4);
     64   typedef R result_type;
     65   typedef T1 arg1_type;
     66   typedef T2 arg2_type;
     67   typedef T3 arg3_type;
     68   typedef T4 arg4_type;
     69 };
     70 
     71 template<typename R, typename T1, typename T2, typename T3, typename T4,
     72          typename T5>
     73 struct function_traits_helper<R (*)(T1, T2, T3, T4, T5)>
     74 {
     75   BOOST_STATIC_CONSTANT(unsigned, arity = 5);
     76   typedef R result_type;
     77   typedef T1 arg1_type;
     78   typedef T2 arg2_type;
     79   typedef T3 arg3_type;
     80   typedef T4 arg4_type;
     81   typedef T5 arg5_type;
     82 };
     83 
     84 template<typename R, typename T1, typename T2, typename T3, typename T4,
     85          typename T5, typename T6>
     86 struct function_traits_helper<R (*)(T1, T2, T3, T4, T5, T6)>
     87 {
     88   BOOST_STATIC_CONSTANT(unsigned, arity = 6);
     89   typedef R result_type;
     90   typedef T1 arg1_type;
     91   typedef T2 arg2_type;
     92   typedef T3 arg3_type;
     93   typedef T4 arg4_type;
     94   typedef T5 arg5_type;
     95   typedef T6 arg6_type;
     96 };
     97 
     98 template<typename R, typename T1, typename T2, typename T3, typename T4,
     99          typename T5, typename T6, typename T7>
    100 struct function_traits_helper<R (*)(T1, T2, T3, T4, T5, T6, T7)>
    101 {
    102   BOOST_STATIC_CONSTANT(unsigned, arity = 7);
    103   typedef R result_type;
    104   typedef T1 arg1_type;
    105   typedef T2 arg2_type;
    106   typedef T3 arg3_type;
    107   typedef T4 arg4_type;
    108   typedef T5 arg5_type;
    109   typedef T6 arg6_type;
    110   typedef T7 arg7_type;
    111 };
    112 
    113 template<typename R, typename T1, typename T2, typename T3, typename T4,
    114          typename T5, typename T6, typename T7, typename T8>
    115 struct function_traits_helper<R (*)(T1, T2, T3, T4, T5, T6, T7, T8)>
    116 {
    117   BOOST_STATIC_CONSTANT(unsigned, arity = 8);
    118   typedef R result_type;
    119   typedef T1 arg1_type;
    120   typedef T2 arg2_type;
    121   typedef T3 arg3_type;
    122   typedef T4 arg4_type;
    123   typedef T5 arg5_type;
    124   typedef T6 arg6_type;
    125   typedef T7 arg7_type;
    126   typedef T8 arg8_type;
    127 };
    128 
    129 template<typename R, typename T1, typename T2, typename T3, typename T4,
    130          typename T5, typename T6, typename T7, typename T8, typename T9>
    131 struct function_traits_helper<R (*)(T1, T2, T3, T4, T5, T6, T7, T8, T9)>
    132 {
    133   BOOST_STATIC_CONSTANT(unsigned, arity = 9);
    134   typedef R result_type;
    135   typedef T1 arg1_type;
    136   typedef T2 arg2_type;
    137   typedef T3 arg3_type;
    138   typedef T4 arg4_type;
    139   typedef T5 arg5_type;
    140   typedef T6 arg6_type;
    141   typedef T7 arg7_type;
    142   typedef T8 arg8_type;
    143   typedef T9 arg9_type;
    144 };
    145 
    146 template<typename R, typename T1, typename T2, typename T3, typename T4,
    147          typename T5, typename T6, typename T7, typename T8, typename T9,
    148          typename T10>
    149 struct function_traits_helper<R (*)(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>
    150 {
    151   BOOST_STATIC_CONSTANT(unsigned, arity = 10);
    152   typedef R result_type;
    153   typedef T1 arg1_type;
    154   typedef T2 arg2_type;
    155   typedef T3 arg3_type;
    156   typedef T4 arg4_type;
    157   typedef T5 arg5_type;
    158   typedef T6 arg6_type;
    159   typedef T7 arg7_type;
    160   typedef T8 arg8_type;
    161   typedef T9 arg9_type;
    162   typedef T10 arg10_type;
    163 };
    164 
    165 } // end namespace detail
    166 
    167 template<typename Function>
    168 struct function_traits :
    169   public boost::detail::function_traits_helper<typename boost::add_pointer<Function>::type>
    170 {
    171 };
    172 
    173 #else
    174 
    175 namespace detail {
    176 
    177 template<unsigned N>
    178 struct type_of_size
    179 {
    180   char elements[N];
    181 };
    182 
    183 template<typename R>
    184 type_of_size<1> function_arity_helper(R (*f)());
    185 
    186 template<typename R, typename T1>
    187 type_of_size<2> function_arity_helper(R (*f)(T1));
    188 
    189 template<typename R, typename T1, typename T2>
    190 type_of_size<3> function_arity_helper(R (*f)(T1, T2));
    191 
    192 template<typename R, typename T1, typename T2, typename T3>
    193 type_of_size<4> function_arity_helper(R (*f)(T1, T2, T3));
    194 
    195 template<typename R, typename T1, typename T2, typename T3, typename T4>
    196 type_of_size<5> function_arity_helper(R (*f)(T1, T2, T3, T4));
    197 
    198 template<typename R, typename T1, typename T2, typename T3, typename T4,
    199          typename T5>
    200 type_of_size<6> function_arity_helper(R (*f)(T1, T2, T3, T4, T5));
    201 
    202 template<typename R, typename T1, typename T2, typename T3, typename T4,
    203          typename T5, typename T6>
    204 type_of_size<7> function_arity_helper(R (*f)(T1, T2, T3, T4, T5, T6));
    205 
    206 template<typename R, typename T1, typename T2, typename T3, typename T4,
    207          typename T5, typename T6, typename T7>
    208 type_of_size<8> function_arity_helper(R (*f)(T1, T2, T3, T4, T5, T6, T7));
    209 
    210 template<typename R, typename T1, typename T2, typename T3, typename T4,
    211          typename T5, typename T6, typename T7, typename T8>
    212 type_of_size<9> function_arity_helper(R (*f)(T1, T2, T3, T4, T5, T6, T7, T8));
    213 
    214 template<typename R, typename T1, typename T2, typename T3, typename T4,
    215          typename T5, typename T6, typename T7, typename T8, typename T9>
    216 type_of_size<10> function_arity_helper(R (*f)(T1, T2, T3, T4, T5, T6, T7, T8,
    217                                               T9));
    218 
    219 template<typename R, typename T1, typename T2, typename T3, typename T4,
    220          typename T5, typename T6, typename T7, typename T8, typename T9,
    221          typename T10>
    222 type_of_size<11> function_arity_helper(R (*f)(T1, T2, T3, T4, T5, T6, T7, T8,
    223                                               T9, T10));
    224 } // end namespace detail
    225 
    226 // Won't work with references
    227 template<typename Function>
    228 struct function_traits
    229 {
    230   BOOST_STATIC_CONSTANT(unsigned, arity = (sizeof(boost::detail::function_arity_helper((Function*)0))-1));
    231 };
    232 
    233 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
    234 }
    235 
    236 #endif // BOOST_TT_FUNCTION_TRAITS_HPP_INCLUDED
    237