[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 |
|
---|
| 19 | namespace nv
|
---|
| 20 | {
|
---|
[262] | 21 | class object;
|
---|
| 22 |
|
---|
[59] | 23 | class uid_store
|
---|
| 24 | {
|
---|
| 25 | public:
|
---|
[110] | 26 |
|
---|
| 27 | /**
|
---|
[262] | 28 | * Creates a new instance of the unique indentifier store.
|
---|
[110] | 29 | */
|
---|
[59] | 30 | uid_store();
|
---|
[110] | 31 |
|
---|
| 32 | /**
|
---|
[132] | 33 | * Gets the object with the specified ID.
|
---|
[110] | 34 | *
|
---|
[132] | 35 | * @param auid The ID to fetch.
|
---|
| 36 | * @returns The stored object for that ID.
|
---|
[110] | 37 | */
|
---|
[59] | 38 | object* get( uid auid ) const;
|
---|
[110] | 39 |
|
---|
| 40 | /**
|
---|
[132] | 41 | * Removes the object with the specified ID.
|
---|
[110] | 42 | *
|
---|
[132] | 43 | * @param auid The ID to remove.
|
---|
| 44 | * @returns True if the removal was successful, false if the ID didn't exist.
|
---|
[110] | 45 | */
|
---|
[59] | 46 | bool remove( uid auid );
|
---|
[110] | 47 |
|
---|
| 48 | /**
|
---|
[132] | 49 | * Adds an object to the store assigned to the indicated ID.
|
---|
[110] | 50 | *
|
---|
[132] | 51 | * @param o The object to add to the store.
|
---|
| 52 | * @param auid The ID to assign to the object.
|
---|
[110] | 53 | */
|
---|
[59] | 54 | void insert( object* o, uid auid );
|
---|
[110] | 55 |
|
---|
| 56 | /**
|
---|
[132] | 57 | * Adds an object to the store and assigns it a new ID.
|
---|
[110] | 58 | *
|
---|
[132] | 59 | * @param o The object to add to the store.
|
---|
| 60 | * @returns The ID the object was store under.
|
---|
[110] | 61 | */
|
---|
[59] | 62 | uid insert( object* o );
|
---|
[110] | 63 |
|
---|
| 64 | /**
|
---|
[132] | 65 | * Gets the next available ID.
|
---|
[110] | 66 | *
|
---|
[132] | 67 | * @returns The next ID in the store.
|
---|
[110] | 68 | */
|
---|
[59] | 69 | uid request_uid();
|
---|
[110] | 70 |
|
---|
| 71 | /**
|
---|
[132] | 72 | * Destroys the unique identifier store.
|
---|
[110] | 73 | */
|
---|
[59] | 74 | ~uid_store();
|
---|
[110] | 75 |
|
---|
| 76 | /**
|
---|
[132] | 77 | * Retrieves an object and casts it to the specified type.
|
---|
[110] | 78 | *
|
---|
[132] | 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.
|
---|
[110] | 82 | */
|
---|
[59] | 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;
|
---|
[110] | 90 | map m_map; ///< The hash map everything is stored in.
|
---|
| 91 | uid m_current; ///< The last UID assigned.
|
---|
[59] | 92 | };
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | #endif // NV_UID_HH
|
---|