Changeset 262
- Timestamp:
- 06/19/14 00:02:55 (11 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/common.hh
r261 r262 143 143 namespace nv 144 144 { 145 class object;146 class root;147 class type_database;148 145 class uid_store; 149 146 -
trunk/nv/io_event.hh
r214 r262 15 15 16 16 #include <nv/common.hh> 17 #include <nv/types.hh> 17 18 18 19 namespace nv -
trunk/nv/lua/lua_dispatch.hh
r253 r262 147 147 int object_method_wrapper( lua_State* L ) 148 148 { 149 C* c = (C*)to_ object( L, 1 );149 C* c = (C*)to_ref_object( L, 1 ); 150 150 return dispatcher<typename return_type<F>::type>::call( L, 2, *c, f ); 151 151 } -
trunk/nv/lua/lua_state.hh
r256 r262 16 16 #include <map> 17 17 #include <nv/flags.hh> 18 #include <nv/types.hh>19 18 20 19 #include <nv/lua/lua_path.hh> … … 191 190 explicit state( bool load_libs = false ); 192 191 explicit state( lua_State* state ); 193 virtual ~state();194 192 bool do_string( const std::string& code, const std::string& name, int rvalues = 0 ); 195 193 bool do_stream( std::istream& stream, const std::string& name ); … … 198 196 void log_stack(); 199 197 lua_State* get_raw(); 200 reference register_object( object * o ); 201 reference register_object( object * o, const char* lua_name ); 202 reference register_proto( object * o, const char* storage ); 203 204 template< typename TYPE > 205 type_entry& register_type( const char* name ) 206 { 207 m_type_database->create_type<TYPE>( name ); 208 } 209 210 template< typename TYPE, int SIZE > 211 void register_enum( const char* name, const char* prefix, type_enum (&init_enums)[SIZE] ) 212 { 213 m_type_database->create_type<TYPE>( name ).enums( init_enums ); 214 register_enum_values( name, prefix ); 215 } 216 void register_enum_values( const std::string& name, const std::string& prefix = std::string() ); 217 218 void store_metadata( object* o, const std::string& metaname, void* pointer ); 219 void unregister_object( object * o ); // DELETE ME 220 void unregister_object( int index ); 198 reference register_object( void* o, const char* lua_name ); 199 reference register_proto( const char* id, const char* storage ); 200 void store_metadata( reference object_index, const std::string& metaname, void* pointer ); 201 void unregister_object( reference object_index ); 202 203 void register_enum( const char* name, int value ); 204 221 205 void register_native_object_method( const char* lua_name, const char* name, lfunction f ); 222 206 template < typename F, F f > … … 226 210 } 227 211 operator lua_State*() { return m_state; } 228 type_database* get_type_database() { return m_type_database; }229 212 private: 230 213 int load_string( const std::string& code, const std::string& name ); … … 233 216 int do_current( const std::string& name, int rvalues = 0 ); 234 217 void deep_pointer_copy( int index, void* obj ); 235 private:236 type_database* m_type_database;237 218 }; 238 219 -
trunk/nv/lua/lua_values.hh
r228 r262 60 60 void push_string ( lua_State *L, const std::string& s ); 61 61 void push_cstring ( lua_State *L, const char* s ); 62 void push_object ( lua_State *L, object* o );63 62 void push_pointer ( lua_State *L, void* p ); 64 63 65 lunsigned to_unsigned ( lua_State *L, int index );66 linteger to_integer ( lua_State *L, int index );67 lnumber to_number ( lua_State *L, int index );68 bool to_bool ( lua_State *L, int index );69 std::string to_string ( lua_State *L, int index );70 const char* to_cstring ( lua_State *L, int index );71 object* to_object( lua_State *L, int index );72 void* to_ pointer( lua_State *L, int index );73 74 lunsigned to_unsigned ( lua_State *L, int index, lunsigned def );75 linteger to_integer ( lua_State *L, int index, linteger def );76 lnumber to_number ( lua_State *L, int index, lnumber def );77 bool to_bool ( lua_State *L, int index, bool def );78 std::string to_string ( lua_State *L, int index, const std::string& def );79 const char* to_cstring ( lua_State *L, int index, const char* def );80 object* to_object ( lua_State *L, int index, object* def );81 void* to_ pointer( lua_State *L, int index, void* def );64 lunsigned to_unsigned ( lua_State *L, int index ); 65 linteger to_integer ( lua_State *L, int index ); 66 lnumber to_number ( lua_State *L, int index ); 67 bool to_bool ( lua_State *L, int index ); 68 std::string to_string ( lua_State *L, int index ); 69 const char* to_cstring ( lua_State *L, int index ); 70 void* to_pointer ( lua_State *L, int index ); 71 void* to_ref_object ( lua_State *L, int index ); 72 73 lunsigned to_unsigned ( lua_State *L, int index, lunsigned def ); 74 linteger to_integer ( lua_State *L, int index, linteger def ); 75 lnumber to_number ( lua_State *L, int index, lnumber def ); 76 bool to_bool ( lua_State *L, int index, bool def ); 77 std::string to_string ( lua_State *L, int index, const std::string& def ); 78 const char* to_cstring ( lua_State *L, int index, const char* def ); 79 void* to_pointer ( lua_State *L, int index, void* def ); 80 void* to_ref_object ( lua_State *L, int index, void* def ); 82 81 83 82 void pop_and_discard( lua_State *L, int count ); … … 140 139 }; 141 140 142 template <>143 struct pass_traits<object*>144 {145 static void push( lua_State *L, object* o ) { detail::push_object( L, o ); }146 static object* to( lua_State *L, int index ) { return detail::to_object( L, index ); }147 static object* to( lua_State *L, int index, object* def ) { return detail::to_object( L, index, def ); }148 };149 150 141 namespace detail 151 142 { … … 185 176 { 186 177 typedef std::string type; 187 };188 189 template <typename T>190 struct type_degrade< T*, typename std::enable_if< std::is_base_of<object, T >::value >::type >191 {192 typedef object* type;193 178 }; 194 179 -
trunk/nv/root.hh
r257 r262 13 13 namespace nv 14 14 { 15 15 16 /** 16 17 * Implements the root of a object tree-like structure. -
trunk/nv/types.hh
r214 r262 22 22 23 23 struct type_entry; 24 class type_database; 24 25 25 26 enum type_flag -
trunk/nv/uid.hh
r132 r262 19 19 namespace nv 20 20 { 21 class object; 22 21 23 class uid_store 22 24 { … … 24 26 25 27 /** 26 * Creates a new instance of the unique indentif er store.28 * Creates a new instance of the unique indentifier store. 27 29 */ 28 30 uid_store(); -
trunk/src/lua/lua_state.cc
r256 r262 11 11 #include "nv/logging.hh" 12 12 #include "nv/string.hh" 13 #include "nv/root.hh"14 #include "nv/types.hh"15 13 16 14 using namespace nv; … … 205 203 // state 206 204 207 lua::state::state( lua_State* state ) : state_wrapper( state, false ) , m_type_database( nullptr )208 { 209 210 } 211 212 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) , m_type_database( nullptr )205 lua::state::state( lua_State* state ) : state_wrapper( state, false ) 206 { 207 208 } 209 210 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) 213 211 { 214 212 load_lua_library(); … … 222 220 if ( load_libs ) 223 221 { 224 m_type_database = new type_database();225 222 stack_guard guard( this ); 226 223 static const luaL_Reg lualibs[] = … … 331 328 } 332 329 333 lua::reference lua::state::register_object( object* o, const char* lua_name )330 lua::reference lua::state::register_object( void* o, const char* lua_name ) 334 331 { 335 332 if ( o == nullptr ) return ref_none; … … 344 341 } 345 342 346 lua::reference lua::state::register_proto( object * o, const char* storage )343 lua::reference lua::state::register_proto( const char* id, const char* storage ) 347 344 { 348 345 stack_guard guard( this ); … … 352 349 NV_THROW( runtime_error, std::string( storage ) + " storage not registered!" ); 353 350 } 354 lua_getfield( m_state, -1, o->get_id().c_str());351 lua_getfield( m_state, -1, id ); 355 352 if ( lua_isnil( m_state, -1 ) ) 356 353 { 357 NV_THROW( runtime_error, std::string( o->get_id()) + " not found in " + std::string( storage ) + " storage!" );354 NV_THROW( runtime_error, std::string( id ) + " not found in " + std::string( storage ) + " storage!" ); 358 355 } 359 356 return luaL_ref( m_state, LUA_REGISTRYINDEX ); 360 }361 362 lua::reference lua::state::register_object( object * o )363 {364 if ( o == nullptr || m_type_database == nullptr ) return ref_none;365 type_entry* t = m_type_database->get_type(typeid(*o));366 if ( t == nullptr ) return ref_none;367 return register_object( o, t->name.c_str() );368 357 } 369 358 … … 380 369 } 381 370 382 void lua::state::unregister_object( object * o ) 383 { 384 if (!o) return; 385 unregister_object( o->get_lua_index() ); 386 } 387 388 void lua::state::unregister_object( int index ) 389 { 371 void lua::state::unregister_object( reference object_index ) 372 { 373 if ( object_index == ref_nil ) return; 390 374 stack_guard guard( this ); 391 lua_rawgeti( m_state, LUA_REGISTRYINDEX, index );375 lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index ); 392 376 lua_pushstring( m_state, "__ptr" ); 393 377 lua_pushboolean( m_state, false ); 394 378 lua_rawset( m_state, -3 ); 395 379 lua_pop( m_state, 1 ); 396 luaL_unref( m_state, LUA_REGISTRYINDEX, index );380 luaL_unref( m_state, LUA_REGISTRYINDEX, object_index ); 397 381 } 398 382 … … 435 419 } 436 420 437 void lua::state::register_enum_values( const std::string& name, const std::string& prefix /*= std::string() */ ) 438 { 439 type_entry* et = m_type_database->get_type( name ); 440 441 for ( const auto& entry : et->enum_list ) 442 { 443 lua_pushinteger( m_state, entry.value ); 444 if ( prefix.empty() ) 445 { 446 lua_setglobal( m_state, entry.name.c_str() ); 447 } 448 else 449 { 450 lua_setglobal( m_state, ( prefix + entry.name ).c_str() ); 451 } 452 } 453 } 454 455 void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer ) 456 { 457 if (!o) return; 421 void nv::lua::state::store_metadata( reference object_index, const std::string& metaname, void* pointer ) 422 { 423 if ( object_index == ref_nil ) return; 458 424 stack_guard guard( this ); 459 lua_rawgeti( m_state, LUA_REGISTRYINDEX, o ->get_lua_index());425 lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index ); 460 426 lua_pushstring( m_state, metaname.c_str() ); 461 427 lua_pushlightuserdata( m_state, pointer ); … … 464 430 } 465 431 466 nv::lua::state::~state() 467 { 468 if (m_type_database != nullptr ) 469 { 470 delete m_type_database; 471 } 472 } 473 432 void nv::lua::state::register_enum( const char* name, int value ) 433 { 434 lua_pushinteger( m_state, value ); 435 lua_setglobal( m_state, name ); 436 } 437 -
trunk/src/lua/lua_values.cc
r213 r262 8 8 9 9 #include "nv/lua/lua_raw.hh" 10 #include "nv/object.hh"11 10 12 11 using nv::lua::linteger; … … 71 70 } 72 71 73 void nv::lua::detail::push_object ( lua_State *L, object* o )74 {75 if ( o == nullptr )76 {77 lua_pushnil( L );78 }79 else80 {81 lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );82 }83 }84 85 72 void nv::lua::detail::push_pointer ( lua_State *L, void* p ) 86 73 { … … 118 105 } 119 106 120 nv::object* nv::lua::detail::to_object( lua_State *L, int index )107 void* nv::lua::detail::to_pointer ( lua_State *L, int index ) 121 108 { 122 object* o = nullptr; 109 return lua_touserdata( L, index ); 110 } 111 112 void* nv::lua::detail::to_ref_object ( lua_State *L, int index ) 113 { 114 void* o = nullptr; 123 115 if ( lua_istable( L , index ) ) 124 116 { … … 127 119 if ( lua_isuserdata( L, -1 ) ) 128 120 { 129 o = static_cast<object*>( lua_touserdata( L, -1 ));121 o = lua_touserdata( L, -1 ); 130 122 } 131 123 lua_pop( L, 1 ); 132 124 } 133 125 return o; 134 }135 136 void* nv::lua::detail::to_pointer ( lua_State *L, int index )137 {138 return lua_touserdata( L, index );139 126 } 140 127 … … 169 156 } 170 157 171 nv::object* nv::lua::detail::to_object ( lua_State *L, int index, nv::object* def )158 void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def ) 172 159 { 173 object* o = def; 160 return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def ); 161 } 162 163 void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def ) 164 { 165 void* o = def; 174 166 if ( lua_istable( L , index ) ) 175 167 { … … 178 170 if ( lua_isuserdata( L, -1 ) ) 179 171 { 180 o = static_cast<object*>( lua_touserdata( L, -1 ));172 o = lua_touserdata( L, -1 ); 181 173 } 182 174 lua_pop( L, 1 ); … … 184 176 return o; 185 177 } 186 187 void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )188 {189 return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );190 } -
trunk/src/root.cc
r257 r262 24 24 if ( m_lua_state && o->m_lua_index != lua::ref_none ) 25 25 { 26 m_lua_state->unregister_object( o );26 m_lua_state->unregister_object( o->m_lua_index ); 27 27 } 28 28 if ( m_uid_store && o->m_uid != 0 ) … … 51 51 if ( storage != nullptr ) 52 52 { 53 o->m_lua_proto_index = m_lua_state->register_proto( o , storage );53 o->m_lua_proto_index = m_lua_state->register_proto( o->get_id().c_str(), storage ); 54 54 } 55 55 }
Note: See TracChangeset
for help on using the changeset viewer.