string_access.hpp
来自「用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、W」· HPP 代码 · 共 1,042 行 · 第 1/3 页
HPP
1,042 行
/* /////////////////////////////////////////////////////////////////////////
* File: comstl/string_access.hpp (formerly comstl_string_access.h)
*
* Purpose: Contains classes and functions for dealing with OLE/COM strings.
*
* Created: 24th May 2002
* Updated: 10th June 2006
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2002-2006, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/// \file comstl/string_access.hpp
///
/// Contains classes and functions for dealing with OLE/COM strings.
#ifndef COMSTL_INCL_COMSTL_HPP_STRING_ACCESS
#define COMSTL_INCL_COMSTL_HPP_STRING_ACCESS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define COMSTL_VER_COMSTL_HPP_STRING_ACCESS_MAJOR 4
# define COMSTL_VER_COMSTL_HPP_STRING_ACCESS_MINOR 2
# define COMSTL_VER_COMSTL_HPP_STRING_ACCESS_REVISION 3
# define COMSTL_VER_COMSTL_HPP_STRING_ACCESS_EDIT 93
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef COMSTL_INCL_COMSTL_H_COMSTL
# include <comstl/comstl.h>
#endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
#ifndef COMSTL_INCL_COMSTL_H_BSTR_FUNCTIONS
# include <comstl/bstr_functions.h>
#endif /* !COMSTL_INCL_COMSTL_H_BSTR_FUNCTIONS */
#ifndef STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS
# include <stlsoft/string_access.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS */
#include <wchar.h>
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _COMSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::comstl */
namespace comstl
{
# else
/* Define stlsoft::comstl_project */
namespace stlsoft
{
namespace comstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_COMSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Constants
*/
const cs_size_t COMSTL_CCH_GUID = 38;
/* /////////////////////////////////////////////////////////////////////////
* Functions
*/
inline cs_size_t guid2string_w(GUID const &guid, cs_char_w_t buff[1 + COMSTL_CCH_GUID])
{
return static_cast<cs_size_t>(StringFromGUID2(guid, buff, 1 + COMSTL_CCH_GUID));
}
inline cs_size_t guid2string_a(GUID const &guid, cs_char_a_t buff[1 + COMSTL_CCH_GUID])
{
const cs_size_t COMSTL_CCH_GUID_AND_NULL = COMSTL_CCH_GUID + 1;
/* Don't ask! */
#ifdef STLSOFT_COMPILER_IS_BORLAND
int __buff[COMSTL_CCH_GUID_AND_NULL];
cs_char_w_t *_buff = (wchar_t *)__buff;
#else /* ? compiler */
cs_char_w_t _buff[COMSTL_CCH_GUID_AND_NULL];
#endif /* compiler */
cs_size_t cch = guid2string_w(guid, _buff);
::WideCharToMultiByte(0, 0, _buff, COMSTL_CCH_GUID_AND_NULL, buff, COMSTL_CCH_GUID_AND_NULL, 0, 0);
return cch;
}
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/* GUID */
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_null() function, such that the text of a given GUID
/// may be accessed as a null-terminated string.
template <ss_typename_param_k C>
class c_str_ptr_GUID_proxy
{
public:
typedef C char_type;
typedef c_str_ptr_GUID_proxy<C> class_type;
// Construction
public:
/// Constructs an instance of the proxy from the given GUID instance
///
/// \param guid The GUID instance from which the text will be retrieved
ss_explicit_k c_str_ptr_GUID_proxy(GUID const &guid);
/// Copy constructor
c_str_ptr_GUID_proxy(class_type const &rhs)
: m_(m_buffer)
{
for(cs_size_t i = 0; i < STLSOFT_NUM_ELEMENTS(m_buffer); ++i)
{
m_buffer[i] = rhs.m_buffer[i];
}
}
// Accessors
public:
/// Returns a null-terminated string representing the GUID contents
operator char_type const *() const
{
return m_buffer;
}
// Members
private:
char_type const *const m_;
char_type m_buffer[1 + COMSTL_CCH_GUID];
// Not to be implemented
private:
void operator =(class_type const &rhs);
};
STLSOFT_TEMPLATE_SPECIALISATION
inline c_str_ptr_GUID_proxy<cs_char_a_t>::c_str_ptr_GUID_proxy(GUID const &guid)
: m_(m_buffer)
{
#ifndef STLSOFT_COMPILER_IS_BORLAND
COMSTL_STATIC_ASSERT(STLSOFT_NUM_ELEMENTS(m_buffer) > COMSTL_CCH_GUID);
#endif /* compiler */
guid2string_a(guid, m_buffer);
}
STLSOFT_TEMPLATE_SPECIALISATION
inline c_str_ptr_GUID_proxy<cs_char_w_t>::c_str_ptr_GUID_proxy(GUID const &guid)
: m_(m_buffer)
{
#ifndef STLSOFT_COMPILER_IS_BORLAND
COMSTL_STATIC_ASSERT(STLSOFT_NUM_ELEMENTS(m_buffer) > COMSTL_CCH_GUID);
#endif /* compiler */
guid2string_w(guid, m_buffer);
}
/* VARIANT */
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_null() function, such that the text of a given variant
/// may be accessed as a null-terminated string.
class c_str_null_VARIANT_proxy
{
public:
typedef c_str_null_VARIANT_proxy class_type;
// Construction
public:
/// Constructs an instance of the proxy from the given BSTR
///
/// \param s The BSTR from which the text will be retrieved
ss_explicit_k c_str_null_VARIANT_proxy(const BSTR s)
: m_bstr(s)
, m_own(false)
{}
/// Constructs an instance of the proxy from the given BSTR
///
/// \param ps Pointer to the BSTR from which the text will be retrieved
ss_explicit_k c_str_null_VARIANT_proxy(BSTR *ps)
: m_bstr(*ps)
, m_own(true)
{
if(m_own)
{
*ps = NULL;
}
}
/// Default constructor
c_str_null_VARIANT_proxy()
: m_bstr(NULL)
, m_own(false)
{}
#ifdef STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT
/// Move constructor
///
/// This <a href = "http://synesis.com.au/resources/articles/cpp/movectors.pdf">move constructor</a>
/// is for circumstances when the compiler does not, or cannot, apply the
/// return value optimisation. It causes the contents of \c rhs to be
/// transferred into the constructing instance. This is completely safe
/// because the \c rhs instance will never be accessed in its own right, so
/// does not need to maintain ownership of its contents.
c_str_null_VARIANT_proxy(class_type &rhs)
: m_bstr(rhs.m_bstr)
, m_own(rhs.m_own)
{
move_lhs_from_rhs(rhs).m_bstr = NULL;
move_lhs_from_rhs(rhs).m_own = false;
}
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
// Copy constructor
c_str_null_VARIANT_proxy(class_type const &rhs)
: m_bstr(bstr_dup(rhs.m_bstr))
{}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
/// Releases any storage aquired by the proxy
~c_str_null_VARIANT_proxy() stlsoft_throw_0()
{
if(m_own)
{
::SysFreeString(m_bstr);
}
}
// Accessors
public:
/// Returns a null-terminated string representing the VARIANT contents, or
/// NULL if the VARIANT contents cannot be converted to text.
operator LPCOLESTR () const
{
return m_bstr;
}
// Members
private:
BSTR m_bstr;
cs_bool_t m_own;
// Not to be implemented
private:
void operator =(class_type const &rhs);
};
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_w() function, such that the text of a given variant
/// may be accessed as a null-terminated string.
class c_str_VARIANT_proxy_w
{
public:
typedef c_str_VARIANT_proxy_w class_type;
// Construction
public:
/// Constructs an instance of the proxy from the given BSTR
///
/// \param s The BSTR from which the text will be retrieved
ss_explicit_k c_str_VARIANT_proxy_w(BSTR &s)
: m_bstr(s)
{
s = NULL;
}
#ifdef STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT
/// Move constructor
///
/// This <a href = "http://synesis.com.au/resources/articles/cpp/movectors.pdf">move constructor</a>
/// is for circumstances when the compiler does not, or cannot, apply the
/// return value optimisation. It causes the contents of \c rhs to be
/// transferred into the constructing instance. This is completely safe
/// because the \c rhs instance will never be accessed in its own right, so
/// does not need to maintain ownership of its contents.
c_str_VARIANT_proxy_w(class_type &rhs)
: m_bstr(rhs.m_bstr)
{
move_lhs_from_rhs(rhs).m_bstr = NULL;
}
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
// Copy constructor
c_str_VARIANT_proxy_w(class_type const &rhs)
: m_bstr(bstr_dup(rhs.m_bstr))
{}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
/// Releases any storage aquired by the proxy
~c_str_VARIANT_proxy_w() stlsoft_throw_0()
{
::SysFreeString(m_bstr);
}
// Accessors
public:
/// Returns a null-terminated string representing the VARIANT contents.
operator LPCOLESTR () const
{
return (m_bstr == NULL) ? L"" : m_bstr;
}
// Members
private:
BSTR m_bstr;
// Not to be implemented
private:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?