Changeset 539 for trunk/src/lua
- Timestamp:
- 01/24/17 17:55:00 (8 years ago)
- Location:
- trunk/src/lua
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_proxy.cc
r538 r539 13 13 using namespace nv::lua; 14 14 15 nv::string64 nv::lua::stack_proxy::get_string64() const 15 uint32 nv::lua::stack_proxy::get_uint32( uint32 def ) const 16 { 17 return lua_type( *m_state, m_index ) == LUA_TNUMBER ? nlua_tounsigned( *m_state, m_index ) : def; 18 } 19 20 sint32 nv::lua::stack_proxy::get_sint32( sint32 def ) const 21 { 22 return lua_type( *m_state, m_index ) == LUA_TNUMBER ? static_cast<sint32>( lua_tointeger( *m_state, m_index ) ) : def; 23 } 24 25 char nv::lua::stack_proxy::get_char( char def /*= ' ' */ ) const 26 { 27 return ( lua_type( *m_state, m_index ) == LUA_TSTRING && nlua_rawlen( *m_state, m_index ) > 0 ) ? lua_tostring( *m_state, m_index )[0] : def; 28 } 29 30 nv::f64 nv::lua::stack_proxy::get_f64( f64 def /*= 0.0 */ ) const 31 { 32 return lua_type( *m_state, m_index ) == LUA_TNUMBER ? lua_tonumber( *m_state, m_index ) : def; 33 } 34 35 nv::f32 nv::lua::stack_proxy::get_f32( f32 def /*= 0.0f */ ) const 36 { 37 return lua_type( *m_state, m_index ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( *m_state, m_index ) ) : def; 38 } 39 40 bool nv::lua::stack_proxy::get_bool( bool def /*= false */ ) const 41 { 42 return lua_type( *m_state, m_index ) == LUA_TBOOLEAN ? lua_toboolean( *m_state, m_index ) != 0 : def; 43 } 44 45 bool nv::lua::stack_proxy::is_number() const 46 { 47 return lua_type( *m_state, m_index ) == LUA_TNUMBER; 48 } 49 50 bool nv::lua::stack_proxy::is_bool() const 51 { 52 return lua_type( *m_state, m_index ) == LUA_TBOOLEAN; 53 } 54 55 bool nv::lua::stack_proxy::is_string() const 56 { 57 return lua_type( *m_state, m_index ) == LUA_TSTRING; 58 } 59 60 bool nv::lua::stack_proxy::is_table() const 61 { 62 return lua_type( *m_state, m_index ) == LUA_TTABLE; 63 } 64 65 nv::string_view nv::lua::stack_proxy::get_string_view() const 16 66 { 17 67 size_t l = 0; … … 21 71 str = lua_tolstring( *m_state, m_index, &l ); 22 72 } 23 return string 64( str, static_cast<uint32>( l ) );73 return string_view( str, static_cast<uint32>( l ) ); 24 74 } 25 75 26 nv::string 64 nv::lua::stack_proxy::to_string64()76 nv::string_view nv::lua::stack_proxy::as_string_view() 27 77 { 28 78 size_t l = 0; 29 79 const char* str = nullptr; 30 80 str = lua_tolstring( *m_state, m_index, &l ); 31 return string 64( str, static_cast<uint32>( l ) );81 return string_view( str, static_cast<uint32>( l ) ); 32 82 } 83 84 nv::lua::temporary_proxy::temporary_proxy( state* state ) 85 : stack_proxy( state, -1 ) 86 { 87 88 } 89 90 nv::lua::temporary_proxy::~temporary_proxy() 91 { 92 lua_pop( *m_state, 1 ); 93 } -
trunk/src/lua/lua_state.cc
r538 r539 185 185 } 186 186 187 shash64 nv::lua::table_guard::get_string_hash_64( string_view element, uint64 defval /*= 0 */ ) 188 { 189 lua_getfield( m_state, -1, element.data() ); 190 size_t l = 0; 191 const char* str = nullptr; 192 uint64 result = defval; 193 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 194 { 195 str = lua_tolstring( m_state, -1, &l ); 196 result = hash_string< uint64 >( str, static_cast< uint32 >( l ) ); 197 //NV_LOG_DEBUG( str ); 198 } 199 lua_pop( m_state, 1 ); 200 return shash64( result ); 201 } 202 203 nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ ) 204 { 205 lua_getfield( m_state, -1, element.data() ); 206 size_t l = 0; 207 const char* str = nullptr; 208 shash64 result = shash64( defval ); 209 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 210 { 211 str = lua_tolstring( m_state, -1, &l ); 212 string_view sv( str, static_cast< uint32 >( 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, static_cast< uint32 >( 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, static_cast< uint32 >( 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, static_cast< uint32 >( 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 ); 276 size_t l = 0; 277 const char* str = nullptr; 278 shash64 result = shash64( defval ); 279 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 280 { 281 str = lua_tolstring( m_state, -1, &l ); 282 string_view sv( str, static_cast< uint32 >( l ) ); 283 result = table ? table->insert( sv ) : shash64( sv ); 284 } 285 lua_pop( m_state, 1 ); 286 return result; 287 } 288 289 const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ ) 290 { 291 lua_getfield( m_state, -1, element.data() ); 292 size_t l = 0; 293 const char* str = nullptr; 294 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 295 { 296 str = lua_tolstring( m_state, -1, &l ); 297 } 298 else 299 { 300 l = defval.size(); 301 str = defval.data(); 302 } 303 const_string result( str, static_cast< uint32 >( l ) ); 304 lua_pop( m_state, 1 ); 305 return result; 306 } 307 308 string128 lua::table_guard::get_string128( string_view element, string_view defval ) 309 { 310 lua_getfield( m_state, -1, element.data() ); 311 size_t l = 0; 312 const char* str = nullptr; 313 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 314 { 315 str = lua_tolstring( m_state, -1, &l ); 316 } 317 else 318 { 319 l = defval.size(); 320 str = defval.data(); 321 } 322 string128 result( str, static_cast< uint32 >( l ) ); 323 lua_pop( m_state, 1 ); 324 return result; 325 } 326 327 nv::string64 nv::lua::table_guard::get_string64( string_view element, string_view defval /*= string_view() */ ) 328 { 329 lua_getfield( m_state, -1, element.data() ); 330 size_t l = 0; 331 const char* str = nullptr; 332 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 333 { 334 str = lua_tolstring( m_state, -1, &l ); 335 } 336 else 337 { 338 l = defval.size(); 339 str = defval.data(); 340 } 341 string64 result( str, static_cast< uint32 >( l ) ); 342 lua_pop( m_state, 1 ); 343 return result; 344 } 345 346 347 nv::string32 nv::lua::table_guard::get_string32( string_view element, string_view defval /*= string_view() */ ) 348 { 349 lua_getfield( m_state, -1, element.data() ); 350 size_t l = 0; 351 const char* str = nullptr; 352 if ( lua_type( m_state, -1 ) == LUA_TSTRING ) 353 { 354 str = lua_tolstring( m_state, -1, &l ); 355 } 356 else 357 { 358 l = defval.size(); 359 str = defval.data(); 360 } 361 string32 result( str, static_cast< uint32 >( l ) ); 362 lua_pop( m_state, 1 ); 363 return result; 364 } 365 366 char lua::table_guard::get_char( string_view element, char defval /*= "" */ ) 367 { 368 lua_getfield( m_state, -1, element.data() ); 369 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 370 lua_pop( m_state, 1 ); 371 return result; 372 } 373 374 int lua::table_guard::get_integer( string_view element, int defval /*= "" */ ) 375 { 376 lua_getfield( m_state, -1, element.data() ); 377 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 378 lua_pop( m_state, 1 ); 379 return static_cast< int >( result ); 380 } 381 382 unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ ) 383 { 384 lua_getfield( m_state, -1, element.data() ); 385 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval; 386 lua_pop( m_state, 1 ); 387 return result; 388 } 389 390 double lua::table_guard::get_double( string_view element, double defval /*= "" */ ) 391 { 392 lua_getfield( m_state, -1, element.data() ); 393 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 394 lua_pop( m_state, 1 ); 395 return result; 396 } 397 398 char nv::lua::table_guard::get_char( int i, char defval /*= ' ' */ ) 399 { 400 lua_rawgeti( m_state, -1, i ); 401 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 402 lua_pop( m_state, 1 ); 403 return result; 404 } 405 406 int nv::lua::table_guard::get_integer( int i, int defval /*= 0 */ ) 407 { 408 lua_rawgeti( m_state, -1, i ); 409 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 410 lua_pop( m_state, 1 ); 411 return static_cast<int>( result ); 412 } 413 414 unsigned nv::lua::table_guard::get_unsigned( int i, unsigned defval /*= 0 */ ) 415 { 416 lua_rawgeti( m_state, -1, i ); 417 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval; 418 lua_pop( m_state, 1 ); 419 return result; 420 } 421 422 double nv::lua::table_guard::get_double( int i, double defval /*= 0.0 */ ) 423 { 424 lua_rawgeti( m_state, -1, i ); 425 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 426 lua_pop( m_state, 1 ); 427 return result; 428 } 429 430 float nv::lua::table_guard::get_float( int i, float defval /*= 0.0 */ ) 431 { 432 lua_rawgeti( m_state, -1, i ); 433 float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval; 434 lua_pop( m_state, 1 ); 435 return result; 436 } 437 438 bool nv::lua::table_guard::get_boolean( int i, bool defval /*= false */ ) 439 { 440 lua_rawgeti( m_state, -1, i ); 441 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 442 lua_pop( m_state, 1 ); 443 return result; 444 } 445 446 float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ ) 447 { 448 lua_getfield( m_state, -1, element.data() ); 449 float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval; 450 lua_pop( m_state, 1 ); 451 return result; 452 } 453 454 bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ ) 455 { 456 lua_getfield( m_state, -1, element.data() ); 457 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 458 lua_pop( m_state, 1 ); 459 return result; 460 } 461 462 bool nv::lua::table_guard::is_table( string_view element ) 463 { 464 lua_getfield( m_state, -1, element.data() ); 465 bool result = lua_type( m_state, -1 ) == LUA_TTABLE; 466 lua_pop( m_state, 1 ); 467 return result; 468 } 469 470 bool nv::lua::table_guard::is_table( int i ) 471 { 472 lua_rawgeti( m_state, -1, i ); 473 bool result = lua_type( m_state, -1 ) == LUA_TTABLE; 474 lua_pop( m_state, 1 ); 475 return result; 476 } 477 478 bool nv::lua::table_guard::is_number( int i ) 479 { 480 lua_rawgeti( m_state, -1, i ); 481 bool result = lua_type( m_state, -1 ) == LUA_TNUMBER; 482 lua_pop( m_state, 1 ); 483 return result; 484 } 485 486 bool nv::lua::table_guard::is_boolean( int i ) 487 { 488 lua_rawgeti( m_state, -1, i ); 489 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN; 490 lua_pop( m_state, 1 ); 491 return result; 492 } 493 494 bool nv::lua::table_guard::is_string( int i ) 495 { 496 lua_rawgeti( m_state, -1, i ); 497 bool result = lua_type( m_state, -1 ) == LUA_TSTRING; 498 lua_pop( m_state, 1 ); 499 return result; 500 } 501 502 bool nv::lua::table_guard::is_number( string_view element ) 503 { 504 lua_getfield( m_state, -1, element.data() ); 505 bool result = lua_type( m_state, -1 ) == LUA_TNUMBER; 506 lua_pop( m_state, 1 ); 507 return result; 508 } 509 510 bool nv::lua::table_guard::is_boolean( string_view element ) 511 { 512 lua_getfield( m_state, -1, element.data() ); 513 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN; 514 lua_pop( m_state, 1 ); 515 return result; 516 } 517 518 bool nv::lua::table_guard::is_string( string_view element ) 519 { 520 lua_getfield( m_state, -1, element.data() ); 521 bool result = lua_type( m_state, -1 ) == LUA_TSTRING; 522 lua_pop( m_state, 1 ); 523 return result; 187 const nv::lua::temporary_proxy nv::lua::table_guard::operator[]( const string_view& key ) const 188 { 189 lua_getfield( m_state, -1, key.data() ); 190 return temporary_proxy( m_parent ); 191 } 192 193 const nv::lua::temporary_proxy nv::lua::table_guard::operator[]( sint32 key ) const 194 { 195 lua_rawgeti( m_state, -1, key ); 196 return temporary_proxy( m_parent ); 524 197 } 525 198
Note: See TracChangeset
for help on using the changeset viewer.