• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ut_uuid.h

Go to the documentation of this file.
00001 /* AbiSource Program Utilities
00002  * Copyright (C) 2003-2004 Tomas Frydrych <tomasfrydrych@yahoo.co.uk>
00003  *
00004  * Based on libuuid
00005  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
00006  *
00007  * The hash functions can be compile to use Fowler/Noll/Vo (FNV) public domain algorithm;
00008  * see http://www.isthe.com/chongo/tech/comp/fnv/index.html
00009  *
00010  * This program is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU General Public License
00012  * as published by the Free Software Foundation; either version 2
00013  * of the License, or (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00023  * 02111-1307, USA.
00024  */
00025 
00026 #ifndef UT_UUID_H
00027 #define UT_UUID_H
00028 
00029 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
00030  * so even if it's commented out in-file that's still a lot of work for
00031  * the preprocessor to do...
00032  */
00033 #ifndef UT_TYPES_H
00034 #include "ut_types.h"
00035 #endif
00036 #include "ut_vector.h"
00037 #include <time.h>
00038 
00039 #include <string>
00040 
00041 class UT_UTF8String;
00042 
00043 
00044 /* UUID Variant definitions */
00045 typedef enum
00046 {
00047     UT_UUID_VARIANT_NCS = 0,
00048     UT_UUID_VARIANT_DCE = 1,
00049     UT_UUID_VARIANT_MICROSOFT = 2,
00050     UT_UUID_VARIANT_OTHER = 3,
00051     UT_UUID_VARIANT_ERROR = 0xffffffff
00052 } UT_UUIDVariant;
00053 
00054 struct uuid
00055 {
00056     UT_uint32   time_low;
00057     UT_uint16   time_mid;
00058     UT_uint16   time_high_and_version;
00059     UT_uint16   clock_seq;
00060     UT_Byte     node[6];
00061 };
00062 
00063 // forward declaration
00064 class UT_UUIDGenerator;
00065 
00066 
00079 class ABI_EXPORT UT_UUID
00080 {
00081   public:
00082     /*
00083        All constructors are protected; instances of UT_UUID will be
00084        created through UT_UUIDGenerator declared below.
00085     */
00086 
00087     /* virtual destructor */
00088     virtual ~UT_UUID (){};
00089 
00090     /*
00091        Various manipulation functions; in general functions that take
00092        some kind of an output parameter (usually named 'out' or 'to'
00093        below) DO NOT modifiy internal state of the class, and return
00094        value of 'true' indicates success.
00095     */
00096 
00097     /* These generate new UUIDs */
00098     bool            makeUUID();                // changes internal state
00099     bool            makeUUID(UT_UTF8String & out); // does not change internal state !!!
00100 
00101     /* these set m_uuid to given UUID, i.e., force internal state change */
00102     bool            setUUID(const UT_UTF8String &s);
00103     bool            setUUID(const char *s);
00104     bool            setUUID(const struct uuid &u);
00105 
00106     /* translate internal state into string representation; do not change
00107        internal state */
00108     bool            toString(UT_UTF8String & to) const;
00109     std::string&    toString( std::string& to ) const;
00110 
00111     /* get the binary representation of the uuid */
00112     bool            toBinary(struct uuid &u) const;
00113 
00114     /* convert binary uuid representaiton to a string */
00115     static bool     toStringFromBinary(char * s, UT_uint32 len, const struct uuid &u);
00116 
00117     /* create hash of the uuid -- if all you need is the hash, use
00118        UT_UUIDGenerator::getUUID*() instead of these; it provides collision correction */
00119     UT_uint32       hash32() const;
00120     UT_uint64       hash64() const;
00121 
00122     /* return a NULL uuid; useful in fuction that return reference to
00123        UT_UUID to indicate failure */
00124     static const UT_UUID & getNull() {return s_Null;}
00125 
00126     /* these retrieve various information from UUID; internal and
00127        external variants */
00128     time_t          getTime() const; // NB: time_t has only 1s granularity !!!
00129     UT_sint32       getType() const;
00130     UT_UUIDVariant  getVariant() const;
00131 
00132     bool            resetTime(); // sets the time of UUID to now
00133 
00134 
00135     /* NB: these are operators over the UUID space, not temporal
00136        operators !!! */
00137     bool            operator ==(const UT_UUID &u) const;
00138     bool            operator !=(const UT_UUID &u) const;
00139     bool            operator < (const UT_UUID &u) const;
00140     bool            operator > (const UT_UUID &u) const;
00141 
00142     UT_UUID &       operator = (const UT_UUID &u);
00143 
00144     /* temporal comparisons */
00145     bool            isOlder(const UT_UUID &u) const;
00146     bool            isYounger(const UT_UUID &u) const;
00147     bool            isOfSameAge(const UT_UUID &u) const;
00148 
00149     bool            isValid() const {return m_bIsValid;}
00150     bool            isNull() const;
00151 
00152     /* reset internal state to NULL uuid */
00153     void            clear();
00154 
00155   protected:
00156     friend class UT_UUIDGenerator;
00157 
00158     /* various protected constructors */
00159 
00160     UT_UUID(); // constructs NULL uuid; subsequent call to makeUUID() needed to initialise
00161     UT_UUID(const UT_UTF8String &s); // initialises from string
00162     UT_UUID(const char *s);      // initialises from string
00163     UT_UUID(const struct uuid&u);   // initialise from binary representation
00164     UT_UUID(const UT_UUID &u);   // copy constructor
00165 
00166     /* the following function can be ovewritten when a better source
00167        of randomness than UT_rand() is available on given platform
00168        (see ut_Win32Uuid.h/cpp for an example) */
00169     virtual bool    _getRandomBytes(void *buf, int nbytes) const;
00170 
00171   private:
00172     bool            _parse(const char * in, struct uuid &u) const;
00173 
00174     bool            _makeUUID(struct uuid & u);
00175     bool            _toString(const struct uuid &uu, UT_UTF8String & s) const;
00176 
00177     // these three functions could be made public, but I think it better not to
00178     // encourage operations on the struct -- create an instance of UT_UUID if you need to
00179     // do any operations with it
00180     static time_t         _getTime(const struct uuid & uu);//NB: time_t has only 1s granularity !!!
00181     static UT_sint32      _getType(const struct uuid &uu);
00182     static UT_UUIDVariant _getVariant(const struct uuid &uu);
00183 
00184     bool           _getClock(UT_uint32 &iHigh, UT_uint32 &iLow, UT_uint16 &iSeq) const;
00185 
00186   private:
00187     uuid                   m_uuid;
00188     bool                   m_bIsValid;
00189     static bool            s_bInitDone;
00190     static unsigned char   s_node[6];
00191     static UT_UUID         s_Null;
00192 };
00193 
00194 /*
00195     This class mediates creation of UT_UUID instances.
00196 
00197     We create an instance of UT_UUIDGeneratr (or derived) class in
00198     XAP_App() and have XAP_App::getUUIDGenerator() to gain access to
00199     it.  This allows us to create platform specific instances in place
00200     for generic UT_UUID from xp code.
00201 */
00202 //#define UT_UUID_HASH_TEST
00203 class ABI_EXPORT UT_UUIDGenerator
00204 {
00205   public:
00206     UT_UUIDGenerator()
00207         :m_pUUID(NULL)
00208     {
00209 #if defined(UT_UUID_HASH_TEST) && defined(DEBUG)
00210         __test();
00211 #endif
00212     };
00213 
00214     virtual ~UT_UUIDGenerator(){if(m_pUUID) delete m_pUUID;};
00215 
00216     // because the default constructor creates NULL uuid, we also need
00217     // to call makeUUID() with this one
00218     virtual UT_UUID * createUUID(){UT_UUID *p = new UT_UUID(); if(p)p->makeUUID(); return p;}
00219 
00220     virtual UT_UUID * createUUID(const UT_UTF8String &s){return new UT_UUID(s);}
00221     virtual UT_UUID * createUUID(const char *s){return new UT_UUID(s);}
00222     virtual UT_UUID * createUUID(const UT_UUID &u){return new UT_UUID(u);}
00223     virtual UT_UUID * createUUID(const struct uuid &u){return new UT_UUID(u);}
00224 
00225     UT_uint32 getNewUUID32();
00226     UT_uint64 getNewUUID64();
00227 
00228 #if defined(UT_UUID_HASH_TEST) && defined(DEBUG)
00229   public:
00230 void                    __test();
00231 #endif
00232 
00233   private:
00234 
00235     UT_UUID * m_pUUID;
00236 };
00237 
00238 #endif /* UT_UUID_H */

Generated on Mon May 28 2012 for AbiWord by  doxygen 1.7.1