- Timestamp:
- 07/29/15 16:24:39 (10 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/core/types.hh
r438 r445 19 19 namespace nv 20 20 { 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 }; 22 39 23 40 template< typename T > … … 57 74 { 58 75 shash64 name; //!< name of the field 59 type_hash hash; //!< typeinfo for later retrieval of type60 76 type_entry* type; //!< pointer to field type 61 77 uint32 flags; //!< flags … … 71 87 struct type_entry 72 88 { 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 83 95 vector<type_field> field_list; //!< Field list 84 96 vector<type_enum> enum_list; //!< Enum list 97 hash_store< shash64, type_field* > field_names; 98 hash_store< shash64, type_enum* > enum_names; 85 99 }; 86 100 … … 95 109 96 110 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 ); 98 112 99 113 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 ); 103 117 private: 104 118 type_database* m_database; … … 112 126 113 127 template< typename TYPE > 114 type_creator create_type( const char*name )128 type_creator create_type( const string_view& name ) 115 129 { 116 130 NV_ASSERT( !m_names.exists( name ), "Type redefinition!" ); 117 uint64 name_hash = m_names.insert( name );118 131 type_entry* i_type = new type_entry; 119 132 i_type->type_db = this; 120 i_type->name = name_hash;133 i_type->name = m_names.insert( name ); 121 134 i_type->size = sizeof( TYPE ); 122 135 123 136 i_type->constructor = raw_construct_object < TYPE >; 124 137 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; 127 140 m_type_list.push_back( i_type ); 128 141 return type_creator( this, i_type ); 129 142 } 130 143 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 ); 135 147 return it != m_index_by_name.end() ? it->second : nullptr; 136 148 } 137 149 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; 146 161 } 147 162 … … 156 171 } 157 172 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; 164 177 }; 165 178 … … 167 180 type_creator type_creator::base() 168 181 { 169 m_entry->base_type = m_database->get_type ( typeid( TYPE ));182 m_entry->base_type = m_database->get_type< TYPE >(); 170 183 return *this; 171 184 } 172 185 173 186 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 ) 175 188 { 176 189 typedef typename TFIELD::value_type field_type; … … 178 191 type_field f; 179 192 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> >(); 182 194 f.flags = TF_CONTAINER | 183 195 ( is_pointer<field_type>::value ? TF_POINTER : 0 ) | … … 189 201 190 202 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 ) 192 204 { 193 205 NV_ASSERT( m_entry->enum_list.empty(), "Type cannot have both enums and fields!" ); 194 206 type_field f; 195 207 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> >(); 198 209 f.flags = 199 210 ( is_pointer<TFIELD>::value ? TF_POINTER : 0 ) | … … 204 215 } 205 216 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 ) 207 218 { 208 219 NV_ASSERT( m_entry->field_list.empty(), "Type cannot have both enums and fields!" ); 209 220 type_enum e; 210 e.name = m_database->m_names.insert( name );221 e.name = m_database->m_names.insert( aname ); 211 222 e.value = value; 212 223 m_entry->enum_list.push_back( e ); -
trunk/nv/lua/lua_state.hh
r440 r445 111 111 112 112 template< typename R, typename T > 113 R get( const T& key, const R& def )113 R get( T&& key, const R& def ) 114 114 { 115 115 if ( m_global ) push_global_table(); 116 detail::push_value( m_state, key);116 detail::push_value( m_state, ::nv::forward<T>( key ) ); 117 117 call_get(); 118 118 if ( m_global ) pop_global_table(); … … 121 121 122 122 template< typename R, typename T > 123 R raw_get( const T& key, const R& def )123 R raw_get( T&& key, const R& def ) 124 124 { 125 125 if ( m_global ) push_global_table(); 126 detail::push_value( m_state, key);126 detail::push_value( m_state, ::nv::forward<T>( key ) ); 127 127 call_get_raw(); 128 128 if ( m_global ) pop_global_table(); … … 247 247 } 248 248 249 using state_wrapper::call; 250 249 251 template < typename R > 250 R call ( ref table, const path& p )252 R call_table( ref table, const path& p ) 251 253 { 252 254 stack_guard guard( this ); // TODO: remove … … 262 264 } 263 265 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 ) 265 267 { 266 268 stack_guard guard( this ); // TODO: remove -
trunk/nv/lua/lua_values.hh
r440 r445 36 36 typedef double lnumber; 37 37 38 struct passer39 {40 virtual void push( lua_State *L ) const = 0;41 virtual ~passer(){}42 };43 44 template < typename T >45 struct returner46 {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 // }; 57 57 58 58 template < typename T > … … 82 82 lnumber to_number ( lua_State *L, int index ); 83 83 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 ); 85 85 string_view to_string_view( lua_State *L, int index ); 86 86 void* to_pointer ( lua_State *L, int index ); … … 91 91 lnumber to_number ( lua_State *L, int index, lnumber def ); 92 92 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 ); 94 94 string_view to_string_view( lua_State *L, int index, string_view def ); 95 95 void* to_pointer ( lua_State *L, int index, void* def ); … … 139 139 }; 140 140 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 // }; 148 148 149 149 template <> … … 177 177 }; 178 178 179 179 180 namespace detail 180 181 { 181 182 182 183 template <typename T, class ENABLE = void > 183 struct type_degrade184 { 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 > > 190 191 { 191 192 typedef lnumber type; … … 193 194 194 195 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 > > 196 197 { 197 198 typedef linteger type; … … 199 200 200 201 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 > > 202 203 { 203 204 typedef lunsigned type; … … 205 206 206 207 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; 210 211 }; 211 212 212 213 template <> 213 struct type_degrade< bool >214 struct lua_type_impl< bool > 214 215 { 215 216 typedef bool type; 216 217 }; 217 218 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 { 220 232 221 233 template < typename T > 222 void push_value( lua_State *L, const T& p )234 void push_value( lua_State *L, T&& p ) 223 235 { 224 pass_traits< type_degrade_t<T> >::push( L, p);236 pass_traits< converted_type_t<T> >::push( L, ::nv::forward<T>( p ) ); 225 237 } 226 238 … … 241 253 inline void pop_value( lua_State *L, T& p ) 242 254 { 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 ) ); 244 256 detail::pop_and_discard(L, 1); 245 257 } … … 247 259 inline T pop_return_value( lua_State *L ) 248 260 { 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 ) ); 250 262 detail::pop_and_discard(L, 1); 251 263 return ret; … … 260 272 inline void pop_value( lua_State *L, T& p, const T& def ) 261 273 { 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 ) ); 263 275 detail::pop_and_discard(L, 1); 264 276 } 265 277 template < typename T > 266 inline Tpop_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 ) ); 269 281 detail::pop_and_discard(L, 1); 270 282 return ret; … … 272 284 273 285 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 Tget_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 ); 283 295 } 284 296 -
trunk/nv/stl/algorithm.hh
r442 r445 156 156 inline void reverse( BidirectionalIterator first, BidirectionalIterator last ) 157 157 { 158 for ( ; distance( first, --last ) > 0; ++first )158 for ( ; ::nv::distance( first, --last ) > 0; ++first ) 159 159 iter_swap( first, last ); 160 160 } -
trunk/nv/stl/memory.hh
r435 r445 27 27 namespace nv 28 28 { 29 30 using constructor_t = void(*)( void* ); 31 using destructor_t = void(*)( void* ); 32 29 33 30 34 namespace mem_flags -
trunk/nv/stl/string/string_base.hh
r442 r445 123 123 // Non-literal constructors 124 124 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 ) {} 126 126 127 127 // Non-literal constructors -
trunk/src/lua/lua_path.cc
r440 r445 78 78 string128 buffer; 79 79 bool dot = false; 80 for ( const element& e : m_elements)80 for ( size_t c = 0; c < m_count; ++c ) 81 81 { 82 82 if ( dot ) buffer.append( "." ); 83 if ( e.length == 0 )83 if ( m_elements[c].length == 0 ) 84 84 { 85 buffer.append( "["_ls + e.value + "]"_ls );85 buffer.append( "["_ls + m_elements[c].value + "]"_ls ); 86 86 dot = false; 87 87 } 88 88 else 89 89 { 90 buffer.append( e.str, e.length );90 buffer.append( m_elements[c].str, m_elements[c].length ); 91 91 dot = true; 92 92 } -
trunk/src/lua/lua_values.cc
r440 r445 106 106 } 107 107 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 // } 112 112 113 113 nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index ) … … 164 164 } 165 165 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 // } 170 170 171 171 void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
Note: See TracChangeset
for help on using the changeset viewer.