Changeset 445 for trunk


Ignore:
Timestamp:
07/29/15 16:24:39 (10 years ago)
Author:
epyon
Message:
  • cleaning up string usage in lua (WIP)
  • cleaned up types.hh (WIP)
Location:
trunk
Files:
8 edited

Legend:

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

    r438 r445  
    1919namespace nv
    2020{
    21         typedef nv::uint64 type_hash;
     21
     22        struct type_hash_tag {};
     23
     24        class thash64 : public hash_value< uint64, type_hash_tag >
     25        {
     26        public:
     27                typedef uint64 hash_type;
     28                typedef uint64 value_type;
     29                typedef thash64 this_type;
     30                typedef hash_value< uint64, type_hash_tag > inherited_type;
     31
     32                // TODO: enforce exact type?
     33                constexpr thash64() : inherited_type() {}
     34                template < typename T >
     35                constexpr thash64() : inherited_type( rtti_type_hash< T >::hash() ) {}
     36                constexpr explicit thash64( hash_type value ) : inherited_type( value ) {}
     37                constexpr thash64( const thash64& value ) = default;
     38        };
    2239
    2340        template< typename T >
     
    5774        {
    5875                shash64     name;     //!< name of the field
    59                 type_hash   hash; //!< typeinfo for later retrieval of type
    6076                type_entry* type;     //!< pointer to field type
    6177                uint32      flags;    //!< flags
     
    7187        struct type_entry
    7288        {
    73                 // Function types for the constructor and destructor of registered types
    74                 typedef void( *constructor_func )( void* );
    75                 typedef void( *destructor_func )( void* );
    76 
    77                 type_database*          type_db;     //!< Parent type database
    78                 uint64                  name;        //!< Scoped C++ name of the type
    79                 constructor_func        constructor; //!< Pointers to the constructor
    80                 destructor_func         destructor;  //!< Pointers to the destructor
    81                 size_t                  size;        //!< Result of sizeof(type) operation
    82                 type_entry*             base_type;   //!< Base type
     89                type_database*     type_db;     //!< Parent type database
     90                shash64            name;        //!< Scoped C++ name of the type
     91                constructor_t      constructor; //!< Pointers to the constructor
     92                destructor_t       destructor;  //!< Pointers to the destructor
     93                size_t             size;        //!< Result of sizeof(type) operation
     94                type_entry*        base_type;   //!< Base type
    8395                vector<type_field> field_list;  //!< Field list
    8496                vector<type_enum>  enum_list;   //!< Enum list
     97                hash_store< shash64, type_field* > field_names;
     98                hash_store< shash64, type_enum* >  enum_names;
    8599        };
    86100
     
    95109
    96110                template< typename TOBJECT, typename TFIELD >
    97                 type_creator field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< is_container<TFIELD>::value, void* >::type = nullptr );
     111                type_creator field( const string_view& aname, TFIELD TOBJECT::*field, typename enable_if< is_container<TFIELD>::value, void* >::type = nullptr );
    98112
    99113                template< typename TOBJECT, typename TFIELD >
    100                 type_creator field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type = nullptr );
    101 
    102                 type_creator value( const char* name, sint32 value );
     114                type_creator field( const string_view& aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type = nullptr );
     115
     116                type_creator value( const string_view& aname, sint32 value );
    103117        private:
    104118                type_database* m_database;
     
    112126
    113127                template< typename TYPE >
    114                 type_creator create_type( const char* name )
     128                type_creator create_type( const string_view& name )
    115129                {
    116130                        NV_ASSERT( !m_names.exists( name ), "Type redefinition!" );
    117                         uint64 name_hash = m_names.insert( name );
    118131                        type_entry* i_type = new type_entry;
    119132                        i_type->type_db = this;
    120                         i_type->name = name_hash;
     133                        i_type->name = m_names.insert( name );
    121134                        i_type->size = sizeof( TYPE );
    122135
    123136                        i_type->constructor = raw_construct_object < TYPE >;
    124137                        i_type->destructor  = raw_destroy_object < TYPE >;
    125                         m_index_by_type[typeid( TYPE )] = i_type;
    126                         m_index_by_name[name_hash] = i_type;
     138                        m_index_by_type[ thash64< TYPE >() ] = i_type;
     139                        m_index_by_name[ i_type->name ] = i_type;
    127140                        m_type_list.push_back( i_type );
    128141                        return type_creator( this, i_type );
    129142                }
    130143
    131                 type_entry* get_type( const char* name )
    132                 {
    133                         uint64 name_hash = hash_string<type_hash>( name );
    134                         auto it = m_index_by_name.find( name_hash );
     144                type_entry* get_type( shash64 name )
     145                {
     146                        auto it = m_index_by_name.find( name );
    135147                        return it != m_index_by_name.end() ? it->second : nullptr;
    136148                }
    137149
    138                 type_entry* get_type( type_hash hash )
    139                 {
    140                         type_hash_map::iterator it = m_index_by_type.find( hash );
    141                         if ( it != m_index_by_type.end() )
    142                         {
    143                                 return it->second;
    144                         }
    145                         return nullptr;
     150                type_entry* get_type( thash64 hash )
     151                {
     152                        auto it = m_index_by_type.find( hash );
     153                        return it != m_index_by_type.end() ? it->second : nullptr;
     154                }
     155
     156                template< typename T >
     157                type_entry* get_type()
     158                {
     159                        auto it = m_index_by_type.find( thash64< T >() );
     160                        return it != m_index_by_type.end() ? it->second : nullptr;
    146161                }
    147162
     
    156171                }
    157172        private:
    158                 typedef vector<type_entry*>                   type_list;
    159                 typedef unordered_map<type_hash, type_entry*> type_hash_map;
    160                 string_table  m_names;
    161                 type_list     m_type_list;
    162                 type_hash_map m_index_by_type;
    163                 type_hash_map m_index_by_name;
     173                string_table                       m_names;
     174                vector<type_entry*>                m_type_list;
     175                hash_store< shash64, type_entry* > m_index_by_name;
     176                hash_store< thash64, type_entry* > m_index_by_type;
    164177        };
    165178       
     
    167180        type_creator type_creator::base()
    168181        {
    169                 m_entry->base_type = m_database->get_type( typeid( TYPE ) );
     182                m_entry->base_type = m_database->get_type< TYPE >();
    170183                return *this;
    171184        }
    172185
    173186        template< typename TOBJECT, typename TFIELD >
    174         type_creator type_creator::field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< is_container<TFIELD>::value, void* >::type )
     187        type_creator type_creator::field( const string_view& aname, TFIELD TOBJECT::*field, typename enable_if< is_container<TFIELD>::value, void* >::type )
    175188        {
    176189                typedef typename TFIELD::value_type field_type;
     
    178191                type_field f;
    179192                f.name = m_database->m_names.insert( name );
    180                 f.hash = rtti_type_hash< remove_pointer_t<field_type> >::hash();
    181                 f.type = type_db->get_type( f.hash );
     193                f.type = type_db->get_type< remove_pointer_t<TFIELD> >();
    182194                f.flags = TF_CONTAINER |
    183195                        ( is_pointer<field_type>::value ? TF_POINTER : 0 ) |
     
    189201
    190202        template< typename TOBJECT, typename TFIELD >
    191         type_creator type_creator::field( const char* aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type )
     203        type_creator type_creator::field( const string_view& aname, TFIELD TOBJECT::*field, typename enable_if< !is_container<TFIELD>::value, void* >::type )
    192204        {
    193205                NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" );
    194206                type_field f;
    195207                f.name = m_database->m_names.insert( name );
    196                 f.hash = rtti_type_hash< remove_pointer_t<TFIELD> >::hash();
    197                 f.type = type_db->get_type( f.hash );
     208                f.type = type_db->get_type< remove_pointer_t<TFIELD> >();
    198209                f.flags =
    199210                        ( is_pointer<TFIELD>::value ? TF_POINTER : 0 ) |
     
    204215        }
    205216
    206         inline type_creator type_creator::value( const char* name, sint32 value )
     217        inline type_creator type_creator::value( const string_view& aname, sint32 value )
    207218        {
    208219                NV_ASSERT( m_entry->field_list.empty(), "Type cannot have both enums and fields!" );
    209220                type_enum e;
    210                 e.name = m_database->m_names.insert( name );
     221                e.name = m_database->m_names.insert( aname );
    211222                e.value = value;
    212223                m_entry->enum_list.push_back( e );
  • trunk/nv/lua/lua_state.hh

    r440 r445  
    111111
    112112                        template< typename R, typename T >
    113                         R get( const T& key, const R& def )
     113                        R get( T&& key, const R& def )
    114114                        {
    115115                                if ( m_global ) push_global_table();
    116                                 detail::push_value( m_state, key );
     116                                detail::push_value( m_state, ::nv::forward<T>( key ) );
    117117                                call_get();
    118118                                if ( m_global ) pop_global_table();
     
    121121
    122122                        template< typename R, typename T >
    123                         R raw_get( const T& key, const R& def )
     123                        R raw_get( T&& key, const R& def )
    124124                        {
    125125                                if ( m_global ) push_global_table();
    126                                 detail::push_value( m_state, key );
     126                                detail::push_value( m_state, ::nv::forward<T>( key ) );
    127127                                call_get_raw();
    128128                                if ( m_global ) pop_global_table();
     
    247247                        }
    248248
     249                        using state_wrapper::call;
     250
    249251                        template < typename R >
    250                         R call( ref table, const path& p )
     252                        R call_table( ref table, const path& p )
    251253                        {
    252254                                stack_guard guard( this ); // TODO: remove
     
    262264                        }
    263265                        template < typename R, typename... Args >
    264                         R call( ref table, const path& p, Args&&... args )
     266                        R call_table( ref table, const path& p, Args&&... args )
    265267                        {
    266268                                stack_guard guard( this ); // TODO: remove
  • trunk/nv/lua/lua_values.hh

    r440 r445  
    3636                typedef double        lnumber;
    3737
    38                 struct passer
    39                 {
    40                         virtual void push( lua_State *L ) const = 0;
    41                         virtual ~passer(){}
    42                 };
    43 
    44                 template < typename T >
    45                 struct returner
    46                 {
    47                         returner( lua_State *, int ) : value() {}
    48                         returner( lua_State *, int, const T& ) : value() {}
    49                         operator T() { return value; }
    50                         operator const T() const { return value; }
    51                         virtual returner<T> to( lua_State *L, int index ) = 0;
    52                         virtual returner<T> to( lua_State *L, int index, const T& def ) = 0;
    53                         virtual ~returner(){}
    54                 protected:
    55                         T value;
    56                 };
     38//              struct passer
     39//              {
     40//                      virtual void push( lua_State *L ) const = 0;
     41//                      virtual ~passer(){}
     42//              };
     43//
     44//              template < typename T >
     45//              struct returner
     46//              {
     47//                      returner( lua_State *, int ) : value() {}
     48//                      returner( lua_State *, int, const T& ) : value() {}
     49//                      operator T() { return value; }
     50//                      operator const T() const { return value; }
     51//                      virtual returner<T> to( lua_State *L, int index ) = 0;
     52//                      virtual returner<T> to( lua_State *L, int index, const T& def ) = 0;
     53//                      virtual ~returner(){}
     54//              protected:
     55//                      T value;
     56//              };
    5757
    5858                template < typename T >
     
    8282                        lnumber     to_number     ( lua_State *L, int index );
    8383                        bool        to_bool       ( lua_State *L, int index );
    84                         const char* to_cstring    ( lua_State *L, int index );
     84//                      const char* to_cstring    ( lua_State *L, int index );
    8585                        string_view to_string_view( lua_State *L, int index );
    8686                        void*       to_pointer    ( lua_State *L, int index );
     
    9191                        lnumber     to_number     ( lua_State *L, int index, lnumber def );
    9292                        bool        to_bool       ( lua_State *L, int index, bool def );
    93                         const char* to_cstring    ( lua_State *L, int index, const char* def );
     93//                      const char* to_cstring    ( lua_State *L, int index, const char* def );
    9494                        string_view to_string_view( lua_State *L, int index, string_view def );
    9595                        void*       to_pointer    ( lua_State *L, int index, void* def );
     
    139139                };
    140140
    141                 template <>
    142                 struct pass_traits<const char*>
    143                 {
    144                         static void push( lua_State *L, const char* s ) { detail::push_cstring( L, s ); }
    145                         static const char* to( lua_State *L, int index ) { return detail::to_cstring( L, index ); }
    146                         static const char* to( lua_State *L, int index, const char* def ) { return detail::to_cstring( L, index, def ); }
    147                 };
     141//              template <>
     142//              struct pass_traits<const char*>
     143//              {
     144//                      static void push( lua_State *L, const char* s ) { detail::push_cstring( L, s ); }
     145//                      static const char* to( lua_State *L, int index ) { return detail::to_cstring( L, index ); }
     146//                      static const char* to( lua_State *L, int index, const char* def ) { return detail::to_cstring( L, index, def ); }
     147//              };
    148148
    149149                template <>
     
    177177                };
    178178
     179
    179180                namespace detail
    180181                {
    181182
    182183                        template <typename T, class ENABLE = void >
    183                         struct type_degrade
    184                         {
    185                                 typedef remove_cvr_t<T> type;
    186                         };
    187 
    188                         template <typename T>
    189                         struct type_degrade< T, enable_if_t< is_floating_point< T >::value > >
     184                        struct lua_type_impl
     185                        {
     186                                typedef T type;
     187                        };
     188
     189                        template <typename T>
     190                        struct lua_type_impl< T, enable_if_t< is_floating_point< T >::value > >
    190191                        {
    191192                                typedef lnumber type;
     
    193194
    194195                        template <typename T>
    195                         struct type_degrade< T, enable_if_t< is_integral< T >::value && is_signed< T >::value > >
     196                        struct lua_type_impl< T, enable_if_t< is_integral< T >::value && is_signed< T >::value > >
    196197                        {
    197198                                typedef linteger type;
     
    199200
    200201                        template <typename T>
    201                         struct type_degrade< T, enable_if_t< is_integral< T >::value && is_unsigned< T >::value > >
     202                        struct lua_type_impl< T, enable_if_t< is_integral< T >::value && is_unsigned< T >::value > >
    202203                        {
    203204                                typedef lunsigned type;
     
    205206
    206207                        template <typename T>
    207                         struct type_degrade< T, enable_if_t< is_cstring< T >::value > >
    208                         {
    209                                 typedef const char* type;
     208                        struct lua_type_impl< T, enable_if_t< is_cstring< T >::value > >
     209                        {
     210                                typedef string_view type;
    210211                        };
    211212
    212213                        template <>
    213                         struct type_degrade< bool >
     214                        struct lua_type_impl< bool >
    214215                        {
    215216                                typedef bool type;
    216217                        };
    217218
    218                         template < typename T >
    219                         using type_degrade_t = typename type_degrade< T >::type;
     219                }
     220
     221                template < typename T >
     222                struct converted_type
     223                {
     224                        typedef typename detail::lua_type_impl< remove_cvr_t<T> >::type type;
     225                };
     226
     227                template < typename T >
     228                using converted_type_t = typename converted_type< T >::type;
     229
     230                namespace detail
     231                {
    220232
    221233                        template < typename T >
    222                         void push_value( lua_State *L, const T& p )
     234                        void push_value( lua_State *L, T&& p )
    223235                        {
    224                                 pass_traits< type_degrade_t<T> >::push( L, p );
     236                                pass_traits< converted_type_t<T> >::push( L, ::nv::forward<T>( p ) );
    225237                        }
    226238
     
    241253                        inline void pop_value( lua_State *L, T& p )
    242254                        {
    243                                 p = static_cast<T>( pass_traits< type_degrade_t<T> >::to( L, -1 ) );
     255                                p = static_cast<T>( pass_traits< converted_type_t<T> >::to( L, -1 ) );
    244256                                detail::pop_and_discard(L, 1);
    245257                        }
     
    247259                        inline T pop_return_value( lua_State *L )
    248260                        {
    249                                 T ret = static_cast<T>( pass_traits< type_degrade_t<T> >::to( L, -1 ) );
     261                                T ret = static_cast<T>( pass_traits< converted_type_t<T> >::to( L, -1 ) );
    250262                                detail::pop_and_discard(L, 1);
    251263                                return ret;
     
    260272                        inline void pop_value( lua_State *L, T& p, const T& def )
    261273                        {
    262                                 p = static_cast<T>( pass_traits< type_degrade_t<T> >::to( L, -1, def ) );
     274                                p = static_cast<T>( pass_traits< converted_type_t<T> >::to( L, -1, def ) );
    263275                                detail::pop_and_discard(L, 1);
    264276                        }
    265277                        template < typename T >
    266                         inline T pop_return_value( lua_State *L, const T& def )
    267                         {
    268                                 T ret = static_cast<T>( pass_traits< type_degrade_t<T> >::to( L, -1, def ) );
     278                        inline converted_type_t<T> pop_return_value( lua_State *L, const T& def )
     279                        {
     280                                T ret = static_cast<T>( pass_traits< converted_type_t<T> >::to( L, -1, def ) );
    269281                                detail::pop_and_discard(L, 1);
    270282                                return ret;
     
    272284
    273285                        template < typename T >
    274                         inline type_degrade_t<T> get_value( lua_State *L, int index )
    275                         {
    276                                 return pass_traits< type_degrade_t<T> >::to( L, index );
    277                         }
    278 
    279                         template < typename T >
    280                         inline T get_value( lua_State *L, int index, const T& def )
    281                         {
    282                                 return pass_traits< type_degrade_t<T> >::to( L, index, def );
     286                        inline converted_type_t<T> get_value( lua_State *L, int index )
     287                        {
     288                                return pass_traits< converted_type_t<T> >::to( L, index );
     289                        }
     290
     291                        template < typename T >
     292                        inline converted_type_t<T> get_value( lua_State *L, int index, const T& def )
     293                        {
     294                                return pass_traits< converted_type_t<T> >::to( L, index, def );
    283295                        }
    284296
  • trunk/nv/stl/algorithm.hh

    r442 r445  
    156156        inline void reverse( BidirectionalIterator first, BidirectionalIterator last )
    157157        {
    158                 for ( ; distance( first, --last ) > 0; ++first )
     158                for ( ; ::nv::distance( first, --last ) > 0; ++first )
    159159                        iter_swap( first, last );
    160160        }
  • trunk/nv/stl/memory.hh

    r435 r445  
    2727namespace nv
    2828{
     29
     30        using constructor_t = void(*)( void* );
     31        using destructor_t  = void(*)( void* );
     32
    2933       
    3034        namespace mem_flags
  • trunk/nv/stl/string/string_base.hh

    r442 r445  
    123123                // Non-literal constructors
    124124                template< typename U >
    125                 inline string_view( U str, typename enable_if<is_same<U, const char*>::value>::type* = nullptr ) : this_type( str, nvstrlen( str ) ) {}
     125                inline string_view( U str, typename enable_if<is_same<U, const char*>::value>::type* = nullptr ) : this_type( str, str ? nvstrlen( str ) : 0 ) {}
    126126
    127127                // Non-literal constructors
  • trunk/src/lua/lua_path.cc

    r440 r445  
    7878        string128 buffer;
    7979        bool dot = false;
    80         for ( const element& e : m_elements )
     80        for ( size_t c = 0; c < m_count; ++c )
    8181        {
    8282                if ( dot ) buffer.append( "." );
    83                 if ( e.length == 0 )
     83                if ( m_elements[c].length == 0 )
    8484                {
    85                         buffer.append( "["_ls + e.value + "]"_ls );
     85                        buffer.append( "["_ls + m_elements[c].value + "]"_ls );
    8686                        dot = false;
    8787                }
    8888                else
    8989                {
    90                         buffer.append( e.str, e.length );
     90                        buffer.append( m_elements[c].str, m_elements[c].length );
    9191                        dot = true;
    9292                }
  • trunk/src/lua/lua_values.cc

    r440 r445  
    106106}
    107107
    108 const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
    109 {
    110         return lua_tolstring( L, index, nullptr );
    111 }
     108// const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
     109// {
     110//      return lua_tolstring( L, index, nullptr );
     111// }
    112112
    113113nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index )
     
    164164}
    165165
    166 const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
    167 {
    168         return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
    169 }
     166// const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
     167// {
     168//      return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
     169// }
    170170
    171171void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
Note: See TracChangeset for help on using the changeset viewer.