Changeset 333


Ignore:
Timestamp:
09/17/14 14:18:10 (11 years ago)
Author:
epyon
Message:
  • extended handle_operator (unsafe ops)
  • lua_handle library and support (multiple handle types supported)
Location:
trunk
Files:
2 added
3 edited

Legend:

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

    r323 r333  
    2929        public:
    3030                typedef T value_type;
     31                typedef TAG tag_type;
    3132                static const int INDEX_BITS   = IBITS;
    3233                static const int COUNTER_BITS = IBITS;
     
    4849
    4950                handle( T a_index, T a_counter ) : m_index( a_index ), m_counter( a_counter ) {}
    50                 template < typename H, typename I >
    51                 friend class index_store;
    52         };
    53 
     51                template < typename H >
     52                friend class handle_operator;
     53        };
     54
     55        template < typename HANDLE >
     56        class handle_operator
     57        {
     58        public:
     59                typedef typename HANDLE::value_type value_type;
     60
     61                static HANDLE create( value_type index, value_type counter )
     62                {
     63                        return HANDLE( index, counter );
     64                }
     65                static value_type get_counter( const HANDLE& h ) { return h.m_counter; }
     66                static value_type get_index( const HANDLE& h ) { return h.m_index; }
     67        };
    5468
    5569
     
    6680                handle create_handle( index_type index )
    6781                {
     82                        typedef handle_operator<HANDLE> hop;
    6883                        value_type i       = get_free_entry();
    6984                        m_entries[i].index = index;
    7085                        m_entries[i].counter++;
    71                         return handle( i, m_entries[i].counter );
     86                        return hop::create( i, m_entries[i].counter );
    7287                }
    7388
    7489                void free_handle( handle h )
    7590                {
    76                         m_entries[ h.m_index ].index     = -1;
    77                         m_entries[ h.m_index ].next_free = -1;
     91                        m_entries[ h.index() ].index     = -1;
     92                        m_entries[ h.index() ].next_free = -1;
    7893                        if ( m_last_free == -1 )
    7994                        {
    80                                 m_first_free = m_last_free = h.m_index;
     95                                m_first_free = m_last_free = h.index();
    8196                                return;
    8297                        }
    83                         m_entries[ (unsigned)m_last_free ].next_free = h.m_index;
    84                         m_last_free = h.m_index;
     98                        m_entries[ (unsigned)m_last_free ].next_free = h.index();
     99                        m_last_free = h.index();
    85100                }
    86101
    87102                void swap_indices( handle h1, handle h2 )
    88103                {
    89                         std::swap( m_entries[ h1.m_index ].index, m_entries[ h2.m_index ].index );
     104                        std::swap( m_entries[ h1.index() ].index, m_entries[ h2.index() ].index );
    90105                }
    91106
    92107                sint32 get_index( handle h ) const
    93108                {
    94                         return m_entries[ h.m_index ].counter == h.m_counter ? m_entries[ h.m_index ].index : -1;
     109                        typedef handle_operator<HANDLE> hop;
     110                        return m_entries[ h.index() ].counter == hop::get_counter(h) ? m_entries[ h.index() ].index : -1;
    95111                }
    96112        private:
  • trunk/nv/lua/lua_state.hh

    r319 r333  
    1414#include <nv/core/common.hh>
    1515#include <nv/core/flags.hh>
     16#include <nv/core/handle.hh>
    1617#include <istream>
    1718#include <map>
  • trunk/src/lua/lua_state.cc

    r319 r333  
    261261        lua_pushliteral(m_state, LUA_TABLIBNAME);
    262262        lua_call(m_state, 1, 0);
     263
     264        {
     265                // Preregister 8 references for Handle registry
     266                lua_newtable(m_state);
     267                NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
     268                for ( uint32 i = 1; i < 8; ++i )
     269                {
     270                        lua_newtable(m_state);
     271                        luaL_ref( m_state, LUA_REGISTRYINDEX );
     272                }
     273        }
    263274
    264275        if ( load_libs )
Note: See TracChangeset for help on using the changeset viewer.