VirtualBox

source: vbox/trunk/include/VBox/com/string.h@ 8155

Last change on this file since 8155 was 8155, checked in by vboxsync, 16 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.0 KB
Line 
1/* $Id: string.h 8155 2008-04-18 15:16:47Z vboxsync $ */
2
3/** @file
4 * MS COM / XPCOM Abstraction Layer:
5 * Smart string classes declaration
6 */
7
8/*
9 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 *
28 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
29 * Clara, CA 95054 USA or visit http://www.sun.com if you need
30 * additional information or have any questions.
31 */
32
33#ifndef ___VBox_com_string_h
34#define ___VBox_com_string_h
35
36#if defined (VBOX_WITH_XPCOM)
37#include <nsMemory.h>
38#endif
39
40#include "VBox/com/defs.h"
41#include "VBox/com/assert.h"
42
43#include <iprt/string.h>
44#include <iprt/cpputils.h>
45#include <iprt/alloc.h>
46
47namespace com
48{
49
50class Utf8Str;
51
52/**
53 * Helper class that represents the |BSTR| type and hides platform-specific
54 * implementation details.
55 *
56 * This class uses COM/XPCOM-provided memory management routines to allocate
57 * and free string buffers. This makes it possible to:
58 * - use it as a type of member variables of COM/XPCOM components and pass
59 * their values to callers through component methods' output parameters
60 * using the #cloneTo() operation;
61 * - adopt (take ownership of) string buffers returned in output parameters
62 * of COM methods using the #asOutParam() operation and correctly free them
63 * afterwards.
64 */
65class Bstr
66{
67public:
68
69 typedef BSTR String;
70 typedef const BSTR ConstString;
71
72 Bstr () : bstr (NULL) {}
73
74 Bstr (const Bstr &that) : bstr (NULL) { raw_copy (bstr, that.bstr); }
75 Bstr (const BSTR that) : bstr (NULL) { raw_copy (bstr, that); }
76 Bstr (const wchar_t *that) : bstr (NULL)
77 {
78 AssertCompile (sizeof (wchar_t) == sizeof (OLECHAR));
79 raw_copy (bstr, (const BSTR) that);
80 }
81
82 Bstr (const Utf8Str &that);
83 Bstr (const char *that);
84
85 /** Shortcut that calls #alloc(aSize) right after object creation. */
86 Bstr (size_t aSize) : bstr (NULL) { alloc (aSize); }
87
88 ~Bstr () { setNull(); }
89
90 Bstr &operator = (const Bstr &that) { safe_assign (that.bstr); return *this; }
91 Bstr &operator = (const BSTR that) { safe_assign (that); return *this; }
92
93 Bstr &operator = (const Utf8Str &that);
94 Bstr &operator = (const char *that);
95
96 Bstr &setNull()
97 {
98 if (bstr)
99 {
100 ::SysFreeString (bstr);
101 bstr = NULL;
102 }
103 return *this;
104 }
105
106 Bstr &setNullIfEmpty()
107 {
108 if (bstr && *bstr == 0)
109 {
110 ::SysFreeString (bstr);
111 bstr = NULL;
112 }
113 return *this;
114 }
115
116 /**
117 * Allocates memory for a string capable to store \a aSize - 1 characters
118 * plus the terminating zero character. If \a aSize is zero, or if a
119 * memory allocation error occurs, this object will become null.
120 */
121 Bstr &alloc (size_t aSize)
122 {
123 setNull();
124 if (aSize)
125 {
126 unsigned int size = (unsigned int) aSize; Assert (size == aSize);
127 bstr = ::SysAllocStringLen (NULL, size - 1);
128 if (bstr)
129 bstr [0] = 0;
130 }
131 return *this;
132 }
133
134 int compare (const BSTR str) const
135 {
136 return ::RTUtf16Cmp ((PRTUTF16) bstr, (PRTUTF16) str);
137 }
138
139 bool operator == (const Bstr &that) const { return !compare (that.bstr); }
140 bool operator != (const Bstr &that) const { return !!compare (that.bstr); }
141 bool operator == (const BSTR that) const { return !compare (that); }
142 bool operator != (const wchar_t *that) const
143 {
144 AssertCompile (sizeof (wchar_t) == sizeof (OLECHAR));
145 return !!compare ((const BSTR) that);
146 }
147 bool operator == (const wchar_t *that) const
148 {
149 AssertCompile (sizeof (wchar_t) == sizeof (OLECHAR));
150 return !compare ((const BSTR) that);
151 }
152 bool operator != (const BSTR that) const { return !!compare (that); }
153 bool operator < (const Bstr &that) const { return compare (that.bstr) < 0; }
154 bool operator < (const BSTR that) const { return compare (that) < 0; }
155 bool operator < (const wchar_t *that) const
156 {
157 AssertCompile (sizeof (wchar_t) == sizeof (OLECHAR));
158 return compare ((const BSTR) that) < 0;
159 }
160
161 int compareIgnoreCase (const BSTR str) const
162 {
163 return ::RTUtf16LocaleICmp (bstr, str);
164 }
165
166 bool isNull() const { return bstr == NULL; }
167 operator bool() const { return !isNull(); }
168
169 bool isEmpty() const { return isNull() || *bstr == 0; }
170
171 size_t length() const { return isNull() ? 0 : ::RTUtf16Len ((PRTUTF16) bstr); }
172
173 /** Intended to to pass instances as |BSTR| input parameters to methods. */
174 operator const BSTR () const { return bstr; }
175
176 /** The same as operator const BSTR(), but for situations where the compiler
177 cannot typecast implicitly (for example, in printf() argument list). */
178 const BSTR raw() const { return bstr; }
179
180 /**
181 * Returns a non-const raw pointer that allows to modify the string directly.
182 * @warning
183 * Be sure not to modify data beyond the allocated memory! The
184 * guaranteed size of the allocated memory is at least #length()
185 * bytes after creation and after every assignment operation.
186 */
187 BSTR mutableRaw() { return bstr; }
188
189 /**
190 * Intended to assign copies of instances to |BSTR| out parameters from
191 * within the interface method. Transfers the ownership of the duplicated
192 * string to the caller.
193 */
194 const Bstr &cloneTo (BSTR *pstr) const
195 {
196 if (pstr)
197 {
198 *pstr = NULL;
199 raw_copy (*pstr, bstr);
200 }
201 return *this;
202 }
203
204 /**
205 * Intended to assign instances to |BSTR| out parameters from within the
206 * interface method. Transfers the ownership of the original string to the
207 * caller and resets the instance to null.
208 *
209 * As opposed to cloneTo(), this method doesn't create a copy of the
210 * string.
211 */
212 Bstr &detachTo (BSTR *pstr)
213 {
214 *pstr = bstr;
215 bstr = NULL;
216 return *this;
217 }
218
219 /**
220 * Intended to assign copies of instances to |char *| out parameters from
221 * within the interface method. Transfers the ownership of the duplicated
222 * string to the caller.
223 */
224 const Bstr &cloneTo (char **pstr) const;
225
226 /**
227 * Intended to pass instances as |BSTR| out parameters to methods.
228 * Takes the ownership of the returned data.
229 */
230 BSTR *asOutParam() { setNull(); return &bstr; }
231
232 /**
233 * Static immutable null object. May be used for comparison purposes.
234 */
235 static const Bstr Null;
236
237private:
238
239 void safe_assign (const BSTR str)
240 {
241 if (bstr != str)
242 {
243 setNull();
244 raw_copy (bstr, str);
245 }
246 }
247
248 inline static void raw_copy (BSTR &ls, const BSTR rs)
249 {
250 if (rs)
251 ls = ::SysAllocString ((const OLECHAR *) rs);
252 }
253
254 inline static void raw_copy (BSTR &ls, const char *rs)
255 {
256 if (rs)
257 {
258 PRTUTF16 s = NULL;
259 ::RTStrToUtf16 (rs, &s);
260 raw_copy (ls, (BSTR) s);
261 ::RTUtf16Free (s);
262 }
263 }
264
265 BSTR bstr;
266
267 friend class Utf8Str; // to access our raw_copy()
268};
269
270// symmetric compare operators
271inline bool operator== (const BSTR l, const Bstr &r) { return r.operator== (l); }
272inline bool operator!= (const BSTR l, const Bstr &r) { return r.operator!= (l); }
273
274////////////////////////////////////////////////////////////////////////////////
275
276/**
277 * Helper class that represents UTF8 (|char *|) strings. Useful in
278 * conjunction with Bstr to simplify conversions beetween UTF16 (|BSTR|)
279 * and UTF8.
280 *
281 * This class uses COM/XPCOM-provided memory management routines to allocate
282 * and free string buffers. This makes it possible to:
283 * - use it as a type of member variables of COM/XPCOM components and pass
284 * their values to callers through component methods' output parameters
285 * using the #cloneTo() operation;
286 * - adopt (take ownership of) string buffers returned in output parameters
287 * of COM methods using the #asOutParam() operation and correctly free them
288 * afterwards.
289 */
290class Utf8Str
291{
292public:
293
294 typedef char *String;
295 typedef const char *ConstString;
296
297 Utf8Str () : str (NULL) {}
298
299 Utf8Str (const Utf8Str &that) : str (NULL) { raw_copy (str, that.str); }
300 Utf8Str (const char *that) : str (NULL) { raw_copy (str, that); }
301
302 Utf8Str (const Bstr &that) : str (NULL) { raw_copy (str, that); }
303 Utf8Str (const BSTR that) : str (NULL) { raw_copy (str, that); }
304
305 /** Shortcut that calls #alloc(aSize) right after object creation. */
306 Utf8Str (size_t aSize) : str (NULL) { alloc(aSize); }
307
308 virtual ~Utf8Str () { setNull(); }
309
310 Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; }
311 Utf8Str &operator = (const char *that) { safe_assign (that); return *this; }
312
313 Utf8Str &operator = (const Bstr &that)
314 {
315 setNull();
316 raw_copy (str, that);
317 return *this;
318 }
319 Utf8Str &operator = (const BSTR that)
320 {
321 setNull();
322 raw_copy (str, that);
323 return *this;
324 }
325
326 Utf8Str &setNull()
327 {
328 if (str)
329 {
330#if !defined (VBOX_WITH_XPCOM)
331 ::RTStrFree (str);
332#else
333 nsMemory::Free (str);
334#endif
335 str = NULL;
336 }
337 return *this;
338 }
339
340 Utf8Str &setNullIfEmpty()
341 {
342 if (str && *str == 0)
343 {
344#if !defined (VBOX_WITH_XPCOM)
345 ::RTStrFree (str);
346#else
347 nsMemory::Free (str);
348#endif
349 str = NULL;
350 }
351 return *this;
352 }
353
354 /**
355 * Allocates memory for a string capable to store \a aSize - 1 characters
356 * plus the terminating zero character. If \a aSize is zero, or if a
357 * memory allocation error occurs, this object will become null.
358 */
359 Utf8Str &alloc (size_t aSize)
360 {
361 setNull();
362 if (aSize)
363 {
364#if !defined (VBOX_WITH_XPCOM)
365 str = (char *) ::RTMemTmpAlloc (aSize);
366#else
367 str = (char *) nsMemory::Alloc (aSize);
368#endif
369 if (str)
370 str [0] = 0;
371 }
372 return *this;
373 }
374
375 int compare (const char *s) const
376 {
377 if (str == s)
378 return 0;
379 if (str == NULL)
380 return -1;
381 if (s == NULL)
382 return 1;
383
384 return ::strcmp (str, s);
385 }
386
387 bool operator == (const Utf8Str &that) const { return !compare (that.str); }
388 bool operator != (const Utf8Str &that) const { return !!compare (that.str); }
389 bool operator == (const char *that) const { return !compare (that); }
390 bool operator != (const char *that) const { return !!compare (that); }
391 bool operator < (const Utf8Str &that) const { return compare (that.str) < 0; }
392 bool operator < (const char *that) const { return compare (that) < 0; }
393
394 bool isNull() const { return str == NULL; }
395 operator bool() const { return !isNull(); }
396
397 bool isEmpty() const { return isNull() || *str == 0; }
398
399 size_t length() const { return isNull() ? 0 : ::strlen (str); }
400
401 /** Intended to to pass instances as input (|char *|) parameters to methods. */
402 operator const char *() const { return str; }
403
404 /** The same as operator const char *(), but for situations where the compiler
405 cannot typecast implicitly (for example, in printf() argument list). */
406 const char *raw() const { return str; }
407
408 /**
409 * Returns a non-const raw pointer that allows to modify the string directly.
410 * @warning
411 * Be sure not to modify data beyond the allocated memory! The
412 * guaranteed size of the allocated memory is at least #length()
413 * bytes after creation and after every assignment operation.
414 */
415 char *mutableRaw() { return str; }
416
417 /**
418 * Intended to assign instances to |char *| out parameters from within the
419 * interface method. Transfers the ownership of the duplicated string to the
420 * caller.
421 */
422 const Utf8Str &cloneTo (char **pstr) const
423 {
424 if (pstr)
425 {
426 *pstr = NULL;
427 raw_copy (*pstr, str);
428 }
429 return *this;
430 }
431
432 /**
433 * Intended to assign instances to |char *| out parameters from within the
434 * interface method. Transfers the ownership of the original string to the
435 * caller and resets the instance to null.
436 *
437 * As opposed to cloneTo(), this method doesn't create a copy of the
438 * string.
439 */
440 Utf8Str &detachTo (char **pstr)
441 {
442 *pstr = str;
443 str = NULL;
444 return *this;
445 }
446
447 /**
448 * Intended to assign instances to |BSTR| out parameters from within the
449 * interface method. Transfers the ownership of the duplicated string to the
450 * caller.
451 */
452 const Utf8Str &cloneTo (BSTR *pstr) const
453 {
454 if (pstr)
455 {
456 *pstr = NULL;
457 Bstr::raw_copy (*pstr, str);
458 }
459 return *this;
460 }
461
462 /**
463 * Intended to pass instances as out (|char **|) parameters to methods.
464 * Takes the ownership of the returned data.
465 */
466 char **asOutParam() { setNull(); return &str; }
467
468 /**
469 * Static immutable null object. May be used for comparison purposes.
470 */
471 static const Utf8Str Null;
472
473private:
474
475 void safe_assign (const char *s)
476 {
477 if (str != s)
478 {
479 setNull();
480 raw_copy (str, s);
481 }
482 }
483
484 inline static void raw_copy (char *&ls, const char *rs)
485 {
486 if (rs)
487#if !defined (VBOX_WITH_XPCOM)
488 ::RTStrDupEx (&ls, rs);
489#else
490 ls = (char *) nsMemory::Clone (rs, strlen (rs) + 1);
491#endif
492 }
493
494 inline static void raw_copy (char *&ls, const BSTR rs)
495 {
496 if (rs)
497 {
498#if !defined (VBOX_WITH_XPCOM)
499 ::RTUtf16ToUtf8 ((PRTUTF16) rs, &ls);
500#else
501 char *s = NULL;
502 ::RTUtf16ToUtf8 ((PRTUTF16) rs, &s);
503 raw_copy (ls, s);
504 ::RTStrFree (s);
505#endif
506 }
507 }
508
509 char *str;
510
511 friend class Bstr; // to access our raw_copy()
512};
513
514// symmetric compare operators
515inline bool operator== (const char *l, const Utf8Str &r) { return r.operator== (l); }
516inline bool operator!= (const char *l, const Utf8Str &r) { return r.operator!= (l); }
517
518// work around error C2593 of the stupid MSVC 7.x ambiguity resolver
519WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Bstr)
520WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Utf8Str)
521
522////////////////////////////////////////////////////////////////////////////////
523
524// inlined Bstr members that depend on Utf8Str
525
526inline Bstr::Bstr (const Utf8Str &that) : bstr (NULL) { raw_copy (bstr, that); }
527inline Bstr::Bstr (const char *that) : bstr (NULL) { raw_copy (bstr, that); }
528
529inline Bstr &Bstr::operator = (const Utf8Str &that)
530{
531 setNull();
532 raw_copy (bstr, that);
533 return *this;
534}
535inline Bstr &Bstr::operator = (const char *that)
536{
537 setNull();
538 raw_copy (bstr, that);
539 return *this;
540}
541
542inline const Bstr &Bstr::cloneTo (char **pstr) const
543{
544 if (pstr) {
545 *pstr = NULL;
546 Utf8Str::raw_copy (*pstr, bstr);
547 }
548 return *this;
549}
550
551////////////////////////////////////////////////////////////////////////////////
552
553/**
554 * This class is a printf-like formatter for Utf8Str strings. Its purpose is
555 * to construct Utf8Str objects from a format string and a list of arguments
556 * for the format string.
557 *
558 * The usage of this class is like the following:
559 * <code>
560 * Utf8StrFmt string ("program name = %s", argv[0]);
561 * </code>
562 */
563class Utf8StrFmt : public Utf8Str
564{
565public:
566
567 /**
568 * Constructs a new string given the format string and the list
569 * of the arguments for the format string.
570 *
571 * @param format printf-like format string (in UTF-8 encoding)
572 * @param ... list of the arguments for the format string
573 */
574 explicit Utf8StrFmt (const char *format, ...)
575 {
576 va_list args;
577 va_start (args, format);
578 init (format, args);
579 va_end (args);
580 }
581
582protected:
583
584 Utf8StrFmt() {}
585
586 void init (const char *format, va_list args);
587
588private:
589
590 static DECLCALLBACK(size_t) strOutput (void *pvArg, const char *pachChars,
591 size_t cbChars);
592};
593
594/**
595 * This class is a vprintf-like formatter for Utf8Str strings. It is
596 * identical to Utf8StrFmt except that its constructor takes a va_list
597 * argument instead of ellipsis.
598 *
599 * Note that a separate class is necessary because va_list is defined as
600 * |char *| on most platforms. For this reason, if we had two overloaded
601 * constructors in Utf8StrFmt (one taking ellipsis and another one taking
602 * va_list) then composing a constructor call using exactly two |char *|
603 * arguments would cause the compiler to use the va_list overload instead of
604 * the ellipsis one which is obviously wrong. The compiler would choose
605 * va_list because ellipsis has the lowest rank when it comes to resolving
606 * overloads, as opposed to va_list which is an exact match for |char *|.
607 */
608class Utf8StrFmtVA : public Utf8StrFmt
609{
610public:
611
612 /**
613 * Constructs a new string given the format string and the list
614 * of the arguments for the format string.
615 *
616 * @param format printf-like format string (in UTF-8 encoding)
617 * @param args list of arguments for the format string
618 */
619 Utf8StrFmtVA (const char *format, va_list args) { init (format, args); }
620};
621
622} /* namespace com */
623
624#endif /* ___VBox_com_string_h */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use