- Timestamp:
- 05/04/15 16:30:44 (10 years ago)
- Location:
- trunk
- Files:
-
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/core/string.hh
r352 r360 2 2 // This file is part of NV Libraries. 3 3 // For conditions of distribution and use, see copyright notice in nv.hh 4 // 5 // TODO: tests for string_length 6 // * performance tests 7 // * matching tests 8 // * compatibility tests 9 // * check if we can get rid of const templates 10 // 11 // String reference implementation based of string_ref proposal and boost::string_ref 12 // 13 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html 14 // http://www.boost.org/doc/libs/1_58_0/libs/utility/doc/html/string_ref.html 15 // 16 // TODO: WARNING: this code is centered around 8bit char. In particular, 17 // const_string wont work with non-8bit chars. 18 // TODO: remove templating on char type and TRAITS, make a string_utf8 instead. 4 19 5 20 #ifndef NV_CORE_STRING_HH … … 225 240 } 226 241 242 namespace detail 243 { 244 template< typename T > 245 struct string_length_impl 246 { 247 static size_t get( T ) { return 0; } 248 }; 249 template< size_t S > 250 struct string_length_impl < char[S] > 251 { 252 static size_t get( const char[S] ) { return S - 1; } 253 }; 254 template< size_t S > 255 struct string_length_impl < const char[S] > 256 { 257 static size_t get( const char[S] ) { return S - 1; } 258 }; 259 template<> 260 struct string_length_impl < char* > 261 { 262 static size_t get( const char* s ) { return std::strlen( s ); } 263 }; 264 template<> 265 struct string_length_impl < const char* > 266 { 267 static size_t get( const char* s ) { return std::strlen( s ); } 268 }; 269 template<> 270 struct string_length_impl < std::string > 271 { 272 static size_t get( const std::string& s ) { return s.length(); } 273 }; 274 template<> 275 struct string_length_impl < const std::string > 276 { 277 static size_t get( const std::string& s ) { return s.length(); } 278 }; 279 } 280 227 281 template< typename T > 228 struct string_length 229 { 230 static size_t get( T ) { return 0; } 282 using string_length = detail::string_length_impl < 283 typename std::remove_cv < typename std::remove_reference< T >::type >::type >; 284 285 template<typename T, typename TRAITS = std::char_traits<T> > class basic_string_ref; 286 287 // string base class - will become a base for a string class later 288 template<typename T, typename TRAITS = std::char_traits<T> > 289 class basic_string_base 290 { 291 public: 292 typedef T value_type; 293 typedef const T* pointer; 294 typedef const T& reference; 295 typedef const T& const_reference; 296 typedef pointer const_iterator; 297 typedef const_iterator iterator; 298 typedef std::size_t size_type; 299 typedef std::ptrdiff_t difference_type; 300 301 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 302 typedef const_reverse_iterator reverse_iterator; 303 304 static NV_CONSTEXPR_CONST size_type npos = size_type( -1 ); 305 306 // conversion to std::string 307 std::basic_string<T, TRAITS> to_string() const 308 { 309 return std::basic_string<T, TRAITS>( m_data, m_size ); 310 } 311 312 // iterators 313 NV_CONSTEXPR const_iterator cbegin() const { return m_data; } 314 NV_CONSTEXPR const_iterator cend() const { return m_data + m_size; } 315 const_reverse_iterator crbegin() const { return const_reverse_iterator( end() ); } 316 const_reverse_iterator crend() const { return const_reverse_iterator( begin() ); } 317 318 NV_CONSTEXPR size_type size() const { return m_size; } 319 NV_CONSTEXPR size_type length() const { return m_size; } 320 NV_CONSTEXPR size_type max_size() const { return m_size; } 321 NV_CONSTEXPR bool empty() const { return m_size == 0; } 322 323 // access 324 NV_CONSTEXPR const T& operator[]( size_type i ) const { return m_data[i]; } 325 const T& at( size_t i ) const 326 { 327 // if ( i >= m_data ) NV_THROW( std::out_of_range( "string_ref::at" ) ); 328 return m_data[i]; 329 } 330 331 NV_CONSTEXPR const T& front() const { return m_data[0]; } 332 NV_CONSTEXPR const T& back() const { return m_data[m_size - 1]; } 333 NV_CONSTEXPR const T* data() const { return m_data; } 334 335 // string operations 336 int compare( basic_string_base rhs ) const 337 { 338 int cmp = TRAITS::compare( m_data, rhs.m_data, ( std::min )( m_size, rhs.m_size ) ); 339 return cmp != 0 ? cmp : ( m_size == rhs.m_size ? 0 : m_size < rhs.m_size ? -1 : 1 ); 340 } 341 bool starts_with( T c ) const { return !empty() && TRAITS::eq( c, front() ); } 342 bool starts_with( basic_string_base s ) const 343 { 344 return m_size >= s.m_size && TRAITS::compare( m_data, s.m_data, s.m_size ) == 0; 345 } 346 bool ends_with( T c ) const { return !empty() && TRAITS::eq( c, back() ); } 347 bool ends_with( basic_string_base s ) const 348 { 349 return m_size >= s.m_size && TRAITS::compare( m_data + m_size - s.m_size, s.m_data, s.m_size ) == 0; 350 } 351 size_type find( basic_string_base s, size_type pos = 0 ) const 352 { 353 if ( pos >= m_size ) return npos; 354 const_iterator it = std::search( this->cbegin() + pos, this->cend(), s.cbegin(), s.cend(), TRAITS::eq ); 355 return it == this->cend() ? npos : std::distance( this->cbegin(), it ); 356 } 357 size_type find( T c, size_type pos = 0 ) const 358 { 359 if ( pos >= m_size ) return npos; 360 const_iterator it = std::find_if( this->cbegin() + pos, this->cend(), [=] ( T val ) { return TRAITS::eq( val, c ); } ); 361 return it == this->cend() ? npos : std::distance( this->cbegin(), it ); 362 } 363 size_type rfind( basic_string_base s, size_type pos = 0 ) const 364 { 365 if ( pos >= m_size ) return npos; 366 const_reverse_iterator it = std::search( this->crbegin() + pos, this->crend(), s.crbegin(), s.crend(), TRAITS::eq ); 367 return it == this->crend() ? npos : m_size - 1 - std::distance( this->crbegin(), it ); 368 } 369 size_type rfind( T c, size_type pos = 0 ) const 370 { 371 if ( pos >= m_size ) return npos; 372 const_reverse_iterator it = std::find_if( this->crbegin() + pos, this->crend(), [=] ( T val ) { return TRAITS::eq( val, c ); } ); 373 return it == this->crend() ? npos : m_size - 1 - std::distance( this->crbegin(), it ); 374 } 375 size_type find_first_of( T c ) const { return find( c ); } 376 size_type find_first_of( basic_string_base s ) const 377 { 378 const_iterator it = std::find_first_of( this->cbegin(), this->cend(), s.cbegin(), s.cend(), TRAITS::eq ); 379 return it == this->cend() ? npos : std::distance( this->cbegin(), it ); 380 } 381 size_type find_last_of( T c ) const { return rfind( c ); } 382 size_type find_last_of( basic_string_base s ) const 383 { 384 const_reverse_iterator it = std::find_first_of( this->crbegin(), this->crend(), s.cbegin(), s.cend(), TRAITS::eq ); 385 return it == this->crend() ? npos : m_size - 1 - std::distance( this->crbegin(), it ); 386 } 387 size_type find_first_not_of( basic_string_base s ) const 388 { 389 for ( const_iterator it = this->cbegin(); it != this->cend(); ++it ) 390 if ( 0 == TRAITS::find( s.m_data, s.m_size, *first ) ) 391 return std::distance( this->cbegin(), it ); 392 return npos; 393 } 394 size_type find_first_not_of( T c ) const 395 { 396 for ( const_iterator it = this->cbegin(); it != this->cend(); ++it ) 397 if ( !TRAITS::eq( c, *it ) ) 398 return std::distance( this->cbegin(), it ); 399 return npos; 400 } 401 size_type find_last_not_of( basic_string_base s ) const 402 { 403 for ( const_reverse_iterator it = this->crbegin(); it != this->crend(); ++it ) 404 if ( 0 == TRAITS::find( s.m_data, s.m_size, *it ) ) 405 return m_size - 1 - std::distance( this->crbegin(), it );; 406 return npos; 407 } 408 size_type find_last_not_of( T c ) const 409 { 410 for ( const_reverse_iterator it = this->crbegin(); it != this->crend(); ++it ) 411 if ( !TRAITS::eq( c, *it ) ) 412 return m_size - 1 - std::distance( this->crbegin(), it ); 413 return npos; 414 } 415 416 // string operations 417 basic_string_ref< T, TRAITS > substr( size_type p, size_type n = npos ) const 418 { 419 if ( p > size() ) return basic_string_ref< T, TRAITS >(); // NV_THROW( std::out_of_range( "substr" ) ); 420 if ( n == p || p + n > size() ) n = size() - p; 421 return basic_string_ref< T, TRAITS >( data() + p, n ); 422 } 423 424 protected: 425 NV_CONSTEXPR basic_string_base() : m_data( nullptr ), m_size( 0 ) {} 426 NV_CONSTEXPR basic_string_base( pointer a_data, size_type a_lenght ) 427 : m_data( a_data ), m_size( a_lenght ) {} 428 429 protected: 430 pointer m_data; 431 size_type m_size; 231 432 }; 232 template< size_t S > 233 struct string_length< char[S] > 234 { 235 static size_t get( const char[S] ) { return S-1; } 433 434 #define NV_STRING_BASE_CAST_OPERATORS( OPERATOR )\ 435 template < typename T, typename TRAITS, typename ALLOCATOR >\ 436 inline bool operator OPERATOR ( basic_string_base< T, TRAITS > lhs, const std::basic_string< T, TRAITS, ALLOCATOR >& rhs )\ 437 {\ 438 return lhs OPERATOR basic_string_base< T, TRAITS >( rhs );\ 439 }\ 440 template < typename T, typename TRAITS, typename ALLOCATOR >\ 441 inline bool operator OPERATOR ( const std::basic_string< T, TRAITS, ALLOCATOR >& lhs, basic_string_base< T, TRAITS > rhs )\ 442 {\ 443 return basic_string_base< T, TRAITS >( lhs ) OPERATOR rhs;\ 444 }\ 445 template < typename T, typename TRAITS >\ 446 inline bool operator OPERATOR ( basic_string_base<T, TRAITS> lhs, const T* rhs )\ 447 {\ 448 return lhs OPERATOR basic_string_base< T, TRAITS >( rhs );\ 449 }\ 450 template < typename T, typename TRAITS >\ 451 inline bool operator OPERATOR ( const T* lhs, basic_string_base< T, TRAITS > rhs )\ 452 {\ 453 return basic_string_base< T, TRAITS >( lhs ) OPERATOR rhs;\ 454 }\ 455 456 // Base operators 457 template < typename T, typename TRAITS > 458 inline bool operator==( basic_string_base< T, TRAITS > lhs, basic_string_base< T, TRAITS > rhs ) 459 { 460 return lhs.size() != rhs.size() ? false : ( lhs.compare( rhs ) == 0 ); 461 } 462 template < typename T, typename TRAITS > 463 inline bool operator!=( basic_string_base< T, TRAITS > lhs, basic_string_base< T, TRAITS > rhs ) 464 { 465 return lhs.size() != rhs.size() ? true : ( lhs.compare( rhs ) != 0 ); 466 } 467 template < typename T, typename TRAITS > 468 inline bool operator<( basic_string_base< T, TRAITS > lhs, basic_string_base< T, TRAITS > rhs ) 469 { 470 return lhs.compare( rhs ) < 0; 471 } 472 template < typename T, typename TRAITS > 473 inline bool operator>( basic_string_base< T, TRAITS > lhs, basic_string_base< T, TRAITS > rhs ) 474 { 475 return lhs.compare( rhs ) > 0; 476 } 477 template < typename T, typename TRAITS > 478 inline bool operator<=( basic_string_base< T, TRAITS > lhs, basic_string_base< T, TRAITS > rhs ) 479 { 480 return lhs.compare( rhs ) <= 0; 481 } 482 template < typename T, typename TRAITS > 483 inline bool operator>=( basic_string_base< T, TRAITS > lhs, basic_string_base< T, TRAITS > rhs ) 484 { 485 return lhs.compare( rhs ) >= 0; 486 } 487 488 NV_STRING_BASE_CAST_OPERATORS( == ) 489 NV_STRING_BASE_CAST_OPERATORS( != ) 490 NV_STRING_BASE_CAST_OPERATORS( < ) 491 NV_STRING_BASE_CAST_OPERATORS( > ) 492 NV_STRING_BASE_CAST_OPERATORS( <= ) 493 NV_STRING_BASE_CAST_OPERATORS( >= ) 494 495 template < class T, class TRAITS > 496 inline std::basic_ostream<T, TRAITS>& operator<<( std::basic_ostream<T, TRAITS>& os, const basic_string_base<T, TRAITS>& str ) 497 { 498 if ( os.good() ) 499 { 500 os.write( str.data(), str.size() ); 501 } 502 return os; 503 } 504 505 #undef NV_STRING_REF_CAST_OPERATORS 506 507 template<typename T, typename TRAITS> 508 class basic_string_ref : public basic_string_base < T, TRAITS > 509 { 510 public: 511 512 NV_CONSTEXPR basic_string_ref() {} 513 NV_CONSTEXPR basic_string_ref( const basic_string_ref &rhs ) 514 : basic_string_base( rhs.m_data, rhs.m_size ) 515 { 516 } 517 NV_CONSTEXPR basic_string_ref( const basic_string_base< T, TRAITS > &rhs ) 518 : basic_string_base( rhs.m_data, rhs.m_size ) 519 { 520 } 521 522 template< typename ALLOCATOR > 523 basic_string_ref( const std::basic_string<T, TRAITS, ALLOCATOR>& str ) 524 : basic_string_base( str.data(), str.length() ) 525 { 526 } 527 528 NV_CONSTEXPR basic_string_ref( const T* str, size_type len ) 529 : basic_string_base( str, len ) 530 { 531 } 532 533 // Literal constructors 534 template< size_t N, typename std::enable_if<std::is_same<T, char>::value>::type* = nullptr > 535 basic_string_ref( T( &s )[N] ) : basic_string_base( s, N - 1 ) {} 536 template< size_t N, typename std::enable_if<std::is_same<T, char>::value>::type* = nullptr > 537 basic_string_ref( const T( &s )[N] ) : basic_string_base( s, N - 1 ) {} 538 539 // Non-literal constructors 540 template< typename U, typename std::enable_if<std::is_same<U, const char*>::value>::type* = nullptr > 541 basic_string_ref( U str ) : basic_string_base( str, TRAITS::length( str ) ) {} 542 543 basic_string_ref& operator=( const basic_string_ref &rhs ) 544 { 545 m_data = rhs.m_data; 546 m_size = rhs.m_size; 547 return *this; 548 } 549 550 // iterators 551 NV_CONSTEXPR const_iterator begin() const { return m_data; } 552 NV_CONSTEXPR const_iterator end() const { return m_data + m_size; } 553 const_reverse_iterator rbegin() const { return const_reverse_iterator( end() ); } 554 const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); } 555 556 // modifiers 557 void clear() 558 { 559 m_size = 0; 560 m_data = nullptr; 561 } 562 void remove_prefix( size_type n ) 563 { 564 if ( n > m_size ) n = m_size; 565 m_data += n; 566 m_size -= n; 567 } 568 void remove_suffix( size_type n ) 569 { 570 if ( n > m_size ) n = m_size; 571 m_size -= n; 572 } 573 236 574 }; 237 template< size_t S > 238 struct string_length< const char[S] > 239 { 240 static size_t get( const char[S] ) { return S-1; } 575 576 typedef basic_string_ref<char, std::char_traits<char> > string_ref; 577 578 // const string is movable but not copyable 579 template<typename T, typename TRAITS> 580 class basic_const_string : public basic_string_base < T, TRAITS > 581 { 582 public: 583 basic_const_string() {} 584 basic_const_string( const T* str, size_type len ) 585 { 586 initialize( str, len ); 587 } 588 explicit basic_const_string( const T* str ) 589 { 590 initialize( str, TRAITS::length( str ) ); 591 } 592 593 ~basic_const_string() 594 { 595 if ( m_data ) delete m_data; 596 } 597 598 basic_const_string( const basic_const_string&& other ) 599 { 600 std::swap( m_data, other.m_data ); 601 return *this; 602 } 603 604 basic_const_string& operator=( const basic_const_string&& other ) 605 { 606 std::swap( m_data, other.m_data ); 607 return *this; 608 } 609 610 private: 611 // blocked copy constructor 612 basic_const_string( const basic_const_string & ); 613 // blocked copy operator 614 basic_const_string& operator=( const basic_const_string& ); 615 616 void assign( const T* p, size_type s ) 617 { 618 m_data = new T[s + 1]; 619 TRAITS::copy( m_data, p, s ); 620 m_data[s] = 0; 621 } 241 622 }; 242 template<>243 struct string_length< char* >244 {245 static size_t get( const char* s ) { return std::strlen( s ); }246 };247 template<>248 struct string_length< const char* >249 {250 static size_t get( const char* s ) { return std::strlen( s ); }251 };252 template<>253 struct string_length< std::string >254 {255 static size_t get( const std::string& s ) { return s.length(); }256 };257 623 258 624 -
trunk/nv/lua/lua_path.hh
r359 r360 16 16 #include <nv/core/common.hh> 17 17 #include <nv/core/string.hh> 18 #include <nv/core/string_ref.hh>19 18 #include <cstring> 20 19 -
trunk/nv/lua/lua_state.hh
r358 r360 15 15 #include <nv/core/flags.hh> 16 16 #include <nv/core/handle.hh> 17 #include <nv/core/string_ref.hh>18 17 #include <istream> 19 18 #include <map> … … 23 22 #include <nv/lua/lua_values.hh> 24 23 #include <nv/lua/lua_dispatch.hh> 25 #include <string> 24 26 25 27 26 #ifdef NV_DEBUG … … 130 129 } 131 130 bool is_defined( const path& p ) { return is_defined( p, m_global ); } 132 void register_native_function( lfunction f, const char*name );131 void register_native_function( lfunction f, string_ref name ); 133 132 134 133 template < typename F, F f > 135 void register_function( const char*name )134 void register_function( string_ref name ) 136 135 { 137 136 register_native_function( detail::function_wrapper< F, f >, name ); … … 139 138 140 139 template < typename C, typename F, F f > 141 struct register_member 142 { 143 register_member( const char* ) { } 144 }; 145 146 template < typename C, typename F, F f > 147 void register_function( const char* name ) 140 void register_function( string_ref name ) 148 141 { 149 142 register_native_function( detail::object_method_wrapper< C, F, f >, name ); … … 199 192 explicit state( bool load_libs = false ); 200 193 explicit state( lua_State* state ); 201 bool do_string( const std::string& code, const std::string&name, int rvalues = 0 );202 bool do_stream( std::istream& stream, const std::string&name );203 bool do_file( const std::string&filename );194 bool do_string( string_ref code, string_ref name, int rvalues = 0 ); 195 bool do_stream( std::istream& stream, string_ref name ); 196 bool do_file( string_ref filename ); 204 197 int get_stack_size(); 205 198 void log_stack(); 206 199 lua_State* get_raw(); 207 ref register_object( void* o, const char*lua_name );208 ref register_proto( const char* id, const char*storage );209 void store_metadata( ref object_index, const std::string&metaname, void* pointer );200 ref register_object( void* o, string_ref lua_name ); 201 ref register_proto( string_ref id, string_ref storage ); 202 void store_metadata( ref object_index, string_ref metaname, void* pointer ); 210 203 void unregister_object( ref object_index ); 211 204 212 void register_enum( const char*name, int value );213 void register_singleton( const char*name, void* o );214 215 void register_native_object_method( const char* lua_name, const char*name, lfunction f );205 void register_enum( string_ref name, int value ); 206 void register_singleton( string_ref name, void* o ); 207 208 void register_native_object_method( string_ref lua_name, string_ref name, lfunction f ); 216 209 template < typename F, F f > 217 void register_object_method( const char* lua_name, const char*name )210 void register_object_method( string_ref lua_name, string_ref name ) 218 211 { 219 212 register_native_object_method( lua_name, name, detail::object_method_wrapper< typename memfn_class_type<F>::type, F, f > ); … … 232 225 } 233 226 template < typename H > 234 ref register_handle_component( const H& handle, const std::string&id )227 ref register_handle_component( const H& handle, string_ref id ) 235 228 { 236 229 nv::lua::push_handle( m_state, handle ); … … 238 231 } 239 232 template < typename H > 240 ref register_handle_component( const H& handle, const std::string&id, const path& proto )233 ref register_handle_component( const H& handle, string_ref id, const path& proto ) 241 234 { 242 235 if ( !proto.resolve( m_state, true ) ) … … 248 241 } 249 242 template < typename H > 250 void unregister_handle_component( const H& handle, const std::string&id )243 void unregister_handle_component( const H& handle, string_ref id ) 251 244 { 252 245 nv::lua::push_handle( m_state, handle ); … … 285 278 286 279 private: 287 ref register_handle_component_impl( const std::string&id, bool empty );288 void unregister_handle_component_impl( const std::string&id );289 290 int load_string( const std::string& code, const std::string&name );291 int load_stream( std::istream& stream, const std::string&name );292 int load_file( const std::string&filename );293 int do_current( const std::string&name, int rvalues = 0 );280 ref register_handle_component_impl( string_ref id, bool empty ); 281 void unregister_handle_component_impl( string_ref id ); 282 283 int load_string( string_ref code, string_ref name ); 284 int load_stream( std::istream& stream, string_ref name ); 285 int load_file( string_ref filename ); 286 int do_current( string_ref name, int rvalues = 0 ); 294 287 void deep_pointer_copy( int index, void* obj ); 295 288 }; … … 302 295 virtual ~table_guard(); 303 296 size_t get_size(); 304 bool has_field( const std::string&element );305 std::string get_string( const std::string& element, const std::string& defval = "");306 char get_char( const std::string&element, char defval = ' ' );307 int get_integer( const std::string&element, int defval = 0 );308 unsigned get_unsigned( const std::string&element, unsigned defval = 0 );309 double get_double( const std::string&element, double defval = 0.0 );310 float get_float( const std::string&element, float defval = 0.0 );311 bool get_boolean( const std::string&element, bool defval = false );312 bool is_table( const std::string&element );313 bool is_number( const std::string&element );314 bool is_boolean( const std::string&element );315 bool is_string( const std::string&element );297 bool has_field( string_ref element ); 298 std::string get_string( string_ref element, string_ref defval = string_ref() ); 299 char get_char( string_ref element, char defval = ' ' ); 300 int get_integer( string_ref element, int defval = 0 ); 301 unsigned get_unsigned( string_ref element, unsigned defval = 0 ); 302 double get_double( string_ref element, double defval = 0.0 ); 303 float get_float( string_ref element, float defval = 0.0 ); 304 bool get_boolean( string_ref element, bool defval = false ); 305 bool is_table( string_ref element ); 306 bool is_number( string_ref element ); 307 bool is_boolean( string_ref element ); 308 bool is_string( string_ref element ); 316 309 private: 317 310 int m_level; -
trunk/nv/lua/lua_values.hh
r358 r360 11 11 #include <nv/core/type_traits.hh> 12 12 #include <nv/core/string.hh> 13 #include <nv/core/string_ref.hh>14 13 15 14 struct lua_State; -
trunk/src/lua/lua_map_area.cc
r319 r360 57 57 if ( lua_istable( L , index ) ) 58 58 { 59 lua_push string( L, "__map_area_ptr" );59 lua_pushliteral( L, "__map_area_ptr" ); 60 60 lua_rawget( L, index ); 61 61 if ( lua_isuserdata( L, -1 ) ) … … 96 96 static int nlua_map_area_tostring( lua_State* L ) 97 97 { 98 lua_push string( L, "map_area" );98 lua_pushliteral ( L, "map_area" ); 99 99 return 1; 100 100 } … … 236 236 { 237 237 lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() ); 238 lua_push string( L, "__map_area_ptr" );238 lua_pushliteral( L, "__map_area_ptr" ); 239 239 lua_pushlightuserdata( L, (map_area*)area ); 240 240 lua_rawset( L, -3 ); -
trunk/src/lua/lua_path.cc
r359 r360 18 18 size_t point = spath.find( '.' ); 19 19 20 while ( point != st d::string::npos )20 while ( point != string_ref::npos ) 21 21 { 22 22 m_elements[m_count].str = spath.data(); -
trunk/src/lua/lua_state.cc
r341 r360 80 80 } 81 81 82 void lua::state_wrapper::register_native_function( lfunction f, const char*name )82 void lua::state_wrapper::register_native_function( lfunction f, string_ref name ) 83 83 { 84 84 if ( m_global ) push_global_table(); 85 85 lua_pushcfunction( m_state, f ); 86 lua_setfield( m_state, -2, name );86 lua_setfield( m_state, -2, name.data() ); 87 87 if ( m_global ) pop_global_table(); 88 88 } … … 129 129 if ( status != 0 ) 130 130 { 131 std::string error = lua_tostring( m_state, -1);131 NV_LOG( LOG_ERROR, "Lua error : " << lua_tostring( m_state, -1 ) ); 132 132 lua_pop( m_state, 1 ); 133 NV_LOG( LOG_ERROR, "Lua error : " << error )134 133 } 135 134 return status; … … 172 171 } 173 172 174 bool lua::table_guard::has_field( const string&element )175 { 176 lua_getfield( m_state, -1, element. c_str() );173 bool lua::table_guard::has_field( string_ref element ) 174 { 175 lua_getfield( m_state, -1, element.data() ); 177 176 bool result = !( lua_isnil( m_state, -1 ) ); 178 177 lua_pop( m_state, 1 ); … … 180 179 } 181 180 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() );181 string 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 189 char lua::table_guard::get_char( string_ref element, char defval /*= "" */ ) 190 { 191 lua_getfield( m_state, -1, element.data() ); 193 192 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 194 193 lua_pop( m_state, 1 ); … … 196 195 } 197 196 198 int lua::table_guard::get_integer( const string&element, int defval /*= "" */ )199 { 200 lua_getfield( m_state, -1, element. c_str() );197 int lua::table_guard::get_integer( string_ref element, int defval /*= "" */ ) 198 { 199 lua_getfield( m_state, -1, element.data() ); 201 200 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 202 201 lua_pop( m_state, 1 ); … … 204 203 } 205 204 206 unsigned lua::table_guard::get_unsigned( const string&element, unsigned defval /*= "" */ )207 { 208 lua_getfield( m_state, -1, element. c_str() );205 unsigned lua::table_guard::get_unsigned( string_ref element, unsigned defval /*= "" */ ) 206 { 207 lua_getfield( m_state, -1, element.data() ); 209 208 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval; 210 209 lua_pop( m_state, 1 ); … … 212 211 } 213 212 214 double lua::table_guard::get_double( const string&element, double defval /*= "" */ )215 { 216 lua_getfield( m_state, -1, element. c_str() );213 double lua::table_guard::get_double( string_ref element, double defval /*= "" */ ) 214 { 215 lua_getfield( m_state, -1, element.data() ); 217 216 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 218 217 lua_pop( m_state, 1 ); … … 221 220 222 221 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() );222 float nv::lua::table_guard::get_float( string_ref element, float defval /*= 0.0 */ ) 223 { 224 lua_getfield( m_state, -1, element.data() ); 226 225 float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? (float)lua_tonumber( m_state, -1 ) : defval; 227 226 lua_pop( m_state, 1 ); … … 229 228 } 230 229 231 bool lua::table_guard::get_boolean( const string&element, bool defval /*= "" */ )232 { 233 lua_getfield( m_state, -1, element. c_str() );230 bool lua::table_guard::get_boolean( string_ref element, bool defval /*= "" */ ) 231 { 232 lua_getfield( m_state, -1, element.data() ); 234 233 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 235 234 lua_pop( m_state, 1 ); … … 237 236 } 238 237 239 bool nv::lua::table_guard::is_table( const std::string&element )240 { 241 lua_getfield( m_state, -1, element. c_str() );238 bool nv::lua::table_guard::is_table( string_ref element ) 239 { 240 lua_getfield( m_state, -1, element.data() ); 242 241 bool result = lua_type( m_state, -1 ) == LUA_TTABLE; 243 242 lua_pop( m_state, 1 ); … … 245 244 } 246 245 247 bool nv::lua::table_guard::is_number( const std::string&element )248 { 249 lua_getfield( m_state, -1, element. c_str() );246 bool nv::lua::table_guard::is_number( string_ref element ) 247 { 248 lua_getfield( m_state, -1, element.data() ); 250 249 bool result = lua_type( m_state, -1 ) == LUA_TNUMBER; 251 250 lua_pop( m_state, 1 ); … … 253 252 } 254 253 255 bool nv::lua::table_guard::is_boolean( const std::string&element )256 { 257 lua_getfield( m_state, -1, element. c_str() );254 bool nv::lua::table_guard::is_boolean( string_ref element ) 255 { 256 lua_getfield( m_state, -1, element.data() ); 258 257 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN; 259 258 lua_pop( m_state, 1 ); … … 261 260 } 262 261 263 bool nv::lua::table_guard::is_string( const std::string&element )264 { 265 lua_getfield( m_state, -1, element. c_str() );262 bool nv::lua::table_guard::is_string( string_ref element ) 263 { 264 lua_getfield( m_state, -1, element.data() ); 266 265 bool result = lua_type( m_state, -1 ) == LUA_TSTRING; 267 266 lua_pop( m_state, 1 ); … … 321 320 } 322 321 323 int lua::state::load_string( const std::string& code, const std::string&name )322 int lua::state::load_string( string_ref code, string_ref name ) 324 323 { 325 324 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 328 int lua::state::load_stream( std::istream& stream, string_ref name ) 330 329 { 331 330 NV_LOG( nv::LOG_NOTICE, "Loading Lua stream '" << name << "'"); … … 335 334 } 336 335 337 int lua::state::load_file( const std::string&filename )336 int lua::state::load_file( string_ref filename ) 338 337 { 339 338 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 342 bool lua::state::do_string( string_ref code, string_ref name, int rvalues ) 344 343 { 345 344 lua::stack_guard( this ); … … 353 352 } 354 353 355 bool lua::state::do_stream( std::istream& stream, const std::string&name )354 bool lua::state::do_stream( std::istream& stream, string_ref name ) 356 355 { 357 356 lua::stack_guard( this ); … … 365 364 } 366 365 367 bool lua::state::do_file( const std::string&filename )366 bool lua::state::do_file( string_ref filename ) 368 367 { 369 368 lua::stack_guard( this ); … … 377 376 } 378 377 379 int lua::state::do_current( const std::string&name, int rvalues )378 int lua::state::do_current( string_ref name, int rvalues ) 380 379 { 381 380 int result = lua_pcall(m_state, 0, rvalues, 0); … … 408 407 } 409 408 410 lua::ref lua::state::register_object( void* o, const char*lua_name )409 lua::ref lua::state::register_object( void* o, string_ref lua_name ) 411 410 { 412 411 if ( o == nullptr ) return lua::ref( lua::ref::none ); 413 412 stack_guard guard( this ); 414 lua_getglobal( m_state, lua_name );413 lua_getglobal( m_state, lua_name.data() ); 415 414 if ( lua_isnil( m_state, -1 ) ) 416 415 { 417 NV_THROW( runtime_error, std::string( lua_name) + " type not registered!" );416 NV_THROW( runtime_error, lua_name.to_string() + " type not registered!" ); 418 417 } 419 418 deep_pointer_copy( -1, o ); … … 421 420 } 422 421 423 lua::ref lua::state::register_proto( const char* id, const char*storage )422 lua::ref lua::state::register_proto( string_ref id, string_ref storage ) 424 423 { 425 424 stack_guard guard( this ); 426 lua_getglobal( m_state, storage );425 lua_getglobal( m_state, storage.data() ); 427 426 if ( lua_isnil( m_state, -1 ) ) 428 427 { 429 NV_THROW( runtime_error, st d::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() ); 432 431 if ( lua_isnil( m_state, -1 ) ) 433 432 { 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!" ); 435 434 } 436 435 return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) ); 437 436 } 438 437 439 void lua::state::register_native_object_method( const char* lua_name, const char*name, lfunction f )438 void lua::state::register_native_object_method( string_ref lua_name, string_ref name, lfunction f ) 440 439 { 441 440 stack_guard guard( this ); 442 lua_getglobal( m_state, lua_name );441 lua_getglobal( m_state, lua_name.data() ); 443 442 if ( lua_isnil( m_state, -1 ) ) 444 443 { 445 NV_THROW( runtime_error, std::string( lua_name) + " type not registered!" );444 NV_THROW( runtime_error, lua_name.to_string() + " type not registered!" ); 446 445 } 447 446 lua_pushcfunction( m_state, f ); 448 lua_setfield( m_state, -2, name );447 lua_setfield( m_state, -2, name.data() ); 449 448 } 450 449 … … 454 453 stack_guard guard( this ); 455 454 lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() ); 456 lua_push string( m_state, "__ptr" );455 lua_pushliteral( m_state, "__ptr" ); 457 456 lua_pushboolean( m_state, false ); 458 457 lua_rawset( m_state, -3 ); … … 499 498 } 500 499 501 void nv::lua::state::store_metadata( ref object_index, const std::string&metaname, void* pointer )500 void nv::lua::state::store_metadata( ref object_index, string_ref metaname, void* pointer ) 502 501 { 503 502 if ( !object_index.is_valid() ) return; 504 503 lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() ); 505 lua_push string( m_state, metaname.c_str() );504 lua_pushlstring( m_state, metaname.data(), metaname.size() ); 506 505 lua_pushlightuserdata( m_state, pointer ); 507 506 lua_rawset( m_state, -3 ); … … 509 508 } 510 509 511 void nv::lua::state::register_enum( const char*name, int value )510 void nv::lua::state::register_enum( string_ref name, int value ) 512 511 { 513 512 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 516 nv::lua::ref nv::lua::state::register_handle_component_impl( string_ref id, bool empty ) 518 517 { 519 518 int args = empty ? 1 : 2; … … 529 528 else 530 529 nlua_deepcopy( m_state, -2 ); 531 lua_push string( m_state, id.c_str() );530 lua_pushlstring( m_state, id.data(), id.size() ); 532 531 lua_pushvalue( m_state, -2 ); 533 532 lua_rawset( m_state, -4 ); … … 538 537 } 539 538 540 void nv::lua::state::unregister_handle_component_impl( const std::string&id )539 void nv::lua::state::unregister_handle_component_impl( string_ref id ) 541 540 { 542 541 NV_LUA_STACK_ASSERT( m_state, -1 ); … … 547 546 return; 548 547 } 549 lua_push string( m_state, id.c_str() );548 lua_pushlstring( m_state, id.data(), id.size() ); 550 549 lua_pushnil( m_state ); 551 550 lua_rawset( m_state, -3 ); … … 553 552 } 554 553 555 void nv::lua::state::register_singleton( const char*name, void* o )554 void nv::lua::state::register_singleton( string_ref name, void* o ) 556 555 { 557 556 if ( o == nullptr ) return; 558 557 stack_guard guard( this ); 559 lua_getglobal( m_state, name );558 lua_getglobal( m_state, name.data() ); 560 559 if ( lua_isnil( m_state, -1 ) ) 561 560 { 562 NV_THROW( runtime_error, std::string( name) + " type not registered!" );561 NV_THROW( runtime_error, name.to_string() + " type not registered!" ); 563 562 } 564 563 deep_pointer_copy( -1, o ); 565 lua_setglobal( m_state, name );566 } 567 564 lua_setglobal( m_state, name.data() ); 565 } 566 -
trunk/src/lua/lua_values.cc
r358 r360 191 191 if ( lua_istable( L , index ) ) 192 192 { 193 lua_push string( L, "__ptr" );193 lua_pushliteral( L, "__ptr" ); 194 194 lua_rawget( L, index ); 195 195 if ( lua_isuserdata( L, -1 ) )
Note: See TracChangeset
for help on using the changeset viewer.