source: trunk/nv/uid.hh @ 262

Last change on this file since 262 was 262, checked in by epyon, 11 years ago
  • major decoupling lua system independent of nv::object nv::object support for lua opt-in lua system independent of types.hh
File size: 2.1 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        class object;
22
23        class uid_store
24        {
25        public:
26               
27                /**
28                 * Creates a new instance of the unique indentifier store.
29                 */
30                uid_store();
31
32                /**
33                 * Gets the object with the specified ID.
34                 *
35                 * @param auid The ID to fetch.
36                 * @returns The stored object for that ID.
37                 */
38                object* get( uid auid ) const;
39
40                /**
41                 * Removes the object with the specified ID.
42                 *
43                 * @param auid The ID to remove.
44                 * @returns True if the removal was successful, false if the ID didn't exist.
45                 */
46                bool remove( uid auid );
47
48                /**
49                 * Adds an object to the store assigned to the indicated ID.
50                 *
51                 * @param o The object to add to the store.
52                 * @param auid The ID to assign to the object.
53                 */
54                void insert( object* o, uid auid );
55
56                /**
57                 * Adds an object to the store and assigns it a new ID.
58                 *
59                 * @param o The object to add to the store.
60                 * @returns The ID the object was store under.
61                 */
62                uid insert( object* o );
63
64                /**
65                 * Gets the next available ID.
66                 *
67                 * @returns The next ID in the store.
68                 */
69                uid request_uid();
70
71                /**
72                 * Destroys the unique identifier store.
73                 */
74                ~uid_store();
75
76                /**
77                 * Retrieves an object and casts it to the specified type.
78                 *
79                 * @tparam T The type to cast to.
80                 * @param auid The ID the object is stored under.
81                 * @returns An object of the indicated type that was stored at the indicated ID.
82                 */
83                template< typename T >
84                T* get_as( uid auid ) const
85                {
86                        return dynamic_cast< T* >( get(auid) );
87                }
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.
92        };
93}
94
95#endif // NV_UID_HH
Note: See TracBrowser for help on using the repository browser.