Changeset 2012 in vbox for trunk/include
- Timestamp:
- Apr 10, 2007 2:50:19 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/VBox/com/string.h
r1962 r2012 68 68 Bstr (const char *that); 69 69 70 /** 71 * Allocates memory for a new string capable to store \a aSize - 1 72 * characters plus the terminating zero character. If \a aSize is zero, 73 * a null object will be created. 74 */ 75 Bstr (size_t aSize) : bstr (NULL) 70 /** Shortcut that calls #alloc(aSize) right after object creation. */ 71 Bstr (size_t aSize) : bstr (NULL) { alloc (aSize); } 72 73 ~Bstr () { setNull(); } 74 75 Bstr &operator = (const Bstr &that) { safe_assign (that.bstr); return *this; } 76 Bstr &operator = (const BSTR that) { safe_assign (that); return *this; } 77 78 Bstr &operator = (const Utf8Str &that); 79 Bstr &operator = (const char *that); 80 81 Bstr &setNull() 82 { 83 if (bstr) 84 { 85 ::SysFreeString (bstr); 86 bstr = NULL; 87 } 88 return *this; 89 } 90 91 Bstr &setNullIfEmpty() 92 { 93 if (bstr && *bstr == 0) 94 { 95 ::SysFreeString (bstr); 96 bstr = NULL; 97 } 98 return *this; 99 } 100 101 /** 102 * Allocates memory for a string capable to store \a aSize - 1 characters 103 * plus the terminating zero character. If \a aSize is zero, or if a 104 * memory allocation error occurs, this object will become null. 105 */ 106 Bstr &alloc (size_t aSize) 76 107 { 77 108 if (aSize) 78 109 { 79 unsigned int size = (unsigned int)aSize; Assert(size == aSize); 110 AssertCompile (sizeof (unsigned int) >= sizeof (aSize)); 111 unsigned int size = (unsigned int) aSize; 80 112 bstr = ::SysAllocStringLen (NULL, size - 1); 81 113 if (bstr) 82 114 bstr [0] = 0; 83 115 } 84 } 85 86 ~Bstr () { setNull(); } 87 88 Bstr &operator = (const Bstr &that) { safe_assign (that.bstr); return *this; } 89 Bstr &operator = (const BSTR that) { safe_assign (that); return *this; } 90 91 Bstr &operator = (const Utf8Str &that); 92 Bstr &operator = (const char *that); 93 94 Bstr &setNull() { 95 if (bstr) { 96 ::SysFreeString (bstr); 97 bstr = NULL; 98 } 99 return *this; 100 } 101 102 Bstr &setNullIfEmpty() { 103 if (bstr && *bstr == 0) { 104 ::SysFreeString (bstr); 105 bstr = NULL; 106 } 107 return *this; 108 } 109 110 int compare (const BSTR str) const { 116 return *this; 117 } 118 119 int compare (const BSTR str) const 120 { 111 121 return ::RTStrUcs2Cmp ((PRTUCS2) bstr, (PRTUCS2) str); 112 122 } … … 134 144 } 135 145 136 int compareIgnoreCase (const BSTR str) const { 146 int compareIgnoreCase (const BSTR str) const 147 { 137 148 return ::RTUtf16LocaleICmp (bstr, str); 138 149 } … … 166 177 * the caller. 167 178 */ 168 const Bstr &cloneTo (BSTR *pstr) const { 169 if (pstr) { 179 const Bstr &cloneTo (BSTR *pstr) const 180 { 181 if (pstr) 182 { 170 183 *pstr = NULL; 171 184 raw_copy (*pstr, bstr); … … 189 202 private: 190 203 191 void safe_assign (const BSTR str) { 192 if (bstr != str) { 204 void safe_assign (const BSTR str) 205 { 206 if (bstr != str) 207 { 193 208 setNull(); 194 209 raw_copy (bstr, str); … … 196 211 } 197 212 198 inline static void raw_copy (BSTR &ls, const BSTR rs) { 213 inline static void raw_copy (BSTR &ls, const BSTR rs) 214 { 199 215 if (rs) 200 216 ls = ::SysAllocString ((const OLECHAR *) rs); 201 217 } 202 218 203 inline static void raw_copy (BSTR &ls, const char *rs) { 204 if (rs) { 219 inline static void raw_copy (BSTR &ls, const char *rs) 220 { 221 if (rs) 222 { 205 223 PRTUCS2 s = NULL; 206 224 ::RTStrUtf8ToUcs2 (&s, rs); … … 241 259 Utf8Str (const BSTR that) : str (NULL) { raw_copy (str, that); } 242 260 243 /** 244 * Allocates memory for a new string capable to store \a aSize - 1 245 * characters plus the terminating zero character. If \a aSize is zero, 246 * a null object will be created. 247 */ 248 void alloc (size_t aSize) 261 /** Shortcut that calls #alloc(aSize) right after object creation. */ 262 Utf8Str (size_t aSize) : str (NULL) { alloc(aSize); } 263 264 virtual ~Utf8Str () { setNull(); } 265 266 Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; } 267 Utf8Str &operator = (const char *that) { safe_assign (that); return *this; } 268 269 Utf8Str &operator = (const Bstr &that) 270 { 271 setNull(); 272 raw_copy (str, that); 273 return *this; 274 } 275 Utf8Str &operator = (const BSTR that) 276 { 277 setNull(); 278 raw_copy (str, that); 279 return *this; 280 } 281 282 Utf8Str &setNull() 283 { 284 if (str) 285 { 286 #if defined (__WIN__) 287 ::RTStrFree (str); 288 #else 289 nsMemory::Free (str); 290 #endif 291 str = NULL; 292 } 293 return *this; 294 } 295 296 Utf8Str &setNullIfEmpty() 297 { 298 if (str && *str == 0) 299 { 300 #if defined (__WIN__) 301 ::RTStrFree (str); 302 #else 303 nsMemory::Free (str); 304 #endif 305 str = NULL; 306 } 307 return *this; 308 } 309 310 /** 311 * Allocates memory for a string capable to store \a aSize - 1 characters 312 * plus the terminating zero character. If \a aSize is zero, or if a 313 * memory allocation error occurs, this object will become null. 314 */ 315 Utf8Str &alloc (size_t aSize) 249 316 { 250 317 if (aSize) … … 259 326 str [0] = 0; 260 327 } 261 } 262 263 Utf8Str (size_t aSize) : str (NULL) 264 { 265 alloc(aSize); 266 } 267 268 virtual ~Utf8Str () { setNull(); } 269 270 Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; } 271 Utf8Str &operator = (const char *that) { safe_assign (that); return *this; } 272 273 Utf8Str &operator = (const Bstr &that) { 274 setNull(); 275 raw_copy (str, that); 276 return *this; 277 } 278 Utf8Str &operator = (const BSTR that) { 279 setNull(); 280 raw_copy (str, that); 281 return *this; 282 } 283 284 Utf8Str &setNull() { 285 if (str) { 286 #if defined (__WIN__) 287 ::RTStrFree (str); 288 #else 289 nsMemory::Free (str); 290 #endif 291 str = NULL; 292 } 293 return *this; 294 } 295 296 Utf8Str &setNullIfEmpty() { 297 if (str && *str == 0) { 298 #if defined (__WIN__) 299 ::RTStrFree (str); 300 #else 301 nsMemory::Free (str); 302 #endif 303 str = NULL; 304 } 305 return *this; 306 } 307 308 int compare (const char *s) const { 328 return *this; 329 } 330 331 int compare (const char *s) const 332 { 309 333 return str == s ? 0 : ::strcmp (str, s); 310 334 } … … 345 369 * caller. 346 370 */ 347 const Utf8Str &cloneTo (char **pstr) const { 348 if (pstr) { 371 const Utf8Str &cloneTo (char **pstr) const 372 { 373 if (pstr) 374 { 349 375 *pstr = NULL; 350 376 raw_copy (*pstr, str); … … 358 384 * caller. 359 385 */ 360 const Utf8Str &cloneTo (BSTR *pstr) const { 361 if (pstr) { 386 const Utf8Str &cloneTo (BSTR *pstr) const 387 { 388 if (pstr) 389 { 362 390 *pstr = NULL; 363 391 Bstr::raw_copy (*pstr, str); … … 374 402 private: 375 403 376 void safe_assign (const char *s) { 377 if (str != s) { 404 void safe_assign (const char *s) 405 { 406 if (str != s) 407 { 378 408 setNull(); 379 409 raw_copy (str, s); … … 381 411 } 382 412 383 inline static void raw_copy (char *&ls, const char *rs) { 413 inline static void raw_copy (char *&ls, const char *rs) 414 { 384 415 if (rs) 385 416 #if defined (__WIN__) … … 390 421 } 391 422 392 inline static void raw_copy (char *&ls, const BSTR rs) { 393 if (rs) { 423 inline static void raw_copy (char *&ls, const BSTR rs) 424 { 425 if (rs) 426 { 394 427 #if defined (__WIN__) 395 428 ::RTStrUcs2ToUtf8 (&ls, (PRTUCS2) rs); … … 423 456 inline Bstr::Bstr (const char *that) : bstr (NULL) { raw_copy (bstr, that); } 424 457 425 inline Bstr &Bstr::operator = (const Utf8Str &that) { 458 inline Bstr &Bstr::operator = (const Utf8Str &that) 459 { 426 460 setNull(); 427 461 raw_copy (bstr, that); 428 462 return *this; 429 463 } 430 inline Bstr &Bstr::operator = (const char *that) { 464 inline Bstr &Bstr::operator = (const char *that) 465 { 431 466 setNull(); 432 467 raw_copy (bstr, that); … … 434 469 } 435 470 436 inline const Bstr &Bstr::cloneTo (char **pstr) const { 471 inline const Bstr &Bstr::cloneTo (char **pstr) const 472 { 437 473 if (pstr) { 438 474 *pstr = NULL; … … 470 506 * platforms. If unsure, add an extra dummy argument. 471 507 */ 472 explicit Utf8StrFmt (const char *format, ...) { 508 explicit Utf8StrFmt (const char *format, ...) 509 { 473 510 va_list args; 474 511 va_start (args, format);
Note:
See TracChangeset
for help on using the changeset viewer.