📄 variant.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: comstl/util/variant.hpp (originally MOVriant.h/.cpp, ::SynesisCom)
*
* Purpose: variant class.
*
* Created: 12th December 1996
* Updated: 29th March 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 1996-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/util/variant.hpp
*
* \brief [C++ only; requires COM] Definition of the comstl::variant class
* (\ref group__library__utility__com "COM Utility" Library).
*/
#ifndef COMSTL_INCL_COMSTL_UTIL_HPP_COMSTL_VARIANT
#define COMSTL_INCL_COMSTL_UTIL_HPP_COMSTL_VARIANT
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define _COMSTL_VER_COMSTL_UTIL_HPP_COMSTL_VARIANT_MAJOR 2
# define _COMSTL_VER_COMSTL_UTIL_HPP_COMSTL_VARIANT_MINOR 2
# define _COMSTL_VER_COMSTL_UTIL_HPP_COMSTL_VARIANT_REVISION 2
# define _COMSTL_VER_COMSTL_UTIL_HPP_COMSTL_VARIANT_EDIT 146
#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 COMSTL_INCL_COMSTL_SHIMS_ACCESS_HPP_STRING
# include <comstl/shims/access/string.hpp>
#endif /* !COMSTL_INCL_COMSTL_SHIMS_ACCESS_HPP_STRING */
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# ifndef COMSTL_INCL_COMSTL_ERROR_HPP_EXCEPTIONS
# include <comstl/error/exceptions.hpp>
# endif /* !COMSTL_INCL_COMSTL_ERROR_HPP_EXCEPTIONS */
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
#ifndef COMSTL_INCL_COMSTL_UTIL_HPP_INTERFACE_TRAITS
# include <comstl/util/interface_traits.hpp>
#endif /* !COMSTL_INCL_COMSTL_UTIL_HPP_INTERFACE_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
# include <stlsoft/util/constraints.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS */
/* /////////////////////////////////////////////////////////////////////////
* 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
*/
/** \brief Facade for the COM VARIANT type
*
* \ingroup group__library__utility__com
*
* \remarks comstl::variant publicly derives from \c VARIANT as a measure
* of expedience, rather than as an act of design sophistication. Any
* manual manipulation of the instances, or their member variables, is
* at the user's risk. Notwithstanding, this is helped by the fact that
* comstl::variant declares no member variables and no virtual member
* functions - for most compilers this means that the Empty Derived
* Optimisation (see Section 12.4 of Imperfect C++) will apply.
*/
class variant
: public VARIANT
{
/// Member Types
public:
typedef variant class_type;
typedef cs_bool_t bool_type;
typedef cs_size_t size_type;
/// Conversion
public:
/** Default constructor
*
* Initialises the instance
*
* \post <code>assert(VT_EMPTY == this->vt)</code>
*
* \exception - Does not throw an exception
*/
variant();
/** Copying constructor
*
* Initialises the instance with a copy of the given \c VARIANT
*
* \post <code>assert(rhs == *this)</code>
*
* \exception comstl::com_exception If the copy fails
*/
variant(VARIANT const& rhs);
/** Copy constructor
*
* \post <code>assert(rhs == *this)</code>
*
* \exception comstl::com_exception If the copy fails
*/
variant(class_type const& rhs);
/** Copy assignment operator
*
* \post <code>assert(rhs == *this)</code>
*
* \exception comstl::com_exception If the copy fails
*/
class_type& operator =(class_type const& rhs);
/** Conversion constructor
*
* Initialises the instance with the given boolean value
*
* \post <code>assert(VT_BOOL == this->vt)</code>
* \post <code>assert(b == (VARIANT_TRUE == this->boolVal))</code>
*
* \exception - Does not throw an exception
*/
variant(bool b);
/** Conversion constructor
*
* Initialises the instance with the given short value
*
* \post <code>assert(VT_I2 == this->vt)</code>
* \post <code>assert(i == this->iVal)</code>
*
* \exception - Does not throw an exception
*/
variant(short i);
/** Conversion constructor
*
* Initialises the instance with the given int value
*
* \post <code>assert(VT_I4 == this->vt)</code>
* \post <code>assert(i == this->lVal)</code>
*
* \exception - Does not throw an exception
*/
variant(int i);
/** Conversion constructor
*
* Initialises the instance with the given long value
*
* \post <code>assert(VT_I4 == this->vt)</code>
* \post <code>assert(i == this->lVal)</code>
*
* \exception - Does not throw an exception
*/
variant(long i);
/** Conversion constructor
*
* Initialises the instance with the given float value
*
* \post <code>assert(VT_R4 == this->vt)</code>
* \post <code>assert(r == this->fltVal)</code>
*
* \exception - Does not throw an exception
*/
variant(float r);
/** Conversion constructor
*
* Initialises the instance with the given double value
*
* \post <code>assert(VT_R8 == this->vt)</code>
* \post <code>assert(r == this->dblVal)</code>
*
* \exception - Does not throw an exception
*/
variant(double r);
/** Conversion constructor
*
* Initialises the instance with the given currency (CY) value
*
* \post <code>assert(VT_CY == this->vt)</code>
* \post <code>assert(r == this->cyVal)</code>
*
* \exception - Does not throw an exception
*/
variant(CY cy);
variant(LPUNKNOWN punk, bool_type bAddRef);
variant(LPDISPATCH pdisp, bool_type bAddRef);
variant(cs_char_a_t const* s, int len = -1);
variant(cs_char_w_t const* s, int len = -1);
variant(VARIANT const& var, VARTYPE vt);
~variant() stlsoft_throw_0()
{
stlsoft_constraint_must_be_same_size(class_type, VARIANT);
::VariantClear(this);
}
void clear();
/// Operations
public:
HRESULT try_conversion_copy(VARIANT const& var, VARTYPE vt);
HRESULT try_convert(VARTYPE vt);
class_type &convert(VARTYPE vt);
HRESULT QueryInterface(REFIID riid, void **ppv) const;
template <ss_typename_param_k I>
HRESULT QueryInterfaceValue(I **ppi)
{
return QueryInterface(IID_traits<I>::iid(), reinterpret_cast<void**>(ppi));
}
public:
void swap(class_type& rhs);
/// Comparison
public:
bool_type equal(class_type const& rhs) const;
bool_type equal(VARIANT const& rhs) const;
/// Operators
public:
private:
static void swap_(VARIANT& lhs, VARIANT& rhs);
void handle_error_(char const* message, HRESULT hr);
};
/* /////////////////////////////////////////////////////////////////////////
* String access shims
*/
// No string access shims are defined, because there're already a set
// defined for VARIANT, in comstl/shims/access/string.hpp, which is included
// by this file.
/* /////////////////////////////////////////////////////////////////////////
* Operators
*/
inline cs_bool_t operator ==(variant const& lhs, variant const& rhs)
{
return lhs.equal(rhs);
}
inline cs_bool_t operator !=(variant const& lhs, variant const& rhs)
{
return !operator ==(lhs, rhs);
}
inline cs_bool_t operator ==(variant const& lhs, VARIANT const& rhs)
{
return lhs.equal(rhs);
}
inline cs_bool_t operator !=(variant const& lhs, VARIANT const& rhs)
{
return !operator ==(lhs, rhs);
}
inline cs_bool_t operator ==(VARIANT const& lhs, variant const& rhs)
{
return rhs.equal(lhs);
}
inline cs_bool_t operator !=(VARIANT const& lhs, variant const& rhs)
{
return !operator ==(lhs, rhs);
}
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/variant_unittest_.h"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -