Changeset 505 for trunk/src/lua/lua_state.cc
- Timestamp:
- 07/12/16 20:22:23 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_state.cc
r503 r505 201 201 } 202 202 203 shash64 nv::lua::table_guard::get_string( string_view element, string_table&table, uint64 defval /*= 0 */ )203 nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ ) 204 204 { 205 205 lua_getfield( m_state, -1, element.data() ); … … 210 210 { 211 211 str = lua_tolstring( m_state, -1, &l ); 212 result = table.insert( string_view( str, l ) ); 213 } 214 lua_pop( m_state, 1 ); 215 return result; 216 } 217 218 nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ ) 219 { 220 lua_getfield( m_state, -1, element.data() ); 212 string_view sv( str, l ); 213 result = table ? table->insert( sv ) : shash64( sv ); 214 } 215 lua_pop( m_state, 1 ); 216 return result; 217 } 218 219 shash64 nv::lua::table_guard::get_string_hash_64( int i, uint64 defval /*= 0 */ ) 220 { 221 lua_rawgeti( m_state, -1, i ); 222 size_t l = 0; 223 const char* str = nullptr; 224 uint64 result = defval; 225 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 226 { 227 str = lua_tolstring( m_state, -1, &l ); 228 result = hash_string< uint64 >( str, l ); 229 //NV_LOG_DEBUG( str ); 230 } 231 lua_pop( m_state, 1 ); 232 return shash64( result ); 233 } 234 235 nv::string128 nv::lua::table_guard::get_string128( int i, string_view defval /*= string_view() */ ) 236 { 237 lua_rawgeti( m_state, -1, i ); 238 size_t l = 0; 239 const char* str = nullptr; 240 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 241 { 242 str = lua_tolstring( m_state, -1, &l ); 243 } 244 else 245 { 246 l = defval.size(); 247 str = defval.data(); 248 } 249 string128 result( str, l ); 250 lua_pop( m_state, 1 ); 251 return result; 252 } 253 254 const_string nv::lua::table_guard::get_string( int i, string_view defval /*= string_view() */ ) 255 { 256 lua_rawgeti( m_state, -1, i ); 257 size_t l = 0; 258 const char* str = nullptr; 259 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 260 { 261 str = lua_tolstring( m_state, -1, &l ); 262 } 263 else 264 { 265 l = defval.size(); 266 str = defval.data(); 267 } 268 const_string result( str, l ); 269 lua_pop( m_state, 1 ); 270 return result; 271 } 272 273 shash64 nv::lua::table_guard::get_string( int i, string_table* table, uint64 defval /*= 0 */ ) 274 { 275 lua_rawgeti( m_state, -1, i ); 221 276 size_t l = 0; 222 277 const char* str = nullptr; … … 302 357 } 303 358 359 char nv::lua::table_guard::get_char( int i, char defval /*= ' ' */ ) 360 { 361 lua_rawgeti( m_state, -1, i ); 362 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 363 lua_pop( m_state, 1 ); 364 return result; 365 } 366 367 int nv::lua::table_guard::get_integer( int i, int defval /*= 0 */ ) 368 { 369 lua_rawgeti( m_state, -1, i ); 370 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 371 lua_pop( m_state, 1 ); 372 return static_cast<int>( result ); 373 } 374 375 unsigned nv::lua::table_guard::get_unsigned( int i, unsigned defval /*= 0 */ ) 376 { 377 lua_rawgeti( m_state, -1, i ); 378 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval; 379 lua_pop( m_state, 1 ); 380 return result; 381 } 382 383 double nv::lua::table_guard::get_double( int i, double defval /*= 0.0 */ ) 384 { 385 lua_rawgeti( m_state, -1, i ); 386 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 387 lua_pop( m_state, 1 ); 388 return result; 389 } 390 391 float nv::lua::table_guard::get_float( int i, float defval /*= 0.0 */ ) 392 { 393 lua_rawgeti( m_state, -1, i ); 394 float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval; 395 lua_pop( m_state, 1 ); 396 return result; 397 } 398 399 bool nv::lua::table_guard::get_boolean( int i, bool defval /*= false */ ) 400 { 401 lua_rawgeti( m_state, -1, i ); 402 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 403 lua_pop( m_state, 1 ); 404 return result; 405 } 406 304 407 float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ ) 305 408 { … … 326 429 } 327 430 431 bool nv::lua::table_guard::is_table( int i ) 432 { 433 lua_rawgeti( m_state, -1, i ); 434 bool result = lua_type( m_state, -1 ) == LUA_TTABLE; 435 lua_pop( m_state, 1 ); 436 return result; 437 } 438 439 bool nv::lua::table_guard::is_number( int i ) 440 { 441 lua_rawgeti( m_state, -1, i ); 442 bool result = lua_type( m_state, -1 ) == LUA_TNUMBER; 443 lua_pop( m_state, 1 ); 444 return result; 445 } 446 447 bool nv::lua::table_guard::is_boolean( int i ) 448 { 449 lua_rawgeti( m_state, -1, i ); 450 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN; 451 lua_pop( m_state, 1 ); 452 return result; 453 } 454 455 bool nv::lua::table_guard::is_string( int i ) 456 { 457 lua_rawgeti( m_state, -1, i ); 458 bool result = lua_type( m_state, -1 ) == LUA_TSTRING; 459 lua_pop( m_state, 1 ); 460 return result; 461 } 462 328 463 bool nv::lua::table_guard::is_number( string_view element ) 329 464 { … … 349 484 return result; 350 485 } 351 352 486 353 487 bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
Note: See TracChangeset
for help on using the changeset viewer.