string.h revision 16223988a6f272e21cd0c79d89ab6d3b8d767028
/* $Id$ */
/** @file
* MS COM / XPCOM Abstraction Layer:
* Smart string classes declaration
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#ifndef ___VBox_com_string_h
#define ___VBox_com_string_h
/* Make sure all the stdint.h macros are included - must come first! */
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#if defined (VBOX_WITH_XPCOM)
# include <nsMemory.h>
#endif
#include <iprt/cpputils.h>
{
/**
* Helper class that represents the |BSTR| type and hides platform-specific
* implementation details.
*
* This class uses COM/XPCOM-provided memory management routines to allocate
* and free string buffers. This makes it possible to:
* their values to callers through component methods' output parameters
* using the #cloneTo() operation;
* - adopt (take ownership of) string buffers returned in output parameters
* of COM methods using the #asOutParam() operation and correctly free them
* afterwards.
*/
{
typedef const BSTR ConstString;
{
}
/** Shortcut that calls #alloc(aSize) right after object creation. */
{
if (bstr)
{
::SysFreeString (bstr);
}
return *this;
}
{
{
::SysFreeString (bstr);
}
return *this;
}
/**
* Allocates memory for a string capable to store \a aSize - 1 characters
* plus the terminating zero character. If \a aSize is zero, or if a
* memory allocation error occurs, this object will become null.
*/
{
setNull();
if (aSize)
{
if (bstr)
bstr [0] = 0;
}
return *this;
}
{
}
{
}
{
}
{
}
{
}
/** Intended to to pass instances as |BSTR| input parameters to methods. */
/** The same as operator const BSTR(), but for situations where the compiler
cannot typecast implicitly (for example, in printf() argument list). */
/**
* Returns a non-const raw pointer that allows to modify the string directly.
* @warning
* Be sure not to modify data beyond the allocated memory! The
* guaranteed size of the allocated memory is at least #length()
* bytes after creation and after every assignment operation.
*/
/**
* Intended to assign copies of instances to |BSTR| out parameters from
* within the interface method. Transfers the ownership of the duplicated
* string to the caller.
*/
{
if (pstr)
{
}
return *this;
}
/**
* Intended to assign instances to |BSTR| out parameters from within the
* interface method. Transfers the ownership of the original string to the
* caller and resets the instance to null.
*
* As opposed to cloneTo(), this method doesn't create a copy of the
* string.
*/
{
return *this;
}
/**
* Intended to assign copies of instances to |char *| out parameters from
* within the interface method. Transfers the ownership of the duplicated
* string to the caller.
*/
/**
* Intended to pass instances as |BSTR| out parameters to methods.
* Takes the ownership of the returned data.
*/
/**
* Static immutable null object. May be used for comparison purposes.
*/
{
{
setNull();
}
}
{
if (rs)
}
{
if (rs)
{
::RTStrToUtf16 (rs, &s);
::RTUtf16Free (s);
}
}
};
// symmetric compare operators
////////////////////////////////////////////////////////////////////////////////
/**
* Helper class that represents UTF8 (|char *|) strings. Useful in
* conjunction with Bstr to simplify conversions beetween UTF16 (|BSTR|)
* and UTF8.
*
* This class uses COM/XPCOM-provided memory management routines to allocate
* and free string buffers. This makes it possible to:
* their values to callers through component methods' output parameters
* using the #cloneTo() operation;
* - adopt (take ownership of) string buffers returned in output parameters
* of COM methods using the #asOutParam() operation and correctly free them
* afterwards.
*/
{
typedef char *String;
typedef const char *ConstString;
/** Shortcut that calls #alloc(aSize) right after object creation. */
{
setNull();
return *this;
}
{
setNull();
return *this;
}
{
if (str)
{
#if !defined (VBOX_WITH_XPCOM)
#else
#endif
}
return *this;
}
{
{
#if !defined (VBOX_WITH_XPCOM)
#else
#endif
}
return *this;
}
/**
* Allocates memory for a string capable to store \a aSize - 1 characters
* plus the terminating zero character. If \a aSize is zero, or if a
* memory allocation error occurs, this object will become null.
*/
{
setNull();
if (aSize)
{
#if !defined (VBOX_WITH_XPCOM)
#else
#endif
if (str)
str [0] = 0;
}
return *this;
}
int compare (const char *s) const
{
if (str == s)
return 0;
return -1;
if (s == NULL)
return 1;
}
/** Intended to to pass instances as input (|char *|) parameters to methods. */
/** The same as operator const char *(), but for situations where the compiler
cannot typecast implicitly (for example, in printf() argument list). */
/**
* Returns a non-const raw pointer that allows to modify the string directly.
* @warning
* Be sure not to modify data beyond the allocated memory! The
* guaranteed size of the allocated memory is at least #length()
* bytes after creation and after every assignment operation.
*/
char *mutableRaw() { return str; }
/**
* Intended to assign instances to |char *| out parameters from within the
* interface method. Transfers the ownership of the duplicated string to the
* caller.
*/
{
if (pstr)
{
}
return *this;
}
/**
* Intended to assign instances to |char *| out parameters from within the
* interface method. Transfers the ownership of the original string to the
* caller and resets the instance to null.
*
* As opposed to cloneTo(), this method doesn't create a copy of the
* string.
*/
{
return *this;
}
/**
* Intended to assign instances to |BSTR| out parameters from within the
* interface method. Transfers the ownership of the duplicated string to the
* caller.
*/
{
if (pstr)
{
}
return *this;
}
/**
* Intended to pass instances as out (|char **|) parameters to methods.
* Takes the ownership of the returned data.
*/
/**
* Static immutable null object. May be used for comparison purposes.
*/
void safe_assign (const char *s)
{
if (str != s)
{
setNull();
}
}
{
if (rs)
#if !defined (VBOX_WITH_XPCOM)
#else
#endif
}
{
if (rs)
{
#if !defined (VBOX_WITH_XPCOM)
#else
char *s = NULL;
::RTStrFree (s);
#endif
}
}
char *str;
};
// symmetric compare operators
// work around error C2593 of the stupid MSVC 7.x ambiguity resolver
////////////////////////////////////////////////////////////////////////////////
// inlined Bstr members that depend on Utf8Str
{
setNull();
return *this;
}
{
setNull();
return *this;
}
{
if (pstr) {
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/**
* This class is a printf-like formatter for Utf8Str strings. Its purpose is
* to construct Utf8Str objects from a format string and a list of arguments
* for the format string.
*
* The usage of this class is like the following:
* <code>
* Utf8StrFmt string ("program name = %s", argv[0]);
* </code>
*/
{
/**
* Constructs a new string given the format string and the list
* of the arguments for the format string.
*
* @param format printf-like format string (in UTF-8 encoding)
* @param ... list of the arguments for the format string
*/
{
}
Utf8StrFmt() {}
};
/**
* This class is a vprintf-like formatter for Utf8Str strings. It is
* identical to Utf8StrFmt except that its constructor takes a va_list
* argument instead of ellipsis.
*
* Note that a separate class is necessary because va_list is defined as
* |char *| on most platforms. For this reason, if we had two overloaded
* constructors in Utf8StrFmt (one taking ellipsis and another one taking
* va_list) then composing a constructor call using exactly two |char *|
* arguments would cause the compiler to use the va_list overload instead of
* the ellipsis one which is obviously wrong. The compiler would choose
* va_list because ellipsis has the lowest rank when it comes to resolving
* overloads, as opposed to va_list which is an exact match for |char *|.
*/
{
/**
* Constructs a new string given the format string and the list
* of the arguments for the format string.
*
* @param format printf-like format string (in UTF-8 encoding)
* @param args list of arguments for the format string
*/
};
} /* namespace com */
#endif /* ___VBox_com_string_h */