Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / UUID.h
blobaade27a2ad03861188d062d2a8f8b720f8f4814d
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file UUID.h
7 * @author Andrew T. Finnel <andrew@activesol.net>
8 * @author Yamuna Krishnmaurthy <yamuna@oomworks.com>
9 */
10 //=============================================================================
12 #ifndef ACE_UUID_H
13 #define ACE_UUID_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/config-all.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # pragma once
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/SString.h"
23 #include "ace/Singleton.h"
24 #include "ace/Synch_Traits.h"
25 #include <memory>
27 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
29 namespace ACE_Utils
31 /**
32 * @class UUID_Node
34 * @brief Holds the MAC-address of the UUID.
36 class ACE_Export UUID_Node
38 public:
39 /// Size of the node in bytes.
40 enum {NODE_ID_SIZE = 6};
42 /// Type definition of the node.
43 typedef u_char Node_ID[NODE_ID_SIZE];
45 /// Get the node id
46 Node_ID & node_ID ();
48 /**
49 * @overload
51 const Node_ID & node_ID () const;
53 /// Test for equality.
54 bool operator == (const UUID_Node & right) const;
56 /// Test for inequality.
57 bool operator != (const UUID_Node & right) const;
59 private:
60 /// The value of the node id.
61 Node_ID node_ID_;
64 /**
65 * @class ACE_UUID
67 * ACE_UUID represents a Universally Unique IDentifier (UUID) as
68 * described in (the expired) INTERNET-DRAFT specification entitled
69 * UUIDs and GUIDs. All instances of UUID are of the time-based
70 * variety. That is, the version number part of the timeHiAndVersion
71 * field is 1.
73 * The default constructor creates a nil UUID.
75 * UUIDs have value semantics. In addition, they may be compared for
76 * ordering and equality.
78 * Additionally in this implementation provisions have been made to include
79 * process and thread ids to make the UUIDs more unique. The variant 0xc0
80 * has been added to facilitate this.
82 class ACE_Export UUID
84 public:
85 /// The size of a binary UUID.
86 enum { BINARY_SIZE = 16 };
88 /// Constructor
89 UUID ();
91 #ifndef ACE_LACKS_SSCANF
92 /// Constructs a UUID from a string representation.
93 UUID (const ACE_CString& uuidString);
94 #endif
96 UUID (const UUID &right);
98 // Destructor
99 ~UUID () = default;
101 ACE_UINT32 time_low () const;
102 void time_low (ACE_UINT32);
104 ACE_UINT16 time_mid () const;
105 void time_mid (ACE_UINT16);
107 ACE_UINT16 time_hi_and_version () const;
108 void time_hi_and_version (ACE_UINT16);
110 u_char clock_seq_hi_and_reserved () const;
111 void clock_seq_hi_and_reserved (u_char);
113 u_char clock_seq_low () const;
114 void clock_seq_low (u_char);
116 UUID_Node & node ();
117 const UUID_Node & node () const;
119 void node (const UUID_Node & node);
121 ACE_CString* thr_id ();
122 void thr_id (char*);
124 ACE_CString* pid ();
125 void pid (char*);
127 /// Returns a string representation of the UUID
128 const ACE_CString* to_string () const;
130 #ifndef ACE_LACKS_SSCANF
131 /// Set the value using a string
132 void from_string (const ACE_CString& uuid_string);
133 #endif
135 /// NIL UUID
136 static const UUID NIL_UUID;
138 /// Equality Operations
139 bool operator == (const UUID &right) const;
140 bool operator != (const UUID &right) const;
142 /// Compute a hash value for the UUID.
143 unsigned long hash () const;
145 /// Assign an existing UUID to this UUID.
146 const UUID & operator = (const UUID & rhs);
148 ACE_ALLOC_HOOK_DECLARE;
150 private:
151 /// Initialize the UUID
152 void init ();
155 * Helper method to convert from a string UUID.
157 * @param[in] uuid_string String version of UUID.
159 #ifndef ACE_LACKS_SSCANF
160 void from_string_i (const ACE_CString& uuid_string);
161 #endif
163 /// Data Members for Class Attributes
164 struct data
166 /// Time low.
167 ACE_UINT32 time_low_;
169 /// Time mid.
170 ACE_UINT16 time_mid_;
172 /// Time high and version.
173 ACE_UINT16 time_hi_and_version_;
175 /// Clock sequence high and reserved space.
176 u_char clock_seq_hi_and_reserved_;
178 /// Clock sequence low.
179 u_char clock_seq_low_;
181 /// MAC-address within the UUID.
182 UUID_Node node_;
183 } uuid_;
185 ACE_CString thr_id_;
186 ACE_CString pid_;
188 /// The string representation of the UUID. This is created and
189 /// updated only on demand.
190 mutable std::unique_ptr <ACE_CString> as_string_;
194 * @class ACE_UUID_Generator
196 * Singleton class that generates UUIDs.
199 class ACE_Export UUID_Generator
201 public:
202 enum {ACE_UUID_CLOCK_SEQ_MASK = 0x3FFF};
204 /// Default constructor.
205 UUID_Generator();
207 /// Destructor.
208 ~UUID_Generator();
210 /// Initialize the UUID generator
211 /// @deprecated This method may go away in some future release.
212 void init ();
214 /// Format timestamp, clockseq, and nodeID into an UUID of the
215 /// specified version and variant. For generating UUID's with
216 /// thread and process ids use variant=0xc0
217 void generate_UUID (UUID&, ACE_UINT16 version=0x0001, u_char variant=0x80);
219 /// Format timestamp, clockseq, and nodeID into a VI UUID. For
220 /// generating UUID's with thread and process ids use variant=0xc0
221 UUID* generate_UUID (ACE_UINT16 version=0x0001, u_char variant=0x80);
223 /// Type to represent UTC as a count of 100 nanosecond intervals
224 /// since 00:00:00.00, 15 October 1582.
225 typedef ACE_UINT64 UUID_Time;
227 /// The locking strategy prevents multiple generators from accessing
228 /// the UUID_state at the same time. Get the locking strategy.
229 ACE_SYNCH_MUTEX* lock ();
231 /// Set a new locking strategy and return the old one.
232 void lock (ACE_SYNCH_MUTEX* lock, bool release_lock);
234 private:
235 /// The system time when that last uuid was generated.
236 UUID_Time time_last_;
238 /// Type to contain the UUID generator persistent state. This will
239 /// be kept in memory mapped shared memory
240 struct UUID_State
242 UUID_Time timestamp;
243 UUID_Node node;
244 ACE_UINT16 clock_sequence;
247 /// Obtain a UUID timestamp. Compensate for the fact that the time
248 /// obtained from getSystem time has a resolution less than 100ns.
249 void get_timestamp (UUID_Time& timestamp);
251 /// Obtain a UUID timestamp and clock sequence. Compensate for the
252 /// fact that the time obtained from getSystem time has a
253 /// resolution less than 100ns.
254 void get_timestamp_and_clocksequence (UUID_Time& timestamp,
255 ACE_UINT16& clockSequence);
257 /// Obtain the system time in UTC as a count of 100 nanosecond intervals
258 /// since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to
259 /// the Christian calendar).
260 void get_systemtime( UUID_Time& timeNow);
262 /// The UUID generator persistent state.
263 UUID_State uuid_state_;
265 ACE_SYNCH_MUTEX* lock_;
267 bool destroy_lock_;
269 /// Initialization state of the generator.
270 bool is_init_;
274 ACE_SINGLETON_DECLARE (ACE_Singleton, ACE_Utils::UUID_Generator, ACE_SYNCH_MUTEX)
276 namespace ACE_Utils
278 typedef ACE_Singleton <ACE_Utils::UUID_Generator, ACE_SYNCH_MUTEX> UUID_GENERATOR;
281 ACE_END_VERSIONED_NAMESPACE_DECL
283 #if defined (__ACE_INLINE__)
284 #include "ace/UUID.inl"
285 #endif /* __ACE_INLINE__ */
287 #include /**/ "ace/post.h"
288 #endif // ACE_UUID_H