UUID.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file  UUID.h
00006  *
00007  *  $Id: UUID.h 74958 2006-10-23 23:31:00Z ossama $
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 NodeID[NODE_ID_SIZE];
00042 
00043     NodeID& nodeID (void);
00044     void nodeID (NodeID&);
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     NodeID nodeID_;
00055   };
00056 
00057 
00058 
00059   /**
00060    *  @class ACE_UUID
00061    *
00062    * ACE_UUID represents a Universally Unique IDentifier (UUID) as
00063    * described in (the expired) INTERNET-DRAFT specification entitled
00064    * UUIDs and GUIDs. All instances of UUID are of the time-based
00065    * variety. That is, the version number part of the timeHiAndVersion
00066    * field is 1.
00067    *
00068    * The default constructor creates a nil UUID.
00069    *
00070    * UUIDs have value semantics. In addition, they may be compared for
00071    * ordering and equality.
00072    *
00073    * Additionally in this implementation provisions have been made to include
00074    * process and thread ids to make the UUIDs more unique. The variant 0xc0
00075    * has been added to facilitate this.
00076    */
00077   class ACE_Export UUID
00078   {
00079   public:
00080 
00081     /// Constructor
00082     UUID (void);
00083 
00084     /// Constructs a UUID from a string representation.
00085     UUID (const ACE_CString& uuidString);
00086 
00087     UUID (const UUID &right);
00088 
00089     // Destructor
00090     ~UUID (void);
00091 
00092     ACE_UINT32 timeLow (void) const;
00093     void timeLow (ACE_UINT32);
00094 
00095     ACE_UINT16 timeMid (void) const;
00096     void timeMid (ACE_UINT16);
00097 
00098     ACE_UINT16 timeHiAndVersion (void) const;
00099     void timeHiAndVersion (ACE_UINT16);
00100 
00101     u_char clockSeqHiAndReserved (void) const;
00102     void clockSeqHiAndReserved (u_char);
00103 
00104     u_char clockSeqLow (void) const;
00105     void clockSeqLow (u_char);
00106 
00107     UUID_node* node (void) const;
00108     void node (UUID_node*);
00109 
00110     ACE_CString* thr_id (void);
00111     void thr_id (char*);
00112 
00113     ACE_CString* pid (void);
00114     void pid (char*);
00115 
00116     /// Returns a string representation of the UUID
00117     const ACE_CString* to_string (void);
00118 
00119     /// Set the value using a string
00120     void from_string (const ACE_CString& uuid_string);
00121 
00122     static UUID NIL_UUID;
00123 
00124     /// Equality Operations
00125     bool operator== (const UUID &right) const;
00126     bool operator!= (const UUID &right) const;
00127 
00128     /// Relational Operations
00129     //bool operator<  (const UUID &right) const;
00130     //bool operator>  (const UUID &right) const;
00131     //bool operator<= (const UUID &right) const;
00132     //bool operator>= (const UUID &right) const;
00133 
00134   private:
00135     void from_string_i (const ACE_CString& uuidString);
00136 
00137     UUID& operator= (const UUID&);
00138 
00139     /// Data Members for Class Attributes
00140     ACE_UINT32 timeLow_;
00141     ACE_UINT16 timeMid_;
00142     ACE_UINT16 timeHiAndVersion_;
00143     u_char clockSeqHiAndReserved_;
00144     u_char clockSeqLow_;
00145     UUID_node* node_;
00146     bool node_release_;
00147     ACE_CString thr_id_;
00148     ACE_CString pid_;
00149 
00150     /// The string representation of the UUID. This is created and
00151     /// updated only on demand.
00152     ACE_CString *as_string_;
00153   };
00154 
00155 
00156   /**
00157    * @class ACE_UUID_Generator
00158    *
00159    * Singleton class that generates UUIDs.
00160    *
00161    */
00162   class ACE_Export UUID_Generator
00163   {
00164   public:
00165 
00166     enum {ACE_UUID_CLOCK_SEQ_MASK = 0x3FFF};
00167 
00168     UUID_Generator();
00169     ~UUID_Generator();
00170 
00171     void init (void);
00172 
00173     /// Format timestamp, clockseq, and nodeID into an UUID of the
00174     /// specified version and variant. For generating UUID's with
00175     /// thread and process ids use variant=0xc0
00176     void generateUUID (UUID&, ACE_UINT16 version=0x0001, u_char variant=0x80);
00177 
00178     /// Format timestamp, clockseq, and nodeID into a VI UUID. For
00179     /// generating UUID's with thread and process ids use variant=0xc0
00180     UUID* generateUUID (ACE_UINT16 version=0x0001, u_char variant=0x80);
00181 
00182     /// Type to represent UTC as a count of 100 nanosecond intervals
00183     /// since 00:00:00.00, 15 October 1582.
00184     typedef ACE_UINT64 UUID_time;
00185 
00186     /// The locking strategy prevents multiple generators from accessing
00187     /// the UUID_state at the same time. Get the locking strategy.
00188     ACE_SYNCH_MUTEX* lock (void);
00189 
00190     /// Set a new locking strategy and return the old one.
00191     void lock (ACE_SYNCH_MUTEX* lock,
00192                bool release_lock);
00193 
00194   private:
00195 
00196     /// The system time when that last uuid was generated.
00197     UUID_time timeLast_;
00198 
00199     /// Type to contain the UUID generator persistent state. This will
00200     /// be kept in memory mapped shared memory
00201     struct UUID_State
00202     {
00203       UUID_time timestamp;
00204       UUID_node node;
00205       ACE_UINT16 clockSequence;
00206     };
00207 
00208     /// Obtain a UUID timestamp. Compensate for the fact that the time
00209     /// obtained from getSystem time has a resolution less than 100ns.
00210   void get_timestamp (UUID_time& timestamp);
00211 
00212     /// Obtain the system time in UTC as a count of 100 nanosecond intervals
00213     /// since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to
00214   /// the Christian calendar).
00215     void get_systemtime( UUID_time& timeNow);
00216 
00217     /// The UUID generator persistent state.
00218     UUID_State uuid_state_;
00219 
00220     ACE_SYNCH_MUTEX* lock_;
00221     bool destroy_lock_;
00222   };
00223 
00224   typedef ACE_Singleton<UUID_Generator, ACE_SYNCH_MUTEX> UUID_GENERATOR;
00225 
00226 }
00227 
00228 ACE_END_VERSIONED_NAMESPACE_DECL
00229 
00230 #if defined (__ACE_INLINE__)
00231 #include "ace/UUID.inl"
00232 #endif /* __ACE_INLINE__ */
00233 
00234 #include /**/ "ace/post.h"
00235 #endif // ACE_UUID_H

Generated on Sun Jan 27 12:05:43 2008 for ACE by doxygen 1.3.6