Changeset 347


Ignore:
Timestamp:
01/03/15 20:28:18 (10 years ago)
Author:
epyon
Message:
  • entity_store was a misnomer -> handle_store
  • index_store had two functions, and entity_store double functionality: handle_manager added for pure handle management without index handle_manager+packed_index_array is handle_store (old entity_store) removed old entity_store and index_store
Location:
trunk/nv
Files:
8 edited

Legend:

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

    r322 r347  
    196196        {
    197197        protected:
    198 //              noncopyable() = default;
    199 //              ~noncopyable() = default;
    200 //              noncopyable( const noncopyable& ) = delete;
    201 //              noncopyable& operator=( const noncopyable& ) = delete;
    202                 noncopyable() {}
    203                 ~noncopyable() {}
    204         private:
    205                 noncopyable( const noncopyable& );
    206                 noncopyable& operator=( const noncopyable& );
     198                noncopyable() = default;
     199                ~noncopyable() = default;
     200                noncopyable( const noncopyable& ) = delete;
     201                noncopyable& operator=( const noncopyable& ) = delete;
    207202        };
    208203
  • trunk/nv/core/handle.hh

    r339 r347  
    6767        };
    6868
    69 
    70         template < typename HANDLE, typename TINDEX = sint32 >
    71         class index_store
    72         {
    73         public:
     69        template < typename HANDLE >
     70        class handle_manager
     71        {
     72                static const sint32 NONE = -1;
     73                static const sint32 USED = -2;
     74        public:
     75
    7476                typedef HANDLE handle;
    75                 typedef TINDEX index_type;
    7677                typedef typename HANDLE::value_type value_type;
    7778
    78                 index_store() : m_first_free(-1), m_last_free(-1) {}
    79 
    80                 handle create_handle( index_type index )
    81                 {
    82                         typedef handle_operator<HANDLE> hop;
    83                         value_type i       = get_free_entry();
    84                         m_entries[i].index = index;
     79                handle_manager() : m_first_free( NONE ), m_last_free( NONE ) {}
     80
     81                handle create_handle()
     82                {
     83                        typedef handle_operator<HANDLE> hop;
     84                        value_type i = get_free_entry();
    8585                        m_entries[i].counter++;
     86                        m_entries[i].next_free = USED;
    8687                        return hop::create( i, m_entries[i].counter );
    8788                }
     
    8990                void free_handle( handle h )
    9091                {
    91                         m_entries[ h.index() ].index     = -1;
    92                         m_entries[ h.index() ].next_free = -1;
     92                        m_entries[h.index()].next_free = NONE;
    9393                        if ( m_last_free == -1 )
    9494                        {
     
    9696                                return;
    9797                        }
    98                         m_entries[ (unsigned)m_last_free ].next_free = h.index();
     98                        m_entries[(unsigned)m_last_free].next_free = h.index();
    9999                        m_last_free = h.index();
    100100                }
    101101
    102                 void swap_indices( handle h1, handle h2 )
    103                 {
    104                         std::swap( m_entries[ h1.index() ].index, m_entries[ h2.index() ].index );
    105                 }
    106 
    107102                bool is_valid( handle h ) const
    108103                {
    109                         typedef handle_operator<HANDLE> hop; 
     104                        typedef handle_operator<HANDLE> hop;
    110105                        if ( h.is_nil() ) return false;
    111106                        if ( h.index() >= m_entries.size() ) return false;
    112                         const index_entry& entry = m_entries[ h.index() ];
    113                         return entry.index >= 0 && entry.counter == hop::get_counter(h);
    114                 }
    115 
    116                 sint32 get_index( handle h ) const
    117                 {
    118                         typedef handle_operator<HANDLE> hop;
    119                         return m_entries[ h.index() ].counter == hop::get_counter(h) ? m_entries[ h.index() ].index : -1;
    120                 }
     107                        const index_entry& entry = m_entries[h.index()];
     108                        return entry.next_free == USED && entry.counter == hop::get_counter( h );
     109                }
     110
    121111        private:
    122112                struct index_entry
    123113                {
    124                         index_type index;
    125114                        value_type counter;
    126                         index_type next_free;
    127 
    128                         index_entry() : index(0), counter(0), next_free(-1) {}
     115                        sint32    next_free;
     116
     117                        index_entry() : counter( 0 ), next_free( NONE ) {}
    129118                };
    130119
     
    135124                                value_type result = (value_type)m_first_free;
    136125                                m_first_free = m_entries[result].next_free;
    137                                 m_entries[result].next_free = -1;
     126                                m_entries[result].next_free = USED;
    138127                                if ( m_first_free == -1 ) m_last_free = -1;
    139128                                return result;
     
    143132                }
    144133
    145                 index_type m_first_free;
    146                 index_type m_last_free;
     134                sint32 m_first_free;
     135                sint32 m_last_free;
    147136                std::vector< index_entry > m_entries;
    148137        };
     
    188177                        index_type i = m_indexes[ h.index() ];
    189178                        return i >= 0 ? &(m_data[ (unsigned)i ]) : nullptr;
     179                }
     180
     181                const T* get( handle h ) const
     182                {
     183                        if ( h.is_nil() || h.index() >= m_indexes.size() ) return nullptr;
     184                        index_type i = m_indexes[h.index()];
     185                        return i >= 0 ? &( m_data[(unsigned)i] ) : nullptr;
    190186                }
    191187
     
    214210                }
    215211
     212                handle get_handle( index_type i ) const { return m_handles[(unsigned)i]; }
     213
    216214                const value_type& operator[] ( index_type i ) const { return m_data[i]; }
    217215                value_type& operator[] ( index_type i ) { return m_data[i]; }
     
    245243
    246244        template < typename T, typename HANDLE = handle<>, typename TINDEX = sint32 >
    247         class entity_store
     245        class handle_store
    248246        {
    249247        public:
     
    257255                typedef typename storage::const_reference const_reference;
    258256
    259                 entity_store() {}
    260 
    261                 explicit entity_store( uint32 reserve )
    262                 {
    263                         m_handles.reserve( reserve );
     257                handle_store() {}
     258
     259                explicit handle_store( uint32 reserve )
     260                {
    264261                        m_data.reserve( reserve );
    265262                }
     
    267264                handle create()
    268265                {
    269                         m_data.emplace_back();
    270                         m_handles.push_back( m_indexes.create_handle( sint32( m_data.size() - 1 ) ) );
    271                         return m_handles.back();
     266                        handle h = m_indexes.create_handle();
     267                        m_data.insert( h );
     268                        return h;
    272269                }
    273270
    274271                bool is_valid( handle h )
    275272                {
    276                         return m_indexes.is_valid(h);
     273                        return m_indexes.is_valid( h );
    277274                }
    278275
    279276                const value_type* get( handle h ) const
    280277                {
    281                         if ( h.is_nil() ) return nullptr;
    282                         sint32 eindex = m_indexes.get_index( h );
    283                         return eindex >= 0 ? &(m_data[ (unsigned)eindex ]) : nullptr;
     278                        return m_data.get( h );
    284279                }
    285280
    286281                value_type* get( handle h )
    287282                {
    288                         if ( h.is_nil() ) return nullptr;
    289                         sint32 eindex = m_indexes.get_index( h );
    290                         return eindex >= 0 ? &(m_data[ (unsigned)eindex ]) : nullptr;
     283                        return m_data.get( h );
    291284                }
    292285
    293286                void destroy( handle e )
    294287                {
    295                         handle swap_handle    = m_handles.back();
    296                         sint32 dead_eindex    = m_indexes.get_index( e );
    297                         if ( dead_eindex != (sint32)m_data.size()-1 )
    298                         {
    299                                 m_data[ (unsigned)dead_eindex ]    = m_data.back();
    300                                 m_handles[ (unsigned)dead_eindex ] = m_handles.back();
    301                         }
    302                         m_data.pop_back();
    303                         m_handles.pop_back();
    304                         m_indexes.swap_indices( e, swap_handle );
     288                        m_data.remove( e );
    305289                        m_indexes.free_handle( e );
    306290                }
    307291
    308                 handle get_handle( index_type i ) const { return m_handles[(unsigned)i]; }
     292                handle get_handle( index_type i ) const { return m_data.get_handle(i); }
    309293                const value_type& operator[] ( index_type i ) const { return m_data[(unsigned)i]; }
    310294                value_type& operator[] ( index_type i ) { return m_data[(unsigned)i]; }
    311295                size_t size() const { return m_data.size(); }
    312296
    313                 iterator        begin()        { return m_data.begin(); }
     297                iterator        begin() { return m_data.begin(); }
    314298                const_iterator  begin()  const { return m_data.cbegin(); }
    315299                const_iterator  cbegin() const { return m_data.cbegin(); }
    316300
    317                 iterator        end()        { return m_data.end(); }
     301                iterator        end() { return m_data.end(); }
    318302                const_iterator  end()  const { return m_data.cend(); }
    319303                const_iterator  cend() const { return m_data.cend(); }
    320304
    321305        private:
    322                 std::vector< handle >         m_handles;
    323                 storage                       m_data;
    324                 index_store< handle, TINDEX > m_indexes;
    325         };
    326 
     306                packed_indexed_array< T, handle, TINDEX > m_data;
     307                handle_manager< handle >                  m_indexes;
     308        };
     309       
    327310}
    328311
  • trunk/nv/engine/particle_engine.hh

    r323 r347  
    190190                vec3     m_camera_pos;
    191191
    192                 entity_store< particle_system_info, particle_system >      m_systems;
     192                handle_store< particle_system_info, particle_system >      m_systems;
    193193                std::unordered_map< std::string, uint32 >                  m_names;
    194194                std::vector< particle_system_data >                        m_data;
  • trunk/nv/fmod/fmod_audio.hh

    r330 r347  
    4040                        virtual ~audio();
    4141                private:
    42                         entity_store< sound_info, sound >     m_sounds;
     42                        handle_store< sound_info, sound >     m_sounds;
    4343                        vec3                                  m_position;
    4444                        void* m_system;
  • trunk/nv/gl/gl_context.hh

    r342 r347  
    9292                void* m_handle;
    9393
    94                 entity_store< vertex_array_info, vertex_array >  m_vertex_arrays;
    95                 entity_store< gl_framebuffer_info, framebuffer > m_framebuffers;
     94                handle_store< vertex_array_info, vertex_array >  m_vertex_arrays;
     95                handle_store< gl_framebuffer_info, framebuffer > m_framebuffers;
    9696        };
    9797
  • trunk/nv/gl/gl_device.hh

    r331 r347  
    6767                bool compile( uint32 sh_type, const std::string& shader_code, unsigned& glid );
    6868                std::string m_shader_header;
    69                 entity_store< gl_texture_info, texture >         m_textures;
    70                 entity_store< gl_buffer_info,  buffer >          m_buffers;
    71                 entity_store< gl_program_info, program >         m_programs;
     69                handle_store< gl_texture_info, texture >         m_textures;
     70                handle_store< gl_buffer_info,  buffer >          m_buffers;
     71                handle_store< gl_program_info, program >         m_programs;
    7272        };
    7373
  • trunk/nv/gui/gui_environment.hh

    r319 r347  
    5858                        void recalculate_absolute( handle e );
    5959                       
    60                         entity_store< element, handle > m_elements;
     60                        handle_store< element, handle > m_elements;
    6161                        renderer*     m_renderer;
    6262                        window*       m_window;
  • trunk/nv/sdl/sdl_audio.hh

    r330 r347  
    4141                        vec3                                  m_forward;
    4242                        vec3                                  m_up;
    43                         entity_store< sound_info, sound >     m_sounds;
     43                        handle_store< sound_info, sound >     m_sounds;
    4444                };
    4545        }
Note: See TracChangeset for help on using the changeset viewer.