⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string_access.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:
{
    typedef cstring_maker<WCHAR>                string_maker_type;
public:
    typedef c_str_ptr_LSA_UNICODE_STRING_proxy  class_type;

// Construction
public:
    /// Constructs an instance of the proxy from the given LSA_UNICODE_STRING instance
    ///
    /// \param s The LSA_UNICODE_STRING instance from which the text will be retrieved
    ss_explicit_k c_str_ptr_LSA_UNICODE_STRING_proxy(const LSA_UNICODE_STRING &s)
        : m_buffer(string_maker_type::alloc(s.Length))
    {
        if(NULL != m_buffer)
        {
            wcsncpy(m_buffer, s.Buffer, s.Length);
            m_buffer[s.Length] = L'\0';
        }
    }

#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_LSA_UNICODE_STRING_proxy(class_type &rhs)
        : m_buffer(rhs.m_buffer)
    {
        rhs.m_buffer = NULL;
    }
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
    // Copy constructor
    c_str_ptr_LSA_UNICODE_STRING_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_LSA_UNICODE_STRING_proxy() stlsoft_throw_0()
    {
        string_maker_type::free(m_buffer);
    }

// Accessors
public:
    /// Returns a null-terminated string representing the string contents, or
    /// the empty string "" if the string has no contents.
    operator LPCWSTR () const
    {
        return m_buffer;
    }

// Members
private:
    LPWSTR  m_buffer;

// Not to be implemented
private:
    void operator =(class_type const &rhs);
};
#endif /* _NTSECAPI_ */

#ifdef _NTSECAPI_
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_null() function, such that the text of a given LSA_UNICODE_STRING
/// string may be accessed as a null-terminated string.
class c_str_ptr_null_LSA_UNICODE_STRING_proxy
{
    typedef cstring_maker<WCHAR>                    string_maker_type;
public:
    typedef c_str_ptr_null_LSA_UNICODE_STRING_proxy class_type;

// Construction
public:
    /// Constructs an instance of the proxy from the given LSA_UNICODE_STRING instance
    ///
    /// \param s The LSA_UNICODE_STRING instance from which the text will be retrieved
    ss_explicit_k c_str_ptr_null_LSA_UNICODE_STRING_proxy(const LSA_UNICODE_STRING &s)
        : m_buffer((s.Length != 0) ? string_maker_type::alloc(s.Length) : NULL)
    {
        if(m_buffer != NULL)
        {
            wcsncpy(m_buffer, s.Buffer, s.Length);
            m_buffer[s.Length] = L'\0';
        }
    }

#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_LSA_UNICODE_STRING_proxy(class_type &rhs)
        : m_buffer(rhs.m_buffer)
    {
        rhs.m_buffer = NULL;
    }
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
    // Copy constructor
    c_str_ptr_null_LSA_UNICODE_STRING_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_LSA_UNICODE_STRING_proxy() stlsoft_throw_0()
    {
        string_maker_type::free(m_buffer);
    }

// Accessors
public:
    /// Returns a null-terminated string representing the string contents, or
    /// NULL if the string has no contents.
    operator LPCWSTR () const
    {
        return m_buffer;
    }

// Members
private:
    LPWSTR  m_buffer;

// Not to be implemented
private:
    void operator =(class_type const &rhs);
};
#endif /* _NTSECAPI_ */

/* /////////////////////////////////////////////////////////////////////////
 * IOStream compatibility
 */

template<   ss_typename_param_k C
        ,   ss_typename_param_k S
        >
