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