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

📄 variant.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        comstl/shims/access/string/variant.hpp
 *
 * Purpose:     Contains classes and functions for dealing with OLE/COM strings.
 *
 * Created:     24th May 2002
 * Updated:     22nd March 2007
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2002-2007, 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/shims/access/string/variant.hpp
 *
 * \brief [C++] Definition of the string access shims for
 *   <code>VARIANT</code>
 *   (\ref group__concept__shim__string_access "String Access Shims" Concept).
 */

#ifndef COMSTL_INCL_COMSTL_SHIMS_ACCESS_STRING_HPP_VARIANT
#define COMSTL_INCL_COMSTL_SHIMS_ACCESS_STRING_HPP_VARIANT

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define COMSTL_VER_COMSTL_SHIMS_ACCESS_STRING_HPP_VARIANT_MAJOR    5
# define COMSTL_VER_COMSTL_SHIMS_ACCESS_STRING_HPP_VARIANT_MINOR    0
# define COMSTL_VER_COMSTL_SHIMS_ACCESS_STRING_HPP_VARIANT_REVISION 2
# define COMSTL_VER_COMSTL_SHIMS_ACCESS_STRING_HPP_VARIANT_EDIT     108
#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_STRING_H_BSTR_FUNCTIONS
# include <comstl/string/bstr_functions.h>
#endif /* !COMSTL_INCL_COMSTL_STRING_H_BSTR_FUNCTIONS */
#ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
# include <stlsoft/shims/access/string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
#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 */

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

/* VARIANT */

/** \brief 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.
 *
 * \ingroup group__concept__shim__string_access
 *
 */
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);
};

/** \brief 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.
 *
 * \ingroup group__concept__shim__string_access
 *
 */
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:
    void operator =(class_type const& rhs);
};

/** \brief This class provides an intermediary object that may be returned by the
 * c_str_ptr_a() function, such that the text of a given variant
 * may be accessed as a null-terminated string.
 *
 * \ingroup group__concept__shim__string_access
 *
 */
class c_str_VARIANT_proxy_a
{
public:
    typedef c_str_VARIANT_proxy_a   class_type;
    typedef c_str_VARIANT_proxy_w   class_w_type;

// Construction
public:
    /// Constructs an instance of the proxy from the given c_str_VARIANT_proxy_w
    ///
    /// \param rhs The c_str_VARIANT_proxy_w from which the text will be retrieved
    ss_explicit_k c_str_VARIANT_proxy_a(class_w_type rhs)
        : m_proxyw(rhs)
        , m_buffer(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_VARIANT_proxy_a(class_type& rhs)
        : m_proxyw(rhs.m_proxyw)
        , m_buffer(rhs.m_buffer)
    {
        move_lhs_from_rhs(rhs).m_buffer = NULL;
    }
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
    // Copy constructor
    c_str_VARIANT_proxy_a(class_type const& rhs)
        : m_proxyw(rhs.m_proxyw)
        , m_buffer(NULL)
    {}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */

    /// Releases any storage aquired by the proxy
    ~c_str_VARIANT_proxy_a() stlsoft_throw_0()
    {
        if(m_buffer != empty_string_())
        {
            ::CoTaskMemFree(m_buffer);
        }
    }

// Accessors
public:
    /// Returns a null-terminated string representing the VARIANT contents.
    operator cs_char_a_t const* () const
    {
        if(NULL == m_buffer)
        {
            LPCOLESTR   w_value     =   m_proxyw;
            cs_char_a_t *&buffer_   =   const_cast<class_type*>(this)->m_buffer;

            if( NULL == w_value ||
                L'\0' == *w_value)
            {
                buffer_ = empty_string_();
            }
            else
            {
                cs_size_t   cch =   ::SysStringLen((BSTR)w_value);

                buffer_ = static_cast<cs_char_a_t *>(::CoTaskMemAlloc((1 + cch) * sizeof(cs_char_a_t)));

                if(NULL == buffer_)
                {
                    buffer_ = empty_string_();
                }
                else
                {
#ifdef WIN32
                    ::WideCharToMultiByte(0, 0, w_value, -1, buffer_, static_cast<int>(cch + 1), NULL, NULL);
#else /* ? WIN32 */
# error Not currently implemented for operating systems other than Win32
#endif /* WIN32 */
                }
            }
        }

        return m_buffer;
    }

// Implementation
private:
    static cs_char_a_t *empty_string_()
    {
        // This character array is initialised to 0, which conveniently happens to
        // be the empty string, by the module/application load, so it is
        // guaranteed to be valid, and there are no threading/race conditions
        static cs_char_a_t  s_empty[1];

        COMSTL_ASSERT(s_empty[0] == '\0'); // Paranoid check

        return s_empty;
    }

// Members
private:
    c_str_VARIANT_proxy_w   m_proxyw;
    cs_char_a_t             *m_buffer;

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

/* /////////////////////////////////////////////////////////////////////////
 * Equivalence testing
 */

// c_str_null_VARIANT_proxy
inline cs_bool_t operator ==(LPCOLESTR lhs, c_str_null_VARIANT_proxy const& rhs)
{
    return lhs == static_cast<LPCOLESTR>(rhs);
}

inline cs_bool_t operator ==(c_str_null_VARIANT_proxy const& lhs, LPCOLESTR rhs)
{
    return static_cast<LPCOLESTR>(lhs) == rhs;
}

inline cs_bool_t operator !=(LPCOLESTR lhs, c_str_null_VARIANT_proxy const& rhs)
{

⌨️ 快捷键说明

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