VirtualBox

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

Last change on this file since 7095 was 6935, checked in by vboxsync, 17 years ago

Main: Changed 'defined (RT_OS_WINDOWS)' => '!defined (VBOX_WITH_XPCOM)' in relevant places, for clarity (not XPCOM is possible only on Windows so far).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette