Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
uuid_fwd.h
Go to the documentation of this file.00001 00004 /* 00005 * Copyright © 2001, 2002 Sofus Mortensen 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef COMET_UUID_FWD_H 00020 #define COMET_UUID_FWD_H 00021 00022 #include <rpc.h> 00023 #include <string> 00024 #include <algorithm> 00025 #include <iostream> 00026 #include <comet/tstring.h> 00027 00028 namespace comet{ 00029 00030 00034 00036 class uuid_t : public COMET_GUID_BASE 00037 { 00038 struct unitialized_t {}; 00039 uuid_t(unitialized_t) {} 00040 00041 friend class bstr_t; 00042 00043 public: 00045 uuid_t() 00046 { 00047 UuidCreateNil(this); 00048 } 00049 00051 uuid_t(const COMET_GUID_BASE& u) 00052 { 00053 memcpy(this, &u, sizeof(COMET_GUID_BASE)); 00054 } 00055 00057 uuid_t& operator=(const COMET_GUID_BASE& u) 00058 { 00059 memcpy(this, &u, sizeof(COMET_GUID_BASE)); 00060 return *this; 00061 } 00062 00064 00065 explicit uuid_t(const std::string& s) 00066 { 00067 if (!init_from_str(s.c_str(), s.length())) 00068 throw std::runtime_error(err_msg()); 00069 } 00070 00071 explicit uuid_t(const char* s) 00072 { 00073 if (!init_from_str(s, strlen(s))) 00074 throw std::runtime_error(err_msg()); 00075 } 00076 00077 explicit uuid_t(const std::wstring& s) 00078 { 00079 if (!init_from_str(s.c_str(), s.length())) 00080 throw std::runtime_error(err_msg()); 00081 } 00082 00083 explicit uuid_t(const wchar_t* s) 00084 { 00085 if (!init_from_str(s, wcslen(s))) 00086 throw std::runtime_error(err_msg()); 00087 } 00088 00089 explicit uuid_t(const bstr_t& bs); 00091 00093 static uuid_t create() 00094 { 00095 unitialized_t x; 00096 uuid_t g(x); 00097 UuidCreate(&g); 00098 return g; 00099 } 00100 00102 void clear() 00103 { 00104 UuidCreateNil(this); 00105 } 00106 00108 unsigned short hash() const 00109 { 00110 RPC_STATUS status; 00111 unsigned short r = UuidHash(const_cast<uuid_t*>(this), &status); 00112 if (status != RPC_S_OK) 00113 throw std::runtime_error("uuid_t::hash - UuidHash failed."); 00114 return r; 00115 } 00116 00118 00119 00120 std::string str() const 00121 { 00122 std::string r; 00123 r.resize(36); 00124 copy_to_str(&r[0]); 00125 return r; 00126 } 00128 inline std::string s_str() const 00129 { 00130 return str(); 00131 } 00132 00134 std::wstring w_str() const 00135 { 00136 std::wstring r; 00137 r.resize(36); 00138 copy_to_str(&r[0]); 00139 return r; 00140 } 00141 00143 tstring t_str() const 00144 { 00145 tstring r; 00146 r.resize(36); 00147 copy_to_str(&r[0]); 00148 return r; 00149 } 00151 00153 bool is_null() const throw() 00154 { 00155 return ((PLONG) this)[0] == 0 && 00156 ((PLONG) this)[1] == 0 && 00157 ((PLONG) this)[2] == 0 && 00158 ((PLONG) this)[3] == 0; 00159 } 00160 00162 bool operator!() const throw() 00163 { 00164 return is_null(); 00165 } 00166 00168 template<class E, class TR> 00169 friend inline std::basic_ostream<E, TR>& operator<<(std::basic_ostream<E, TR>& os, const uuid_t& u); 00170 00172 template<class E, class TR> 00173 friend inline std::basic_istream<E, TR>& operator>>(std::basic_istream<E, TR>& is, uuid_t& u); 00174 00177 class bool_tester 00178 { void operator delete(void*); }; 00179 00181 template<typename C> void copy_to_str(C s[]) const throw(); 00183 template<typename C> bool init_from_str(const C s[], size_t len) throw(); 00184 00185 static const int* uuid_table() 00186 { 00187 static const int table[] = { 3, 2, 1, 0, -1, 5, 4, -1, 7, 6, -1, 8, 9, -1, 10, 11, 12, 13, 14, 15 }; 00188 return table; 00189 } 00190 00191 static const char* hex_table() 00192 { 00193 static const char hex[] = "0123456789abcdef"; 00194 return hex; 00195 } 00196 00197 static const char* err_msg() 00198 { 00199 static const char msg[] = "uuid_t: invalid format"; 00200 return msg; 00201 } 00202 00203 static int parse_nibble(int c) 00204 { 00205 return c >= 'A' ? (c|0x20) - 'a' + 10 : c - '0'; 00206 } 00207 00209 inline long cmp(const comet::uuid_t &rhs) const 00210 { 00211 long i; // The code below may be a bit dense, but it is compact and works. 00212 if((i = (Data1 - rhs.Data1)) == 0 && (i = (Data2 - rhs.Data2)) == 0 && (i = (Data3 - rhs.Data3)) == 0) 00213 (i = ::memcmp(Data4, rhs.Data4, 8)); 00214 return i; 00215 } 00216 00218 inline bool is_equal( const comet::uuid_t &rhs) const 00219 { 00220 // From ATL 00221 return ((PLONG) this)[0] == ((PLONG) &rhs)[0] && 00222 ((PLONG) this)[1] == ((PLONG) &rhs)[1] && 00223 ((PLONG) this)[2] == ((PLONG) &rhs)[2] && 00224 ((PLONG) this)[3] == ((PLONG) &rhs)[3]; 00225 } 00226 00227 public: 00230 operator bool_tester*() const throw() 00231 { if (is_null()) return 0; static bool_tester test; return &test; } 00232 00234 static const uuid_t& create_const_reference(const COMET_GUID_BASE& x) 00235 { 00236 return *reinterpret_cast<const uuid_t*>(&x); 00237 } 00238 00240 static uuid_t& create_reference(COMET_GUID_BASE& x) 00241 { 00242 return *reinterpret_cast<uuid_t*>(&x); 00243 } 00244 00246 00247 COMET_GUID_BASE* out() { return this; } 00248 COMET_GUID_BASE* inout() { return this; } 00249 COMET_GUID_BASE in() const { return *this; } 00250 COMET_GUID_BASE* in_ptr() const { return const_cast<COMET_GUID_BASE*>(static_cast<const COMET_GUID_BASE*>(this)); } 00252 }; 00254 00256 00257 00260 inline bool operator==(const comet::uuid_t& lhs, const comet::uuid_t& rhs) 00261 { 00262 return lhs.is_equal(rhs); 00263 } 00264 00268 inline bool operator!=(const comet::uuid_t& lhs, const comet::uuid_t& rhs) 00269 { 00270 return !lhs.is_equal(rhs); 00271 } 00272 00276 inline bool operator==(const comet::uuid_t& lhs, const COMET_GUID_BASE& rhs) 00277 { 00278 return lhs.is_equal( comet::uuid_t::create_const_reference(rhs) ); 00279 } 00280 00284 inline bool operator!=(const comet::uuid_t& lhs, const COMET_GUID_BASE& rhs) 00285 { 00286 return !lhs.is_equal( comet::uuid_t::create_const_reference(rhs) ); 00287 } 00288 00292 inline bool operator==(const COMET_GUID_BASE& lhs, const comet::uuid_t& rhs) 00293 { 00294 return comet::uuid_t::create_const_reference(lhs).is_equal(rhs); 00295 } 00296 00300 inline bool operator!=(const COMET_GUID_BASE& lhs, const comet::uuid_t& rhs) 00301 { 00302 return !comet::uuid_t::create_const_reference(lhs).is_equal(rhs); 00303 } 00304 00308 inline bool operator<(const comet::uuid_t& lhs, const comet::uuid_t& rhs) 00309 { 00310 return lhs.cmp(rhs)<0; 00311 } 00312 00316 inline bool operator<(const comet::uuid_t& lhs, const COMET_GUID_BASE& rhs) 00317 { 00318 return lhs.cmp(comet::uuid_t::create_const_reference(rhs))<0; 00319 } 00320 00324 inline bool operator<(const COMET_GUID_BASE& lhs, const comet::uuid_t& rhs) 00325 { 00326 return comet::uuid_t::create_const_reference(lhs).cmp(rhs)<0; 00327 } 00328 00332 inline bool operator>=(const comet::uuid_t& lhs, const comet::uuid_t& rhs) 00333 { 00334 return lhs.cmp(rhs)>=0; 00335 } 00336 00340 inline bool operator>=(const comet::uuid_t& lhs, const COMET_GUID_BASE& rhs) 00341 { 00342 return lhs.cmp(comet::uuid_t::create_const_reference(rhs))>=0; 00343 } 00344 00348 inline bool operator>=(const COMET_GUID_BASE& lhs, const comet::uuid_t& rhs) 00349 { 00350 return comet::uuid_t::create_const_reference(lhs).cmp(rhs)>=0; 00351 } 00352 00356 inline bool operator>(const comet::uuid_t& lhs, const comet::uuid_t& rhs) 00357 { 00358 return lhs.cmp(rhs)>0; 00359 } 00360 00364 inline bool operator>(const comet::uuid_t& lhs, const COMET_GUID_BASE& rhs) 00365 { 00366 return lhs.cmp(comet::uuid_t::create_const_reference(rhs))>0; 00367 } 00368 00372 inline bool operator>(const COMET_GUID_BASE& lhs, const comet::uuid_t& rhs) 00373 { 00374 return comet::uuid_t::create_const_reference(lhs).cmp(rhs)>0; 00375 } 00376 00380 inline bool operator<=(const comet::uuid_t& lhs, const comet::uuid_t& rhs) 00381 { 00382 return lhs.cmp(rhs)<=0; 00383 } 00384 00388 inline bool operator<=(const comet::uuid_t& lhs, const COMET_GUID_BASE& rhs) 00389 { 00390 return lhs.cmp(comet::uuid_t::create_const_reference(rhs))<=0; 00391 } 00392 00396 inline bool operator<=(const COMET_GUID_BASE& lhs, const comet::uuid_t& rhs) 00397 { 00398 return comet::uuid_t::create_const_reference(lhs).cmp(rhs)<=0; 00399 } 00401 00404 00408 template<class E, class TR> 00409 inline std::basic_ostream<E, TR>& operator<<(std::basic_ostream<E, TR>& os, const comet::uuid_t& u) 00410 { 00411 E buf[36]; 00412 u.copy_to_str(buf); 00413 os.write(buf, 36); 00414 return os; 00415 } 00416 00420 template<class E, class TR> 00421 inline std::basic_istream<E, TR>& operator>>(std::basic_istream<E, TR>& is, comet::uuid_t& u) 00422 { 00423 std::basic_istream<E,TR>::sentry se(is); 00424 if (se) 00425 { 00426 E buf[36]; 00427 is.read(buf, 36); 00428 if (!u.init_from_str(buf, is.gcount())) 00429 is.setstate(std::ios::badbit); 00430 } 00431 return is; 00432 } 00434 00435 } // namespace comet 00436 00437 #endif