📄 string_access.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: atlstl/string_access.hpp (formerly atlstl_string_access.h)
*
* Purpose: Contains classes and functions for dealing with OLE/COM strings.
*
* Created: 27th May 2002
* Updated: 13th 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 atlstl/string_access.hpp
///
/// Contains classes and functions for dealing with OLE/COM strings.
#ifndef ATLSTL_INCL_ATLSTL_HPP_STRING_ACCESS
#define ATLSTL_INCL_ATLSTL_HPP_STRING_ACCESS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define ATLSTL_VER_ATLSTL_HPP_STRING_ACCESS_MAJOR 3
# define ATLSTL_VER_ATLSTL_HPP_STRING_ACCESS_MINOR 2
# define ATLSTL_VER_ATLSTL_HPP_STRING_ACCESS_REVISION 4
# define ATLSTL_VER_ATLSTL_HPP_STRING_ACCESS_EDIT 85
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef ATLSTL_INCL_ATLSTL_HPP_ATLSTL
# include <atlstl/atlstl.hpp>
#endif /* !ATLSTL_INCL_ATLSTL_HPP_ATLSTL */
#ifndef STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS
# include <stlsoft/string_access.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
# include <stlsoft/string/cstring_maker.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER */
#ifdef STLSOFT_UNITTEST
# include <wchar.h>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _ATLSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::atlstl */
namespace atlstl
{
# else
/* Define stlsoft::atlstl_project */
namespace stlsoft
{
namespace atlstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_ATLSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/* CWindow */
#ifdef __ATLWIN_H__
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_null() function, such that the window text of a given window
/// may be accessed as a null-terminated string.
class c_str_ptr_null_CWindow_proxy
{
private:
typedef cstring_maker<TCHAR> string_maker_type;
typedef c_str_ptr_null_CWindow_proxy class_type;
// Construction
public:
/// Constructs an instance of the proxy from the given CWindow instance
///
/// \param w The CWindow instance from which the text will be retrieved
ss_explicit_k c_str_ptr_null_CWindow_proxy(CWindow const &w)
{
int length = (NULL == w.m_hWnd) ? 0 : w.GetWindowTextLength();
if(length == 0)
{
m_buffer = NULL;
}
else
{
m_buffer = string_maker_type::alloc(length);
if(NULL != m_buffer)
{
w.GetWindowText(m_buffer, length + 1);
}
}
}
#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_ptr_null_CWindow_proxy(class_type &rhs)
: m_buffer(rhs.m_buffer)
{
rhs.m_buffer = 0;
}
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
// Copy constructor
c_str_ptr_null_CWindow_proxy(class_type const &rhs)
: m_buffer(string_maker_type::dup_null(rhs.m_buffer))
{}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
/// Releases any storage aquired by the proxy
~c_str_ptr_null_CWindow_proxy() stlsoft_throw_0()
{
string_maker_type::free(m_buffer);
}
// Accessors
public:
/// Returns a null-terminated string representing the window contents, or
/// NULL if the window contains no text.
operator LPCTSTR () const
{
return m_buffer;
}
// Members
private:
LPTSTR m_buffer;
// 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() function, such that the window text of a given window may be
/// accessed as a null-terminated string.
class c_str_ptr_CWindow_proxy
{
private:
typedef cstring_maker<TCHAR> string_maker_type;
typedef c_str_ptr_CWindow_proxy class_type;
// Construction
public:
/// Constructs an instance of the proxy from the given CWindow instance
///
/// \param w The CWindow instance from which the text will be retrieved
ss_explicit_k c_str_ptr_CWindow_proxy(CWindow const &w)
{
int length = (NULL == w.m_hWnd) ? 0 : w.GetWindowTextLength();
if(length == 0)
{
m_buffer = string_maker_type::dup("");
}
else
{
m_buffer = string_maker_type::alloc(length);
if(NULL != m_buffer)
{
w.GetWindowText(m_buffer, length + 1);
}
}
}
#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_ptr_CWindow_proxy(class_type &rhs)
: m_buffer(rhs.m_buffer)
{
rhs.m_buffer = 0;
}
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
// Copy constructor
c_str_ptr_CWindow_proxy(class_type const &rhs)
: m_buffer(string_maker_type::dup_null(rhs.m_buffer))
{}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
/// Releases any storage aquired by the proxy
~c_str_ptr_CWindow_proxy() stlsoft_throw_0()
{
string_maker_type::free(m_buffer);
}
// Accessors
public:
/// Returns a null-terminated string representing the window contents, or
/// the empty string "" if the window contains no text.
operator LPCTSTR () const
{
return (NULL == m_buffer) ? "" : m_buffer;
}
// Members
protected:
LPTSTR m_buffer;
// Not to be implemented
protected:
void operator =(class_type const &rhs);
};
#endif /* __ATLWIN_H__ */
/* /////////////////////////////////////////////////////////////////////////
* Equivalence testing
*/
#ifdef __ATLWIN_H__
inline as_bool_t operator ==(LPCTSTR lhs, c_str_ptr_null_CWindow_proxy const &rhs)
{
return lhs == static_cast<LPCTSTR>(rhs);
}
inline as_bool_t operator ==(c_str_ptr_null_CWindow_proxy const &lhs, LPCTSTR rhs)
{
return static_cast<LPCTSTR>(lhs) == rhs;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -