Changeset 51688 in vbox for trunk/include
- Timestamp:
- Jun 23, 2014 1:22:09 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/VBox/com/Guid.h
r51687 r51688 103 103 Guid(const char *that) 104 104 { 105 initString(that); 106 } 107 108 /** 109 * Construct a GUID from a BSTR. 110 * 111 * @param that The UUID BSTR. Can be with or without the curly 112 * brackets. Empty strings are translated to a zero 113 * GUID, and strings which are not confirming to 114 * valid GUID string representations are marked as 115 * invalid. 116 */ 117 Guid(CBSTR that) 118 { 119 initBSTR(that); 120 } 121 122 /** 123 * Construct a GUID from a Utf8Str. 124 * 125 * @param that The UUID Utf8Str. Can be with or without the curly 126 * brackets. Empty strings are translated to a zero 127 * GUID, and strings which are not confirming to 128 * valid GUID string representations are marked as 129 */ 130 Guid(const Utf8Str &that) 131 { 132 initString(that.c_str()); 133 } 134 135 /** 136 * Construct a GUID from a RTCString. 137 * 138 * @param that The UUID RTCString. Can be with or without the curly 139 * brackets. Empty strings are translated to a zero 140 * GUID, and strings which are not confirming to 141 * valid GUID string representations are marked as 142 */ 143 Guid(const RTCString &that) 144 { 145 initString(that.c_str()); 146 } 147 148 /** 149 * Construct a GUID from a Bstr. 150 * 151 * @param that The UUID Bstr. Can be with or without the curly 152 * brackets. Empty strings are translated to a zero 153 * GUID, and strings which are not confirming to 154 * valid GUID string representations are marked as 155 */ 156 Guid(const Bstr &that) 157 { 158 initBSTR(that.raw()); 159 } 160 161 Guid& operator=(const Guid &that) 162 { 163 mUuid = that.mUuid; 164 mGuidState = that.mGuidState; 165 dbg_refresh(); 166 return *this; 167 } 168 169 Guid& operator=(const RTUUID &guid) 170 { 171 mUuid = guid; 172 mGuidState = GUID_NORMAL; 173 if (isZero()) 174 mGuidState = GUID_ZERO; 175 dbg_refresh(); 176 return *this; 177 } 178 179 Guid& operator=(const GUID &guid) 180 { 181 AssertCompileSize(GUID, sizeof(RTUUID)); 182 ::memcpy(&mUuid, &guid, sizeof(GUID)); 183 mGuidState = GUID_NORMAL; 184 if (isZero()) 185 mGuidState = GUID_ZERO; 186 dbg_refresh(); 187 return *this; 188 } 189 190 Guid& operator=(const char *str) 191 { 192 if (!str || !*str) 193 { 194 ::RTUuidClear(&mUuid); 195 mGuidState = GUID_ZERO; 196 } 197 else 198 { 199 mGuidState = GUID_NORMAL; 200 int rc = ::RTUuidFromStr(&mUuid, str); 201 if (RT_FAILURE(rc)) 202 { 203 ::RTUuidClear(&mUuid); 204 mGuidState = GUID_INVALID; 205 } 206 else if (isZero()) 207 mGuidState = GUID_ZERO; 208 } 209 dbg_refresh(); 210 return *this; 211 } 212 213 Guid& operator=(CBSTR str) 214 { 215 if (!str || !*str) 216 { 217 ::RTUuidClear(&mUuid); 218 mGuidState = GUID_ZERO; 219 } 220 else 221 { 222 mGuidState = GUID_NORMAL; 223 int rc = ::RTUuidFromUtf16(&mUuid, str); 224 if (RT_FAILURE(rc)) 225 { 226 ::RTUuidClear(&mUuid); 227 mGuidState = GUID_INVALID; 228 } 229 else if (isZero()) 230 mGuidState = GUID_ZERO; 231 } 232 dbg_refresh(); 233 return *this; 234 } 235 236 Guid& operator=(const Utf8Str &str) 237 { 238 return operator=(str.c_str()); 239 } 240 241 Guid& operator=(const RTCString &str) 242 { 243 return operator=(str.c_str()); 244 } 245 246 Guid& operator=(const Bstr &str) 247 { 248 return operator=(str.raw()); 249 } 250 251 void create() 252 { 253 ::RTUuidCreate(&mUuid); 254 mGuidState = GUID_NORMAL; 255 dbg_refresh(); 256 } 257 258 void clear() 259 { 260 ::RTUuidClear(&mUuid); 261 mGuidState = GUID_ZERO; 262 dbg_refresh(); 263 } 264 265 /** 266 * Convert the GUID to a string. 267 * 268 * @returns String object containing the formatted GUID. 269 * @throws std::bad_alloc 270 */ 271 Utf8Str toString() const 272 { 273 if (mGuidState == GUID_INVALID) 274 { 275 /* What to return in case of wrong Guid */ 276 return Utf8Str("00000000-0000-0000-0000-00000000000"); 277 } 278 279 char buf[RTUUID_STR_LENGTH]; 280 ::memset(buf, '\0', sizeof(buf)); 281 ::RTUuidToStr(&mUuid, buf, sizeof(buf)); 282 283 return Utf8Str(buf); 284 } 285 286 /** 287 * Like toString, but encloses the returned string in curly brackets. 288 * 289 * @returns String object containing the formatted GUID in curly brackets. 290 * @throws std::bad_alloc 291 */ 292 Utf8Str toStringCurly() const 293 { 294 if (mGuidState == GUID_INVALID) 295 { 296 /* What to return in case of wrong Guid */ 297 return Utf8Str("{00000000-0000-0000-0000-00000000000}"); 298 } 299 300 char buf[RTUUID_STR_LENGTH + 2]; 301 ::memset(buf, '\0', sizeof(buf)); 302 ::RTUuidToStr(&mUuid, buf + 1, sizeof(buf) - 2); 303 buf[0] = '{'; 304 buf[sizeof(buf) - 2] = '}'; 305 306 return Utf8Str(buf); 307 } 308 309 /** 310 * Convert the GUID to a string. 311 * 312 * @returns Bstr object containing the formatted GUID. 313 * @throws std::bad_alloc 314 */ 315 Bstr toUtf16() const 316 { 317 if (mGuidState == GUID_INVALID) 318 { 319 /* What to return in case of wrong Guid */ 320 return Bstr("00000000-0000-0000-0000-00000000000"); 321 } 322 323 RTUTF16 buf[RTUUID_STR_LENGTH]; 324 ::memset(buf, '\0', sizeof(buf)); 325 ::RTUuidToUtf16(&mUuid, buf, RT_ELEMENTS(buf)); 326 327 return Bstr(buf); 328 } 329 330 bool isValid() const 331 { 332 return mGuidState != GUID_INVALID; 333 } 334 335 bool isZero() const 336 { 337 return mGuidState == GUID_ZERO; 338 } 339 340 bool operator==(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) == 0; } 341 bool operator==(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) == 0; } 342 bool operator==(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) == 0; } 343 bool operator!=(const Guid &that) const { return !operator==(that); } 344 bool operator!=(const GUID &guid) const { return !operator==(guid); } 345 bool operator!=(const RTUUID &guid) const { return !operator==(guid); } 346 bool operator<(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) < 0; } 347 bool operator<(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) < 0; } 348 bool operator<(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) < 0; } 349 350 /** 351 * To directly copy the contents to a GUID, or for passing it as an input 352 * parameter of type (const GUID *), the compiler converts. */ 353 const GUID &ref() const 354 { 355 return *(const GUID *)&mUuid; 356 } 357 358 /** 359 * To pass instances to printf-like functions. 360 */ 361 PCRTUUID raw() const 362 { 363 return (PCRTUUID)&mUuid; 364 } 365 366 #if !defined(VBOX_WITH_XPCOM) 367 368 /** To assign instances to OUT_GUID parameters from within the interface 369 * method. */ 370 const Guid &cloneTo(GUID *pguid) const 371 { 372 if (pguid) 373 ::memcpy(pguid, &mUuid, sizeof(GUID)); 374 return *this; 375 } 376 377 /** To pass instances as OUT_GUID parameters to interface methods. */ 378 GUID *asOutParam() 379 { 380 return (GUID *)&mUuid; 381 } 382 383 #else 384 385 /** To assign instances to OUT_GUID parameters from within the 386 * interface method */ 387 const Guid &cloneTo(nsID **ppGuid) const 388 { 389 if (ppGuid) 390 *ppGuid = (nsID *)nsMemory::Clone(&mUuid, sizeof(nsID)); 391 392 return *this; 393 } 394 395 /** 396 * Internal helper class for asOutParam(). 397 * 398 * This takes a GUID refrence in the constructor and copies the mUuid from 399 * the method to that instance in its destructor. 400 */ 401 class GuidOutParam 402 { 403 GuidOutParam(Guid &guid) 404 : ptr(0), 405 outer(guid) 406 { 407 outer.clear(); 408 } 409 410 nsID *ptr; 411 Guid &outer; 412 GuidOutParam(const GuidOutParam &that); // disabled 413 GuidOutParam &operator=(const GuidOutParam &that); // disabled 414 public: 415 operator nsID**() { return &ptr; } 416 ~GuidOutParam() 417 { 418 if (ptr && outer.isEmpty()) 419 { 420 outer = *ptr; 421 outer.refresh(); 422 nsMemory::Free(ptr); 423 } 424 } 425 friend class Guid; 426 }; 427 428 /** to pass instances as OUT_GUID parameters to interface methods */ 429 GuidOutParam asOutParam() { return GuidOutParam(*this); } 430 431 #endif 432 433 /** 434 * Static immutable empty (zero) object. May be used for comparison purposes. 435 */ 436 static const Guid Empty; 437 438 private: 439 void initString(const char *that) 440 { 105 441 if (!that || !*that) 106 442 { … … 123 459 } 124 460 125 /** 126 * Construct a GUID from a BSTR. 127 * 128 * @param that The UUID BSTR. Can be with or without the curly 129 * brackets. Empty strings are translated to a zero 130 * GUID, and strings which are not confirming to 131 * valid GUID string representations are marked as 132 * invalid. 133 */ 134 Guid(CBSTR that) 461 void initBSTR(CBSTR that) 135 462 { 136 463 if (!that || !*that) … … 155 482 156 483 /** 157 * Construct a GUID from a Utf8Str.158 *159 * @param that The UUID Utf8Str. Can be with or without the curly160 * brackets. Empty strings are translated to a zero161 * GUID, and strings which are not confirming to162 * valid GUID string representations are marked as163 */164 Guid(const Utf8Str &that)165 {166 Guid(that.c_str());167 }168 169 /**170 * Construct a GUID from a RTCString.171 *172 * @param that The UUID RTCString. Can be with or without the curly173 * brackets. Empty strings are translated to a zero174 * GUID, and strings which are not confirming to175 * valid GUID string representations are marked as176 */177 Guid(const RTCString &that)178 {179 Guid(that.c_str());180 }181 182 /**183 * Construct a GUID from a Bstr.184 *185 * @param that The UUID Bstr. Can be with or without the curly186 * brackets. Empty strings are translated to a zero187 * GUID, and strings which are not confirming to188 * valid GUID string representations are marked as189 */190 Guid(const Bstr &that)191 {192 Guid(that.raw());193 }194 195 Guid& operator=(const Guid &that)196 {197 mUuid = that.mUuid;198 mGuidState = that.mGuidState;199 dbg_refresh();200 return *this;201 }202 203 Guid& operator=(const RTUUID &guid)204 {205 mUuid = guid;206 mGuidState = GUID_NORMAL;207 if (isZero())208 mGuidState = GUID_ZERO;209 dbg_refresh();210 return *this;211 }212 213 Guid& operator=(const GUID &guid)214 {215 AssertCompileSize(GUID, sizeof(RTUUID));216 ::memcpy(&mUuid, &guid, sizeof(GUID));217 mGuidState = GUID_NORMAL;218 if (isZero())219 mGuidState = GUID_ZERO;220 dbg_refresh();221 return *this;222 }223 224 Guid& operator=(const char *str)225 {226 if (!str || !*str)227 {228 ::RTUuidClear(&mUuid);229 mGuidState = GUID_ZERO;230 }231 else232 {233 mGuidState = GUID_NORMAL;234 int rc = ::RTUuidFromStr(&mUuid, str);235 if (RT_FAILURE(rc))236 {237 ::RTUuidClear(&mUuid);238 mGuidState = GUID_INVALID;239 }240 else if (isZero())241 mGuidState = GUID_ZERO;242 }243 dbg_refresh();244 return *this;245 }246 247 Guid& operator=(CBSTR str)248 {249 if (!str || !*str)250 {251 ::RTUuidClear(&mUuid);252 mGuidState = GUID_ZERO;253 }254 else255 {256 mGuidState = GUID_NORMAL;257 int rc = ::RTUuidFromUtf16(&mUuid, str);258 if (RT_FAILURE(rc))259 {260 ::RTUuidClear(&mUuid);261 mGuidState = GUID_INVALID;262 }263 else if (isZero())264 mGuidState = GUID_ZERO;265 }266 dbg_refresh();267 return *this;268 }269 270 Guid& operator=(const Utf8Str &str)271 {272 return operator=(str.c_str());273 }274 275 Guid& operator=(const RTCString &str)276 {277 return operator=(str.c_str());278 }279 280 Guid& operator=(const Bstr &str)281 {282 return operator=(str.raw());283 }284 285 void create()286 {287 ::RTUuidCreate(&mUuid);288 mGuidState = GUID_NORMAL;289 dbg_refresh();290 }291 292 void clear()293 {294 ::RTUuidClear(&mUuid);295 mGuidState = GUID_ZERO;296 dbg_refresh();297 }298 299 /**300 * Convert the GUID to a string.301 *302 * @returns String object containing the formatted GUID.303 * @throws std::bad_alloc304 */305 Utf8Str toString() const306 {307 if (mGuidState == GUID_INVALID)308 {309 /* What to return in case of wrong Guid */310 return Utf8Str("00000000-0000-0000-0000-00000000000");311 }312 313 char buf[RTUUID_STR_LENGTH];314 ::memset(buf, '\0', sizeof(buf));315 ::RTUuidToStr(&mUuid, buf, sizeof(buf));316 317 return Utf8Str(buf);318 }319 320 /**321 * Like toString, but encloses the returned string in curly brackets.322 *323 * @returns String object containing the formatted GUID in curly brackets.324 * @throws std::bad_alloc325 */326 Utf8Str toStringCurly() const327 {328 if (mGuidState == GUID_INVALID)329 {330 /* What to return in case of wrong Guid */331 return Utf8Str("{00000000-0000-0000-0000-00000000000}");332 }333 334 char buf[RTUUID_STR_LENGTH + 2];335 ::memset(buf, '\0', sizeof(buf));336 ::RTUuidToStr(&mUuid, buf + 1, sizeof(buf) - 2);337 buf[0] = '{';338 buf[sizeof(buf) - 2] = '}';339 340 return Utf8Str(buf);341 }342 343 /**344 * Convert the GUID to a string.345 *346 * @returns Bstr object containing the formatted GUID.347 * @throws std::bad_alloc348 */349 Bstr toUtf16() const350 {351 if (mGuidState == GUID_INVALID)352 {353 /* What to return in case of wrong Guid */354 return Bstr("00000000-0000-0000-0000-00000000000");355 }356 357 RTUTF16 buf[RTUUID_STR_LENGTH];358 ::memset(buf, '\0', sizeof(buf));359 ::RTUuidToUtf16(&mUuid, buf, RT_ELEMENTS(buf));360 361 return Bstr(buf);362 }363 364 bool isValid() const365 {366 return mGuidState != GUID_INVALID;367 }368 369 bool isZero() const370 {371 return mGuidState == GUID_ZERO;372 }373 374 bool operator==(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) == 0; }375 bool operator==(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) == 0; }376 bool operator==(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) == 0; }377 bool operator!=(const Guid &that) const { return !operator==(that); }378 bool operator!=(const GUID &guid) const { return !operator==(guid); }379 bool operator!=(const RTUUID &guid) const { return !operator==(guid); }380 bool operator<(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) < 0; }381 bool operator<(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) < 0; }382 bool operator<(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) < 0; }383 384 /**385 * To directly copy the contents to a GUID, or for passing it as an input386 * parameter of type (const GUID *), the compiler converts. */387 const GUID &ref() const388 {389 return *(const GUID *)&mUuid;390 }391 392 /**393 * To pass instances to printf-like functions.394 */395 PCRTUUID raw() const396 {397 return (PCRTUUID)&mUuid;398 }399 400 /**401 * Static immutable empty (zero) object. May be used for comparison purposes.402 */403 static const Guid Empty;404 405 private:406 /**407 484 * Refresh the debug-only UUID string. 408 485 *
Note:
See TracChangeset
for help on using the changeset viewer.