Scopira 20080306

uuid.h

00001 
00002 /*
00003  *  Copyright (c) 2002-2003    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INLUCDED__SCOPIRA_TOOL_UUID_H_
00015 #define __INLUCDED__SCOPIRA_TOOL_UUID_H_
00016 
00017 #include <scopira/tool/export.h>
00018 #include <scopira/tool/platform.h>
00019 #include <scopira/tool/export.h>
00020 #include <scopira/tool/mutex.h>
00021 #include <scopira/tool/flow.h>
00022 #include <scopira/tool/random.h>  // for time_seed
00023 #include <scopira/tool/linconrandom.h>  // for lincon_core
00024 
00025 #if (defined(PLATFORM_linux) || defined(PLATFORM_osx)) && !defined(PLATFORM_DONT_USE_LIBUUID)
00026 #define PLATFORM_E2UUID
00027 #endif
00028 
00029 #ifdef PLATFORM_E2UUID
00030 #include <uuid/uuid.h>
00031 #endif
00032 
00033 // THIS FILE HAS BEEN FULLY DOCUMENTED
00034 
00035 namespace scopira
00036 {
00037   namespace tool
00038   {
00039     // rename this to "key" or "ukey" or "token"?
00040     class uuid;
00041     class uuid_generator;
00042   }
00043 }
00044 
00046 SCOPIRA_EXPORT scopira::tool::oflow_i& operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
00047 
00048 
00049 #ifdef PLATFORM_E2UUID
00050 
00061 class scopira::tool::uuid                       // super-Linux implementation
00062 {
00063   private:
00064     ::uuid_t dm_id;
00065   public:
00067     uuid(void) { ::uuid_clear(dm_id); }
00074     uuid(const char *s);
00075 
00080     void set_zero(void) { ::uuid_clear(dm_id); }
00085     bool is_zero(void) const { return ::uuid_is_null(dm_id) != 0; }
00086 
00088     bool operator == (const uuid &rhs) const { return ::uuid_compare(dm_id, rhs.dm_id) == 0; }
00090     bool operator != (const uuid &rhs) const { return ::uuid_compare(dm_id, rhs.dm_id) != 0; }
00092     bool operator < (const uuid &rhs) const { return ::uuid_compare(dm_id, rhs.dm_id) < 0; }
00093 
00095     bool load(scopira::tool::itflow_i& in) { return in.read(dm_id, sizeof(dm_id)) == sizeof(dm_id); }
00097     void save(scopira::tool::otflow_i& out) const { out.write(dm_id, sizeof(dm_id)); }
00098 
00100     std::string as_string(void) const;
00102     bool parse_string(const std::string &s);
00103 
00104   public:
00105     friend class scopira::tool::uuid_generator;
00106     friend scopira::tool::oflow_i& ::operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
00107 };
00108 #endif
00109 
00110 #ifdef PLATFORM_win32
00111 
00122 class scopira::tool::uuid                       // super-Win32 implementation
00123 {
00124   private:
00125     GUID dm_id;
00126   public:
00128     uuid(void) { dm_id = GUID_NULL; }
00135     uuid(const char *s);
00136 
00141     void set_zero(void) { dm_id = GUID_NULL; }
00146     bool is_zero(void) const { return *this == uuid(); }
00147 
00149     bool operator == (const uuid &rhs) const { return IsEqualGUID(dm_id, rhs.dm_id) == TRUE; }
00151     bool operator != (const uuid &rhs) const { return IsEqualGUID(dm_id, rhs.dm_id) == FALSE; }
00153     SCOPIRA_EXPORT bool operator < (const uuid &rhs) const;
00154 
00156     bool load(scopira::tool::itflow_i& in) { return in.read_void(&dm_id, sizeof(dm_id)) == sizeof(dm_id); }
00158     void save(scopira::tool::otflow_i& out) const { out.write_void(&dm_id, sizeof(dm_id)); }
00159 
00161     SCOPIRA_EXPORT std::string as_string(void) const;
00163     SCOPIRA_EXPORT bool parse_string(const std::string &s);
00164 
00165   public:
00166     friend class scopira::tool::uuid_generator;
00167     friend scopira::tool::oflow_i& ::operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
00168 };
00169 #endif
00170 
00171 #if !defined(PLATFORM_E2UUID) && !defined(PLATFORM_win32)
00172 
00183 class scopira::tool::uuid                       // lame non-Linux implementation[1]
00184 {
00185   private:
00186     int64_t dm_id[2];
00187                   // [1] This implementation will NEED to be synchronized with the Linux
00188                   // one if there is to be any network communication/collabaration
00189                   // Under Win32, we'll use MS's GUID stuff, which is basicaly the same thing
00190                   // What to do for the (non-Linux) UNIXes though?
00191 
00192   public:
00194     SCOPIRA_EXPORT uuid(void);
00201     SCOPIRA_EXPORT uuid(const char *s);
00202 
00207     SCOPIRA_EXPORT void set_zero(void);
00212     SCOPIRA_EXPORT bool is_zero(void) const;
00213 
00215     SCOPIRA_EXPORT bool operator == (const uuid &rhs) const;
00217     SCOPIRA_EXPORT bool operator != (const uuid &rhs) const;
00219     SCOPIRA_EXPORT bool operator < (const uuid &rhs) const;
00220 
00222     SCOPIRA_EXPORT bool load(scopira::tool::itflow_i& in);
00224     SCOPIRA_EXPORT void save(scopira::tool::otflow_i& out) const;
00225 
00227     SCOPIRA_EXPORT std::string as_string(void) const;
00229     SCOPIRA_EXPORT bool parse_string(const std::string &s);
00230 
00231   public:
00232     friend class scopira::tool::uuid_generator;
00233     friend scopira::tool::oflow_i& ::operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
00234 };
00235 #endif
00236 
00242 class scopira::tool::uuid_generator
00243 {
00244   private:
00245 #if !defined(PLATFORM_E2UUID) && !defined(PLATFORM_win32)
00246     mutex dm_mut;
00247     scopira::tool::lincon_core dm_core;
00248 #endif
00249   public:
00251 #if !defined(PLATFORM_E2UUID) && !defined(PLATFORM_win32)
00252     SCOPIRA_EXPORT uuid_generator(void);
00253 #endif
00254 
00258     SCOPIRA_EXPORT void next(uuid &out);
00259 
00260     // array/multi gen version of next()??
00261     // or just do some kind of +1 system?
00262 };
00263 
00264 #endif
00265