UUID.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file  UUID.h
00006  *
00007  *  UUID.h,v 1.10 2005/10/28 23:55:10 ossama Exp
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     // Destructor
00088     ~UUID (void);
00089 
00090     ACE_UINT32 timeLow (void);
00091     void timeLow (ACE_UINT32);
00092 
00093     ACE_UINT16 timeMid (void);
00094     void timeMid (ACE_UINT16);
00095 
00096     ACE_UINT16 timeHiAndVersion (void);
00097     void timeHiAndVersion (ACE_UINT16);
00098 
00099     u_char clockSeqHiAndReserved (void);
00100     void clockSeqHiAndReserved (u_char);
00101 
00102     u_char clockSeqLow (void);
00103     void clockSeqLow (u_char);
00104 
00105     UUID_node* node (void);
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     static UUID NIL_UUID;
00118 
00119     /// Equality Operations
00120     //bool operator== (const UUID &right) const;
00121     //bool operator!= (const UUID &right) const;
00122 
00123     /// Relational Operations
00124     //bool operator<  (const UUID &right) const;
00125     //bool operator>  (const UUID &right) const;
00126     //bool operator<= (const UUID &right) const;
00127     //bool operator>= (const UUID &right) const;
00128 
00129   private:
00130     // Copy constructor and assignment operator were not implemented
00131     // correctly, so I removed their implementation as an easy solution.
00132     UUID (const UUID&);
00133     UUID& operator= (const UUID&);
00134 
00135     /// Data Members for Class Attributes
00136     ACE_UINT32 timeLow_;
00137     ACE_UINT16 timeMid_;
00138     ACE_UINT16 timeHiAndVersion_;
00139     u_char clockSeqHiAndReserved_;
00140     u_char clockSeqLow_;
00141     UUID_node* node_;
00142     int node_release_;
00143     ACE_CString thr_id_;
00144     ACE_CString pid_;
00145 
00146     /// The string representation of the UUID. This is created and
00147     /// updated only on demand.
00148     ACE_CString *as_string_;
00149   };
00150 
00151 
00152   /**
00153    * @class ACE_UUID_Generator
00154    *
00155    * Singleton class that generates UUIDs.
00156    *
00157    */
00158   class ACE_Export UUID_Generator
00159   {
00160   public:
00161 
00162     enum {ACE_UUID_CLOCK_SEQ_MASK = 0x3FFF};
00163 
00164     UUID_Generator();
00165     ~UUID_Generator();
00166 
00167     void init (void);
00168 
00169     /// Format timestamp, clockseq, and nodeID into an UUID of the
00170     /// specified version and variant. For generating UUID's with
00171     /// thread and process ids use variant=0xc0
00172     void generateUUID (UUID&, ACE_UINT16 version=0x0001, u_char variant=0x80);
00173 
00174     /// Format timestamp, clockseq, and nodeID into a VI UUID. For
00175     /// generating UUID's with thread and process ids use variant=0xc0
00176     UUID* generateUUID (ACE_UINT16 version=0x0001, u_char variant=0x80);
00177 
00178     /// Type to represent UTC as a count of 100 nanosecond intervals
00179     /// since 00:00:00.00, 15 October 1582.
00180     typedef ACE_UINT64 UUID_time;
00181 
00182     /// The locking strategy prevents multiple generators from accessing
00183     /// the UUID_state at the same time. Get the locking strategy.
00184     ACE_SYNCH_MUTEX* lock (void);
00185 
00186     /// Set a new locking strategy and return the old one.
00187     ACE_SYNCH_MUTEX* lock (ACE_SYNCH_MUTEX*lock,
00188                            int release_lock);
00189 
00190 
00191   private:
00192 
00193     /// The system time when that last uuid was generated.
00194     UUID_time timeLast_;
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 clockSequence;
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 the system time in UTC as a count of 100 nanosecond intervals
00210     /// since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to
00211   /// the Christian calendar).
00212     void get_systemtime( UUID_time& timeNow);
00213 
00214     /// The UUID generator persistent state.
00215     UUID_State uuid_state_;
00216 
00217     ACE_SYNCH_MUTEX* lock_;
00218     int destroy_lock_;
00219   };
00220 
00221   typedef ACE_Singleton<UUID_Generator, ACE_SYNCH_MUTEX> UUID_GENERATOR;
00222 
00223 }
00224 
00225 ACE_END_VERSIONED_NAMESPACE_DECL
00226 
00227 #if defined (__ACE_INLINE__)
00228 #include "ace/UUID.inl"
00229 #endif /* __ACE_INLINE__ */
00230 
00231 #include /**/ "ace/post.h"
00232 #endif // ACE_UUID_H

Generated on Thu Nov 9 09:42:09 2006 for ACE by doxygen 1.3.6