Changeset 345


Ignore:
Timestamp:
11/17/14 17:55:38 (11 years ago)
Author:
epyon
Message:
  • switching to VS2013
  • Variadic Templates used where applicable (a lot of code removed)
Location:
trunk/nv
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/type_traits.hh

    r319 r345  
    7979                };
    8080
    81                 template < typename R, typename T1 >
    82                 struct function_traits_impl< R (*)( T1 ) >
     81                template < typename R, typename... Args >
     82                struct function_traits_impl < R(*)(Args...) >
    8383                {
    84                         typedef R (*type)();
     84                        typedef R(*type)( Args... );
    8585                        typedef R        return_type;
    8686                        typedef void     class_type;
    87                         static const int arg_count = 1;
    88                 };
    89 
    90                 template < typename R, typename T1, typename T2 >
    91                 struct function_traits_impl< R (*)( T1, T2 ) >
    92                 {
    93                         typedef R (*type)();
    94                         typedef R        return_type;
    95                         typedef void     class_type;
    96                         static const int arg_count = 2;
    97                 };
    98 
    99                 template < typename R, typename T1, typename T2, typename T3 >
    100                 struct function_traits_impl< R (*)( T1, T2, T3 ) >
    101                 {
    102                         typedef R (*type)();
    103                         typedef R        return_type;
    104                         typedef void     class_type;
    105                         static const int arg_count = 3;
    106                 };
    107 
    108                 template < typename R, typename T1, typename T2, typename T3, typename T4 >
    109                 struct function_traits_impl< R (*)( T1, T2, T3, T4 ) >
    110                 {
    111                         typedef R (*type)();
    112                         typedef R        return_type;
    113                         typedef void     class_type;
    114                         static const int arg_count = 4;
     87                        static const int arg_count = sizeof...(Args);
    11588                };
    11689
     
    12497                };
    12598
    126                 template < class C, typename R, typename T1 >
    127                 struct function_traits_impl< R (C::*)( T1 ) >
     99                template < class C, typename R, typename... Args >
     100                struct function_traits_impl < R(C::*)(Args...) >
    128101                {
    129                         typedef R (*type)();
    130                         typedef R       return_type;
    131                         typedef C       class_type;
    132                         static const int arg_count = 1;
    133                 };
    134 
    135                 template < class C, typename R, typename T1, typename T2 >
    136                 struct function_traits_impl< R (C::*)( T1, T2 ) >
    137                 {
    138                         typedef R (*type)();
    139                         typedef R       return_type;
    140                         typedef C       class_type;
    141                         static const int arg_count = 2;
    142                 };
    143 
    144                 template < class C, typename R, typename T1, typename T2, typename T3 >
    145                 struct function_traits_impl< R (C::*)( T1, T2, T3 ) >
    146                 {
    147                         typedef R (*type)();
    148                         typedef R       return_type;
    149                         typedef C       class_type;
    150                         static const int arg_count = 3;
    151                 };
    152 
    153                 template < class C, typename R, typename T1, typename T2, typename T3, typename T4 >
    154                 struct function_traits_impl< R (C::*)( T1, T2, T3, T4 ) >
    155                 {
    156                         typedef R (*type)();
    157                         typedef R       return_type;
    158                         typedef C       class_type;
    159                         static const int arg_count = 4;
     102                        typedef R(C::*type)(Args...);
     103                        typedef R        return_type;
     104                        typedef C        class_type;
     105                        static const int arg_count = sizeof...(Args);
    160106                };
    161107
  • trunk/nv/lua/lua_dispatch.hh

    r319 r345  
    2929                namespace detail
    3030                {
     31                        template <typename...>
     32                        struct types {};
     33
    3134                        template < typename R >
    32                         struct dispatcher
     35                        struct call_and_push
    3336                        {
    34                                 static int call( lua_State* L, int, R (*func)() )
     37                                template <typename... Args, typename... Vs>
     38                                static void do_call(lua_State* L, R(*func)(Args...), Vs&&... vs)
    3539                                {
    36                                         push_value( L, func() ); return 1;
     40                                        push_value(L, func(std::forward<Vs>(vs)...));
    3741                                }
    38                                 template < typename T1 >
    39                                 static int call( lua_State* L, int index, R (*func)( T1 ) )
     42                                template <class C, typename... Args, typename... Vs>
     43                                static void do_call(lua_State* L, C& c, R(C::*func)(Args...), Vs&&... vs)
    4044                                {
    41                                         push_value( L, func( get_value<T1>( L, index + 0 ) ) ); return 1;
    42                                 }
    43                                 template < typename T1, typename T2 >
    44                                 static int call( lua_State* L, int index, R (*func)( T1, T2 ) )
    45                                 {
    46                                         push_value( L, func( get_value<T1>( L, index + 0 ),     get_value<T2>( L, index + 1 ) ) ); return 1;
    47                                 }
    48                                 template < typename T1, typename T2, typename T3 >
    49                                 static int call( lua_State* L, int index, R (*func)( T1, T2, T3 ) )
    50                                 {
    51                                         push_value( L, func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ) ); return 1;
    52                                 }
    53                                 template < typename T1, typename T2, typename T3, typename T4 >
    54                                 static int call( lua_State* L, int index, R (*func)( T1, T2, T3, T4 ) )
    55                                 {
    56                                         push_value( L, func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ) ); return 1;
    57                                 }
    58                                 template < class C >
    59                                 static int call( lua_State* L, int, C& c, R (C::*func)() )
    60                                 {
    61                                         push_value( L, (c.*func)() ); return 1;
    62                                 }
    63                                 template < class C, typename T1 >
    64                                 static int call( lua_State* L, int index, C& c, R (C::*func)( T1 ) )
    65                                 {
    66                                         push_value( L, (c.*func)( get_value<T1>( L, index + 0 ) ) ); return 1;
    67                                 }
    68                                 template < class C, typename T1, typename T2 >
    69                                 static int call( lua_State* L, int index, C& c, R (C::*func)( T1, T2 ) )
    70                                 {
    71                                         push_value( L, (c.*func)( get_value<T1>( L, index + 0 ),        get_value<T2>( L, index + 1 ) ) ); return 1;
    72                                 }
    73                                 template < class C, typename T1, typename T2, typename T3 >
    74                                 static int call( lua_State* L, int index, C& c, R (C::*func)( T1, T2, T3 ) )
    75                                 {
    76                                         push_value( L, (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ) ); return 1;
    77                                 }
    78                                 template < class C, typename T1, typename T2, typename T3, typename T4 >
    79                                 static int call( lua_State* L, int index, C& c, R (C::*func)( T1, T2, T3, T4 ) )
    80                                 {
    81                                         push_value( L, (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ) ); return 1;
     45                                        push_value(L, (c.*func)(std::forward<Vs>(vs)...));
    8246                                }
    8347                        };
    84                        
     48
    8549                        template <>
    86                         struct dispatcher< void >
     50                        struct call_and_push<void>
    8751                        {
    88                                 static int call( lua_State*, int, void (*func)() )
     52                                template <typename... Args, typename... Vs>
     53                                static void do_call(lua_State*, void(*func)(Args...), Vs&&... vs)
    8954                                {
    90                                         func(); return 0;
     55                                        func(std::forward<Vs>(vs)...);
    9156                                }
    92                                 template < typename T1 >
    93                                 static int call( lua_State* L, int index, void (*func)( T1 ) )
     57                                template <class C, typename... Args, typename... Vs>
     58                                static void do_call(lua_State*, C& c, void(C::*func)(Args...), Vs&&... vs)
    9459                                {
    95                                         func( get_value<T1>( L, index + 0 ) ); return 0;
     60                                        (c.*func)(std::forward<Vs>(vs)...);
    9661                                }
    97                                 template < typename T1, typename T2 >
    98                                 static int call( lua_State* L, int index, void (*func)( T1, T2 ) )
    99                                 {
    100                                         func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ) ); return 0;
    101                                 }
    102                                 template < typename T1, typename T2, typename T3 >
    103                                 static int call( lua_State* L, int index, void (*func)( T1, T2, T3 ) )
    104                                 {
    105                                         func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ); return 0;
    106                                 }
    107                                 template < typename T1, typename T2, typename T3, typename T4 >
    108                                 static int call( lua_State* L, int index, void (*func)( T1, T2, T3, T4 ) )
    109                                 {
    110                                         func( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ); return 0;
    111                                 }
    112                                 template < class C >
    113                                 static int call( lua_State*, int, C& c, void (C::*func)() )
    114                                 {
    115                                         (c.*func)(); return 0;
    116                                 }
    117                                 template < class C, typename T1 >
    118                                 static int call( lua_State* L, int index, C& c, void (C::*func)( T1 ) )
    119                                 {
    120                                         (c.*func)( get_value<T1>( L, index + 0 ) ); return 0;
    121                                 }
    122                                 template < class C, typename T1, typename T2 >
    123                                 static int call( lua_State* L, int index, C& c, void (C::*func)( T1, T2 ) )
    124                                 {
    125                                         (c.*func)( get_value<T1>( L, index + 0 ),       get_value<T2>( L, index + 1 ) ); return 0;
    126                                 }
    127                                 template < class C, typename T1, typename T2, typename T3 >
    128                                 static int call( lua_State* L, int index, C& c, void (C::*func)( T1, T2, T3 ) )
    129                                 {
    130                                         (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ) ); return 0;
    131                                 }
    132                                 template < class C, typename T1, typename T2, typename T3, typename T4 >
    133                                 static int call( lua_State* L, int index, C& c, void (C::*func)( T1, T2, T3, T4 ) )
    134                                 {
    135                                         (c.*func)( get_value<T1>( L, index + 0 ), get_value<T2>( L, index + 1 ), get_value<T3>( L, index + 2 ), get_value<T4>( L, index + 3 ) ); return 0;
    136                                 }
     62                        };
    13763
    138                         };
     64                        template <typename R, typename... Args, typename Head, typename ...Tail, typename... Vs>
     65                        int do_call(lua_State* L, int index, R(*func)(Args...), types<Head, Tail...>, Vs&&... vs)
     66                        {
     67                                do_call(L, index + 1, func, types < Tail... > {}, std::forward<Vs>(vs)..., get_value<Head>(L, index)); return 1;
     68                        }
     69
     70                        template <typename R, class C, typename... Args, typename Head, typename ...Tail, typename... Vs>
     71                        int do_call(lua_State* L, int index, C& c, R(C::*func)(Args...), types<Head, Tail...>, Vs&&... vs)
     72                        {
     73                                do_call(L, index + 1, c, func, types < Tail... > {}, std::forward<Vs>(vs)..., get_value<Head>(L, index)); return 1;
     74                        }
     75
     76                        template <typename R, typename... Args, typename... Vs>
     77                        int do_call(lua_State* L, int, R(*func)(Args...), types<>, Vs&&... vs)
     78                        {
     79                                call_and_push<R>::do_call(L, func, std::forward<Vs>(vs)...);
     80                                return 1;
     81                        }
     82
     83                        template <typename R, class C, typename... Args, typename... Vs>
     84                        int do_call(lua_State* L, int, C& c, R(C::*func)(Args...), types<>, Vs&&... vs)
     85                        {
     86                                call_and_push<R>::do_call(L, c, func, std::forward<Vs>(vs)...);
     87                                return 1;
     88                        }
     89
     90                        template < typename R, typename... Args >
     91                        int dispatch(lua_State* L, int index, R(*func)(Args...))
     92                        {
     93                                do_call(L, index, func, types < Args... > {});
     94                                return 1;
     95                        }
     96                        template < typename R, class C, typename... Args >
     97                        int dispatch(lua_State* L, int index, C& c, R(C::*func)(Args...))
     98                        {
     99                                do_call(L, index, c, func, types < Args... > {});
     100                                return 1;
     101                        }
    139102
    140103                        template < typename F, F f >
    141104                        int function_wrapper( lua_State* L )
    142105                        {
    143                                 return dispatcher<typename return_type<F>::type>::call( L, 1, f );
     106                                return dispatch(L, 1, f);
    144107                        }
    145108
     
    148111                        {
    149112                                C* c = (C*)to_ref_object( L, 1 );
    150                                 return dispatcher<typename return_type<F>::type>::call( L, 2, *c, f );
     113                                return dispatch( L, 2, *c, f );
    151114                        }
    152115
     
    155118                        {
    156119                                C* c = (C*)to_pointer( L, nv::lua::detail::upvalue_index(1) );
    157                                 return dispatcher<typename return_type<F>::type>::call( L, 1, *c, f );
     120                                return dispatch( L, 1, *c, f );
    158121                        }
    159122                       
    160                         template< typename F >
    161                         int call( lua_State* L, int index, F func )
    162                         {
    163                                 return dispatcher<typename return_type<F>::type>::call( L, index, func );
    164                         }
    165 
    166                         template< class C, typename F >
    167                         int call( lua_State* L, int index, C& c, F func )
    168                         {
    169                                 return dispatcher<typename return_type<F>::type>::call( L, index, c, func );
    170                         }
    171 
    172123                }
    173124
  • trunk/nv/lua/lua_function.hh

    r319 r345  
    5151                        }
    5252
    53                         template <typename T1>
    54                         R operator()( const T1& p1 )
     53                        template <typename... Args>
     54                        R operator()( Args&&... args )
    5555                        {
    5656                                retrieve();
    57                                 detail::push_value( L, p1 );
    58                                 return call_result<R>(1);
     57                                detail::push_values(L, std::forward<Args>(args)...);
     58                                return call_result<R>(sizeof...(Args));
    5959                        }
    6060
    61                         template <typename T1, typename T2>
    62                         R operator()(const T1& p1, const T2& p2)
    63                         {
    64                                 retrieve();
    65                                 detail::push_values( L, p1, p2 );
    66                                 return call_result<R>(2);
    67                         }
    68 
    69                         template <typename T1, typename T2, typename T3>
    70                         R operator()(const T1& p1, const T2& p2, const T3& p3)
    71                         {
    72                                 retrieve();
    73                                 detail::push_values( L, p1, p2, p3 );
    74                                 return call_result<R>(3);
    75                         }
    7661                protected:
    7762                        template< typename RR >
  • trunk/nv/lua/lua_path.hh

    r319 r345  
    8989                        bool resolve( lua_State* L, bool global = true ) const;
    9090                private:
     91
    9192                        void parse();
    9293                        void push( size_t start, size_t length );
  • trunk/nv/lua/lua_state.hh

    r341 r345  
    5555                                return R();
    5656                        }
    57                         template < typename R, typename T1 >
    58                         R call( const path& p, const T1& p1 )
    59                         {
    60                                 if ( push_function( p, m_global ) )
    61                                 {
    62                                         detail::push_value( m_state, p1 );
    63                                         if ( call_function( 1, detail::return_count< R >::value ) == 0 )
     57                        template < typename R, typename... Args >
     58                        R call(const path& p, Args&&... args )
     59                        {
     60                                if (push_function(p, m_global))
     61                                {
     62                                        detail::push_values(m_state, std::forward<Args>(args)...);
     63                                        if (call_function(sizeof...(Args), detail::return_count< R >::value) == 0)
    6464                                        {
    65                                                 return detail::pop_return_value<R>( m_state );
    66                                         }
    67                                 }
    68                                 return R();
    69                         }
    70                         template < typename R, typename T1, typename T2 >
    71                         R call( const path& p, const T1& p1, const T2& p2 )
    72                         {
    73                                 if ( push_function( p, m_global ) )
    74                                 {
    75                                         detail::push_values( m_state, p1, p2 );
    76                                         if ( call_function( 2, detail::return_count< R >::value ) == 0 )
    77                                         {
    78                                                 return detail::pop_return_value<R>( m_state );
    79                                         }
    80                                 }
    81                                 return R();
    82                         }
    83                         template < typename R, typename T1, typename T2, typename T3 >
    84                         R call( const path& p, const T1& p1, const T2& p2, const T3& p3 )
    85                         {
    86                                 if ( push_function( p, m_global ) )
    87                                 {
    88                                         detail::push_values( m_state, p1, p2, p3 );
    89                                         if ( call_function( 3, detail::return_count< R >::value ) == 0 )
    90                                         {
    91                                                 return detail::pop_return_value<R>( m_state );
    92                                         }
    93                                 }
    94                                 return R();
    95                         }
    96                         template < typename R, typename T1, typename T2, typename T3, typename T4 >
    97                         R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 )
    98                         {
    99                                 if ( push_function( p, m_global ) )
    100                                 {
    101                                         detail::push_value( m_state, p1, p2, p3, p4 );
    102                                         if ( call_function( 4, detail::return_count< R >::value ) == 0 )
    103                                         {
    104                                                 return detail::pop_return_value<R>( m_state );
     65                                                return detail::pop_return_value<R>(m_state);
    10566                                        }
    10667                                }
     
    306267                                return R();
    307268                        }
    308                         template < typename R, typename T1 >
    309                         R call( ref table, const path& p, const T1& p1 )
     269                        template < typename R, typename... Args >
     270                        R call( ref table, const path& p, Args&&... args )
    310271                        {
    311272                                stack_guard guard( this ); // TODO: remove
     
    313274                                if ( push_function( p, false ) )
    314275                                {
    315                                         detail::push_value( m_state, p1 );
    316                                         if ( call_function( 1, detail::return_count< R >::value ) == 0 )
    317                                         {
    318                                                 return detail::pop_return_value<R>( m_state );
    319                                         }
    320                                 }
    321                                 return R();
    322                         }
    323                         template < typename R, typename T1, typename T2 >
    324                         R call( ref table, const path& p, const T1& p1, const T2& p2 )
    325                         {
    326                                 stack_guard guard( this ); // TODO: remove
    327                                 detail::push_ref_object( m_state, table );
    328                                 if ( push_function( p, false ) )
    329                                 {
    330                                         detail::push_values( m_state, p1, p2 );
    331                                         if ( call_function( 2, detail::return_count< R >::value ) == 0 )
    332                                         {
    333                                                 return detail::pop_return_value<R>( m_state );
    334                                         }
    335                                 }
    336                                 return R();
    337                         }
    338                         template < typename R, typename T1, typename T2, typename T3 >
    339                         R call( ref table, const path& p, const T1& p1, const T2& p2, const T3& p3 )
    340                         {
    341                                 stack_guard guard( this ); // TODO: remove
    342                                 detail::push_ref_object( m_state, table );
    343                                 if ( push_function( p, false ) )
    344                                 {
    345                                         detail::push_values( m_state, p1, p2, p3 );
    346                                         if ( call_function( 3, detail::return_count< R >::value ) == 0 )
    347                                         {
    348                                                 return detail::pop_return_value<R>( m_state );
    349                                         }
    350                                 }
    351                                 return R();
    352                         }
    353                         template < typename R, typename T1, typename T2, typename T3, typename T4 >
    354                         R call( ref table, const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 )
    355                         {
    356                                 stack_guard guard( this ); // TODO: remove
    357                                 detail::push_ref_object( m_state, table );
    358                                 if ( push_function( p, false ) )
    359                                 {
    360                                         detail::push_value( m_state, p1, p2, p3, p4 );
    361                                         if ( call_function( 4, detail::return_count< R >::value ) == 0 )
     276                                        detail::push_values( m_state, std::forward<Args>(args)... );
     277                                        if (call_function(sizeof...(Args), detail::return_count< R >::value) == 0)
    362278                                        {
    363279                                                return detail::pop_return_value<R>( m_state );
  • trunk/nv/lua/lua_values.hh

    r319 r345  
    213213                        }
    214214
    215                         template < typename T1 >
    216                         void push_values( lua_State *L, const T1& p1 ) { push_value( L, p1 ); }
    217                         template < typename T1, typename T2 >
    218                         void push_values( lua_State *L, const T1& p1, const T2& p2 ) { push_value( L, p1 ); push_value( L, p2 ); }
    219                         template < typename T1, typename T2, typename T3 >
    220                         void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); }
    221                         template < typename T1, typename T2, typename T3, typename T4 >
    222                         void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); push_value( L, p4 ); }
    223                         template < typename T1, typename T2, typename T3, typename T4, typename T5 >
    224                         void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); push_value( L, p4 ); push_value( L, p5 ); }
     215                        template < typename T >
     216                        void push_values(lua_State *L, T&& p)
     217                        {
     218                                push_value( L, std::forward<T>(p) );
     219                        }
     220                        template < typename T, typename ...Ts >
     221                        void push_values(lua_State *L, T&& p, Ts&& ...ps)
     222                        {
     223                                push_value(L, std::forward<T>(p));
     224                                push_values(L, std::forward<Ts>(ps)...);
     225                        }
     226
    225227
    226228                        template < typename T >
Note: See TracChangeset for help on using the changeset viewer.