VirtualBox

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

Last change on this file since 58920 was 58110, checked in by vboxsync, 9 years ago

include,misc: Doxygen grouping adjustments, collecting all the VMM bits under one parent group, ditto for the COM library.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.6 KB
Line 
1/* $Id: string.h 58110 2015-10-07 18:36:49Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - Smart string classes declaration.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_com_string_h
28#define ___VBox_com_string_h
29
30/* Make sure all the stdint.h macros are included - must come first! */
31#ifndef __STDC_LIMIT_MACROS
32# define __STDC_LIMIT_MACROS
33#endif
34#ifndef __STDC_CONSTANT_MACROS
35# define __STDC_CONSTANT_MACROS
36#endif
37
38#if defined(VBOX_WITH_XPCOM)
39# include <nsMemory.h>
40#endif
41
42#include "VBox/com/defs.h"
43#include "VBox/com/assert.h"
44
45#include <iprt/mem.h>
46#include <iprt/cpp/ministring.h>
47
48
49/** @defgroup grp_com_str Smart String Classes
50 * @ingroup grp_com
51 * @{
52 */
53
54namespace com
55{
56
57class Utf8Str;
58
59// global constant in glue/string.cpp that represents an empty BSTR
60extern const BSTR g_bstrEmpty;
61
62/**
63 * String class used universally in Main for COM-style Utf-16 strings.
64 *
65 * Unfortunately COM on Windows uses UTF-16 everywhere, requiring conversions
66 * back and forth since most of VirtualBox and our libraries use UTF-8.
67 *
68 * To make things more obscure, on Windows, a COM-style BSTR is not just a
69 * pointer to a null-terminated wide character array, but the four bytes (32
70 * bits) BEFORE the memory that the pointer points to are a length DWORD. One
71 * must therefore avoid pointer arithmetic and always use SysAllocString and
72 * the like to deal with BSTR pointers, which manage that DWORD correctly.
73 *
74 * For platforms other than Windows, we provide our own versions of the Sys*
75 * functions in Main/xpcom/helpers.cpp which do NOT use length prefixes though
76 * to be compatible with how XPCOM allocates string parameters to public
77 * functions.
78 *
79 * The Bstr class hides all this handling behind a std::string-like interface
80 * and also provides automatic conversions to RTCString and Utf8Str instances.
81 *
82 * The one advantage of using the SysString* routines is that this makes it
83 * possible to use it as a type of member variables of COM/XPCOM components and
84 * pass their values to callers through component methods' output parameters
85 * using the #cloneTo() operation. Also, the class can adopt (take ownership
86 * of) string buffers returned in output parameters of COM methods using the
87 * #asOutParam() operation and correctly free them afterwards.
88 *
89 * Starting with VirtualBox 3.2, like Utf8Str, Bstr no longer differentiates
90 * between NULL strings and empty strings. In other words, Bstr("") and
91 * Bstr(NULL) behave the same. In both cases, Bstr allocates no memory,
92 * reports a zero length and zero allocated bytes for both, and returns an
93 * empty C wide string from raw().
94 *
95 * @note All Bstr methods ASSUMES valid UTF-16 or UTF-8 input strings.
96 * The VirtualBox policy in this regard is to validate strings coming
97 * from external sources before passing them to Bstr or Utf8Str.
98 */
99class Bstr
100{
101public:
102
103 Bstr()
104 : m_bstr(NULL)
105 { }
106
107 Bstr(const Bstr &that)
108 {
109 copyFrom((const OLECHAR *)that.m_bstr);
110 }
111
112 Bstr(CBSTR that)
113 {
114 copyFrom((const OLECHAR *)that);
115 }
116
117#if defined(VBOX_WITH_XPCOM)
118 Bstr(const wchar_t *that)
119 {
120 AssertCompile(sizeof(wchar_t) == sizeof(OLECHAR));
121 copyFrom((const OLECHAR *)that);
122 }
123#endif
124
125 Bstr(const RTCString &that)
126 {
127 copyFrom(that.c_str());
128 }
129
130 Bstr(const char *that)
131 {
132 copyFrom(that);
133 }
134
135 Bstr(const char *a_pThat, size_t a_cchMax)
136 {
137 copyFromN(a_pThat, a_cchMax);
138 }
139
140 ~Bstr()
141 {
142 setNull();
143 }
144
145 Bstr& operator=(const Bstr &that)
146 {
147 cleanup();
148 copyFrom((const OLECHAR *)that.m_bstr);
149 return *this;
150 }
151
152 Bstr& operator=(CBSTR that)
153 {
154 cleanup();
155 copyFrom((const OLECHAR *)that);
156 return *this;
157 }
158
159#if defined(VBOX_WITH_XPCOM)
160 Bstr& operator=(const wchar_t *that)
161 {
162 cleanup();
163 copyFrom((const OLECHAR *)that);
164 return *this;
165 }
166#endif
167
168 Bstr& setNull()
169 {
170 cleanup();
171 return *this;
172 }
173
174#ifdef _MSC_VER
175# if _MSC_VER >= 1400
176 RTMEMEF_NEW_AND_DELETE_OPERATORS();
177# endif
178#else
179 RTMEMEF_NEW_AND_DELETE_OPERATORS();
180#endif
181
182 /** Case sensitivity selector. */
183 enum CaseSensitivity
184 {
185 CaseSensitive,
186 CaseInsensitive
187 };
188
189 /**
190 * Compares the member string to str.
191 * @param str
192 * @param cs Whether comparison should be case-sensitive.
193 * @return
194 */
195 int compare(CBSTR str, CaseSensitivity cs = CaseSensitive) const
196 {
197 if (cs == CaseSensitive)
198 return ::RTUtf16Cmp((PRTUTF16)m_bstr, (PRTUTF16)str);
199 return ::RTUtf16LocaleICmp((PRTUTF16)m_bstr, (PRTUTF16)str);
200 }
201
202 int compare(BSTR str, CaseSensitivity cs = CaseSensitive) const
203 {
204 return compare((CBSTR)str, cs);
205 }
206
207 int compare(const Bstr &that, CaseSensitivity cs = CaseSensitive) const
208 {
209 return compare(that.m_bstr, cs);
210 }
211
212 bool operator==(const Bstr &that) const { return !compare(that.m_bstr); }
213 bool operator==(CBSTR that) const { return !compare(that); }
214 bool operator==(BSTR that) const { return !compare(that); }
215 bool operator!=(const Bstr &that) const { return !!compare(that.m_bstr); }
216 bool operator!=(CBSTR that) const { return !!compare(that); }
217 bool operator!=(BSTR that) const { return !!compare(that); }
218 bool operator<(const Bstr &that) const { return compare(that.m_bstr) < 0; }
219 bool operator<(CBSTR that) const { return compare(that) < 0; }
220 bool operator<(BSTR that) const { return compare(that) < 0; }
221 bool operator<=(const Bstr &that) const { return compare(that.m_bstr) <= 0; }
222 bool operator<=(CBSTR that) const { return compare(that) <= 0; }
223 bool operator<=(BSTR that) const { return compare(that) <= 0; }
224 bool operator>(const Bstr &that) const { return compare(that.m_bstr) > 0; }
225 bool operator>(CBSTR that) const { return compare(that) > 0; }
226 bool operator>(BSTR that) const { return compare(that) > 0; }
227 bool operator>=(const Bstr &that) const { return compare(that.m_bstr) >= 0; }
228 bool operator>=(CBSTR that) const { return compare(that) >= 0; }
229 bool operator>=(BSTR that) const { return compare(that) >= 0; }
230
231 /**
232 * Returns true if the member string has no length.
233 * This is true for instances created from both NULL and "" input strings.
234 *
235 * @note Always use this method to check if an instance is empty. Do not
236 * use length() because that may need to run through the entire string
237 * (Bstr does not cache string lengths).
238 */
239 bool isEmpty() const { return m_bstr == NULL || *m_bstr == 0; }
240
241 /**
242 * Returns true if the member string has a length of one or more.
243 *
244 * @returns true if not empty, false if empty (NULL or "").
245 */
246 bool isNotEmpty() const { return m_bstr != NULL && *m_bstr != 0; }
247
248 size_t length() const { return isEmpty() ? 0 : ::RTUtf16Len((PRTUTF16)m_bstr); }
249
250#if defined(VBOX_WITH_XPCOM)
251 /**
252 * Returns a pointer to the raw member UTF-16 string. If the member string is empty,
253 * returns a pointer to a global variable containing an empty BSTR with a proper zero
254 * length prefix so that Windows is happy.
255 */
256 CBSTR raw() const
257 {
258 if (m_bstr)
259 return m_bstr;
260
261 return g_bstrEmpty;
262 }
263#else
264 /**
265 * Windows-only hack, as the automatically generated headers use BSTR.
266 * So if we don't want to cast like crazy we have to be more loose than
267 * on XPCOM.
268 *
269 * Returns a pointer to the raw member UTF-16 string. If the member string is empty,
270 * returns a pointer to a global variable containing an empty BSTR with a proper zero
271 * length prefix so that Windows is happy.
272 */
273 BSTR raw() const
274 {
275 if (m_bstr)
276 return m_bstr;
277
278 return g_bstrEmpty;
279 }
280#endif
281
282 /**
283 * Returns a non-const raw pointer that allows to modify the string directly.
284 * As opposed to raw(), this DOES return NULL if the member string is empty
285 * because we cannot return a mutable pointer to the global variable with the
286 * empty string.
287 *
288 * @warning
289 * Be sure not to modify data beyond the allocated memory! The
290 * guaranteed size of the allocated memory is at least #length()
291 * bytes after creation and after every assignment operation.
292 */
293 BSTR mutableRaw() { return m_bstr; }
294
295 /**
296 * Intended to assign copies of instances to |BSTR| out parameters from
297 * within the interface method. Transfers the ownership of the duplicated
298 * string to the caller.
299 *
300 * If the member string is empty, this allocates an empty BSTR in *pstr
301 * (i.e. makes it point to a new buffer with a null byte).
302 *
303 * @deprecated Use cloneToEx instead to avoid throwing exceptions.
304 */
305 void cloneTo(BSTR *pstr) const
306 {
307 if (pstr)
308 {
309 *pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
310#ifdef RT_EXCEPTIONS_ENABLED
311 if (!*pstr)
312 throw std::bad_alloc();
313#endif
314 }
315 }
316
317 /**
318 * A version of cloneTo that does not throw any out of memory exceptions, but
319 * returns E_OUTOFMEMORY intead.
320 * @returns S_OK or E_OUTOFMEMORY.
321 */
322 HRESULT cloneToEx(BSTR *pstr) const
323 {
324 if (!pstr)
325 return S_OK;
326 *pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
327 return pstr ? S_OK : E_OUTOFMEMORY;
328 }
329
330 /**
331 * Intended to assign instances to |BSTR| out parameters from within the
332 * interface method. Transfers the ownership of the original string to the
333 * caller and resets the instance to null.
334 *
335 * As opposed to cloneTo(), this method doesn't create a copy of the
336 * string.
337 *
338 * If the member string is empty, this allocates an empty BSTR in *pstr
339 * (i.e. makes it point to a new buffer with a null byte).
340 *
341 * @param pbstrDst The BSTR variable to detach the string to.
342 *
343 * @throws std::bad_alloc if we failed to allocate a new empty string.
344 */
345 void detachTo(BSTR *pbstrDst)
346 {
347 if (m_bstr)
348 {
349 *pbstrDst = m_bstr;
350 m_bstr = NULL;
351 }
352 else
353 {
354 // allocate null BSTR
355 *pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
356#ifdef RT_EXCEPTIONS_ENABLED
357 if (!*pbstrDst)
358 throw std::bad_alloc();
359#endif
360 }
361 }
362
363 /**
364 * A version of detachTo that does not throw exceptions on out-of-memory
365 * conditions, but instead returns E_OUTOFMEMORY.
366 *
367 * @param pbstrDst The BSTR variable to detach the string to.
368 * @returns S_OK or E_OUTOFMEMORY.
369 */
370 HRESULT detachToEx(BSTR *pbstrDst)
371 {
372 if (m_bstr)
373 {
374 *pbstrDst = m_bstr;
375 m_bstr = NULL;
376 }
377 else
378 {
379 // allocate null BSTR
380 *pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
381 if (!*pbstrDst)
382 return E_OUTOFMEMORY;
383 }
384 return S_OK;
385 }
386
387 /**
388 * Intended to pass instances as |BSTR| out parameters to methods.
389 * Takes the ownership of the returned data.
390 */
391 BSTR *asOutParam()
392 {
393 cleanup();
394 return &m_bstr;
395 }
396
397 /**
398 * Static immutable empty-string object. May be used for comparison purposes.
399 */
400 static const Bstr Empty;
401
402protected:
403
404 void cleanup()
405 {
406 if (m_bstr)
407 {
408 ::SysFreeString(m_bstr);
409 m_bstr = NULL;
410 }
411 }
412
413 /**
414 * Protected internal helper to copy a string. This ignores the previous object
415 * state, so either call this from a constructor or call cleanup() first.
416 *
417 * This variant copies from a zero-terminated UTF-16 string (which need not
418 * be a BSTR, i.e. need not have a length prefix).
419 *
420 * If the source is empty, this sets the member string to NULL.
421 *
422 * @param a_bstrSrc The source string. The caller guarantees
423 * that this is valid UTF-16.
424 *
425 * @throws std::bad_alloc - the object is representing an empty string.
426 */
427 void copyFrom(const OLECHAR *a_bstrSrc)
428 {
429 if (a_bstrSrc && *a_bstrSrc)
430 {
431 m_bstr = ::SysAllocString(a_bstrSrc);
432#ifdef RT_EXCEPTIONS_ENABLED
433 if (!m_bstr)
434 throw std::bad_alloc();
435#endif
436 }
437 else
438 m_bstr = NULL;
439 }
440
441 /**
442 * Protected internal helper to copy a string. This ignores the previous object
443 * state, so either call this from a constructor or call cleanup() first.
444 *
445 * This variant copies and converts from a zero-terminated UTF-8 string.
446 *
447 * If the source is empty, this sets the member string to NULL.
448 *
449 * @param a_pszSrc The source string. The caller guarantees
450 * that this is valid UTF-8.
451 *
452 * @throws std::bad_alloc - the object is representing an empty string.
453 */
454 void copyFrom(const char *a_pszSrc)
455 {
456 copyFromN(a_pszSrc, RTSTR_MAX);
457 }
458
459 /**
460 * Variant of copyFrom for sub-string constructors.
461 *
462 * @param a_pszSrc The source string. The caller guarantees
463 * that this is valid UTF-8.
464 * @param a_cchSrc The maximum number of chars (not codepoints) to
465 * copy. If you pass RTSTR_MAX it'll be exactly
466 * like copyFrom().
467 *
468 * @throws std::bad_alloc - the object is representing an empty string.
469 */
470 void copyFromN(const char *a_pszSrc, size_t a_cchSrc);
471
472 BSTR m_bstr;
473
474 friend class Utf8Str; /* to access our raw_copy() */
475};
476
477/* symmetric compare operators */
478inline bool operator==(CBSTR l, const Bstr &r) { return r.operator==(l); }
479inline bool operator!=(CBSTR l, const Bstr &r) { return r.operator!=(l); }
480inline bool operator==(BSTR l, const Bstr &r) { return r.operator==(l); }
481inline bool operator!=(BSTR l, const Bstr &r) { return r.operator!=(l); }
482
483
484
485
486/**
487 * String class used universally in Main for UTF-8 strings.
488 *
489 * This is based on RTCString, to which some functionality has been
490 * moved. Here we keep things that are specific to Main, such as conversions
491 * with UTF-16 strings (Bstr).
492 *
493 * Like RTCString, Utf8Str does not differentiate between NULL strings
494 * and empty strings. In other words, Utf8Str("") and Utf8Str(NULL) behave the
495 * same. In both cases, RTCString allocates no memory, reports
496 * a zero length and zero allocated bytes for both, and returns an empty
497 * C string from c_str().
498 *
499 * @note All Utf8Str methods ASSUMES valid UTF-8 or UTF-16 input strings.
500 * The VirtualBox policy in this regard is to validate strings coming
501 * from external sources before passing them to Utf8Str or Bstr.
502 */
503class Utf8Str : public RTCString
504{
505public:
506
507 Utf8Str() {}
508
509 Utf8Str(const RTCString &that)
510 : RTCString(that)
511 {}
512
513 Utf8Str(const char *that)
514 : RTCString(that)
515 {}
516
517 Utf8Str(const Bstr &that)
518 {
519 copyFrom(that.raw());
520 }
521
522 Utf8Str(CBSTR that, size_t a_cwcSize = RTSTR_MAX)
523 {
524 copyFrom(that, a_cwcSize);
525 }
526
527 Utf8Str(const char *a_pszSrc, size_t a_cchSrc)
528 : RTCString(a_pszSrc, a_cchSrc)
529 {
530 }
531
532 /**
533 * Constructs a new string given the format string and the list of the
534 * arguments for the format string.
535 *
536 * @param a_pszFormat Pointer to the format string (UTF-8),
537 * @see pg_rt_str_format.
538 * @param a_va Argument vector containing the arguments
539 * specified by the format string.
540 * @sa RTCString::printfV
541 */
542 Utf8Str(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
543 : RTCString(a_pszFormat, a_va)
544 {
545 }
546
547 Utf8Str& operator=(const RTCString &that)
548 {
549 RTCString::operator=(that);
550 return *this;
551 }
552
553 Utf8Str& operator=(const char *that)
554 {
555 RTCString::operator=(that);
556 return *this;
557 }
558
559 Utf8Str& operator=(const Bstr &that)
560 {
561 cleanup();
562 copyFrom(that.raw());
563 return *this;
564 }
565
566 Utf8Str& operator=(CBSTR that)
567 {
568 cleanup();
569 copyFrom(that);
570 return *this;
571 }
572
573 /**
574 * Extended assignment method that returns a COM status code instead of an
575 * exception on failure.
576 *
577 * @returns S_OK or E_OUTOFMEMORY.
578 * @param a_rSrcStr The source string
579 */
580 HRESULT assignEx(Utf8Str const &a_rSrcStr)
581 {
582 return copyFromExNComRC(a_rSrcStr.m_psz, 0, a_rSrcStr.m_cch);
583 }
584
585 /**
586 * Extended assignment method that returns a COM status code instead of an
587 * exception on failure.
588 *
589 * @returns S_OK, E_OUTOFMEMORY or E_INVALIDARG.
590 * @param a_rSrcStr The source string
591 * @param a_offSrc The character (byte) offset of the substring.
592 * @param a_cchSrc The number of characters (bytes) to copy from the source
593 * string.
594 */
595 HRESULT assignEx(Utf8Str const &a_rSrcStr, size_t a_offSrc, size_t a_cchSrc)
596 {
597 if ( a_offSrc + a_cchSrc > a_rSrcStr.m_cch
598 || a_offSrc > a_rSrcStr.m_cch)
599 return E_INVALIDARG;
600 return copyFromExNComRC(a_rSrcStr.m_psz, a_offSrc, a_cchSrc);
601 }
602
603 /**
604 * Extended assignment method that returns a COM status code instead of an
605 * exception on failure.
606 *
607 * @returns S_OK or E_OUTOFMEMORY.
608 * @param a_pcszSrc The source string
609 */
610 HRESULT assignEx(const char *a_pcszSrc)
611 {
612 return copyFromExNComRC(a_pcszSrc, 0, a_pcszSrc ? strlen(a_pcszSrc) : 0);
613 }
614
615 /**
616 * Extended assignment method that returns a COM status code instead of an
617 * exception on failure.
618 *
619 * @returns S_OK or E_OUTOFMEMORY.
620 * @param a_pcszSrc The source string
621 * @param a_cchSrc The number of characters (bytes) to copy from the source
622 * string.
623 */
624 HRESULT assignEx(const char *a_pcszSrc, size_t a_cchSrc)
625 {
626 return copyFromExNComRC(a_pcszSrc, 0, a_cchSrc);
627 }
628
629 RTMEMEF_NEW_AND_DELETE_OPERATORS();
630
631#if defined(VBOX_WITH_XPCOM)
632 /**
633 * Intended to assign instances to |char *| out parameters from within the
634 * interface method. Transfers the ownership of the duplicated string to the
635 * caller.
636 *
637 * This allocates a single 0 byte in the target if the member string is empty.
638 *
639 * This uses XPCOM memory allocation and thus only works on XPCOM. MSCOM doesn't
640 * like char* strings anyway.
641 */
642 void cloneTo(char **pstr) const;
643
644 /**
645 * A version of cloneTo that does not throw allocation errors but returns
646 * E_OUTOFMEMORY instead.
647 * @returns S_OK or E_OUTOFMEMORY (COM status codes).
648 */
649 HRESULT cloneToEx(char **pstr) const;
650#endif
651
652 /**
653 * Intended to assign instances to |BSTR| out parameters from within the
654 * interface method. Transfers the ownership of the duplicated string to the
655 * caller.
656 */
657 void cloneTo(BSTR *pstr) const
658 {
659 if (pstr)
660 {
661 Bstr bstr(*this);
662 bstr.cloneTo(pstr);
663 }
664 }
665
666 /**
667 * A version of cloneTo that does not throw allocation errors but returns
668 * E_OUTOFMEMORY instead.
669 *
670 * @param pbstr Where to store a clone of the string.
671 * @returns S_OK or E_OUTOFMEMORY (COM status codes).
672 */
673 HRESULT cloneToEx(BSTR *pbstr) const
674 {
675 if (!pbstr)
676 return S_OK;
677 Bstr bstr(*this);
678 return bstr.detachToEx(pbstr);
679 }
680
681 /**
682 * Safe assignment from BSTR.
683 *
684 * @param pbstrSrc The source string.
685 * @returns S_OK or E_OUTOFMEMORY (COM status codes).
686 */
687 HRESULT cloneEx(CBSTR pbstrSrc)
688 {
689 cleanup();
690 return copyFromEx(pbstrSrc);
691 }
692
693 /**
694 * Removes a trailing slash from the member string, if present.
695 * Calls RTPathStripTrailingSlash() without having to mess with mutableRaw().
696 */
697 Utf8Str& stripTrailingSlash();
698
699 /**
700 * Removes a trailing filename from the member string, if present.
701 * Calls RTPathStripFilename() without having to mess with mutableRaw().
702 */
703 Utf8Str& stripFilename();
704
705 /**
706 * Removes the path component from the member string, if present.
707 * Calls RTPathFilename() without having to mess with mutableRaw().
708 */
709 Utf8Str& stripPath();
710
711 /**
712 * Removes a trailing file name suffix from the member string, if present.
713 * Calls RTPathStripSuffix() without having to mess with mutableRaw().
714 */
715 Utf8Str& stripSuffix();
716
717 // Parse key=value pairs from string
718 size_t parseKeyValue(Utf8Str &key, Utf8Str &value, size_t pos = 0, const Utf8Str &pairSeparator = ",", const Utf8Str &keyValueSeparator = "=") const;
719
720 /**
721 * Static immutable empty-string object. May be used for comparison purposes.
722 */
723 static const Utf8Str Empty;
724protected:
725
726 void copyFrom(CBSTR a_pbstr, size_t a_cwcMax = RTSTR_MAX);
727 HRESULT copyFromEx(CBSTR a_pbstr);
728 HRESULT copyFromExNComRC(const char *a_pcszSrc, size_t a_offSrc, size_t a_cchSrc);
729
730 friend class Bstr; /* to access our raw_copy() */
731};
732
733/**
734 * Class with RTCString::printf as constructor for your convenience.
735 *
736 * Constructing a Utf8Str string object from a format string and a variable
737 * number of arguments can easily be confused with the other Utf8Str
738 * constructures, thus this child class.
739 *
740 * The usage of this class is like the following:
741 * @code
742 Utf8StrFmt strName("program name = %s", argv[0]);
743 @endcode
744 */
745class Utf8StrFmt : public Utf8Str
746{
747public:
748
749 /**
750 * Constructs a new string given the format string and the list of the
751 * arguments for the format string.
752 *
753 * @param a_pszFormat Pointer to the format string (UTF-8),
754 * @see pg_rt_str_format.
755 * @param ... Ellipsis containing the arguments specified by
756 * the format string.
757 */
758 explicit Utf8StrFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
759 {
760 va_list va;
761 va_start(va, a_pszFormat);
762 printfV(a_pszFormat, va);
763 va_end(va);
764 }
765
766 RTMEMEF_NEW_AND_DELETE_OPERATORS();
767
768protected:
769 Utf8StrFmt()
770 { }
771
772private:
773};
774
775/**
776 * The BstrFmt class is a shortcut to <tt>Bstr(Utf8StrFmt(...))</tt>.
777 */
778class BstrFmt : public Bstr
779{
780public:
781
782 /**
783 * Constructs a new string given the format string and the list of the
784 * arguments for the format string.
785 *
786 * @param aFormat printf-like format string (in UTF-8 encoding).
787 * @param ... List of the arguments for the format string.
788 */
789 explicit BstrFmt(const char *aFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
790 {
791 va_list args;
792 va_start(args, aFormat);
793 copyFrom(Utf8Str(aFormat, args).c_str());
794 va_end(args);
795 }
796
797 RTMEMEF_NEW_AND_DELETE_OPERATORS();
798};
799
800/**
801 * The BstrFmtVA class is a shortcut to <tt>Bstr(Utf8Str(format,va))</tt>.
802 */
803class BstrFmtVA : public Bstr
804{
805public:
806
807 /**
808 * Constructs a new string given the format string and the list of the
809 * arguments for the format string.
810 *
811 * @param aFormat printf-like format string (in UTF-8 encoding).
812 * @param aArgs List of arguments for the format string
813 */
814 BstrFmtVA(const char *aFormat, va_list aArgs) RT_IPRT_FORMAT_ATTR(1, 0)
815 {
816 copyFrom(Utf8Str(aFormat, aArgs).c_str());
817 }
818
819 RTMEMEF_NEW_AND_DELETE_OPERATORS();
820};
821
822} /* namespace com */
823
824/** @} */
825
826#endif /* !___VBox_com_string_h */
827
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