summaryrefslogtreecommitdiff
path: root/ace/UUID.h
blob: 4f966dd77660092dccc8d3b71e1d7bce51db7026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//$Id$
#ifndef ACE_UUID_H
#define ACE_UUID_H

#include "ace/pre.h"
#include "ace/OS.h"
#include "ace/SString.h"
#include "ace/Singleton.h"

typedef unsigned char Octet;

/**
 *  @class ACE_UUID
 *
 *	ACE_UUID represebt a Universally Unique IDentifier (UUID)
 * 	as described in (the expired) INTERNET-DRAFT
 *	specification entitled UUIDs and GUIDs. All instances of
 *	UUID are of the time-based variety. That is, the version
 *	number part of the timeHiAndVersion field is 1.
 *
 *	The default constructor creates a nil UUID.
 *
 *	UUIDs have value semantics. In addition, they may be
 *	compared for ordering and equality.
 *
 *	Note: the statement of UUID uniqueness is less stringent
 *	than the requirement for UUID in general. There is no
 *	guarantee that a UUID generated by by this class is
 *	universally unique with respect to another arbitrary
 *	UUID. 
 */
class ACE_Export ACE_UUID 
{
 public:
  
  /**
   * Type to hold a MAC address
   */
  struct UUID_node_t 
  {
    typedef enum { NODE_ID_SIZE = 6 } ;
    typedef Octet NodeID[NODE_ID_SIZE];
    bool operator == (const UUID_node_t& right) const;
    bool operator != (const UUID_node_t& right) const;
    bool operator <  (const UUID_node_t& right) const;
    NodeID nodeID;
  };

  struct UUID_t 
  {
    // Data Members for Class Attributes
    ACE_UINT32  timeLow;
    ACE_UINT16  timeMid;    
    ACE_UINT16  timeHiAndVersion;    
    Octet       clockSeqHiAndReserved;    
    Octet       clockSeqLow;    
    ACE_UINT16 thr_id;
    ACE_UINT32 pid;
	UUID_node_t node; 
  };

  ACE_UUID(void);

  /**
   * Copy constructor
   */
  ACE_UUID(const ACE_UUID &right);

  /** 
   * Construct a UUID based on a UUID_t
   */
  ACE_UUID (const UUID_t& newUUID);

  /**	
   * Constructs a UUID from a string representation.
   */
  ACE_UUID (const ACE_SString& uuidString);

  ~ACE_UUID(void);

  /**
   * Assignment Operation
   */
  ACE_UUID & operator=(const ACE_UUID &right);

  /** 
   * Equality Operations 
   */
  bool operator==(const ACE_UUID &right) const;
  bool operator!=(const ACE_UUID &right) const;

  /**
   * Relational Operations 
   */
  bool operator< (const ACE_UUID &right) const;
  bool operator> (const ACE_UUID &right) const;
  bool operator<=(const ACE_UUID &right) const;
  bool operator>=(const ACE_UUID &right) const;

  /**
   * Returns a string representation of the UUID. Internally,
   * the string representation, which is contained in as
   * String, is only computed if this operation is called.
   * Once the representation is computed it can never change.
   */
  const ACE_SString* to_string (void);
    
  static ACE_UUID NIL_UUID;
    
 private:
  /**
   * The string representation of the UUID. This is created
   * and updated only on demand.
   */
  //mutable ACE_SString asString; 
  ACE_SString *as_string_; 
    
  // This ACE_UUID_Impl's representation of the UUID
  UUID_t uuid_;
};

/**
 * @class ACE_UUID_Generator
 * Singleton class that generates UUIDs.
 */
class ACE_Export ACE_UUID_Generator
{
 public:

  //static ACE_UUID_Generator* instance();
    
  // Format timestamp, clockseq, and nodeID into a VI UUID.
  void generateV1(ACE_UUID::UUID_t&);

  // Format timestamp, clockseq, and nodeID into a VI UUID.
  ACE_UUID generateUUID ();

  /**
   * Type to represent UTC as a count of 100 nanosecond intervals since
   * 00:00:00.00, 15 October 1582.
   */
  typedef ACE_UINT64 UUID_time_t;

  ACE_UUID_Generator();
  ~ACE_UUID_Generator();

 private:

  //friend class ACE_Singleton< ACE_UUID_Generator, ACE_SYNCH_MUTEX >;

  

  /**
   * The maximum number of uuids that can be generated per tick of the
   * system clock. This number should be the number of 100ns ticks of the
   * actual resolution of the system's clock.
   */
  enum { UUIDS_PER_TICK = 1000 };

  /**
   * The number of uuids generated in this process since the last clock
   * tick. Value never exceeds uuidsPerTick - 1.
   */
  ACE_UINT32 uuidsThisTick;

  /**
   * The system time when that last uuid was generated.
   */
  UUID_time_t timeLast;

  /**
   * Type to map to contain the UUID generator persistent state. This
   * will be kept in memory mapped shared memory
   */
  struct UUID_state
  {
    UUID_time_t timestamp;
    ACE_UUID::UUID_node_t node;
    ACE_UINT16 clockSequence;
  };

  /**
   * Obtain a UUID timestamp. Compensate for the fact that the time
   * obtained from getSystem time has a resolution less than 100ns.
   */
  void getTimestamp( UUID_time_t& timestamp);

  /**
   * Obtain the system time in UTC as a count of 100 nanosecond intervals
   * since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to
   * the Christian calendar).
   */
  void get_system_time( UUID_time_t& timeNow);
  
  /**
   * The UUID generator persistent state.
   */
  UUID_state uuid_state_;
  
  // No value semantics
  //ACE_UNIMPLEMENTED_FUNC (ACE_UUID_Generator( const ACE_UUID_Generator&))
  // ACE_UNIMPLEMENTED_FUNC (ACE_UUID_Generator& operator=( const ACE_UUID_Generator&))
};

typedef ACE_Singleton <ACE_UUID_Generator, ACE_Thread_Mutex> ACE_UUID_GENERATOR;

#if defined (__ACE_INLINE__)
#include "ace/UUID.i"
#endif /* __ACE_INLINE__ */

#include "ace/post.h"
#endif // ACE_UUID_H