Changeset 187 for trunk


Ignore:
Timestamp:
08/03/13 12:07:12 (12 years ago)
Author:
epyon
Message:
  • object - lua registration will now be manual, with an option to be separate from the type system
  • lua/state - explicit object registration
Location:
trunk
Files:
4 edited

Legend:

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

    r185 r187  
    175175                        lua_State* get_raw();
    176176                        reference register_object( object * o );
     177                        reference register_object( object * o, const char* lua_name );
    177178                        void unregister_object( object * o );
    178179                        void register_enum( type_database* db, const std::string& name, const std::string& prefix = std::string() );
  • trunk/nv/object.hh

    r99 r187  
    178178
    179179        protected:
     180                void register_with_lua( const char* lua_name = nullptr );
     181
     182        protected:
    180183                root*   m_root;        ///< pointer to root
    181184                string  m_id;          ///< id type of the object
  • trunk/src/lua/lua_state.cc

    r185 r187  
    303303}
    304304
     305lua::reference lua::state::register_object( object * o, const char* lua_name )
     306{
     307        if ( o == nullptr ) return ref_none;
     308        stack_guard guard( this );
     309        lua_getglobal( L, lua_name );
     310        if ( lua_isnil( L, -1 ) )
     311        {
     312                NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
     313        }
     314        deep_pointer_copy( -1, o );
     315        return luaL_ref( L, LUA_REGISTRYINDEX );
     316}
     317
    305318lua::reference lua::state::register_object( object * o )
    306319{
    307         if (!o) return ref_none;
     320        if ( o == nullptr ) return ref_none;
    308321        type_database *db = o->get_root()->get_type_database();
    309         if (!db) return ref_none;
    310         type_entry* t = db->get_type(typeid(o));
    311         if (!t) return ref_none;
    312         stack_guard guard( this );
    313         lua_getglobal( L, t->name.c_str() );
    314         if ( lua_isnil( L, -1 ) )
    315         {
    316                 NV_THROW( runtime_error, std::string( t->name ) + " type not registered!" );
    317         }
    318         deep_pointer_copy( -1, o );
    319     return luaL_ref( L, LUA_REGISTRYINDEX );
     322        if ( db == nullptr ) return ref_none;
     323        type_entry* t = db->get_type(typeid(*o));
     324        if ( t == nullptr ) return ref_none;
     325        return register_object( o, t->name.c_str() );
    320326}
    321327
     
    403409        if ( !p.resolve( L, global ) )
    404410        {
    405                 lua_pop( L, 1 );
    406411                NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
    407412                return false;
     
    416421        return true;
    417422}
     423
  • trunk/src/object.cc

    r120 r187  
    1616
    1717object::object()
    18         : m_root( nullptr ), m_id(), m_name(), m_uid(0), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
     18        : m_root( nullptr ), m_id(), m_name(), m_uid(0), m_lua_index(lua::ref_none), m_parent( nullptr ), m_children(), m_child_count(0)
    1919{
    2020}
    2121
    2222object::object( root* aroot, const string& aid )
    23         : m_root( aroot ), m_id(aid), m_name(), m_uid( 0 ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
     23        : m_root( aroot ), m_id(aid), m_name(), m_uid( 0 ), m_lua_index(lua::ref_none), m_parent( nullptr ), m_children(), m_child_count(0)
    2424{
    2525        if ( m_root )
    2626        {
    2727                uid_store*  store = get_root()->get_uid_store();
    28                 lua::state* state = get_root()->get_lua_state();
    2928                if (store)
    3029                {
    3130                        m_uid = store->insert( this );
    32                 }
    33                 if (state)
    34                 {
    35                         m_lua_index = state->register_object( this );
    3631                }
    3732        }
     
    195190        db->create_type<object>("object").fields(fields);
    196191}
     192
     193void nv::object::register_with_lua( const char* lua_name /*= nullptr*/ )
     194{
     195        lua::state* state = get_root()->get_lua_state();
     196        if (state)
     197        {
     198                if ( lua_name != nullptr )
     199                {
     200                        m_lua_index = state->register_object( this, lua_name );
     201                }
     202                else
     203                {
     204                        m_lua_index = state->register_object( this );
     205                }
     206        }
     207}
Note: See TracChangeset for help on using the changeset viewer.