• 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., 51 Franklin Street, Fifth Floor, Boston, MA
00023  * 02110-1301 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 <time.h>
00037 
00038 #include <memory>
00039 #include <string>
00040 
00041 #include "ut_option.h"
00042 
00043 /* UUID Variant definitions */
00044 enum UT_UUIDVariant
00045 {
00046     UT_UUID_VARIANT_NCS = 0,
00047     UT_UUID_VARIANT_DCE = 1,
00048     UT_UUID_VARIANT_MICROSOFT = 2,
00049     UT_UUID_VARIANT_OTHER = 3,
00050     UT_UUID_VARIANT_ERROR = 0xffffffff
00051 };
00052 
00053 struct uuid
00054 {
00055     UT_uint32   time_low;
00056     UT_uint16   time_mid;
00057     UT_uint16   time_high_and_version;
00058     UT_uint16   clock_seq;
00059     UT_Byte     node[6];
00060 };
00061 
00062 // forward declaration
00063 class UT_UUIDGenerator;
00064 
00065 
00078 class ABI_EXPORT UT_UUID
00079 {
00080   public:
00081         friend void UT_UUIDGenerator__test(UT_UUIDGenerator*);
00082   public:
00083     /*
00084        All constructors are protected; instances of UT_UUID will be
00085        created through UT_UUIDGenerator declared below.
00086     */
00087 
00088     /* virtual destructor */
00089     virtual ~UT_UUID (){};
00090 
00091     /*
00092        Various manipulation functions; in general functions that take
00093        some kind of an output parameter (usually named 'out' or 'to'
00094        below) DO NOT modifiy internal state of the class, and return
00095        value of 'true' indicates success.
00096     */
00097 
00098     /* These generate new UUIDs */
00099     bool            makeUUID();                // changes internal state
00100     bool            makeUUID(std::string & out); // does not change internal state !!!
00101 
00102     /* these set m_uuid to given UUID, i.e., force internal state change */
00103     bool            setUUID(const std::string &s);
00104     bool            setUUID(const char *s);
00105     bool            setUUID(const struct uuid &u);
00106 
00107     /* translate internal state into string representation; do not change
00108        internal state */
00109     UT_Option<std::string> toString() 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 std::string &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     std::string _toString(const struct uuid &uu) 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 typedef std::unique_ptr<UT_UUID> UT_UUIDPtr;
00195 
00196 /*
00197     This class mediates creation of UT_UUID instances.
00198 
00199     We create an instance of UT_UUIDGenerator (or derived) class in
00200     XAP_App() and have XAP_App::getUUIDGenerator() to gain access to
00201     it.  This allows us to create platform specific instances in place
00202     for generic UT_UUID from xp code.
00203 */
00204 //#define UT_UUID_HASH_TEST
00205 class ABI_EXPORT UT_UUIDGenerator
00206 {
00207   public:
00208     UT_UUIDGenerator()
00209         :m_pUUID(NULL)
00210     {
00211     };
00212 
00213     virtual ~UT_UUIDGenerator(){if(m_pUUID) delete m_pUUID;};
00214 
00215     // because the default constructor creates NULL uuid, we also need
00216     // to call makeUUID() with this one
00217     virtual UT_UUID * createUUID(){UT_UUID *p = new UT_UUID(); if(p)p->makeUUID(); return p;}
00218 
00219     virtual UT_UUID * createUUID(const std::string &s){return new UT_UUID(s);}
00220     virtual UT_UUID * createUUID(const char *s){return new UT_UUID(s);}
00221     virtual UT_UUID * createUUID(const UT_UUID &u){return new UT_UUID(u);}
00222     virtual UT_UUID * createUUID(const struct uuid &u){return new UT_UUID(u);}
00223 
00224     UT_uint32 getNewUUID32();
00225     UT_uint64 getNewUUID64();
00226 
00227   public:
00228         friend void UT_UUIDGenerator__test(UT_UUIDGenerator*);
00229 
00230   private:
00231 
00232     UT_UUID * m_pUUID;
00233 };
00234 
00235 #endif /* UT_UUID_H */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1