Changeset 341


Ignore:
Timestamp:
09/25/14 00:16:14 (11 years ago)
Author:
epyon
Message:
  • lua_state - lua component support
  • lua_state - call function from lua::ref
  • lua_state - minor enhancements
Location:
trunk
Files:
2 edited

Legend:

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

    r335 r341  
    1818#include <map>
    1919
     20#include <nv/lua/lua_handle.hh>
    2021#include <nv/lua/lua_path.hh>
    2122#include <nv/lua/lua_values.hh>
     
    2425
    2526#ifdef NV_DEBUG
    26 #define NV_LUA_STACK_ASSERT( state, value ) nv::lua::stack_assert __lua_stack_assert( state, value );
     27#define NV_LUA_STACK_ASSERT( state, value ) nv::lua::stack_assert __lua_stack_assert( (state), (value) );
    2728#else
    28 #define NV_LUA_STACK_ASSERT( state, value )
     29#define NV_LUA_STACK_ASSERT( (state), (value) )
    2930#endif
    3031
     
    248249
    249250                        void register_enum( const char* name, int value );
     251                        void register_singleton( const char* name, void* o );
    250252
    251253                        void register_native_object_method( const char* lua_name, const char* name, lfunction f );
     
    256258                        }
    257259                        operator lua_State*() { return m_state; }
     260
     261                        template < typename H >
     262                        void register_handle( const H& handle )
     263                        {
     264                                nv::lua::register_handle( m_state, handle, true );
     265                        }
     266                        template < typename H >
     267                        void unregister_handle( const H& handle )
     268                        {
     269                                nv::lua::unregister_handle( m_state, handle );
     270                        }
     271                        template < typename H >
     272                        ref register_handle_component( const H& handle, const std::string& id )
     273                        {
     274                                nv::lua::push_handle( m_state, handle );
     275                                return register_handle_component_impl( id, true );
     276                        }
     277                        template < typename H >
     278                        ref register_handle_component( const H& handle, const std::string& id, const path& proto )
     279                        {
     280                                if ( !proto.resolve( m_state, true ) )
     281                                {
     282                                        return ref();
     283                                }
     284                                nv::lua::push_handle( m_state, handle );
     285                                return register_handle_component_impl( id, false );
     286                        }
     287                        template < typename H >
     288                        void unregister_handle_component( const H& handle, const std::string& id )
     289                        {
     290                                nv::lua::push_handle( m_state, handle );
     291                                unregister_handle_component_impl( id );
     292                        }
     293
     294                        template < typename R >
     295                        R call( ref table, const path& p )
     296                        {
     297                                stack_guard guard( this ); // TODO: remove
     298                                detail::push_ref_object( m_state,table );
     299                                if ( push_function( p, false ) )
     300                                {
     301                                        if ( call_function( 0, detail::return_count< R >::value ) == 0 )
     302                                        {
     303                                                return detail::pop_return_value<R>( m_state );
     304                                        }
     305                                }
     306                                return R();
     307                        }
     308                        template < typename R, typename T1 >
     309                        R call( ref table, const path& p, const T1& p1 )
     310                        {
     311                                stack_guard guard( this ); // TODO: remove
     312                                detail::push_ref_object( m_state,table );
     313                                if ( push_function( p, false ) )
     314                                {
     315                                        detail::push_value( m_state, p1 );
     316                                        if ( call_function( 1, detail::return_count< R >::value ) == 0 )
     317                                        {
     318                                                return detail::pop_return_value<R>( m_state );
     319                                        }
     320                                }
     321                                return R();
     322                        }
     323                        template < typename R, typename T1, typename T2 >
     324                        R call( ref table, const path& p, const T1& p1, const T2& p2 )
     325                        {
     326                                stack_guard guard( this ); // TODO: remove
     327                                detail::push_ref_object( m_state, table );
     328                                if ( push_function( p, false ) )
     329                                {
     330                                        detail::push_values( m_state, p1, p2 );
     331                                        if ( call_function( 2, detail::return_count< R >::value ) == 0 )
     332                                        {
     333                                                return detail::pop_return_value<R>( m_state );
     334                                        }
     335                                }
     336                                return R();
     337                        }
     338                        template < typename R, typename T1, typename T2, typename T3 >
     339                        R call( ref table, const path& p, const T1& p1, const T2& p2, const T3& p3 )
     340                        {
     341                                stack_guard guard( this ); // TODO: remove
     342                                detail::push_ref_object( m_state, table );
     343                                if ( push_function( p, false ) )
     344                                {
     345                                        detail::push_values( m_state, p1, p2, p3 );
     346                                        if ( call_function( 3, detail::return_count< R >::value ) == 0 )
     347                                        {
     348                                                return detail::pop_return_value<R>( m_state );
     349                                        }
     350                                }
     351                                return R();
     352                        }
     353                        template < typename R, typename T1, typename T2, typename T3, typename T4 >
     354                        R call( ref table, const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 )
     355                        {
     356                                stack_guard guard( this ); // TODO: remove
     357                                detail::push_ref_object( m_state, table );
     358                                if ( push_function( p, false ) )
     359                                {
     360                                        detail::push_value( m_state, p1, p2, p3, p4 );
     361                                        if ( call_function( 4, detail::return_count< R >::value ) == 0 )
     362                                        {
     363                                                return detail::pop_return_value<R>( m_state );
     364                                        }
     365                                }
     366                                return R();
     367                        }
     368
    258369                private:
     370                        ref register_handle_component_impl( const std::string& id, bool empty );
     371                        void unregister_handle_component_impl( const std::string& id );
     372
    259373                        int load_string( const std::string& code, const std::string& name );
    260374                        int load_stream( std::istream& stream, const std::string& name );
  • trunk/src/lua/lua_state.cc

    r334 r341  
    515515}
    516516
     517nv::lua::ref nv::lua::state::register_handle_component_impl( const std::string& id, bool empty )
     518{
     519        int args = empty ? 1 : 2;
     520        NV_LUA_STACK_ASSERT( m_state, -args );
     521
     522        if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
     523        {
     524                lua_pop( m_state, args );
     525                return ref();
     526        }
     527        if (empty)
     528                lua_createtable( m_state, 0, 0 );
     529        else
     530                nlua_deepcopy( m_state, -2 );
     531        lua_pushstring( m_state, id.c_str() );
     532        lua_pushvalue( m_state, -2 );
     533        lua_rawset( m_state, -4 );
     534        lua_replace( m_state, -2 );
     535        ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
     536        if (!empty) lua_pop( m_state, 1 );
     537        return result;
     538}
     539
     540void nv::lua::state::unregister_handle_component_impl( const std::string& id )
     541{
     542        NV_LUA_STACK_ASSERT( m_state, -1 );
     543
     544        if ( lua_isnil( m_state, -1 ) )
     545        {
     546                lua_pop( m_state, 1 );
     547                return;
     548        }
     549        lua_pushstring( m_state, id.c_str() );
     550        lua_pushnil( m_state );
     551        lua_rawset( m_state, -3 );
     552        lua_pop( m_state, 1 );
     553}
     554
     555void nv::lua::state::register_singleton( const char* name, void* o )
     556{
     557        if ( o == nullptr ) return;
     558        stack_guard guard( this );
     559        lua_getglobal( m_state, name );
     560        if ( lua_isnil( m_state, -1 ) )
     561        {
     562                NV_THROW( runtime_error, std::string( name ) + " type not registered!" );
     563        }
     564        deep_pointer_copy( -1, o );
     565        lua_setglobal( m_state, name );
     566}
     567
Note: See TracChangeset for help on using the changeset viewer.