Changeset 262


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
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/common.hh

    r261 r262  
    143143namespace nv
    144144{
    145         class object;
    146         class root;
    147         class type_database;
    148145        class uid_store;
    149146
  • trunk/nv/io_event.hh

    r214 r262  
    1515
    1616#include <nv/common.hh>
     17#include <nv/types.hh>
    1718
    1819namespace nv
  • trunk/nv/lua/lua_dispatch.hh

    r253 r262  
    147147                        int object_method_wrapper( lua_State* L )
    148148                        {
    149                                 C* c = (C*)to_object( L, 1 );
     149                                C* c = (C*)to_ref_object( L, 1 );
    150150                                return dispatcher<typename return_type<F>::type>::call( L, 2, *c, f );
    151151                        }
  • trunk/nv/lua/lua_state.hh

    r256 r262  
    1616#include <map>
    1717#include <nv/flags.hh>
    18 #include <nv/types.hh>
    1918
    2019#include <nv/lua/lua_path.hh>
     
    191190                        explicit state( bool load_libs = false );
    192191                        explicit state( lua_State* state );
    193                         virtual ~state();
    194192                        bool do_string( const std::string& code, const std::string& name, int rvalues = 0 );
    195193                        bool do_stream( std::istream& stream, const std::string& name );
     
    198196                        void log_stack();
    199197                        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
    221205                        void register_native_object_method( const char* lua_name, const char* name, lfunction f );
    222206                        template < typename F, F f >
     
    226210                        }
    227211                        operator lua_State*() { return m_state; }
    228                         type_database* get_type_database() { return m_type_database; }
    229212                private:
    230213                        int load_string( const std::string& code, const std::string& name );
     
    233216                        int do_current( const std::string& name, int rvalues = 0 );
    234217                        void deep_pointer_copy( int index, void* obj );
    235                 private:
    236                         type_database* m_type_database;
    237218                };
    238219
  • trunk/nv/lua/lua_values.hh

    r228 r262  
    6060                        void push_string  ( lua_State *L, const std::string& s );
    6161                        void push_cstring ( lua_State *L, const char* s );
    62                         void push_object  ( lua_State *L, object* o );
    6362                        void push_pointer ( lua_State *L, void* p );
    6463
    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 );
    8281
    8382                        void pop_and_discard( lua_State *L, int count );
     
    140139                };
    141140
    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 
    150141                namespace detail
    151142                {
     
    185176                        {
    186177                                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;
    193178                        };
    194179
  • trunk/nv/root.hh

    r257 r262  
    1313namespace nv
    1414{
     15
    1516        /**
    1617         * Implements the root of a object tree-like structure.
  • trunk/nv/types.hh

    r214 r262  
    2222
    2323        struct type_entry;
     24        class type_database;
    2425
    2526        enum type_flag
  • trunk/nv/uid.hh

    r132 r262  
    1919namespace nv
    2020{
     21        class object;
     22
    2123        class uid_store
    2224        {
     
    2426               
    2527                /**
    26                  * Creates a new instance of the unique indentifer store.
     28                 * Creates a new instance of the unique indentifier store.
    2729                 */
    2830                uid_store();
  • 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 }
  • trunk/src/root.cc

    r257 r262  
    2424        if ( m_lua_state && o->m_lua_index != lua::ref_none )
    2525        {
    26                 m_lua_state->unregister_object( o );
     26                m_lua_state->unregister_object( o->m_lua_index );
    2727        }
    2828        if ( m_uid_store && o->m_uid != 0 )
     
    5151                if ( storage != nullptr )
    5252                {
    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 );
    5454                }
    5555        }
Note: See TracChangeset for help on using the changeset viewer.