Changeset 266


Ignore:
Timestamp:
06/19/14 03:49:49 (11 years ago)
Author:
epyon
Message:
  • decoupling - uid_store independent of nv::object
  • decoupling - nv::object no longer linked with lua in any way
  • decoupling - gui::element related object methods moved to element
  • uid_store can operate on void* or specialized base class
  • root class no longer carries uid store nor lua state (will be removed later)
Location:
trunk
Files:
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/common.hh

    r262 r266  
    143143namespace nv
    144144{
    145         class uid_store;
    146 
    147145        namespace lua
    148146        {
  • trunk/nv/gui/gui_element.hh

    r257 r266  
    186186                         */
    187187                        virtual void recalculate_absolute_children();
     188
     189                        /**
     190                         * Moves element to back of child list (top of stack)
     191                         *
     192                         * @returns true if object found, false otherwise
     193                         */
     194                        bool move_to_top( element* child );
     195
     196                        /**
     197                         * Moves element to front of child list (bottom of stack)
     198                         *
     199                         * @returns true if object found, false otherwise
     200                         */
     201                        bool move_to_bottom( element* child );
    188202
    189203                protected:
  • trunk/nv/object.hh

    r265 r266  
    2020        public:
    2121                friend class root;
    22 
    23                 /**
    24                  * Register the object type in the type database.
    25                  */
    26                 //static void register_type( type_database* db );
    2722
    2823                /// List type
     
    134129                const std::string& get_id() const { return m_id; }
    135130
    136                 /**
    137                  * Returns object UID
    138                  */
    139                 int get_lua_index() const { return m_lua_index; }
    140 
    141                 /**
    142                  * Moves object to back of child list (top of stack)
    143                  *
    144                  * @returns true if object found, false otherwise
    145                  */
    146                 bool move_to_top( object* child );
    147 
    148                 /**
    149                  * Moves object to front of child list (bottom of stack)
    150                  *
    151                  * @returns true if object found, false otherwise
    152                  */
    153                 bool move_to_bottom( object* child );
    154 
    155131                list::iterator begin() { return m_children.begin(); }
    156132                list::iterator end()   { return m_children.end(); }
     
    168144                string   m_name;            ///< name of the object
    169145                uid      m_uid;             ///< uid of the object
    170                 int      m_lua_index;       ///< lua reference
    171                 int      m_lua_proto_index; ///< lua reference
    172146                object*  m_parent;          ///< pointer to parent
    173147                list     m_children;        ///< children objects
  • trunk/nv/root.hh

    r264 r266  
    2020        {
    2121        public:
    22                 root() : m_lua_state( nullptr ), m_uid_store( nullptr ) {}
    23                 lua::state*    get_lua_state()     const { return m_lua_state; }
    24                 uid_store*     get_uid_store()     const { return m_uid_store; }
     22                root() {}
    2523                template < typename T >
    2624                object* create_object( const std::string& id )
     
    3028                        return o;
    3129                }
    32                 void register_with_lua( object* o, const char* lua_name, const char* storage );
    3330                void destroy_children( object* o );
    3431                virtual void destroy_object( object* o );
    3532                virtual ~root() {}
    3633        protected:
    37                 virtual void object_created( object* o );
    38 
    39                 lua::state*    m_lua_state;
    40                 uid_store*     m_uid_store;
     34                virtual void object_created( object* ) {}
    4135        };
    4236       
  • trunk/nv/uid.hh

    r262 r266  
    1919namespace nv
    2020{
    21         class object;
    2221
    23         class uid_store
     22        class uid_store_raw
    2423        {
    2524        public:
    2625               
    2726                /**
    28                  * Creates a new instance of the unique indentifier store.
     27                 * Creates a new instance of the unique identifier store.
    2928                 */
    30                 uid_store();
     29                uid_store_raw();
    3130
    3231                /**
     
    3635                 * @returns The stored object for that ID.
    3736                 */
    38                 object* get( uid auid ) const;
     37                void* get( uid auid ) const;
    3938
    4039                /**
     
    5251                 * @param auid The ID to assign to the object.
    5352                 */
    54                 void insert( object* o, uid auid );
     53                void insert( void* o, uid auid );
    5554
    5655                /**
     
    6059                 * @returns The ID the object was store under.
    6160                 */
    62                 uid insert( object* o );
     61                uid insert( void* o );
    6362
    6463                /**
     
    7271                 * Destroys the unique identifier store.
    7372                 */
    74                 ~uid_store();
     73                ~uid_store_raw();
     74
     75        protected:
     76                typedef std::unordered_map< uid, void* > map;
     77                map m_map; ///< The hash map everything is stored in.
     78                uid m_current; ///< The last UID assigned.
     79        };
     80
     81        template < typename T >
     82        class uid_store
     83        {
     84        public:
     85                T* get( uid auid ) const      { return static_cast<T*>( m_store.get( auid ) ); }
     86                bool remove( uid auid )       { m_store.remove( auid ); }
     87                void insert( T* o, uid auid ) { m_store.insert( o, auid ); }
     88                uid insert( T* o )            { return m_store.insert( o ); }
     89                uid request_uid()             { return m_store.request_uid(); }
    7590
    7691                /**
     
    8196                 * @returns An object of the indicated type that was stored at the indicated ID.
    8297                 */
    83                 template< typename T >
    84                 T* get_as( uid auid ) const
     98                template< typename U >
     99                U* get_as( uid auid ) const
    85100                {
    86                         return dynamic_cast< T* >( get(auid) );
     101                        return dynamic_cast< U* >( get(auid) );
    87102                }
    88         private:
    89                 typedef std::unordered_map< uid, object* > map;
    90                 map m_map; ///< The hash map everything is stored in.
    91                 uid m_current; ///< The last UID assigned.
     103        protected:
     104                uid_store_raw m_store;
    92105        };
     106       
    93107}
    94108
  • trunk/src/gui/gui_element.cc

    r257 r266  
    8181}
    8282
     83
     84bool element::move_to_top( element* child )
     85{
     86        list::iterator it = std::find( m_children.begin(), m_children.end(), (object*)child );
     87        if ( it != m_children.end() )
     88        {
     89                m_children.erase( it );
     90                m_children.push_back( child );
     91                return true;
     92        }       
     93        return false;
     94}
     95
     96bool element::move_to_bottom( element* child )
     97{
     98        list::iterator it = std::find( m_children.begin(), m_children.end(), (object*)child );
     99        if ( it != m_children.end() )
     100        {
     101                m_children.erase( it );
     102                m_children.push_front( child );
     103                return true;
     104        }       
     105        return false;
     106}
     107
    83108element::~element()
    84109{
  • trunk/src/object.cc

    r265 r266  
    88
    99#include <algorithm>
    10 #include "nv/types.hh"
    11 #include "nv/lua/lua_state.hh"
    12 #include "nv/uid.hh"
    1310
    1411using namespace nv;
     
    1815        , m_name()
    1916        , m_uid(0)
    20         , m_lua_index(lua::ref::none)
    21         , m_lua_proto_index(lua::ref::none)
    2217        , m_parent( nullptr )
    2318        , m_children()
     
    124119}
    125120
    126 bool object::move_to_top( object* child )
    127 {
    128         list::iterator it = std::find( m_children.begin(), m_children.end(), child );
    129         if ( it != m_children.end() )
    130         {
    131                 m_children.erase( it );
    132                 m_children.push_back( child );
    133                 return true;
    134         }       
    135         return false;
    136 }
    137 
    138 bool object::move_to_bottom( object* child )
    139 {
    140         list::iterator it = std::find( m_children.begin(), m_children.end(), child );
    141         if ( it != m_children.end() )
    142         {
    143                 m_children.erase( it );
    144                 m_children.push_front( child );
    145                 return true;
    146         }       
    147         return false;
    148 }
    149121
    150122// void object::register_type( type_database* db )
  • trunk/src/root.cc

    r265 r266  
    1010#include "nv/lua/lua_state.hh"
    1111
    12 void nv::root::object_created( object* o )
    13 {
    14         if ( m_uid_store )
    15         {
    16                 o->m_uid = m_uid_store->insert( o );
    17         }
    18 }
    19 
    2012void nv::root::destroy_object( object* o )
    2113{
    2214        destroy_children( o );
    2315        o->detach();
    24         if ( m_lua_state && o->m_lua_index != lua::ref::none )
    25         {
    26                 m_lua_state->unregister_object( lua::ref( o->m_lua_index ) );
    27         }
    28         if ( m_uid_store && o->m_uid != 0 )
    29         {
    30                 m_uid_store->remove( o->m_uid );
    31         }
    3216        delete o;
    3317}
     
    4024        }
    4125}
    42 
    43 void nv::root::register_with_lua( object* o, const char* lua_name, const char* storage )
    44 {
    45         if ( m_lua_state )
    46         {
    47                 if ( lua_name != nullptr )
    48                 {
    49                         o->m_lua_index       = m_lua_state->register_object( o, lua_name ).get();
    50                 }
    51                 if ( storage != nullptr )
    52                 {
    53                         o->m_lua_proto_index = m_lua_state->register_proto( o->get_id().c_str(), storage ).get();
    54                 }
    55         }
    56 
    57 }
    58 
  • trunk/src/uid.cc

    r59 r266  
    99using namespace nv;
    1010
    11 uid_store::uid_store()
     11uid_store_raw::uid_store_raw()
    1212        : m_map(), m_current(0)
    1313{
     
    1515}
    1616
    17 object* uid_store::get( uid auid ) const
     17void* uid_store_raw::get( uid auid ) const
    1818{
    1919        map::const_iterator i = m_map.find( auid );
     
    2525}
    2626
    27 bool uid_store::remove( uid auid )
     27bool uid_store_raw::remove( uid auid )
    2828{
    2929        return m_map.erase( auid ) != 0;
    3030}
    3131
    32 void uid_store::insert( object* o, uid auid )
     32void uid_store_raw::insert( void* o, uid auid )
    3333{
    3434        m_map[ auid ] = o;
    3535}
    3636
    37 uid uid_store::insert( object* o )
     37uid uid_store_raw::insert( void* o )
    3838{
    3939        uid u = request_uid();
     
    4242}
    4343
    44 uid uid_store::request_uid()
     44uid uid_store_raw::request_uid()
    4545{
    4646        return ++m_current;
    4747}
    4848
    49 uid_store::~uid_store()
     49uid_store_raw::~uid_store_raw()
    5050{
    5151        // no-op
Note: See TracChangeset for help on using the changeset viewer.