Changeset 449
- Timestamp:
- 07/30/15 14:06:24 (10 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/lua/lua_area.hh
r406 r449 20 20 void register_area( lua_State* L ); 21 21 22 template<> 23 struct pass_traits< rectangle > : metatable_pass_traits< rectangle > 24 { 25 static const char* metatable() { return "area"; } 26 }; 27 22 28 namespace detail 23 29 { 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 ); } 29 34 30 35 inline bool is_coord( lua_State* L, int index ) { return is_vec<ivec2>( L, index ); } … … 32 37 inline ivec2* to_pcoord( lua_State* L, int index ) { return to_pvec<ivec2>( L, index ); } 33 38 inline void push_coord( lua_State* L, const ivec2& v ) { push_vec<ivec2>( L, v ); } 34 35 39 } 36 40 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 };43 41 } 44 42 -
trunk/nv/lua/lua_dispatch.hh
r406 r449 19 19 namespace lua 20 20 { 21 typedef int (__cdecl *lfunction) (struct lua_State *L);22 21 23 22 namespace detail -
trunk/nv/lua/lua_glm.hh
r406 r449 18 18 void register_glm( lua_State* L ); 19 19 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 20 27 namespace detail 21 28 { 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 30 29 template< typename T > 31 30 bool is_vec( lua_State* L, int index ) 32 31 { 33 return is_userdata( L, index, detail::glm_metatable_name<T>() );32 return is_userdata( L, index, pass_traits<T>::metatable() ); 34 33 } 35 34 … … 37 36 T to_vec( lua_State* L, int index ) 38 37 { 39 return *reinterpret_cast<T*>( check_userdata( L, index, detail::glm_metatable_name<T>() ));38 return pass_traits<T>::to( L, index ); 40 39 } 41 40 … … 43 42 T to_vec( lua_State* L, int index, const T& def ) 44 43 { 45 return ( is_vec<T>( L, index ) ? *reinterpret_cast<T*>( to_pointer( L, index ) ) :def );44 return pass_traits<T>::to( L, index, def ); 46 45 } 47 46 … … 49 48 T* to_pvec( lua_State* L, int index ) 50 49 { 51 return reinterpret_cast<T*>( check_userdata( L, index, detail::glm_metatable_name<T>() ));50 return to_userdata<T>( L, index ); 52 51 } 53 52 … … 55 54 void push_vec( lua_State* L, const T& v ) 56 55 { 57 p ush_userdata( L, v, detail::glm_metatable_name<T>());56 pass_traits<T>::push( L, v ); 58 57 } 58 59 59 } 60 60 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 };87 61 } 88 62 -
trunk/nv/lua/lua_values.hh
r445 r449 35 35 typedef unsigned long lunsigned; 36 36 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; 65 42 66 43 namespace detail … … 74 51 void push_bool ( lua_State *L, bool v ); 75 52 void push_string_view( lua_State *L, string_view s ); 76 void push_cstring ( lua_State *L, const char* s );77 53 void push_pointer ( lua_State *L, void* p ); 78 54 void push_ref_object ( lua_State *L, ref object ); … … 82 58 lnumber to_number ( lua_State *L, int index ); 83 59 bool to_bool ( lua_State *L, int index ); 84 // const char* to_cstring ( lua_State *L, int index );85 60 string_view to_string_view( lua_State *L, int index ); 86 61 void* to_pointer ( lua_State *L, int index ); … … 91 66 lnumber to_number ( lua_State *L, int index, lnumber def ); 92 67 bool to_bool ( lua_State *L, int index, bool def ); 93 // const char* to_cstring ( lua_State *L, int index, const char* def );94 68 string_view to_string_view( lua_State *L, int index, string_view def ); 95 69 void* to_pointer ( lua_State *L, int index, void* def ); … … 98 72 void pop_and_discard( lua_State *L, int count ); 99 73 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 102 77 template <typename T> 103 78 void push_userdata( lua_State *L, const T& v, const char* metatable ) 104 79 { 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() ) ); 106 93 } 107 94 } 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 }; 108 116 109 117 template <> … … 139 147 }; 140 148 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 149 149 template <> 150 150 struct pass_traits < string_view > … … 163 163 }; 164 164 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 ) ); } 171 183 }; 172 184 … … 181 193 { 182 194 183 template <typename T, class E NABLE= void >195 template <typename T, class Enable = void > 184 196 struct lua_type_impl 185 197 { … … 240 252 void push_values(lua_State *L, T&& p) 241 253 { 242 push_value( L, forward<T>(p) );254 push_value( L, ::nv::forward<T>(p) ); 243 255 } 244 256 template < typename T, typename ...Ts > 245 257 void push_values(lua_State *L, T&& p, Ts&& ...ps) 246 258 { 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)...); 249 261 } 250 262 … … 276 288 } 277 289 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 ) 279 291 { 280 292 T ret = static_cast<T>( pass_traits< converted_type_t<T> >::to( L, -1, def ) ); -
trunk/src/lua/lua_area.cc
r395 r449 9 9 #include "nv/lua/lua_raw.hh" 10 10 #include "nv/core/random.hh" 11 12 const char* nv::lua::detail::AREA_METATABLE = "area";13 11 14 12 using nv::lua::detail::is_coord; … … 446 444 }; 447 445 448 luaL_newmetatable( L, nv::lua:: detail::AREA_METATABLE);446 luaL_newmetatable( L, nv::lua::pass_traits< nv::rectangle >::metatable() ); 449 447 nlua_register( L, nlua_area_m, -1 ); 450 448 lua_createtable( L, 0, 0 ); -
trunk/src/lua/lua_glm.cc
r406 r449 339 339 }; 340 340 341 luaL_newmetatable( L, nv::lua:: detail::glm_metatable_name<T>() );341 luaL_newmetatable( L, nv::lua::pass_traits<T>::metatable() ); 342 342 nlua_register( L, nlua_vec_m, -1 ); 343 343 lua_createtable( L, 0, 0 ); -
trunk/src/lua/lua_raw.cc
r441 r449 24 24 { 25 25 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 ) ) ); 27 27 return buffer; 28 28 } … … 30 30 { 31 31 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 ) ) ); 33 33 return buffer; 34 34 } -
trunk/src/lua/lua_values.cc
r445 r449 23 23 } 24 24 25 void* nv::lua::detail:: check_userdata( lua_State *L, int index, const char* metatable )25 void* nv::lua::detail::raw_check_userdata( lua_State *L, int index, const char* metatable ) 26 26 { 27 27 return luaL_checkudata( L, index, metatable ); 28 28 } 29 29 30 void* nv::lua::detail:: allocate_userdata( lua_State *L, size_t size, const char* metatable )30 void* nv::lua::detail::raw_allocate_userdata( lua_State *L, size_t size, const char* metatable ) 31 31 { 32 32 void* result = lua_newuserdata(L, size); … … 63 63 { 64 64 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 );70 65 } 71 66 … … 105 100 return lua_toboolean( L, index ) != 0; 106 101 } 107 108 // const char* nv::lua::detail::to_cstring ( lua_State *L, int index )109 // {110 // return lua_tolstring( L, index, nullptr );111 // }112 102 113 103 nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index ) … … 164 154 } 165 155 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 171 156 void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def ) 172 157 {
Note: See TracChangeset
for help on using the changeset viewer.