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

📄 string_accessor.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        dotnetstl/string_accessors.hpp (formerly dotnetstl_string_accessor.h)
 *
 * Purpose:     A useful tool for accessing a String object's content as a c-string.
 *
 * Created:     24th June 2003
 * Updated:     11th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2003-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 dotnetstl/string_accessor.hpp
///
/// A useful tool for accessing a String object's content as a c-string.

#ifndef DOTNETSTL_INCL_DOTNETSTL_HPP_STRING_ACCESSOR
#define DOTNETSTL_INCL_DOTNETSTL_HPP_STRING_ACCESSOR

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define DOTNETSTL_VER_DOTNETSTL_HPP_STRING_ACCESSOR_MAJOR      3
# define DOTNETSTL_VER_DOTNETSTL_HPP_STRING_ACCESSOR_MINOR      2
# define DOTNETSTL_VER_DOTNETSTL_HPP_STRING_ACCESSOR_REVISION   1
# define DOTNETSTL_VER_DOTNETSTL_HPP_STRING_ACCESSOR_EDIT       38
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL
# include <dotnetstl/dotnetstl.hpp>
#endif /* !DOTNETSTL_INCL_DOTNETSTL_HPP_DOTNETSTL */
#ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
# include <stlsoft/conversion/sap_cast.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST */
#ifdef STLSOFT_UNITTEST
# include <string.h>
# include <wchar.h>
#endif /* STLSOFT_UNITTEST */

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

#ifdef _STLSOFT_NO_NAMESPACE
/* There is no stlsoft namespace, so must define ::dotnetstl */
namespace dotnetstl
{
#else
/* Define stlsoft::dotnet_project */

namespace stlsoft
{

namespace dotnetstl_project
{

#endif /* _STLSOFT_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

/// Template providing scoped C-string access to a String
template <ss_typename_param_k C>
class c_string_accessor
{
public:
    typedef C                       char_type;
    typedef C                       *pointer;
    typedef C const                 *const_pointer;
    typedef c_string_accessor<C>    class_type;
#if defined(DOTNETSTL_HAT_SYNTAX_SUPPORT)
    typedef System::String const    ^string_pointer_const_type_;
    typedef System::String          ^string_pointer_type_;
#else /* ? DOTNETSTL_HAT_SYNTAX_SUPPORT */
    typedef System::String const    *string_pointer_const_type_;
    typedef System::String          *string_pointer_type_;
#endif /* DOTNETSTL_HAT_SYNTAX_SUPPORT */

// Construction
public:
    /// Construct from a System::String
    ///
    /// \param s The String for which C-string access is to be provided. Must not be NULL
    ss_explicit_k c_string_accessor(string_pointer_const_type_ s)
        : m_h(get_cstring_(s))
    {}
    /// Release resources
    ~c_string_accessor() stlsoft_throw_0()
    {
        System::Runtime::InteropServices::Marshal::FreeHGlobal(m_h);
    }

    c_string_accessor(c_string_accessor &rhs)
        : m_h(rhs.m_h)
    {
        rhs.m_h = 0;
    }

// Accessors
public:
    /// Implicit conversion operator to a C-string
    operator const_pointer() const
    {
#if defined(DOTNETSTL_HAT_SYNTAX_SUPPORT)
        IntPtr  h = m_h;

        return static_cast<const_pointer>(h.ToPointer());
#else /* ? DOTNETSTL_HAT_SYNTAX_SUPPORT */
        return m_h;
#endif /* DOTNETSTL_HAT_SYNTAX_SUPPORT */
    }

// Implementation
private:
    pointer get_cstring_(string_pointer_const_type_ s);

// Members
private:
#if defined(DOTNETSTL_HAT_SYNTAX_SUPPORT)
    System::IntPtr  m_h;
#else /* ? DOTNETSTL_HAT_SYNTAX_SUPPORT */
    pointer         m_h;
#endif /* DOTNETSTL_HAT_SYNTAX_SUPPORT */

// Not to be implemented
private:
    c_string_accessor &operator =(class_type const &);
};

////////////////////////////////////////////////////////////////////////////
// Implementation

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

STLSOFT_TEMPLATE_SPECIALISATION
inline c_string_accessor<ds_char_a_t>::pointer c_string_accessor<ds_char_a_t>::get_cstring_(string_pointer_const_type_ s)
{
    return sap_cast<ds_char_a_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(const_cast<string_pointer_type_>(s)).ToPointer());
}

STLSOFT_TEMPLATE_SPECIALISATION
inline c_string_accessor<ds_char_w_t>::pointer c_string_accessor<ds_char_w_t>::get_cstring_(string_pointer_const_type_ s)
{
    return sap_cast<ds_char_w_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(const_cast<string_pointer_type_>(s)).ToPointer());
}

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

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

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

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

#ifdef _STLSOFT_NO_NAMESPACE
} // namespace dotnetstl
#else
} // namespace dotnetstl_project
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */

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

#endif /* DOTNETSTL_INCL_DOTNETSTL_HPP_STRING_ACCESSOR */

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

⌨️ 快捷键说明

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