source: trunk/nv/uid.hh @ 266

Last change on this file since 266 was 266, checked in by epyon, 11 years ago
  • 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)
File size: 2.5 KB
RevLine 
[59]1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7/**
8 * @file uid.hh
9 * @author Kornel Kisielewicz
10 * @brief Implements a unique identifier class manager
11 */
12
13#ifndef NV_UID_HH
14#define NV_UID_HH
15
16#include <nv/common.hh>
17#include <unordered_map>
18
19namespace nv
20{
[262]21
[266]22        class uid_store_raw
[59]23        {
24        public:
[110]25               
26                /**
[266]27                 * Creates a new instance of the unique identifier store.
[110]28                 */
[266]29                uid_store_raw();
[110]30
31                /**
[132]32                 * Gets the object with the specified ID.
[110]33                 *
[132]34                 * @param auid The ID to fetch.
35                 * @returns The stored object for that ID.
[110]36                 */
[266]37                void* get( uid auid ) const;
[110]38
39                /**
[132]40                 * Removes the object with the specified ID.
[110]41                 *
[132]42                 * @param auid The ID to remove.
43                 * @returns True if the removal was successful, false if the ID didn't exist.
[110]44                 */
[59]45                bool remove( uid auid );
[110]46
47                /**
[132]48                 * Adds an object to the store assigned to the indicated ID.
[110]49                 *
[132]50                 * @param o The object to add to the store.
51                 * @param auid The ID to assign to the object.
[110]52                 */
[266]53                void insert( void* o, uid auid );
[110]54
55                /**
[132]56                 * Adds an object to the store and assigns it a new ID.
[110]57                 *
[132]58                 * @param o The object to add to the store.
59                 * @returns The ID the object was store under.
[110]60                 */
[266]61                uid insert( void* o );
[110]62
63                /**
[132]64                 * Gets the next available ID.
[110]65                 *
[132]66                 * @returns The next ID in the store.
[110]67                 */
[59]68                uid request_uid();
[110]69
70                /**
[132]71                 * Destroys the unique identifier store.
[110]72                 */
[266]73                ~uid_store_raw();
[110]74
[266]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(); }
90
[110]91                /**
[132]92                 * Retrieves an object and casts it to the specified type.
[110]93                 *
[132]94                 * @tparam T The type to cast to.
95                 * @param auid The ID the object is stored under.
96                 * @returns An object of the indicated type that was stored at the indicated ID.
[110]97                 */
[266]98                template< typename U >
99                U* get_as( uid auid ) const
[59]100                {
[266]101                        return dynamic_cast< U* >( get(auid) );
[59]102                }
[266]103        protected:
104                uid_store_raw m_store;
[59]105        };
[266]106       
[59]107}
108
109#endif // NV_UID_HH
Note: See TracBrowser for help on using the repository browser.