Ignore:
Timestamp:
05/04/15 16:30:44 (10 years ago)
Author:
epyon
Message:
  • more string_ref usage
  • string_ref moved to string.hh
  • initial const_string implemenation
File:
1 edited

Legend:

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

    r341 r360  
    8080}
    8181
    82 void lua::state_wrapper::register_native_function( lfunction f, const char* name )
     82void lua::state_wrapper::register_native_function( lfunction f, string_ref name )
    8383{
    8484        if ( m_global ) push_global_table();
    8585        lua_pushcfunction( m_state, f );
    86         lua_setfield( m_state, -2, name );
     86        lua_setfield( m_state, -2, name.data() );
    8787        if ( m_global ) pop_global_table();
    8888}
     
    129129        if ( status != 0 )
    130130        {
    131                 std::string error = lua_tostring( m_state, -1 );
     131                NV_LOG( LOG_ERROR, "Lua error : " << lua_tostring( m_state, -1 ) );
    132132                lua_pop( m_state, 1 );
    133                 NV_LOG( LOG_ERROR, "Lua error : " << error )
    134133        }
    135134        return status;
     
    172171}
    173172
    174 bool lua::table_guard::has_field( const string& element )
    175 {
    176         lua_getfield( m_state, -1, element.c_str() );
     173bool lua::table_guard::has_field( string_ref element )
     174{
     175        lua_getfield( m_state, -1, element.data() );
    177176        bool result = !( lua_isnil( m_state, -1 ) );
    178177        lua_pop( m_state, 1 );
     
    180179}
    181180
    182 string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
    183 {
    184         lua_getfield( m_state, -1, element.c_str() );
    185         string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );
    186         lua_pop( m_state, 1 );
    187         return result;
    188 }
    189 
    190 char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
    191 {
    192         lua_getfield( m_state, -1, element.c_str() );
     181string lua::table_guard::get_string( string_ref element, string_ref defval /*= string_ref() */ )
     182{
     183        lua_getfield( m_state, -1, element.data() );
     184        string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval.to_string() );
     185        lua_pop( m_state, 1 );
     186        return result;
     187}
     188
     189char lua::table_guard::get_char( string_ref element, char defval /*= "" */ )
     190{
     191        lua_getfield( m_state, -1, element.data() );
    193192        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
    194193        lua_pop( m_state, 1 );
     
    196195}
    197196
    198 int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
    199 {
    200         lua_getfield( m_state, -1, element.c_str() );
     197int lua::table_guard::get_integer( string_ref element, int defval /*= "" */ )
     198{
     199        lua_getfield( m_state, -1, element.data() );
    201200        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
    202201        lua_pop( m_state, 1 );
     
    204203}
    205204
    206 unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )
    207 {
    208         lua_getfield( m_state, -1, element.c_str() );
     205unsigned lua::table_guard::get_unsigned( string_ref element, unsigned defval /*= "" */ )
     206{
     207        lua_getfield( m_state, -1, element.data() );
    209208        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
    210209        lua_pop( m_state, 1 );
     
    212211}
    213212
    214 double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
    215 {
    216         lua_getfield( m_state, -1, element.c_str() );
     213double lua::table_guard::get_double( string_ref element, double defval /*= "" */ )
     214{
     215        lua_getfield( m_state, -1, element.data() );
    217216        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
    218217        lua_pop( m_state, 1 );
     
    221220
    222221
    223 float nv::lua::table_guard::get_float( const std::string& element, float defval /*= 0.0 */ )
    224 {
    225         lua_getfield( m_state, -1, element.c_str() );
     222float nv::lua::table_guard::get_float( string_ref element, float defval /*= 0.0 */ )
     223{
     224        lua_getfield( m_state, -1, element.data() );
    226225        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? (float)lua_tonumber( m_state, -1 ) : defval;
    227226        lua_pop( m_state, 1 );
     
    229228}
    230229
    231 bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
    232 {
    233         lua_getfield( m_state, -1, element.c_str() );
     230bool lua::table_guard::get_boolean( string_ref element, bool defval /*= "" */ )
     231{
     232        lua_getfield( m_state, -1, element.data() );
    234233        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
    235234        lua_pop( m_state, 1 );
     
    237236}
    238237
    239 bool nv::lua::table_guard::is_table( const std::string& element )
    240 {
    241         lua_getfield( m_state, -1, element.c_str() );
     238bool nv::lua::table_guard::is_table( string_ref element )
     239{
     240        lua_getfield( m_state, -1, element.data() );
    242241        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
    243242        lua_pop( m_state, 1 );
     
    245244}
    246245
    247 bool nv::lua::table_guard::is_number( const std::string& element )
    248 {
    249         lua_getfield( m_state, -1, element.c_str() );
     246bool nv::lua::table_guard::is_number( string_ref element )
     247{
     248        lua_getfield( m_state, -1, element.data() );
    250249        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
    251250        lua_pop( m_state, 1 );
     
    253252}
    254253
    255 bool nv::lua::table_guard::is_boolean( const std::string& element )
    256 {
    257         lua_getfield( m_state, -1, element.c_str() );
     254bool nv::lua::table_guard::is_boolean( string_ref element )
     255{
     256        lua_getfield( m_state, -1, element.data() );
    258257        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
    259258        lua_pop( m_state, 1 );
     
    261260}
    262261
    263 bool nv::lua::table_guard::is_string( const std::string& element )
    264 {
    265         lua_getfield( m_state, -1, element.c_str() );
     262bool nv::lua::table_guard::is_string( string_ref element )
     263{
     264        lua_getfield( m_state, -1, element.data() );
    266265        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
    267266        lua_pop( m_state, 1 );
     
    321320}
    322321
    323 int lua::state::load_string( const std::string& code, const std::string& name )
     322int lua::state::load_string( string_ref code, string_ref name )
    324323{
    325324        NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'");
    326         return luaL_loadbuffer( m_state, code.c_str(), code.length(), name.c_str() );
    327 }
    328 
    329 int lua::state::load_stream( std::istream& stream, const std::string& name )
     325        return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
     326}
     327
     328int lua::state::load_stream( std::istream& stream, string_ref name )
    330329{
    331330        NV_LOG( nv::LOG_NOTICE, "Loading Lua stream '" << name << "'");
     
    335334}
    336335
    337 int lua::state::load_file( const std::string& filename )
     336int lua::state::load_file( string_ref filename )
    338337{
    339338        NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'");
    340         return luaL_loadfile( m_state, filename.c_str() );
    341 }
    342 
    343 bool lua::state::do_string( const std::string& code, const std::string& name, int rvalues )
     339        return luaL_loadfile( m_state, filename.data() );
     340}
     341
     342bool lua::state::do_string( string_ref code, string_ref name, int rvalues )
    344343{
    345344        lua::stack_guard( this );
     
    353352}
    354353
    355 bool lua::state::do_stream( std::istream& stream, const std::string& name )
     354bool lua::state::do_stream( std::istream& stream, string_ref name )
    356355{
    357356        lua::stack_guard( this );
     
    365364}
    366365
    367 bool lua::state::do_file( const std::string& filename )
     366bool lua::state::do_file( string_ref filename )
    368367{
    369368        lua::stack_guard( this );
     
    377376}
    378377
    379 int lua::state::do_current( const std::string& name, int rvalues )
     378int lua::state::do_current( string_ref name, int rvalues )
    380379{
    381380        int result = lua_pcall(m_state, 0, rvalues, 0);
     
    408407}
    409408
    410 lua::ref lua::state::register_object( void* o, const char* lua_name )
     409lua::ref lua::state::register_object( void* o, string_ref lua_name )
    411410{
    412411        if ( o == nullptr ) return lua::ref( lua::ref::none );
    413412        stack_guard guard( this );
    414         lua_getglobal( m_state, lua_name );
     413        lua_getglobal( m_state, lua_name.data() );
    415414        if ( lua_isnil( m_state, -1 ) )
    416415        {
    417                 NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
     416                NV_THROW( runtime_error, lua_name.to_string() + " type not registered!" );
    418417        }
    419418        deep_pointer_copy( -1, o );
     
    421420}
    422421
    423 lua::ref lua::state::register_proto( const char* id, const char* storage )
     422lua::ref lua::state::register_proto( string_ref id, string_ref storage )
    424423{
    425424        stack_guard guard( this );
    426         lua_getglobal( m_state, storage );
     425        lua_getglobal( m_state, storage.data() );
    427426        if ( lua_isnil( m_state, -1 ) )
    428427        {
    429                 NV_THROW( runtime_error, std::string( storage ) + " storage not registered!" );
    430         }
    431         lua_getfield( m_state, -1, id );
     428                NV_THROW( runtime_error, storage.to_string() + " storage not registered!" );
     429        }
     430        lua_getfield( m_state, -1, id.data() );
    432431        if ( lua_isnil( m_state, -1 ) )
    433432        {
    434                 NV_THROW( runtime_error, std::string( id ) + " not found in " + std::string( storage ) + " storage!" );
     433                NV_THROW( runtime_error, id.to_string() + " not found in " + storage.to_string() + " storage!" );
    435434        }
    436435        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
    437436}
    438437
    439 void lua::state::register_native_object_method( const char* lua_name, const char* name, lfunction f )
     438void lua::state::register_native_object_method( string_ref lua_name, string_ref name, lfunction f )
    440439{
    441440        stack_guard guard( this );
    442         lua_getglobal( m_state, lua_name );
     441        lua_getglobal( m_state, lua_name.data() );
    443442        if ( lua_isnil( m_state, -1 ) )
    444443        {
    445                 NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
     444                NV_THROW( runtime_error, lua_name.to_string() + " type not registered!" );
    446445        }
    447446        lua_pushcfunction( m_state, f );
    448         lua_setfield( m_state, -2, name );
     447        lua_setfield( m_state, -2, name.data() );
    449448}
    450449
     
    454453        stack_guard guard( this );
    455454        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
    456         lua_pushstring( m_state, "__ptr" );
     455        lua_pushliteral( m_state, "__ptr" );
    457456        lua_pushboolean( m_state, false );
    458457        lua_rawset( m_state, -3 );
     
    499498}
    500499
    501 void nv::lua::state::store_metadata( ref object_index, const std::string& metaname, void* pointer )
     500void nv::lua::state::store_metadata( ref object_index, string_ref metaname, void* pointer )
    502501{
    503502        if ( !object_index.is_valid() ) return;
    504503        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
    505         lua_pushstring( m_state, metaname.c_str() );
     504        lua_pushlstring( m_state, metaname.data(), metaname.size() );
    506505        lua_pushlightuserdata( m_state, pointer );
    507506        lua_rawset( m_state, -3 );
     
    509508}
    510509
    511 void nv::lua::state::register_enum( const char* name, int value )
     510void nv::lua::state::register_enum( string_ref name, int value )
    512511{
    513512        lua_pushinteger( m_state, value );
    514         lua_setglobal( m_state, name );
    515 }
    516 
    517 nv::lua::ref nv::lua::state::register_handle_component_impl( const std::string& id, bool empty )
     513        lua_setglobal( m_state, name.data() );
     514}
     515
     516nv::lua::ref nv::lua::state::register_handle_component_impl( string_ref id, bool empty )
    518517{
    519518        int args = empty ? 1 : 2;
     
    529528        else
    530529                nlua_deepcopy( m_state, -2 );
    531         lua_pushstring( m_state, id.c_str() );
     530        lua_pushlstring( m_state, id.data(), id.size() );
    532531        lua_pushvalue( m_state, -2 );
    533532        lua_rawset( m_state, -4 );
     
    538537}
    539538
    540 void nv::lua::state::unregister_handle_component_impl( const std::string& id )
     539void nv::lua::state::unregister_handle_component_impl( string_ref id )
    541540{
    542541        NV_LUA_STACK_ASSERT( m_state, -1 );
     
    547546                return;
    548547        }
    549         lua_pushstring( m_state, id.c_str() );
     548        lua_pushlstring( m_state, id.data(), id.size() );
    550549        lua_pushnil( m_state );
    551550        lua_rawset( m_state, -3 );
     
    553552}
    554553
    555 void nv::lua::state::register_singleton( const char* name, void* o )
     554void nv::lua::state::register_singleton( string_ref name, void* o )
    556555{
    557556        if ( o == nullptr ) return;
    558557        stack_guard guard( this );
    559         lua_getglobal( m_state, name );
     558        lua_getglobal( m_state, name.data() );
    560559        if ( lua_isnil( m_state, -1 ) )
    561560        {
    562                 NV_THROW( runtime_error, std::string( name ) + " type not registered!" );
     561                NV_THROW( runtime_error, name.to_string() + " type not registered!" );
    563562        }
    564563        deep_pointer_copy( -1, o );
    565         lua_setglobal( m_state, name );
    566 }
    567 
     564        lua_setglobal( m_state, name.data() );
     565}
     566
Note: See TracChangeset for help on using the changeset viewer.