1 /*
2  * Project: VizKit
3  * Version: 2.3
4 
5  * Date: 20090823
6  * File: VisualItemIdentifier.h
7  *
8  */
9 
10 /***************************************************************************
11 
12 Copyright (c) 2004-2009 Heiko Wichmann (http://www.imagomat.de/vizkit)
13 
14 
15 This software is provided 'as-is', without any expressed or implied warranty.
16 In no event will the authors be held liable for any damages
17 arising from the use of this software.
18 
19 Permission is granted to anyone to use this software for any purpose,
20 including commercial applications, and to alter it and redistribute it
21 freely, subject to the following restrictions:
22 
23 1. The origin of this software must not be misrepresented;
24    you must not claim that you wrote the original software.
25    If you use this software in a product, an acknowledgment
26    in the product documentation would be appreciated
27    but is not required.
28 
29 2. Altered source versions must be plainly marked as such,
30    and must not be misrepresented as being the original software.
31 
32 3. This notice may not be removed or altered from any source distribution.
33 
34  ***************************************************************************/
35 
36 #ifndef VisualItemIdentifier_h
37 #define VisualItemIdentifier_h
38 
39 
40 #include "VisualTypes.h"
41 #include <map>
42 
43 
44 namespace VizKit {
45 
46 	/**
47 	 * Identifier of an item (like e.g. a VisualNurbs or a VisualAnimationComponent).
48 	 * The internal token of the identifier stays the same across copy operations. Comparisons of copied instances yield equal result. Identifiers can be shared by supplying a name. If another VisualItemIdentifier with the same name has been created already, it is returned by VisualItemIdentifierRegistry's getVisualItemIdentifier().
49 	 */
50 	class VisualItemIdentifier {
51 
52 	public:
53 
54 		/**
55 		 * The constructor.
56 		 */
57 		VisualItemIdentifier();
58 
59 		/**
60 		 * Another constructor.
61 		 * @param aName An optional identifier name (for shared VisualItemIdentifiers).
62 		 */
63 		VisualItemIdentifier(const char* const aName);
64 
65 		/**
66 		 * The destructor.
67 		 */
68 		~VisualItemIdentifier();
69 
70 		/**
71 		 * Copy constructor.
72 		 * @param other Another VisualItemIdentifier.
73 		 */
74 		VisualItemIdentifier(const VisualItemIdentifier& other);
75 
76 		/**
77 		 * Assignment operator.
78 		 */
79 		VisualItemIdentifier& operator=(const VisualItemIdentifier& other);
80 
81 		/**
82 		 * Less than operator.
83 		 * @param other Another VisualItemIdentifier.
84 		 * @remarks The less than operator is used by std::map for find() and sorting (if VisualItemIdentifier is used as key_type).
85 		 */
86 		bool operator<(const VisualItemIdentifier& other) const;
87 
88 		/**
89 		 * Equality operator.
90 		 * @param other Another VisualItemIdentifier.
91 		 */
92 		bool operator==(const VisualItemIdentifier& other) const;
93 
94 		/**
95 		 * Inequality operator.
96 		 * @param other Another VisualItemIdentifier.
97 		 */
98 		bool operator!=(const VisualItemIdentifier& other);
99 
100 		/**
101 		 * Returns a VisualItemIdentifier identified by name. By using a name as token, the same identifier can be used at different places.
102 		 * @param aName The name of the VisualItemIdentifier.
103 		 * @return A pointer to a visualItemIdentifier identified by its name.
104 		 * @remarks The caller has to release the allocated memory by calling delete() on the pointer.
105 		 */
106 		static VisualItemIdentifier* createVisualItemIdentifier(const char* const aName);
107 
108 		/**
109 		 * Returns the name (NULL if there is none).
110 		 * @return The optional identifier name.
111 		 */
112 		const char* const getName(void);
113 
114 	private:
115 
116 		/**
117 		 * Private constructor.
118 		 * @param aName Name of VisualItemIdentifier.
119 		 * @param aToken Token of VisualItemIdentifier.
120 		 */
121 		VisualItemIdentifier(const char* const aName, uint32 aToken);
122 
123 		/**
124 		 * Copy method for assignment operator and copy constructor.
125 		 * @param other Another VisualItemIdentifier.
126 		 */
127 		void copy(const VisualItemIdentifier& other);
128 
129 		/**
130 		 * The optional identifier name (for shared VisualItemIdentifiers).
131 		 */
132 		char* name;
133 
134 		/**
135 		 * Internal method to receive the next valid token.
136 		 */
137 		static uint32 getNextToken();
138 
139 		/**
140 		 * The internal identifier of the VisualItemIdentifier.
141 		 * @remarks The internal identifier stays the same across copies and instances. Since it is typed as 32bit unsigned integer the number of identifiers is limited to 4294967296.
142 		 */
143 		uint32 token;
144 
145 		/** A VisualItemIdentifierTokenNameMap is a map of names and tokens. */
146 		typedef std::map<std::string, uint32> VisualItemIdentifierNameTokenMap;
147 
148 		/** A map of names and tokens. */
149 		static VisualItemIdentifierNameTokenMap visualItemIdentifierNameTokenMap;
150 
151 		/** A VisualItemIdentifierNameTokenMapIterator is an iterator of a VisualItemIdentifierNameTokenMap. */
152 		typedef VisualItemIdentifierNameTokenMap::iterator VisualItemIdentifierNameTokenMapIterator;
153 
154 	};
155 
156 }
157 
158 #endif /* VisualItemIdentifier_h */
159