Changeset 449


Ignore:
Timestamp:
07/30/15 14:06:24 (10 years ago)
Author:
epyon
Message:
  • metatable types cleanup
  • fix for nlua_typecontent
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/lua/lua_area.hh

    r406 r449  
    2020                void register_area( lua_State* L );
    2121
     22                template<>
     23                struct pass_traits< rectangle > : metatable_pass_traits< rectangle >
     24                {
     25                        static const char* metatable() { return "area"; }
     26                };
     27
    2228                namespace detail
    2329                {
    24                         extern const char* AREA_METATABLE;
    25                         inline bool is_area( lua_State* L, int index ) { return is_userdata( L, index, AREA_METATABLE ); }
    26                         inline rectangle to_area( lua_State* L, int index ) { return *reinterpret_cast<rectangle*>( check_userdata( L, index, AREA_METATABLE ) ); }
    27                         inline rectangle* to_parea( lua_State* L, int index ) { return reinterpret_cast<rectangle*>( check_userdata( L, index, AREA_METATABLE ) ); }
    28                         inline void push_area( lua_State* L, const rectangle& v ) { push_userdata( L, v, AREA_METATABLE ); }
     30                        inline bool is_area( lua_State* L, int index ) { return is_userdata( L, index, pass_traits<rectangle>::metatable() ); }
     31                        inline rectangle to_area( lua_State* L, int index ) { return pass_traits<rectangle>::to( L, index ); }
     32                        inline rectangle* to_parea( lua_State* L, int index ) { return to_userdata<rectangle>( L, index ); }
     33                        inline void push_area( lua_State* L, const rectangle& v ) { pass_traits<rectangle>::push( L, v ); }
    2934
    3035                        inline bool is_coord( lua_State* L, int index ) { return is_vec<ivec2>( L, index ); }
     
    3237                        inline ivec2* to_pcoord( lua_State* L, int index ) { return to_pvec<ivec2>( L, index ); }
    3338                        inline void push_coord( lua_State* L, const ivec2& v ) { push_vec<ivec2>( L, v ); }
    34 
    3539                }
    3640
    37                 template<>
    38                 struct pass_traits< rectangle >
    39                 {
    40                         static void push( lua_State *L, const rectangle& p ) { detail::push_area( L, p ); }
    41                         static rectangle to( lua_State *L, int index ) { return detail::to_area( L, index ); }
    42                 };
    4341        }
    4442
  • trunk/nv/lua/lua_dispatch.hh

    r406 r449  
    1919        namespace lua
    2020        {
    21                 typedef int (__cdecl *lfunction) (struct lua_State *L);
    2221
    2322                namespace detail
  • trunk/nv/lua/lua_glm.hh

    r406 r449  
    1818                void register_glm( lua_State* L );
    1919
     20                template<> struct pass_traits< vec2 >  : metatable_pass_traits< vec2 >  { static const char* metatable() { return "vec2"; } };
     21                template<> struct pass_traits< vec3 >  : metatable_pass_traits< vec3 >  { static const char* metatable() { return "vec3"; } };
     22                template<> struct pass_traits< vec4 >  : metatable_pass_traits< vec4 >  { static const char* metatable() { return "vec4"; } };
     23                template<> struct pass_traits< ivec2 > : metatable_pass_traits< ivec2 > { static const char* metatable() { return "ivec2"; } };
     24                template<> struct pass_traits< ivec3 > : metatable_pass_traits< ivec3 > { static const char* metatable() { return "ivec2"; } };
     25                template<> struct pass_traits< ivec4 > : metatable_pass_traits< ivec4 > { static const char* metatable() { return "ivec2"; } };
     26
    2027                namespace detail
    2128                {
    22                         template< typename T > inline const char* glm_metatable_name() { static_assert(sizeof(T) == 0, "Type not implemented!"); return NULL; }
    23                         template<> inline const char* glm_metatable_name< nv::ivec2 >() { return "ivec2"; }
    24                         template<> inline const char* glm_metatable_name< nv::ivec3 >() { return "ivec3"; }
    25                         template<> inline const char* glm_metatable_name< nv::ivec4 >() { return "ivec4"; }
    26                         template<> inline const char* glm_metatable_name< nv::vec2  >() { return "vec2"; }
    27                         template<> inline const char* glm_metatable_name< nv::vec3  >() { return "vec3"; }
    28                         template<> inline const char* glm_metatable_name< nv::vec4  >() { return "vec4"; }
    29 
    3029                        template< typename T >
    3130                        bool is_vec( lua_State* L, int index )
    3231                        {
    33                                 return is_userdata( L, index, detail::glm_metatable_name<T>() );
     32                                return is_userdata( L, index, pass_traits<T>::metatable() );
    3433                        }
    3534
     
    3736                        T to_vec( lua_State* L, int index )
    3837                        {
    39                                 return *reinterpret_cast<T*>( check_userdata( L, index, detail::glm_metatable_name<T>() ) );
     38                                return pass_traits<T>::to( L, index );
    4039                        }
    4140
     
    4342                        T to_vec( lua_State* L, int index, const T& def )
    4443                        {
    45                                 return ( is_vec<T>( L, index ) ? *reinterpret_cast<T*>( to_pointer( L, index ) ) : def );
     44                                return pass_traits<T>::to( L, index, def );
    4645                        }
    4746
     
    4948                        T* to_pvec( lua_State* L, int index )
    5049                        {
    51                                 return reinterpret_cast<T*>( check_userdata( L, index, detail::glm_metatable_name<T>() ) );
     50                                return to_userdata<T>( L, index );
    5251                        }
    5352
     
    5554                        void push_vec( lua_State* L, const T& v )
    5655                        {
    57                                 push_userdata( L, v, detail::glm_metatable_name<T>() );
     56                                pass_traits<T>::push( L, v );
    5857                        }
     58
    5959                }
    6060
    61                 template< typename T >
    62                 struct pass_traits< tvec2<T> >
    63                 {
    64                         typedef tvec2<T> value_type;
    65                         static void push( lua_State *L, const value_type& p ) { detail::push_vec< value_type >( L, p ); }
    66                         static value_type to( lua_State *L, int index ) { return detail::to_vec< value_type >( L, index ); }
    67                         static value_type to( lua_State *L, int index, const value_type& def ) { return detail::to_vec< value_type >( L, index, def ); }
    68                 };
    69 
    70                 template< typename T >
    71                 struct pass_traits< tvec3<T> >
    72                 {
    73                         typedef tvec3<T> value_type;
    74                         static void push( lua_State *L, const value_type& p ) { detail::push_vec< value_type >( L, p ); }
    75                         static value_type to( lua_State *L, int index ) { return detail::to_vec< value_type >( L, index ); }
    76                         static value_type to( lua_State *L, int index, const value_type& def ) { return detail::to_vec< value_type >( L, index, def ); }
    77                 };
    78 
    79                 template< typename T >
    80                 struct pass_traits< tvec4<T> >
    81                 {
    82                         typedef tvec4<T> value_type;
    83                         static void push( lua_State *L, const value_type& p ) { detail::push_vec< value_type >( L, p ); }
    84                         static value_type to( lua_State *L, int index ) { return detail::to_vec< value_type >( L, index ); }
    85                         static value_type to( lua_State *L, int index, const value_type& def ) { return detail::to_vec< value_type >( L, index, def ); }
    86                 };
    8761        }
    8862
  • trunk/nv/lua/lua_values.hh

    r445 r449  
    3535                typedef unsigned long lunsigned;
    3636                typedef double        lnumber;
    37 
    38 //              struct passer
    39 //              {
    40 //                      virtual void push( lua_State *L ) const = 0;
    41 //                      virtual ~passer(){}
    42 //              };
    43 //
    44 //              template < typename T >
    45 //              struct returner
    46 //              {
    47 //                      returner( lua_State *, int ) : value() {}
    48 //                      returner( lua_State *, int, const T& ) : value() {}
    49 //                      operator T() { return value; }
    50 //                      operator const T() const { return value; }
    51 //                      virtual returner<T> to( lua_State *L, int index ) = 0;
    52 //                      virtual returner<T> to( lua_State *L, int index, const T& def ) = 0;
    53 //                      virtual ~returner(){}
    54 //              protected:
    55 //                      T value;
    56 //              };
    57 
    58                 template < typename T >
    59                 struct pass_traits
    60                 {
    61                         static void push( lua_State *L, const T& p ) { p.push( L ); }
    62                         static T to( lua_State *L, int index ) { return T( L, index ); }
    63                         static T to( lua_State *L, int index, const T& def ) { return T( L, index, def ); }
    64                 };
     37                typedef int( __cdecl *lfunction ) ( struct lua_State *L );
     38
     39
     40                template < typename T, class Enable = void >
     41                struct pass_traits;
    6542
    6643                namespace detail
     
    7451                        void push_bool       ( lua_State *L, bool v );
    7552                        void push_string_view( lua_State *L, string_view s );
    76                         void push_cstring    ( lua_State *L, const char* s );
    7753                        void push_pointer    ( lua_State *L, void* p );
    7854                        void push_ref_object ( lua_State *L, ref object );
     
    8258                        lnumber     to_number     ( lua_State *L, int index );
    8359                        bool        to_bool       ( lua_State *L, int index );
    84 //                      const char* to_cstring    ( lua_State *L, int index );
    8560                        string_view to_string_view( lua_State *L, int index );
    8661                        void*       to_pointer    ( lua_State *L, int index );
     
    9166                        lnumber     to_number     ( lua_State *L, int index, lnumber def );
    9267                        bool        to_bool       ( lua_State *L, int index, bool def );
    93 //                      const char* to_cstring    ( lua_State *L, int index, const char* def );
    9468                        string_view to_string_view( lua_State *L, int index, string_view def );
    9569                        void*       to_pointer    ( lua_State *L, int index, void* def );
     
    9872                        void pop_and_discard( lua_State *L, int count );
    9973                        bool is_userdata( lua_State *L, int index, const char* metatable );
    100                         void* check_userdata( lua_State *L, int index, const char* metatable );
    101                         void* allocate_userdata( lua_State *L, size_t size, const char* metatable );
     74                        void* raw_check_userdata( lua_State *L, int index, const char* metatable );
     75                        void* raw_allocate_userdata( lua_State *L, size_t size, const char* metatable );
     76
    10277                        template <typename T>
    10378                        void push_userdata( lua_State *L, const T& v, const char* metatable )
    10479                        {
    105                                 new (allocate_userdata(L, sizeof(T), metatable)) T(v);
     80                                new ( raw_allocate_userdata( L, sizeof( T ), metatable ) ) T( v );
     81                        }
     82
     83                        template <typename T>
     84                        T* to_userdata( lua_State *L, int index, const char* metatable )
     85                        {
     86                                return reinterpret_cast<T*>( raw_check_userdata( L, index, metatable ) );
     87                        }
     88
     89                        template <typename T>
     90                        T* to_userdata( lua_State *L, int index )
     91                        {
     92                                return reinterpret_cast<T*>( raw_check_userdata( L, index, pass_traits<T>::metatable() ) );
    10693                        }
    10794                }
     95
     96                template < typename T >
     97                struct metatable_pass_traits
     98                {
     99                        typedef T value_type;
     100                        static void push( lua_State *L, const value_type& v )
     101                        {
     102                                detail::push_userdata< value_type >( L, v, pass_traits< value_type >::metatable() );
     103                        }
     104                        static value_type to( lua_State *L, int index )
     105                        {
     106                                // TODO: ASSERT?
     107                                value_type* result = detail::to_userdata< value_type >( L, index, pass_traits< value_type >::metatable() );
     108                                return result ? *result : value_type();
     109                        }
     110                        static value_type to( lua_State *L, int index, const value_type& def )
     111                        {
     112                                value_type* result = detail::to_userdata< value_type >( L, index, pass_traits< value_type >::metatable() );
     113                                return result ? *result : def;
     114                        }
     115                };
    108116
    109117                template <>
     
    139147                };
    140148
    141 //              template <>
    142 //              struct pass_traits<const char*>
    143 //              {
    144 //                      static void push( lua_State *L, const char* s ) { detail::push_cstring( L, s ); }
    145 //                      static const char* to( lua_State *L, int index ) { return detail::to_cstring( L, index ); }
    146 //                      static const char* to( lua_State *L, int index, const char* def ) { return detail::to_cstring( L, index, def ); }
    147 //              };
    148 
    149149                template <>
    150150                struct pass_traits < string_view >
     
    163163                };
    164164
    165                 template <>
    166                 struct pass_traits < string128 >
    167                 {
    168                         static void push( lua_State *L, const string128& s ) { detail::push_string_view( L, s ); }
    169                         static string128 to( lua_State *L, int index ) { return string128( detail::to_string_view( L, index ) ); }
    170                         static string128 to( lua_State *L, int index, const string_view& def ) { return string128( detail::to_string_view( L, index, def ) ); }
     165                // TODO: might be better to pass only string_views?
     166                //       or maybe make them all decay to them?
     167                template < size_t N >
     168                struct pass_traits < short_string<N> >
     169                {
     170                        typedef short_string<N> value_type;
     171                        static void push( lua_State *L, const value_type& s ) { detail::push_string_view( L, s ); }
     172                        static value_type to( lua_State *L, int index ) { return value_type( detail::to_string_view( L, index ) ); }
     173                        static value_type to( lua_State *L, int index, const string_view& def ) { return value_type( detail::to_string_view( L, index, def ) ); }
     174                };
     175
     176                template <>
     177                struct pass_traits < string_buffer >
     178                {
     179                        typedef string_buffer value_type;
     180                        static void push( lua_State *L, const value_type& s ) { detail::push_string_view( L, s ); }
     181                        static value_type to( lua_State *L, int index ) { return value_type( detail::to_string_view( L, index ) ); }
     182                        static value_type to( lua_State *L, int index, const string_view& def ) { return value_type( detail::to_string_view( L, index, def ) ); }
    171183                };
    172184
     
    181193                {
    182194
    183                         template <typename T, class ENABLE = void >
     195                        template <typename T, class Enable = void >
    184196                        struct lua_type_impl
    185197                        {
     
    240252                        void push_values(lua_State *L, T&& p)
    241253                        {
    242                                 push_value( L, forward<T>(p) );
     254                                push_value( L, ::nv::forward<T>(p) );
    243255                        }
    244256                        template < typename T, typename ...Ts >
    245257                        void push_values(lua_State *L, T&& p, Ts&& ...ps)
    246258                        {
    247                                 push_value(L, forward<T>(p));
    248                                 push_values(L, forward<Ts>(ps)...);
     259                                push_value(L, ::nv::forward<T>(p));
     260                                push_values(L, ::nv::forward<Ts>(ps)...);
    249261                        }
    250262
     
    276288                        }
    277289                        template < typename T >
    278                         inline converted_type_t<T> pop_return_value( lua_State *L, const T& def )
     290                        inline T pop_return_value( lua_State *L, const T& def )
    279291                        {
    280292                                T ret = static_cast<T>( pass_traits< converted_type_t<T> >::to( L, -1, def ) );
  • trunk/src/lua/lua_area.cc

    r395 r449  
    99#include "nv/lua/lua_raw.hh"
    1010#include "nv/core/random.hh"
    11 
    12 const char* nv::lua::detail::AREA_METATABLE = "area";
    1311
    1412using nv::lua::detail::is_coord;
     
    446444        };
    447445
    448         luaL_newmetatable( L, nv::lua::detail::AREA_METATABLE );
     446        luaL_newmetatable( L, nv::lua::pass_traits< nv::rectangle >::metatable() );
    449447        nlua_register( L, nlua_area_m, -1 );
    450448        lua_createtable( L, 0, 0 );
  • trunk/src/lua/lua_glm.cc

    r406 r449  
    339339        };
    340340
    341         luaL_newmetatable( L, nv::lua::detail::glm_metatable_name<T>() );
     341        luaL_newmetatable( L, nv::lua::pass_traits<T>::metatable() );
    342342        nlua_register( L, nlua_vec_m, -1 );
    343343        lua_createtable( L, 0, 0 );
  • trunk/src/lua/lua_raw.cc

    r441 r449  
    2424        {
    2525                nv::string64 buffer;
    26                 size_t l = nv::uint64_to_buffer( buffer, nv::uint64( lua_touserdata( L, idx ) ) );
     26                buffer.append( nv::uint64( lua_touserdata( L, idx ) ) );
    2727                return buffer;
    2828        }
     
    3030        {
    3131                nv::string64 buffer;
    32                 size_t l = nv::f64_to_buffer( buffer, lua_tonumber( L, idx ) );
     32                buffer.append( nv::uint64( lua_touserdata( L, idx ) ) );
    3333                return buffer;
    3434        }
  • trunk/src/lua/lua_values.cc

    r445 r449  
    2323}
    2424
    25 void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
     25void* nv::lua::detail::raw_check_userdata( lua_State *L, int index, const char* metatable )
    2626{
    2727        return luaL_checkudata( L, index, metatable );
    2828}
    2929
    30 void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
     30void* nv::lua::detail::raw_allocate_userdata( lua_State *L, size_t size, const char* metatable )
    3131{
    3232        void* result = lua_newuserdata(L, size);
     
    6363{
    6464        lua_pushboolean( L, v );
    65 }
    66 
    67 void nv::lua::detail::push_cstring ( lua_State *L, const char* s )
    68 {
    69         lua_pushstring( L, s );
    7065}
    7166
     
    105100        return lua_toboolean( L, index ) != 0;
    106101}
    107 
    108 // const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
    109 // {
    110 //      return lua_tolstring( L, index, nullptr );
    111 // }
    112102
    113103nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index )
     
    164154}
    165155
    166 // const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
    167 // {
    168 //      return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
    169 // }
    170 
    171156void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
    172157{
Note: See TracChangeset for help on using the changeset viewer.