inline S &operator <<(S & s, c_str_ptr_null_HWND_proxy<C> const &shim)
{
    s << static_cast<C const *>(shim);

    return s;
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k S
        >
inline S &operator <<(S & s, c_str_ptr_HWND_proxy<C> const &shim)
{
    s << static_cast<C const *>(shim);

    return s;
}

#ifdef _NTSECAPI_
template<ss_typename_param_k S>
inline S &operator <<(S & s, c_str_ptr_LSA_UNICODE_STRING_proxy const &shim)
{
    s << static_cast<LPCWSTR>(shim);

    return s;
}

template<ss_typename_param_k S>
inline S &operator <<(S & s, c_str_ptr_null_LSA_UNICODE_STRING_proxy const &shim)
{
    s << static_cast<LPCWSTR>(shim);

    return s;
}
#endif /* _NTSECAPI_ */

/* /////////////////////////////////////////////////////////////////////////
 * c_str_ptr_null
 *
 * This can be applied to an expression, and the return value is either a
 * pointer to the character string or NULL.
 */

/* HWND */
/// \brief Returns the corresponding C-string pointer of the window \c h, or a null pointer
inline c_str_ptr_null_HWND_proxy<ws_char_a_t> c_str_ptr_null_a(HWND h)
{
    return c_str_ptr_null_HWND_proxy<ws_char_a_t>(h);
}

/// \brief Returns the corresponding C-string pointer of the window \c h, or a null pointer
inline c_str_ptr_null_HWND_proxy<ws_char_w_t> c_str_ptr_null_w(HWND h)
{
    return c_str_ptr_null_HWND_proxy<ws_char_w_t>(h);
}

/// \brief Returns the corresponding C-string pointer of the window \c h, or a null pointer
inline c_str_ptr_null_HWND_proxy<TCHAR> c_str_ptr_null(HWND h)
{
    return c_str_ptr_null_HWND_proxy<TCHAR>(h);
}

/* LSA_UNICODE_STRING */
#ifdef _NTSECAPI_
/// \brief Returns the corresponding C-string pointer of the LSA_UNICODE_STRING \c s, or a null pointer
inline c_str_ptr_null_LSA_UNICODE_STRING_proxy c_str_ptr_null(const LSA_UNICODE_STRING &s)
{
    return c_str_ptr_null_LSA_UNICODE_STRING_proxy(s);
}
#endif /* _NTSECAPI_ */

/* /////////////////////////////////////////////////////////////////////////
 * c_str_ptr
 *
 * This can be applied to an expression, and the return value is either a
 * pointer to the character string or to an empty string.
 */

/* HWND */
/// \brief Returns the corresponding C-string pointer of the window \c h
inline c_str_ptr_HWND_proxy<ws_char_a_t> c_str_ptr_a(HWND h)
{
    return c_str_ptr_HWND_proxy<ws_char_a_t>(h);
}

/// \brief Returns the corresponding C-string pointer of the window \c h
inline c_str_ptr_HWND_proxy<ws_char_w_t> c_str_ptr_w(HWND h)
{
    return c_str_ptr_HWND_proxy<ws_char_w_t>(h);
}

/// \brief Returns the corresponding C-string pointer of the window \c h
inline c_str_ptr_HWND_proxy<TCHAR> c_str_ptr(HWND h)
{
    return c_str_ptr_HWND_proxy<TCHAR>(h);
}

/* LSA_UNICODE_STRING */
#ifdef _NTSECAPI_
/// \brief Returns the corresponding C-string pointer of the LSA_UNICODE_STRING \c s
inline c_str_ptr_LSA_UNICODE_STRING_proxy c_str_ptr(const LSA_UNICODE_STRING &s)
{
    return c_str_ptr_LSA_UNICODE_STRING_proxy(s);
}
#endif /* _NTSECAPI_ */

/* /////////////////////////////////////////////////////////////////////////
 * c_str_data
 *
 * This can be applied to an expression, and the return value is either a
 * pointer to the character string or to an empty string.
 */

/// \brief Returns the corresponding C-string pointer of the window \c h
inline c_str_ptr_HWND_proxy<ws_char_a_t> c_str_data_a(HWND h)
{
    return c_str_ptr_HWND_proxy<ws_char_a_t>(h);
}

/// \brief Returns the corresponding C-string pointer of the window \c h
inline c_str_ptr_HWND_proxy<ws_char_w_t> c_str_data_w(HWND h)
{
    return c_str_ptr_HWND_proxy<ws_char_w_t>(h);
}

/// \brief Returns the corresponding C-string pointer of the window \c h
inline c_str_ptr_HWND_proxy<TCHAR> c_str_data(HWND h)
{
    return c_str_ptr_HWND_proxy<TCHAR>(h);
}

/* LSA_UNICODE_STRING */
#ifdef _NTSECAPI_
/// \brief Returns the corresponding potentially unterminated C-string pointer of the LSA_UNICODE_STRING \c s
inline LPCWSTR c_str_data(const LSA_UNICODE_STRING &s)
{
    return s.Buffer;    /
}
#endif /* _NTSECAPI_ */

/* /////////////////////////////////////////////////////////////////////////
 * c_str_len
 *
 * This can be applied to an expression, and the return value is the number of
 * characters in the character string in the expression.
 */

/* HWND */
/// \brief Returns the length (in characters) of the string of \c s, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_len(HWND h)
{
    return GetWindowTextLength__(h);
}

/// \brief Returns the length (in characters) of the string of \c s, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_len_a(HWND h)
{
    return c_str_len(h);
}

/// \brief Returns the length (in characters) of the string of \c s, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_len_w(HWND h)
{
    return c_str_len(h);
}

/* LSA_UNICODE_STRING */
#ifdef _NTSECAPI_
/// \brief Returns the length (in characters) of the LSA_UNICODE_STRING \c s, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_len(const LSA_UNICODE_STRING &s)
{
    return s.Length;
}
#endif /* _NTSECAPI_ */

/* /////////////////////////////////////////////////////////////////////////
 * c_str_size
 *
 * This can be applied to an expression, and the return value is the number of
 * bytes required to store the character string in the expression, NOT including
 * the null-terminating character.
 */

/* HWND */
#if 0
/// \brief Returns the size (in bytes) of the string of \c h, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_size_a(HWND h)
{
    return c_str_len(h) * sizeof(ws_char_a_t);
}
#endif /* 0 */

#if 0
/// \brief Returns the size (in bytes) of the string of \c h, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_size_w(HWND h)
{
    return c_str_len(h) * sizeof(ws_char_w_t);
}
#endif /* 0 */

#if 0
/// \brief Returns the size (in bytes) of the string of \c h, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_size(HWND h)
{
    return c_str_len(h) * sizeof(TCHAR);
}
#endif /* 0 */

/* LSA_UNICODE_STRING */
#ifdef _NTSECAPI_
#if 0
/// \brief Returns the size (in bytes) of the LSA_UNICODE_STRING \c s, <b><i>not</i></b> including the null-terminating character
inline ws_size_t c_str_size(const LSA_UNICODE_STRING &s)
{
    return c_str_len(s) * sizeof(WCHAR);
}
#endif /* 0 */
#endif /* _NTSECAPI_ */

////////////////////////////////////////////////////////////////////////////
// Unit-testing

#ifdef STLSOFT_UNITTEST
# include "./unittest/string_access_unittest_.h"
#endif /* STLSOFT_UNITTEST */

/* ////////////////////////////////////////////////////////////////////// */

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace stlsoft::winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 *
 * The string access shims exist either in the stlsoft namespace, or in the
 * global namespace. This is required by the lookup rules.
 *
 */

#ifndef _WINSTL_NO_NAMESPACE
# if !defined(_STLSOFT_NO_NAMESPACE) && \
     !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
namespace stlsoft
{
# else /* ? _STLSOFT_NO_NAMESPACE */
/* There is no stlsoft namespace, so must define in the global namespace */
# endif /* !_STLSOFT_NO_NAMESPACE */

using ::winstl::c_str_ptr_null;
using ::winstl::c_str_ptr_null_a;
using ::winstl::c_str_ptr_null_w;

using ::winstl::c_str_ptr;
using ::winstl::c_str_ptr_a;
using ::winstl::c_str_ptr_w;

using ::winstl::c_str_data;
using ::winstl::c_str_data_a;
using ::winstl::c_str_data_w;

using ::winstl::c_str_len;
using ::winstl::c_str_len_a;
using ::winstl::c_str_len_w;

#if 0
using ::winstl::c_str_size;
using ::winstl::c_str_size_a;
using ::winstl::c_str_size_w;
#endif /* 0 */

# if !defined(_STLSOFT_NO_NAMESPACE) && \
     !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace stlsoft
# else /* ? _STLSOFT_NO_NAMESPACE */
/* There is no stlsoft namespace, so must define in the global namespace */
# endif /* !_STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* !WINSTL_INCL_WINSTL_HPP_STRING_ACCESS */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -