Changeset 360 for trunk


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
Location:
trunk
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/string.hh

    r352 r360  
    22// This file is part of NV Libraries.
    33// 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.
    419
    520#ifndef NV_CORE_STRING_HH
     
    225240        }
    226241
     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
    227281        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;
    231432        };
    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 )\
     435template < typename T, typename TRAITS, typename ALLOCATOR >\
     436inline 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                }\
     440template < typename T, typename TRAITS, typename ALLOCATOR >\
     441inline 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                }\
     445template < typename T, typename TRAITS >\
     446inline bool operator OPERATOR ( basic_string_base<T, TRAITS> lhs, const T* rhs )\
     447                {\
     448        return lhs OPERATOR basic_string_base< T, TRAITS >( rhs );\
     449                }\
     450template < typename T, typename TRAITS >\
     451inline 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
    236574        };
    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                }
    241622        };
    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         };
    257623
    258624
  • trunk/nv/lua/lua_path.hh

    r359 r360  
    1616#include <nv/core/common.hh>
    1717#include <nv/core/string.hh>
    18 #include <nv/core/string_ref.hh>
    1918#include <cstring>
    2019
  • trunk/nv/lua/lua_state.hh

    r358 r360  
    1515#include <nv/core/flags.hh>
    1616#include <nv/core/handle.hh>
    17 #include <nv/core/string_ref.hh>
    1817#include <istream>
    1918#include <map>
     
    2322#include <nv/lua/lua_values.hh>
    2423#include <nv/lua/lua_dispatch.hh>
    25 #include <string>
     24
    2625
    2726#ifdef NV_DEBUG
     
    130129                        }
    131130                        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 );
    133132
    134133                        template < typename F, F f >
    135                         void register_function( const char* name )
     134                        void register_function( string_ref name )
    136135                        {
    137136                                register_native_function( detail::function_wrapper< F, f >, name );
     
    139138
    140139                        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 )
    148141                        {
    149142                                register_native_function( detail::object_method_wrapper< C, F, f >, name );
     
    199192                        explicit state( bool load_libs = false );
    200193                        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 );
    204197                        int get_stack_size();
    205198                        void log_stack();
    206199                        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 );
    210203                        void unregister_object( ref object_index );
    211204
    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 );
    216209                        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 )
    218211                        {
    219212                                register_native_object_method( lua_name, name, detail::object_method_wrapper< typename memfn_class_type<F>::type, F, f > );
     
    232225                        }
    233226                        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 )
    235228                        {
    236229                                nv::lua::push_handle( m_state, handle );
     
    238231                        }
    239232                        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 )
    241234                        {
    242235                                if ( !proto.resolve( m_state, true ) )
     
    248241                        }
    249242                        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 )
    251244                        {
    252245                                nv::lua::push_handle( m_state, handle );
     
    285278
    286279                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 );
    294287                        void deep_pointer_copy( int index, void* obj );
    295288                };
     
    302295                        virtual ~table_guard();
    303296                        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 );
    316309                private:
    317310                        int m_level;
  • trunk/nv/lua/lua_values.hh

    r358 r360  
    1111#include <nv/core/type_traits.hh>
    1212#include <nv/core/string.hh>
    13 #include <nv/core/string_ref.hh>
    1413
    1514struct lua_State;
  • trunk/src/lua/lua_map_area.cc

    r319 r360  
    5757        if ( lua_istable( L , index ) )
    5858        {
    59                 lua_pushstring( L, "__map_area_ptr" );
     59                lua_pushliteral( L, "__map_area_ptr" );
    6060                lua_rawget( L, index );
    6161                if ( lua_isuserdata( L, -1 ) )
     
    9696static int nlua_map_area_tostring( lua_State* L )
    9797{
    98         lua_pushstring( L, "map_area" );
     98        lua_pushliteral ( L, "map_area" );
    9999        return 1;
    100100}
     
    236236{
    237237        lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() );
    238         lua_pushstring( L, "__map_area_ptr" );
     238        lua_pushliteral( L, "__map_area_ptr" );
    239239        lua_pushlightuserdata( L, (map_area*)area );
    240240        lua_rawset( L, -3 );
  • trunk/src/lua/lua_path.cc

    r359 r360  
    1818        size_t point = spath.find( '.' );
    1919
    20         while ( point != std::string::npos )
     20        while ( point != string_ref::npos )
    2121        {
    2222                m_elements[m_count].str    = spath.data();
  • 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
  • trunk/src/lua/lua_values.cc

    r358 r360  
    191191        if ( lua_istable( L , index ) )
    192192        {
    193                 lua_pushstring( L, "__ptr" );
     193                lua_pushliteral( L, "__ptr" );
    194194                lua_rawget( L, index );
    195195                if ( lua_isuserdata( L, -1 ) )
Note: See TracChangeset for help on using the changeset viewer.