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
Line 
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{
21
22        class uid_store_raw
23        {
24        public:
25               
26                /**
27                 * Creates a new instance of the unique identifier store.
28                 */
29                uid_store_raw();
30
31                /**
32                 * Gets the object with the specified ID.
33                 *
34                 * @param auid The ID to fetch.
35                 * @returns The stored object for that ID.
36                 */
37                void* get( uid auid ) const;
38
39                /**
40                 * Removes the object with the specified ID.
41                 *
42                 * @param auid The ID to remove.
43                 * @returns True if the removal was successful, false if the ID didn't exist.
44                 */
45                bool remove( uid auid );
46
47                /**
48                 * Adds an object to the store assigned to the indicated ID.
49                 *
50                 * @param o The object to add to the store.
51                 * @param auid The ID to assign to the object.
52                 */
53                void insert( void* o, uid auid );
54
55                /**
56                 * Adds an object to the store and assigns it a new ID.
57                 *
58                 * @param o The object to add to the store.
59                 * @returns The ID the object was store under.
60                 */
61                uid insert( void* o );
62
63                /**
64                 * Gets the next available ID.
65                 *
66                 * @returns The next ID in the store.
67                 */
68                uid request_uid();
69
70                /**
71                 * Destroys the unique identifier store.
72                 */
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(); }
90
91                /**
92                 * Retrieves an object and casts it to the specified type.
93                 *
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.
97                 */
98                template< typename U >
99                U* get_as( uid auid ) const
100                {
101                        return dynamic_cast< U* >( get(auid) );
102                }
103        protected:
104                uid_store_raw m_store;
105        };
106       
107}
108
109#endif // NV_UID_HH
Note: See TracBrowser for help on using the repository browser.