VirtualBox

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

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

Must make sure UINT32_C() and friends are defined when stdint.h might be included by someone other than iprt/types.h.

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