UUID.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file  UUID.h
00006  *
00007  *  $Id: UUID.h 80826 2008-03-04 14:51:23Z wotte $
00008  *
00009  *  @author Andrew T. Finnel <andrew@activesol.net>
00010  *  @author Yamuna Krishnmaurthy <yamuna@oomworks.com>
00011  */
00012 //=============================================================================
00013 
00014 #ifndef ACE_UUID_H
00015 #define ACE_UUID_H
00016 #include /**/ "ace/pre.h"
00017 
00018 #include /**/ "ace/config-all.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/SString.h"
00025 #include "ace/Singleton.h"
00026 #include "ace/Synch_Traits.h"
00027 
00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00029 
00030 namespace ACE_Utils
00031 {
00032   /// Class to hold a MAC address
00033   class ACE_Export UUID_Node
00034   {
00035   public:
00036 
00037     /// Constructor
00038     UUID_Node (void);
00039 
00040     enum {NODE_ID_SIZE = 6};
00041     typedef u_char Node_ID[NODE_ID_SIZE];
00042 
00043     Node_ID &node_ID (void);
00044     void node_ID (Node_ID&);
00045 
00046     ///// Equality Operations
00047     bool operator == (const UUID_Node& right) const;
00048     bool operator != (const UUID_Node& right) const;
00049 
00050     ///// Relational Operations
00051     //bool operator <  (const UUID_Node& right) const;
00052 
00053   private:
00054     Node_ID node_ID_;
00055   };
00056 
00057   /**
00058    *  @class ACE_UUID
00059    *
00060    * ACE_UUID represents a Universally Unique IDentifier (UUID) as
00061    * described in (the expired) INTERNET-DRAFT specification entitled
00062    * UUIDs and GUIDs. All instances of UUID are of the time-based
00063    * variety. That is, the version number part of the timeHiAndVersion
00064    * field is 1.
00065    *
00066    * The default constructor creates a nil UUID.
00067    *
00068    * UUIDs have value semantics. In addition, they may be compared for
00069    * ordering and equality.
00070    *
00071    * Additionally in this implementation provisions have been made to include
00072    * process and thread ids to make the UUIDs more unique. The variant 0xc0
00073    * has been added to facilitate this.
00074    */
00075   class ACE_Export UUID
00076   {
00077   public:
00078 
00079     /// Constructor
00080     UUID (void);
00081 
00082     /// Constructs a UUID from a string representation.
00083     UUID (const ACE_CString& uuidString);
00084 
00085     UUID (const UUID &right);
00086 
00087     // Destructor
00088     ~UUID (void);
00089 
00090     ACE_UINT32 time_low (void) const;
00091     void time_low (ACE_UINT32);
00092 
00093     ACE_UINT16 time_mid (void) const;
00094     void time_mid (ACE_UINT16);
00095 
00096     ACE_UINT16 time_hi_and_version (void) const;
00097     void time_hi_and_version (ACE_UINT16);
00098 
00099     u_char clock_seq_hi_and_reserved (void) const;
00100     void clock_seq_hi_and_reserved (u_char);
00101 
00102     u_char clock_seq_low (void) const;
00103     void clock_seq_low (u_char);
00104 
00105     UUID_Node* node (void) const;
00106     void node (UUID_Node*);
00107 
00108     ACE_CString* thr_id (void);
00109     void thr_id (char*);
00110 
00111     ACE_CString* pid (void);
00112     void pid (char*);
00113 
00114     /// Returns a string representation of the UUID
00115     const ACE_CString* to_string (void);
00116 
00117     /// Set the value using a string
00118     void from_string (const ACE_CString& uuid_string);
00119 
00120     static UUID NIL_UUID;
00121 
00122     /// Equality Operations
00123     bool operator== (const UUID &right) const;
00124     bool operator!= (const UUID &right) const;
00125 
00126     /// Relational Operations
00127     //bool operator<  (const UUID &right) const;
00128     //bool operator>  (const UUID &right) const;
00129     //bool operator<= (const UUID &right) const;
00130     //bool operator>= (const UUID &right) const;
00131 
00132   private:
00133     void from_string_i (const ACE_CString& uuid_string);
00134 
00135     UUID& operator= (const UUID&);
00136 
00137     /// Data Members for Class Attributes
00138     ACE_UINT32 time_low_;
00139     ACE_UINT16 time_mid_;
00140     ACE_UINT16 time_hi_and_version_;
00141     u_char clock_seq_hi_and_reserved_;
00142     u_char clock_seq_low_;
00143     UUID_Node* node_;
00144     bool node_release_;
00145     ACE_CString thr_id_;
00146     ACE_CString pid_;
00147 
00148     /// The string representation of the UUID. This is created and
00149     /// updated only on demand.
00150     ACE_CString *as_string_;
00151   };
00152 
00153   /**
00154    * @class ACE_UUID_Generator
00155    *
00156    * Singleton class that generates UUIDs.
00157    *
00158    */
00159   class ACE_Export UUID_Generator
00160   {
00161   public:
00162 
00163     enum {ACE_UUID_CLOCK_SEQ_MASK = 0x3FFF};
00164 
00165     UUID_Generator();
00166     ~UUID_Generator();
00167 
00168     void init (void);
00169 
00170     /// Format timestamp, clockseq, and nodeID into an UUID of the
00171     /// specified version and variant. For generating UUID's with
00172     /// thread and process ids use variant=0xc0
00173     void generate_UUID (UUID&, ACE_UINT16 version=0x0001, u_char variant=0x80);
00174 
00175     /// Format timestamp, clockseq, and nodeID into a VI UUID. For
00176     /// generating UUID's with thread and process ids use variant=0xc0
00177     UUID* generate_UUID (ACE_UINT16 version=0x0001, u_char variant=0x80);
00178 
00179     /// Type to represent UTC as a count of 100 nanosecond intervals
00180     /// since 00:00:00.00, 15 October 1582.
00181     typedef ACE_UINT64 UUID_Time;
00182 
00183     /// The locking strategy prevents multiple generators from accessing
00184     /// the UUID_state at the same time. Get the locking strategy.
00185     ACE_SYNCH_MUTEX* lock (void);
00186 
00187     /// Set a new locking strategy and return the old one.
00188     void lock (ACE_SYNCH_MUTEX* lock,
00189                bool release_lock);
00190 
00191   private:
00192 
00193     /// The system time when that last uuid was generated.
00194     UUID_Time time_last_;
00195 
00196     /// Type to contain the UUID generator persistent state. This will
00197     /// be kept in memory mapped shared memory
00198     struct UUID_State
00199     {
00200       UUID_Time timestamp;
00201       UUID_Node node;
00202       ACE_UINT16 clock_sequence;
00203     };
00204 
00205     /// Obtain a UUID timestamp. Compensate for the fact that the time
00206     /// obtained from getSystem time has a resolution less than 100ns.
00207     void get_timestamp (UUID_Time& timestamp);
00208 
00209     /// Obtain a UUID timestamp and clock sequence. Compensate for the
00210     /// fact that the time obtained from getSystem time has a
00211     /// resolution less than 100ns.
00212     void get_timestamp_and_clocksequence (UUID_Time& timestamp, 
00213                                           ACE_UINT16& clockSequence);
00214 
00215     /// Obtain the system time in UTC as a count of 100 nanosecond intervals
00216     /// since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to
00217   /// the Christian calendar).
00218     void get_systemtime( UUID_Time& timeNow);
00219 
00220     /// The UUID generator persistent state.
00221     UUID_State uuid_state_;
00222 
00223     ACE_SYNCH_MUTEX* lock_;
00224     bool destroy_lock_;
00225   };
00226 
00227   typedef ACE_Singleton<UUID_Generator, ACE_SYNCH_MUTEX> UUID_GENERATOR;
00228 
00229 }
00230 
00231 ACE_END_VERSIONED_NAMESPACE_DECL
00232 
00233 #if defined (__ACE_INLINE__)
00234 #include "ace/UUID.inl"
00235 #endif /* __ACE_INLINE__ */
00236 
00237 #include /**/ "ace/post.h"
00238 #endif // ACE_UUID_H
00239 

Generated on Tue Feb 2 17:18:44 2010 for ACE by  doxygen 1.4.7