Changeset 538
- Timestamp:
- 01/23/17 21:04:56 (8 years ago)
- Location:
- trunk
- Files:
-
- 4 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/ecs/component.hh
r537 r538 29 29 { 30 30 31 template < typename Ecs, typename Component > 31 template < 32 typename Ecs, 33 typename Component, 34 template < typename, typename > class IndexTable = index_table 35 > 32 36 class component : public Ecs::component_interface 33 37 { 34 38 public: 35 typedef Ecs ecs_type; 36 typedef typename ecs_type::message message_type; 37 typedef typename ecs_type::handle_type handle_type; 38 typedef typename ecs_type::time_type time_type; 39 typedef Component value_type; 40 typedef Component component_type; 41 typedef index_storage< handle_type, value_type > storage_type; 42 typedef typename storage_type::index_type index_type; 39 typedef Ecs ecs_type; 40 typedef typename ecs_type::message message_type; 41 typedef typename ecs_type::handle_type handle_type; 42 typedef typename ecs_type::time_type time_type; 43 typedef IndexTable< handle_type, sint32 > index_table_type; 44 typedef Component value_type; 45 typedef Component component_type; 46 typedef vector< value_type > storage_type; 47 typedef typename index_table_type::index_type index_type; 43 48 44 typedef typename storage_type::iterator 45 typedef typename storage_type::const_iterator 46 typedef typename storage_type::reference 47 typedef typename storage_type::const_reference 49 typedef typename storage_type::iterator iterator; 50 typedef typename storage_type::const_iterator const_iterator; 51 typedef typename storage_type::reference reference; 52 typedef typename storage_type::const_reference const_reference; 48 53 49 54 component( ecs_type& a_ecs, string_view a_name, uint32 reserve = 0 ) 50 : m_ecs( a_ecs ) , m_data( reserve )55 : m_ecs( a_ecs ) 51 56 { 52 57 m_ecs.register_component<component_type>( a_name, this ); 58 if ( reserve != 0 ) 59 { 60 m_data.reserve( reserve ); 61 } 53 62 } 54 63 55 inline value_type& insert( handle_type h ) { return m_data.insert( h ); } 64 inline value_type& insert( handle_type h ) 65 { 66 index_type i = m_index.insert( h ); 67 NV_ASSERT( i == index_type( m_data.size() ), "Fail!" ); 68 NV_UNUSED( i ); 69 m_data.emplace_back(); 70 return m_data.back(); 71 } 56 72 57 73 template < typename ...Args > 58 74 value_type& insert( handle_type h, Args&&... args ) 59 75 { 60 return m_data.insert( h, nv::forward<Args>( args )... ); 76 index_type i = m_index.insert( h ); 77 NV_ASSERT( i == index_type( m_data.size() ), "Fail!" ); 78 NV_UNUSED( i ); 79 m_data.emplace_back( nv::forward<Args>( args )... ); 80 return m_data.back(); 61 81 } 62 82 63 83 bool exists( handle_type h ) 64 84 { 65 return m_ data.exists( h );85 return m_index.exists( h ); 66 86 } 67 87 … … 73 93 virtual void clear() 74 94 { 75 for ( uint32 i = 0; i < size(); ++i )95 for ( uint32 i = 0; i < m_data.size(); ++i ) 76 96 destroy( &m_data[i] ); 97 m_index.clear(); 77 98 m_data.clear(); 78 99 } 79 100 80 value_type* get( handle_type h ) { return m_data.get(h); } 81 const value_type* get( handle_type h ) const { return m_data.get( h ); } 101 value_type* get( handle_type h ) 102 { 103 index_type i = m_index.get( h ); 104 return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr; 105 } 106 const value_type* get( handle_type h ) const 107 { 108 index_type i = m_index.get( h ); 109 return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr; 110 } 82 111 83 112 void* get_raw( handle_type h ) { return get( h ); } … … 86 115 virtual void remove( handle_type h ) 87 116 { 88 value_type* v = m_data.get( h );117 value_type* v = get( h ); 89 118 if ( v == nullptr ) return; 90 119 destroy( v ); 91 m_data.remove( h ); 120 index_type dead_eindex = m_index.remove_swap( h ); 121 if ( dead_eindex == -1 ) return; 122 if ( dead_eindex != static_cast<index_type>( m_data.size() - 1 ) ) 123 { 124 m_data[unsigned( dead_eindex )] = move( m_data.back() ); 125 } 126 m_data.pop_back(); 92 127 } 93 128 … … 106 141 clear(); 107 142 } 108 109 110 inline handle_type get_handle( index_type i ) const { return m_data.get_handle( i ); } 143 144 inline handle_type get_handle( index_type i ) const { return m_index.get_handle( i ); } 111 145 112 146 inline const value_type& operator[] ( index_type i ) const { return m_data[i]; } … … 124 158 protected: 125 159 ecs_type& m_ecs; 160 index_table_type m_index; 126 161 storage_type m_data; 127 162 }; -
trunk/nv/ecs/component_storage.hh
r537 r538 18 18 #include <nv/stl/index_table.hh> 19 19 #include <nv/stl/handle_manager.hh> 20 #include <nv/stl/hash_map.hh> 20 21 21 22 namespace nv … … 32 33 typedef Component value_type; 33 34 typedef vector< value_type > storage_type; 34 typedef index_table< handle_type > index_table_type; 35 typedef typename index_table_type::index_type index_type; 35 typedef sint32 index_type; 36 36 typedef typename storage_type::iterator iterator; 37 37 typedef typename storage_type::const_iterator const_iterator; … … 297 297 298 298 class direct_storage; 299 class hash_storage; 299 /* 300 template < typename Handle, typename Component > 301 class hash_storage 302 { 303 public: 304 typedef Handle handle_type; 305 typedef Component value_type; 306 typedef vector< value_type > storage_type; 307 typedef typename storage_type::iterator iterator; 308 typedef typename storage_type::const_iterator const_iterator; 309 typedef typename storage_type::reference reference; 310 typedef typename storage_type::const_reference const_reference; 311 312 explicit hash_storage( uint32 reserve = 0 ) 313 { 314 if ( reserve != 0 ) 315 { 316 m_data.reserve( reserve ); 317 } 318 } 319 320 inline value_type& insert( handle_type h ) 321 { 322 auto result = m_data.insert_key( h ); 323 NV_ASSERT( result.second, "Reinsert of handle!" ); 324 m_data.emplace_back(); 325 return m_data.back(); 326 } 327 328 template < typename ...Args > 329 inline value_type& insert( handle_type h, Args&&... args ) 330 { 331 insert_index( h ); 332 m_data.emplace_back( nv::forward<Args>( args )... ); 333 return m_data.back(); 334 } 335 336 inline bool exists( handle_type h ) 337 { 338 if ( h.is_nil() || h.index() >= m_indexes.size() ) return false; 339 return m_indexes[h.index()] >= 0; 340 } 341 342 value_type* get( handle_type h ) 343 { 344 if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr; 345 index_type i = m_indexes[h.index()]; 346 return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr; 347 } 348 349 const value_type* get( handle_type h ) const 350 { 351 if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr; 352 index_type i = m_indexes[h.index()]; 353 return i >= 0 ? &( m_data[unsigned( i )] ) : nullptr; 354 } 355 356 void clear() 357 { 358 m_data.clear(); 359 } 360 361 void remove( handle_type h ) 362 { 363 364 365 } 366 367 inline handle_type get_handle( sint32 i ) const { return m_handles[unsigned( i )]; } 368 369 inline const value_type& operator[] ( index_type i ) const { return m_data[i]; } 370 inline value_type& operator[] ( index_type i ) { return m_data[i]; } 371 372 inline size_t size() const { return m_data.size(); } 373 374 inline iterator begin() { return m_data.begin(); } 375 inline const_iterator begin() const { return m_data.cbegin(); } 376 377 inline iterator end() { return m_data.end(); } 378 inline const_iterator end() const { return m_data.cend(); } 379 380 protected: 381 vector< value_type > m_data; 382 }; 383 384 300 385 class hash_multi_storage; 301 386 */ 302 387 } 303 388 -
trunk/nv/lua/lua_raw.hh
r533 r538 1 // Copyright (C) 2012-201 5ChaosForge Ltd1 // Copyright (C) 2012-2017 ChaosForge Ltd 2 2 // http://chaosforge.org/ 3 3 // -
trunk/nv/lua/lua_state.hh
r534 r538 19 19 #include <nv/stl/type_traits/function.hh> 20 20 21 #include <nv/lua/lua_proxy.hh> 22 #include <nv/lua/lua_iterator.hh> 21 23 #include <nv/lua/lua_handle.hh> 22 24 #include <nv/lua/lua_path.hh> … … 37 39 38 40 class state; 41 class lua_iterator_provider; 39 42 40 43 using lua_rtti_read_function = bool(*)( state*, const type_entry*, void*, int index ); … … 201 204 202 205 const type_data* get_type_data() const { return m_lua_types; } 206 207 208 203 209 204 210 protected: … … 256 262 int get_stack_size(); 257 263 void log_stack(); 264 258 265 lua_State* get_raw(); 259 266 ref register_object( void* o, string_view lua_name ); … … 363 370 }; 364 371 372 373 374 365 375 class table_guard : public state_wrapper 366 376 { … … 370 380 virtual ~table_guard(); 371 381 uint32 get_size(); 372 382 iterator_provider< kv_iterator > pairs() 383 { 384 return iterator_provider< kv_iterator >( m_parent, -1 ); 385 } 386 iterator_provider< key_iterator > keys() 387 { 388 return iterator_provider< key_iterator >( m_parent, -1 ); 389 } 390 iterator_provider< value_iterator > values() 391 { 392 return iterator_provider< value_iterator >( m_parent, -1 ); 393 } 394 395 // TO FUCKING DO: 396 // const stack_proxy operator[]( const string_view& key ) const; 397 // const stack_proxy operator[]( sint32 key ) const; 398 // stack_proxy operator[]( const string_view& key ); 399 // stack_proxy operator[]( sint32 key ); 400 401 373 402 bool has_field( string_view element ); 374 403 -
trunk/nv/stl/handle.hh
r533 r538 16 16 #include <nv/stl/vector.hh> 17 17 #include <nv/stl/index_table.hh> 18 #include <nv/stl/functional/hash.hh> 18 19 19 20 namespace nv … … 46 47 47 48 constexpr T index() const { return m_index; } 48 //uint32 hash() const { return hash<T>()( T( m_counter << IBITS | m_index ) ); }49 constexpr uint32 hash() const { NV_ASSERT( false, "UNIMPLEMENTED!" ); return 0; }49 template < typename H = uint64 > 50 constexpr H hash() const { return nv::hash<T,H>()( T( m_counter << IBITS | m_index ) ); } 50 51 protected: 51 52 T m_index : IBITS; … … 57 58 }; 58 59 60 template < 61 typename H, 62 typename T, 63 unsigned IBITS, 64 unsigned CBITS, 65 typename TAG 66 > 67 struct hash< handle< T, IBITS, CBITS, TAG >, H > 68 : detail::hash_base< handle< T, IBITS, CBITS, TAG >, H > 69 { 70 static constexpr H get( const handle< T, IBITS, CBITS, TAG >& value ) 71 { 72 return value.hash<H>(); 73 } 74 inline H operator()( const handle< T, IBITS, CBITS, TAG >& value ) const { return get( value ); } 75 }; 59 76 60 77 } -
trunk/nv/stl/index_table.hh
r537 r538 16 16 #include <nv/common.hh> 17 17 #include <nv/stl/vector.hh> 18 #include <nv/stl/hash_map.hh> 18 19 19 20 namespace nv … … 116 117 }; 117 118 119 template < 120 typename Handle, 121 typename Index = sint32 122 > 123 class hashed_index_table 124 { 125 public: 126 typedef Handle handle_type; 127 typedef Index index_type; 128 129 hashed_index_table() {} 130 hashed_index_table( uint32 reserve ) 131 { 132 m_indexes.reserve( reserve ); 133 } 134 135 index_type insert( handle_type h ) 136 { 137 NV_ASSERT( m_indexes.find( h ) == m_indexes.end(), "Reinserting handle!" ); 138 index_type lindex = m_handles.size(); 139 m_indexes[h] = index_type( lindex ); 140 m_handles.push_back( h ); 141 return lindex; 142 } 143 144 bool exists( handle_type h ) const 145 { 146 auto ih = m_indexes.find( h ); 147 return ( ih != m_indexes.end() ); 148 } 149 150 index_type get( handle_type h ) const 151 { 152 auto ih = m_indexes.find( h ); 153 if ( ih == m_indexes.end() ) return -1; 154 return ih->second; 155 } 156 157 index_type remove_swap( handle_type h ) 158 { 159 if ( h.is_nil() ) return -1; 160 auto ih = m_indexes.find( h ); 161 if ( ih == m_indexes.end() ) return -1; 162 handle_type swap_handle = m_handles.back(); 163 index_type dead_eindex = ih->second; 164 if ( dead_eindex != static_cast<index_type>( m_handles.size() - 1 ) ) 165 { 166 m_handles[unsigned( dead_eindex )] = swap_handle; 167 m_indexes[swap_handle] = dead_eindex; 168 } 169 m_handles.pop_back(); 170 m_indexes.erase( h ); 171 return dead_eindex; 172 } 173 174 // index_type remove_swap( index_type dead_eindex ) 175 // { 176 // if ( uint32( dead_eindex ) >= m_handles.size() ) return -1; 177 // handle_type h = m_handles[dead_eindex]; 178 // handle_type swap_handle = m_handles.back(); 179 // if ( dead_eindex != static_cast<index_type>( m_handles.size() - 1 ) ) 180 // { 181 // m_handles[unsigned( dead_eindex )] = swap_handle; 182 // m_indexes[swap_handle] = dead_eindex; 183 // } 184 // m_handles.pop_back(); 185 // m_indexes.erase( h ); 186 // return dead_eindex; 187 // } 188 189 190 void clear() 191 { 192 m_handles.clear(); 193 m_indexes.clear(); 194 } 195 196 handle_type get_handle( index_type i ) const { return m_handles[unsigned( i )]; } 197 198 uint32 size() const { return m_handles.size(); } 199 200 private: 201 202 vector< handle_type > m_handles; 203 hash_map< handle_type, index_type > m_indexes; 204 }; 118 205 119 206 } -
trunk/src/lua/lua_state.cc
r534 r538 938 938 // insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> ); 939 939 } 940
Note: See TracChangeset
for help on using the changeset viewer.