Scopira  20080306
uuid.h
1 
2 /*
3  * Copyright (c) 2002-2003 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INLUCDED__SCOPIRA_TOOL_UUID_H_
15 #define __INLUCDED__SCOPIRA_TOOL_UUID_H_
16 
17 #include <scopira/tool/export.h>
18 #include <scopira/tool/platform.h>
19 #include <scopira/tool/export.h>
20 #include <scopira/tool/mutex.h>
21 #include <scopira/tool/flow.h>
22 #include <scopira/tool/random.h> // for time_seed
23 #include <scopira/tool/linconrandom.h> // for lincon_core
24 
25 #if (defined(PLATFORM_linux) || defined(PLATFORM_osx)) && !defined(PLATFORM_DONT_USE_LIBUUID)
26 #define PLATFORM_E2UUID
27 #endif
28 
29 #ifdef PLATFORM_E2UUID
30 #include <uuid/uuid.h>
31 #endif
32 
33 // THIS FILE HAS BEEN FULLY DOCUMENTED
34 
35 namespace scopira
36 {
37  namespace tool
38  {
39  // rename this to "key" or "ukey" or "token"?
40  class uuid;
41  class uuid_generator;
42  }
43 }
44 
46 SCOPIRA_EXPORT scopira::tool::oflow_i& operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
47 
48 
49 #ifdef PLATFORM_E2UUID
50 
61 class scopira::tool::uuid // super-Linux implementation
62 {
63  private:
64  ::uuid_t dm_id;
65  public:
67  uuid(void) { ::uuid_clear(dm_id); }
74  uuid(const char *s);
75 
80  void set_zero(void) { ::uuid_clear(dm_id); }
85  bool is_zero(void) const { return ::uuid_is_null(dm_id) != 0; }
86 
88  bool operator == (const uuid &rhs) const { return ::uuid_compare(dm_id, rhs.dm_id) == 0; }
90  bool operator != (const uuid &rhs) const { return ::uuid_compare(dm_id, rhs.dm_id) != 0; }
92  bool operator < (const uuid &rhs) const { return ::uuid_compare(dm_id, rhs.dm_id) < 0; }
93 
95  bool load(scopira::tool::itflow_i& in) { return in.read(dm_id, sizeof(dm_id)) == sizeof(dm_id); }
97  void save(scopira::tool::otflow_i& out) const { out.write(dm_id, sizeof(dm_id)); }
98 
100  std::string as_string(void) const;
102  bool parse_string(const std::string &s);
103 
104  public:
105  friend class scopira::tool::uuid_generator;
106  friend scopira::tool::oflow_i& ::operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
107 };
108 #endif
109 
110 #ifdef PLATFORM_win32
111 
122 class scopira::tool::uuid // super-Win32 implementation
123 {
124  private:
125  GUID dm_id;
126  public:
128  uuid(void) { dm_id = GUID_NULL; }
135  uuid(const char *s);
136 
141  void set_zero(void) { dm_id = GUID_NULL; }
146  bool is_zero(void) const { return *this == uuid(); }
147 
149  bool operator == (const uuid &rhs) const { return IsEqualGUID(dm_id, rhs.dm_id) == TRUE; }
151  bool operator != (const uuid &rhs) const { return IsEqualGUID(dm_id, rhs.dm_id) == FALSE; }
153  SCOPIRA_EXPORT bool operator < (const uuid &rhs) const;
154 
156  bool load(scopira::tool::itflow_i& in) { return in.read_void(&dm_id, sizeof(dm_id)) == sizeof(dm_id); }
158  void save(scopira::tool::otflow_i& out) const { out.write_void(&dm_id, sizeof(dm_id)); }
159 
161  SCOPIRA_EXPORT std::string as_string(void) const;
163  SCOPIRA_EXPORT bool parse_string(const std::string &s);
164 
165  public:
166  friend class scopira::tool::uuid_generator;
167  friend scopira::tool::oflow_i& ::operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
168 };
169 #endif
170 
171 #if !defined(PLATFORM_E2UUID) && !defined(PLATFORM_win32)
172 
183 class scopira::tool::uuid // lame non-Linux implementation[1]
184 {
185  private:
186  int64_t dm_id[2];
187  // [1] This implementation will NEED to be synchronized with the Linux
188  // one if there is to be any network communication/collabaration
189  // Under Win32, we'll use MS's GUID stuff, which is basicaly the same thing
190  // What to do for the (non-Linux) UNIXes though?
191 
192  public:
194  SCOPIRA_EXPORT uuid(void);
201  SCOPIRA_EXPORT uuid(const char *s);
202 
207  SCOPIRA_EXPORT void set_zero(void);
212  SCOPIRA_EXPORT bool is_zero(void) const;
213 
215  SCOPIRA_EXPORT bool operator == (const uuid &rhs) const;
217  SCOPIRA_EXPORT bool operator != (const uuid &rhs) const;
219  SCOPIRA_EXPORT bool operator < (const uuid &rhs) const;
220 
222  SCOPIRA_EXPORT bool load(scopira::tool::itflow_i& in);
224  SCOPIRA_EXPORT void save(scopira::tool::otflow_i& out) const;
225 
227  SCOPIRA_EXPORT std::string as_string(void) const;
229  SCOPIRA_EXPORT bool parse_string(const std::string &s);
230 
231  public:
232  friend class scopira::tool::uuid_generator;
233  friend scopira::tool::oflow_i& ::operator << (scopira::tool::oflow_i& o, const scopira::tool::uuid &id);
234 };
235 #endif
236 
243 {
244  private:
245 #if !defined(PLATFORM_E2UUID) && !defined(PLATFORM_win32)
246  mutex dm_mut;
248 #endif
249  public:
251 #if !defined(PLATFORM_E2UUID) && !defined(PLATFORM_win32)
252  SCOPIRA_EXPORT uuid_generator(void);
253 #endif
254 
258  SCOPIRA_EXPORT void next(uuid &out);
259 
260  // array/multi gen version of next()??
261  // or just do some kind of +1 system?
262 };
263 
264 #endif
265 
std::string as_string(void) const
return a nice string representation
Definition: mutex_pthreads.h:40
Definition: archiveflow.h:20
bool load(scopira::tool::itflow_i &in)
Serialization - yes, non-virtual.
Definition: flow.h:212
Definition: flow.h:281
bool parse_string(const std::string &s)
parse the string into this object, return false if failed
virtual size_t write(const byte_t *_buf, size_t _size)=0
Definition: uuid.h:183
void save(scopira::tool::otflow_i &out) const
Serialization - yes, non-virtual.
bool operator!=(const uuid &rhs) const
comparator
bool is_zero(void) const
Definition: linconrandom.h:27
virtual size_t read(byte_t *_buf, size_t _maxsize)=0
bool operator<(const uuid &rhs) const
comparator
uuid(void)
Constructor. Builds a "zero" UUID (one with all zeros)
Definition: uuid.h:242
size_t read_void(void *_buf, size_t _maxsize)
Definition: flow.h:151
size_t write_void(const void *_buf, size_t _size)
Definition: flow.h:202
bool operator==(const uuid &rhs) const
comparator
Definition: flow.h:159