VirtualBox

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

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

Merged dmik/s2 branch (r25959:26751) to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1/* $Id: string.h 6076 2007-12-14 19:23:03Z 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(RT_OS_WINDOWS)
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 instances to |BSTR| out parameters from within the
187 * interface method. Transfers the ownership of the duplicated string to
188 * 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 |char *| out parameters from within the
202 * interface method. Transfers the ownership of the duplicated string to
203 * the caller.
204 */
205 const Bstr &cloneTo (char **pstr) const;
206
207 /**
208 * Intended to pass instances as |BSTR| out parameters to methods.
209 * Takes the ownership of the returned data.
210 */
211 BSTR *asOutParam() { setNull(); return &bstr; }
212
213 /**
214 * Static immutable null object. May be used for comparison purposes.
215 */
216 static const Bstr Null;
217
218private:
219
220 void safe_assign (const BSTR str)
221 {
222 if (bstr != str)
223 {
224 setNull();
225 raw_copy (bstr, str);
226 }
227 }
228
229 inline static void raw_copy (BSTR &ls, const BSTR rs)
230 {
231 if (rs)
232 ls = ::SysAllocString ((const OLECHAR *) rs);
233 }
234
235 inline static void raw_copy (BSTR &ls, const char *rs)
236 {
237 if (rs)
238 {
239 PRTUCS2 s = NULL;
240 ::RTStrUtf8ToUcs2 (&s, rs);
241 raw_copy (ls, (BSTR) s);
242 ::RTStrUcs2Free (s);
243 }
244 }
245
246 BSTR bstr;
247
248 friend class Utf8Str; // to access our raw_copy()
249};
250
251// symmetric compare operators
252inline bool operator== (const BSTR l, const Bstr &r) { return r.operator== (l); }
253inline bool operator!= (const BSTR l, const Bstr &r) { return r.operator!= (l); }
254
255////////////////////////////////////////////////////////////////////////////////
256
257/**
258 * Helper class that represents UTF8 (|char *|) strings. Useful in
259 * conjunction with Bstr to simplify conversions beetween UCS2 (|BSTR|)
260 * and UTF8.
261 *
262 * This class uses COM/XPCOM-provided memory management routines to allocate
263 * and free string buffers. This makes it possible to:
264 * - use it as a type of member variables of COM/XPCOM components and pass
265 * their values to callers through component methods' output parameters
266 * using the #cloneTo() operation;
267 * - adopt (take ownership of) string buffers returned in output parameters
268 * of COM methods using the #asOutParam() operation and correctly free them
269 * afterwards.
270 */
271class Utf8Str
272{
273public:
274
275 typedef char *String;
276 typedef const char *ConstString;
277
278 Utf8Str () : str (NULL) {}
279
280 Utf8Str (const Utf8Str &that) : str (NULL) { raw_copy (str, that.str); }
281 Utf8Str (const char *that) : str (NULL) { raw_copy (str, that); }
282
283 Utf8Str (const Bstr &that) : str (NULL) { raw_copy (str, that); }
284 Utf8Str (const BSTR that) : str (NULL) { raw_copy (str, that); }
285
286 /** Shortcut that calls #alloc(aSize) right after object creation. */
287 Utf8Str (size_t aSize) : str (NULL) { alloc(aSize); }
288
289 virtual ~Utf8Str () { setNull(); }
290
291 Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; }
292 Utf8Str &operator = (const char *that) { safe_assign (that); return *this; }
293
294 Utf8Str &operator = (const Bstr &that)
295 {
296 setNull();
297 raw_copy (str, that);
298 return *this;
299 }
300 Utf8Str &operator = (const BSTR that)
301 {
302 setNull();
303 raw_copy (str, that);
304 return *this;
305 }
306
307 Utf8Str &setNull()
308 {
309 if (str)
310 {
311#if defined (RT_OS_WINDOWS)
312 ::RTStrFree (str);
313#else
314 nsMemory::Free (str);
315#endif
316 str = NULL;
317 }
318 return *this;
319 }
320
321 Utf8Str &setNullIfEmpty()
322 {
323 if (str && *str == 0)
324 {
325#if defined (RT_OS_WINDOWS)
326 ::RTStrFree (str);
327#else
328 nsMemory::Free (str);
329#endif
330 str = NULL;
331 }
332 return *this;
333 }
334
335 /**
336 * Allocates memory for a string capable to store \a aSize - 1 characters
337 * plus the terminating zero character. If \a aSize is zero, or if a
338 * memory allocation error occurs, this object will become null.
339 */
340 Utf8Str &alloc (size_t aSize)
341 {
342 setNull();
343 if (aSize)
344 {
345#if defined (RT_OS_WINDOWS)
346 str = (char *) ::RTMemTmpAlloc (aSize);
347#else
348 str = (char *) nsMemory::Alloc (aSize);
349#endif
350 if (str)
351 str [0] = 0;
352 }
353 return *this;
354 }
355
356 int compare (const char *s) const
357 {
358 return str == s ? 0 : ::strcmp (str, s);
359 }
360
361 bool operator == (const Utf8Str &that) const { return !compare (that.str); }
362 bool operator != (const Utf8Str &that) const { return !!compare (that.str); }
363 bool operator == (const char *that) const { return !compare (that); }
364 bool operator != (const char *that) const { return !!compare (that); }
365 bool operator < (const Utf8Str &that) const { return compare (that.str) < 0; }
366 bool operator < (const char *that) const { return compare (that) < 0; }
367
368 bool isNull() const { return str == NULL; }
369 operator bool() const { return !isNull(); }
370
371 bool isEmpty() const { return isNull() || *str == 0; }
372
373 size_t length() const { return isNull() ? 0 : ::strlen (str); }
374
375 /** Intended to to pass instances as input (|char *|) parameters to methods. */
376 operator const char *() const { return str; }
377
378 /** The same as operator const char *(), but for situations where the compiler
379 cannot typecast implicitly (for example, in printf() argument list). */
380 const char *raw() const { return str; }
381
382 /**
383 * Returns a non-const raw pointer that allows to modify the string directly.
384 * @warning
385 * Be sure not to modify data beyond the allocated memory! The
386 * guaranteed size of the allocated memory is at least #length()
387 * bytes after creation and after every assignment operation.
388 */
389 char *mutableRaw() { return str; }
390
391 /**
392 * Intended to assign instances to |char *| out parameters from within the
393 * interface method. Transfers the ownership of the duplicated string to the
394 * caller.
395 */
396 const Utf8Str &cloneTo (char **pstr) const
397 {
398 if (pstr)
399 {
400 *pstr = NULL;
401 raw_copy (*pstr, str);
402 }
403 return *this;
404 }
405
406 /**
407 * Intended to assign instances to |BSTR| out parameters from within the
408 * interface method. Transfers the ownership of the duplicated string to the
409 * caller.
410 */
411 const Utf8Str &cloneTo (BSTR *pstr) const
412 {
413 if (pstr)
414 {
415 *pstr = NULL;
416 Bstr::raw_copy (*pstr, str);
417 }
418 return *this;
419 }
420
421 /**
422 * Intended to pass instances as out (|char **|) parameters to methods.
423 * Takes the ownership of the returned data.
424 */
425 char **asOutParam() { setNull(); return &str; }
426
427 /**
428 * Static immutable null object. May be used for comparison purposes.
429 */
430 static const Utf8Str Null;
431
432private:
433
434 void safe_assign (const char *s)
435 {
436 if (str != s)
437 {
438 setNull();
439 raw_copy (str, s);
440 }
441 }
442
443 inline static void raw_copy (char *&ls, const char *rs)
444 {
445 if (rs)
446#if defined (RT_OS_WINDOWS)
447 ::RTStrDupEx (&ls, rs);
448#else
449 ls = (char *) nsMemory::Clone (rs, strlen (rs) + 1);
450#endif
451 }
452
453 inline static void raw_copy (char *&ls, const BSTR rs)
454 {
455 if (rs)
456 {
457#if defined (RT_OS_WINDOWS)
458 ::RTStrUcs2ToUtf8 (&ls, (PRTUCS2) rs);
459#else
460 char *s = NULL;
461 ::RTStrUcs2ToUtf8 (&s, (PRTUCS2) rs);
462 raw_copy (ls, s);
463 ::RTStrFree (s);
464#endif
465 }
466 }
467
468 char *str;
469
470 friend class Bstr; // to access our raw_copy()
471};
472
473// symmetric compare operators
474inline bool operator== (const char *l, const Utf8Str &r) { return r.operator== (l); }
475inline bool operator!= (const char *l, const Utf8Str &r) { return r.operator!= (l); }
476
477// work around error C2593 of the stupid MSVC 7.x ambiguity resolver
478WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Bstr)
479WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Utf8Str)
480
481////////////////////////////////////////////////////////////////////////////////
482
483// inlined Bstr members that depend on Utf8Str
484
485inline Bstr::Bstr (const Utf8Str &that) : bstr (NULL) { raw_copy (bstr, that); }
486inline Bstr::Bstr (const char *that) : bstr (NULL) { raw_copy (bstr, that); }
487
488inline Bstr &Bstr::operator = (const Utf8Str &that)
489{
490 setNull();
491 raw_copy (bstr, that);
492 return *this;
493}
494inline Bstr &Bstr::operator = (const char *that)
495{
496 setNull();
497 raw_copy (bstr, that);
498 return *this;
499}
500
501inline const Bstr &Bstr::cloneTo (char **pstr) const
502{
503 if (pstr) {
504 *pstr = NULL;
505 Utf8Str::raw_copy (*pstr, bstr);
506 }
507 return *this;
508}
509
510////////////////////////////////////////////////////////////////////////////////
511
512/**
513 * This class is a printf-like formatter for Utf8Str strings. Its purpose is
514 * to construct Utf8Str objects from a format string and a list of arguments
515 * for the format string.
516 *
517 * The usage of this class is like the following:
518 * <code>
519 * Utf8StrFmt string ("program name = %s", argv[0]);
520 * </code>
521 */
522class Utf8StrFmt : public Utf8Str
523{
524public:
525
526 /**
527 * Constructs a new string given the format string and the list
528 * of the arguments for the format string.
529 *
530 * @param format printf-like format string (in UTF-8 encoding)
531 * @param ... list of the arguments for the format string
532 */
533 explicit Utf8StrFmt (const char *format, ...)
534 {
535 va_list args;
536 va_start (args, format);
537 init (format, args);
538 va_end (args);
539 }
540
541protected:
542
543 Utf8StrFmt() {}
544
545 void init (const char *format, va_list args);
546
547private:
548
549 static DECLCALLBACK(size_t) strOutput (void *pvArg, const char *pachChars,
550 size_t cbChars);
551};
552
553/**
554 * This class is a vprintf-like formatter for Utf8Str strings. It is
555 * identical to Utf8StrFmt except that its constructor takes a va_list
556 * argument instead of ellipsis.
557 *
558 * Note that a separate class is necessary because va_list is defined as
559 * |char *| on most platforms. For this reason, if we had two overloaded
560 * constructors in Utf8StrFmt (one taking ellipsis and another one taking
561 * va_list) then composing a constructor call using exactly two |char *|
562 * arguments would cause the compiler to use the va_list overload instead of
563 * the ellipsis one which is obviously wrong. The compiler would choose
564 * va_list because ellipsis has the lowest rank when it comes to resolving
565 * overloads, as opposed to va_list which is an exact match for |char *|.
566 */
567class Utf8StrFmtVA : public Utf8StrFmt
568{
569public:
570
571 /**
572 * Constructs a new string given the format string and the list
573 * of the arguments for the format string.
574 *
575 * @param format printf-like format string (in UTF-8 encoding)
576 * @param args list of arguments for the format string
577 */
578 Utf8StrFmtVA (const char *format, va_list args) { init (format, args); }
579};
580
581} /* namespace com */
582
583#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