Changeset 262 for trunk/src/lua


Ignore:
Timestamp:
06/19/14 00:02:55 (11 years ago)
Author:
epyon
Message:
  • major decoupling lua system independent of nv::object nv::object support for lua opt-in lua system independent of types.hh
Location:
trunk/src/lua
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lua/lua_state.cc

    r256 r262  
    1111#include "nv/logging.hh"
    1212#include "nv/string.hh"
    13 #include "nv/root.hh"
    14 #include "nv/types.hh"
    1513
    1614using namespace nv;
     
    205203// state
    206204
    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 )
     205lua::state::state( lua_State* state ) : state_wrapper( state, false )
     206{
     207
     208}
     209
     210lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
    213211{
    214212        load_lua_library();
     
    222220        if ( load_libs )
    223221        {
    224                 m_type_database = new type_database();
    225222                stack_guard guard( this );
    226223                static const luaL_Reg lualibs[] =
     
    331328}
    332329
    333 lua::reference lua::state::register_object( object * o, const char* lua_name )
     330lua::reference lua::state::register_object( void* o, const char* lua_name )
    334331{
    335332        if ( o == nullptr ) return ref_none;
     
    344341}
    345342
    346 lua::reference lua::state::register_proto( object * o, const char* storage )
     343lua::reference lua::state::register_proto( const char* id, const char* storage )
    347344{
    348345        stack_guard guard( this );
     
    352349                NV_THROW( runtime_error, std::string( storage ) + " storage not registered!" );
    353350        }
    354         lua_getfield( m_state, -1, o->get_id().c_str() );
     351        lua_getfield( m_state, -1, id );
    355352        if ( lua_isnil( m_state, -1 ) )
    356353        {
    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!" );
    358355        }
    359356        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() );
    368357}
    369358
     
    380369}
    381370
    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 {
     371void lua::state::unregister_object( reference object_index )
     372{
     373        if ( object_index == ref_nil ) return;
    390374        stack_guard guard( this );
    391         lua_rawgeti( m_state, LUA_REGISTRYINDEX, index );
     375        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index );
    392376        lua_pushstring( m_state, "__ptr" );
    393377        lua_pushboolean( m_state, false );
    394378        lua_rawset( m_state, -3 );
    395379        lua_pop( m_state, 1 );
    396         luaL_unref( m_state, LUA_REGISTRYINDEX, index );
     380        luaL_unref( m_state, LUA_REGISTRYINDEX, object_index );
    397381}
    398382
     
    435419}
    436420
    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;
     421void nv::lua::state::store_metadata( reference object_index, const std::string& metaname, void* pointer )
     422{
     423        if ( object_index == ref_nil ) return;
    458424        stack_guard guard( this );
    459         lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
     425        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index );
    460426        lua_pushstring( m_state, metaname.c_str() );
    461427        lua_pushlightuserdata( m_state, pointer );
     
    464430}
    465431
    466 nv::lua::state::~state()
    467 {
    468         if (m_type_database != nullptr )
    469         {
    470                 delete m_type_database;
    471         }
    472 }
    473 
     432void 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  
    88
    99#include "nv/lua/lua_raw.hh"
    10 #include "nv/object.hh"
    1110
    1211using nv::lua::linteger;
     
    7170}
    7271
    73 void nv::lua::detail::push_object  ( lua_State *L, object* o )
    74 {
    75         if ( o == nullptr )
    76         {
    77                 lua_pushnil( L );
    78         }
    79         else
    80         {
    81                 lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
    82         }
    83 }
    84 
    8572void nv::lua::detail::push_pointer ( lua_State *L, void* p )
    8673{
     
    118105}
    119106
    120 nv::object* nv::lua::detail::to_object ( lua_State *L, int index )
     107void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
    121108{
    122         object* o = nullptr;
     109        return lua_touserdata( L, index );
     110}
     111
     112void* nv::lua::detail::to_ref_object  ( lua_State *L, int index )
     113{
     114        void* o = nullptr;
    123115        if ( lua_istable( L , index ) )
    124116        {
     
    127119                if ( lua_isuserdata( L, -1 ) )
    128120                {
    129                         o = static_cast<object*>( lua_touserdata( L, -1 ) );
     121                        o = lua_touserdata( L, -1 );
    130122                }
    131123                lua_pop( L, 1 );
    132124        }
    133125        return o;
    134 }
    135 
    136 void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
    137 {
    138         return lua_touserdata( L, index );
    139126}
    140127
     
    169156}
    170157
    171 nv::object* nv::lua::detail::to_object  ( lua_State *L, int index, nv::object* def )
     158void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
    172159{
    173         object* o = def;
     160        return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
     161}
     162
     163void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def )
     164{
     165        void* o = def;
    174166        if ( lua_istable( L , index ) )
    175167        {
     
    178170                if ( lua_isuserdata( L, -1 ) )
    179171                {
    180                         o = static_cast<object*>( lua_touserdata( L, -1 ) );
     172                        o = lua_touserdata( L, -1 );
    181173                }
    182174                lua_pop( L, 1 );
     
    184176        return o;
    185177}
    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 }
Note: See TracChangeset for help on using the changeset viewer.