summaryrefslogtreecommitdiff
path: root/ACE/ace/UUID.h
blob: 33f5018fcdab03748adb09ece0d44bbc94f86cb9 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// -*- C++ -*-

//=============================================================================
/**
 *  @file  UUID.h
 *
 *  @author Andrew T. Finnel <andrew@activesol.net>
 *  @author Yamuna Krishnmaurthy <yamuna@oomworks.com>
 */
//=============================================================================

#ifndef ACE_UUID_H
#define ACE_UUID_H
#include /**/ "ace/pre.h"

#include /**/ "ace/config-all.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/SString.h"
#include "ace/Singleton.h"
#include "ace/Synch_Traits.h"
#include <memory>

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE_Utils
{
  /**
   * @class UUID_Node
   *
   * @brief Holds the MAC-address of the UUID.
   */
  class ACE_Export UUID_Node
  {
  public:
    /// Size of the node in bytes.
    enum {NODE_ID_SIZE = 6};

    /// Type definition of the node.
    typedef u_char Node_ID[NODE_ID_SIZE];

    /// Get the node id
    Node_ID & node_ID ();

    /**
     * @overload
     */
    const Node_ID & node_ID () const;

    /// Test for equality.
    bool operator == (const UUID_Node & right) const;

    /// Test for inequality.
    bool operator != (const UUID_Node & right) const;

  private:
    /// The value of the node id.
    Node_ID node_ID_;
  };

  /**
   * @class ACE_UUID
   *
   * ACE_UUID represents 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.
   *
   * Additionally in this implementation provisions have been made to include
   * process and thread ids to make the UUIDs more unique. The variant 0xc0
   * has been added to facilitate this.
   */
  class ACE_Export UUID
  {
  public:
    /// The size of a binary UUID.
    enum { BINARY_SIZE = 16 };

    /// Constructor
    UUID ();

#ifndef ACE_LACKS_SSCANF
    /// Constructs a UUID from a string representation.
    UUID (const ACE_CString& uuidString);
#endif

    UUID (const UUID &right);

    // Destructor
    ~UUID () = default;

    ACE_UINT32 time_low () const;
    void time_low (ACE_UINT32);

    ACE_UINT16 time_mid () const;
    void time_mid (ACE_UINT16);

    ACE_UINT16 time_hi_and_version () const;
    void time_hi_and_version (ACE_UINT16);

    u_char clock_seq_hi_and_reserved () const;
    void clock_seq_hi_and_reserved (u_char);

    u_char clock_seq_low () const;
    void clock_seq_low (u_char);

    UUID_Node & node ();
    const UUID_Node & node () const;

    void node (const UUID_Node & node);

    ACE_CString* thr_id ();
    void thr_id (char*);

    ACE_CString* pid ();
    void pid (char*);

    /// Returns a string representation of the UUID
    const ACE_CString* to_string () const;

#ifndef ACE_LACKS_SSCANF
    /// Set the value using a string
    void from_string (const ACE_CString& uuid_string);
#endif

    /// NIL UUID
    static const UUID NIL_UUID;

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

    /// Compute a hash value for the UUID.
    unsigned long hash () const;

    /// Assign an existing UUID to this UUID.
    const UUID & operator = (const UUID & rhs);

    ACE_ALLOC_HOOK_DECLARE;

  private:
    /// Initialize the UUID
    void init ();

    /**
     * Helper method to convert from a string UUID.
     *
     * @param[in]        uuid_string        String version of UUID.
     */
#ifndef ACE_LACKS_SSCANF
    void from_string_i (const ACE_CString& uuid_string);
#endif

    /// Data Members for Class Attributes
    struct data
    {
      /// Time low.
      ACE_UINT32 time_low_;

      /// Time mid.
      ACE_UINT16 time_mid_;

      /// Time high and version.
      ACE_UINT16 time_hi_and_version_;

      /// Clock sequence high and reserved space.
      u_char clock_seq_hi_and_reserved_;

      /// Clock sequence low.
      u_char clock_seq_low_;

      /// MAC-address within the UUID.
      UUID_Node node_;
    } uuid_;

    ACE_CString thr_id_;
    ACE_CString pid_;

    /// The string representation of the UUID. This is created and
    /// updated only on demand.
    mutable std::unique_ptr <ACE_CString> as_string_;
  };

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

    enum {ACE_UUID_CLOCK_SEQ_MASK = 0x3FFF};

    /// Default constructor.
    UUID_Generator();

    /// Destructor.
    ~UUID_Generator();

    /// Initialize the UUID generator
    /// @deprecated This method may go away in some future release.
    void init ();

    /// Format timestamp, clockseq, and nodeID into an UUID of the
    /// specified version and variant. For generating UUID's with
    /// thread and process ids use variant=0xc0
    void generate_UUID (UUID&, ACE_UINT16 version=0x0001, u_char variant=0x80);

    /// Format timestamp, clockseq, and nodeID into a VI UUID. For
    /// generating UUID's with thread and process ids use variant=0xc0
    UUID* generate_UUID (ACE_UINT16 version=0x0001, u_char variant=0x80);

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

    /// The locking strategy prevents multiple generators from accessing
    /// the UUID_state at the same time. Get the locking strategy.
    ACE_SYNCH_MUTEX* lock ();

    /// Set a new locking strategy and return the old one.
    void lock (ACE_SYNCH_MUTEX* lock, bool release_lock);

  private:
    /// The system time when that last uuid was generated.
    UUID_Time time_last_;

    /// Type to contain the UUID generator persistent state. This will
    /// be kept in memory mapped shared memory
    struct UUID_State
    {
      UUID_Time timestamp;
      UUID_Node node;
      ACE_UINT16 clock_sequence;
    };

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

    /// Obtain a UUID timestamp and clock sequence. Compensate for the
    /// fact that the time obtained from getSystem time has a
    /// resolution less than 100ns.
    void get_timestamp_and_clocksequence (UUID_Time& timestamp,
                                          ACE_UINT16& clockSequence);

    /// 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_systemtime( UUID_Time& timeNow);

    /// The UUID generator persistent state.
    UUID_State uuid_state_;

    ACE_SYNCH_MUTEX* lock_;

    bool destroy_lock_;

    /// Initialization state of the generator.
    bool is_init_;
  };

  typedef ACE_Singleton <ACE_Utils::UUID_Generator, ACE_SYNCH_MUTEX>
          UUID_GENERATOR;
}

ACE_SINGLETON_DECLARE (ACE_Singleton, ACE_Utils::UUID_Generator, ACE_SYNCH_MUTEX)

ACE_END_VERSIONED_NAMESPACE_DECL

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

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