📄 interface_cast.hpp
字号:
/// Constructor that attempts the speculative cast
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k J>
ss_explicit_k interface_cast_base(J &j, NullThrowPermission permission)
: m_pi(do_cast(simple_interface_cast(j), permission))
{}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
ss_explicit_k interface_cast_base(LPUNKNOWN punk, NullThrowPermission permission)
: m_pi(do_cast(punk, permission))
{}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
/// Constructor that directly casts (without calling QueryInterface())
ss_explicit_k interface_cast_base(interface_pointer_type pi)
: m_pi(pi)
{
addref(m_pi);
}
/// Releases the acquired interface pointer according to the \c release_type policy
~interface_cast_base() stlsoft_throw_0()
{
if(NULL != m_pi)
{
release_type()(m_pi);
}
}
/// @}
/// \name Implementation
/// @{
private:
/// Perform the cast, throwing the \c exception_policy_type's \c thrown_type if the
/// requested interface cannot be acquired.
///
/// \param punk The interface pointer to cast
/// \return The converted interface pointer
static interface_pointer_type do_cast(LPUNKNOWN punk, NullThrowPermission permission) stlsoft_throw_1(thrown_type)
{
interface_pointer_type pi;
if(NULL == punk)
{
if(throwOnNull == permission)
{
exception_policy_type()(E_INVALIDARG, IID_traits<interface_pointer_type>().iid());
COMSTL_MESSAGE_ASSERT("The derived class does not support null pointers, but the exception policy failed to throw an exception: the program's behaviour will be undefined!", 0);
}
pi = NULL;
}
else
{
REFIID iid = IID_traits<interface_pointer_type>().iid();
HRESULT hr = punk->QueryInterface(iid, reinterpret_cast<void**>(&pi));
if(FAILED(hr))
{
exception_policy_type()(hr, iid);
pi = NULL;
}
}
return pi;
}
/// @}
/// \name Accessors
/// @{
protected:
/// Returns a non-mutating reference to the acquired interface pointer
interface_pointer_type const& get_pointer_()
{
return m_pi;
}
/// Returns a copy of the acquired interface pointer
interface_pointer_type get_pointer_() const
{
return m_pi;
}
/// @}
/// \name Members
/// @{
private:
interface_pointer_type const m_pi;
/// @}
/// \name Not to be implemented
/// @{
protected:
interface_cast_base(class_type const& rhs);
private:
class_type const& operator =(class_type const& rhs);
/// @}
};
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
/** \brief Interface cast for raw pointers that does not add a net reference count
*
* \ingroup group__library__conversion
*
* This class provides a cast between interface pointers, but does not add a net reference count
*
* \param I The interface pointer type
* \param X The exception type
*
* \note This class is not defined when exception-handling is not supported.
*/
// [[synesis:class:cast: comstl::interface_cast_noaddref]]
template< ss_typename_param_k I
, ss_typename_param_k X = throw_bad_interface_cast_exception
>
class interface_cast_noaddref
: protected interface_cast_base<I, noaddref_release<I>, X>
{
private:
typedef interface_cast_base<I, noaddref_release<I>, X> parent_class_type;
public:
/// The type of the current parameterisation
typedef interface_cast_noaddref<I, X> class_type;
/// The interface pointer type
typedef ss_typename_type_k parent_class_type::interface_pointer_type interface_pointer_type;
# ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
/// The interface type
typedef ss_typename_type_k parent_class_type::interface_type interface_type;
typedef protect_refcount<interface_type> *protected_interface_pointer_type;
# else /* ? STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
typedef interface_pointer_type protected_interface_pointer_type;
# endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
// Construction
public:
/// Constructor that attempts the speculative cast
# ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k J>
ss_explicit_k interface_cast_noaddref(J &j)
: parent_class_type(j, parent_class_type::throwOnNull)
{
COMSTL_MESSAGE_ASSERT("Cannot initialise with a null pointer. Program behaviour will be undefined when it this instance is dereference", NULL != this->parent_class_type::get_pointer_());
}
# else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
ss_explicit_k interface_cast_noaddref(LPUNKNOWN punk)
: parent_class_type(punk, parent_class_type::throwOnNull)
{
COMSTL_MESSAGE_ASSERT("Cannot initialise with a null pointer. Program behaviour will be undefined when it this instance is dereference", NULL != this->parent_class_type::get_pointer_());
}
# endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
/// Constructor that directly casts (without calling QueryInterface())
ss_explicit_k interface_cast_noaddref(interface_pointer_type pi)
: parent_class_type(pi, parent_class_type::throwOnNull)
{
COMSTL_MESSAGE_ASSERT("Cannot initialise with a null pointer. Program behaviour will be undefined when it this instance is dereference", NULL != this->parent_class_type::get_pointer_());
}
# ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
~interface_cast_noaddref() stlsoft_throw_0()
{} // We need to provide this to persuade VC6 to call the parent class dtor
# endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
// Accessors
public:
/// Access the members of the interface
protected_interface_pointer_type operator -> () const
{
COMSTL_MESSAGE_ASSERT("Attempting to dereference null pointer. Exception model should not be null", NULL != this->parent_class_type::get_pointer_());
return static_cast<protected_interface_pointer_type>(this->parent_class_type::get_pointer_());
}
// Not to be implemented
private:
interface_cast_noaddref(class_type const& rhs);
class_type const& operator =(class_type const& rhs);
// These are defined to placate Borland C/C++
void *operator new(cs_size_t /* si */) { return 0; }
void operator delete(void * /* pv */) {}
};
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
/** \brief Interface cast for raw pointers that does add a net reference count
*
* \ingroup group__library__conversion
*
* This class provides a cast between interface pointers, that adds a net reference count
*
* \param I The interface pointer type
* \param X The exception type, defaulted to ignore_interface_cast_exception
*/
// [[synesis:class:cast: comstl::interface_cast_addref]]
template< ss_typename_param_k I
, ss_typename_param_k X = ignore_interface_cast_exception
>
class interface_cast_addref
: protected interface_cast_base<I, addref_release<I>, X>
{
private:
typedef interface_cast_base<I, addref_release<I>, X> parent_class_type;
public:
/// The type of the current parameterisation
typedef interface_cast_addref<I, X> class_type;
/// The interface pointer type
typedef ss_typename_type_k parent_class_type::interface_pointer_type interface_pointer_type;
// Construction
public:
/// Constructor that attempts the speculative cast
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k J>
ss_explicit_k interface_cast_addref(J j)
: parent_class_type(j, parent_class_type::allowNull)
{}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
ss_explicit_k interface_cast_addref(LPUNKNOWN punk)
: parent_class_type(punk, parent_class_type::allowNull)
{}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
/// Constructor that directly casts (without calling QueryInterface())
ss_explicit_k interface_cast_addref(interface_pointer_type pi)
: parent_class_type(pi, parent_class_type::allowNull)
{}
// Accessors
public:
/// A pointer to the acquired interface
operator interface_pointer_type ()
{
return this->parent_class_type::get_pointer_();
}
// Not to be implemented
private:
interface_cast_addref(class_type const& rhs);
class_type const& operator =(class_type const& rhs);
// These are defined to placate Borland C/C++
void *operator new(cs_size_t /* si */) { return 0; }
void operator delete(void * /* pv */) {}
};
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
// [[synesis:class:cast: comstl::interface_cast_tester]]
template< ss_typename_param_k I
>
class interface_cast_tester
: protected interface_cast_base<I, noaddref_release<I>, ignore_interface_cast_exception>
{
private:
typedef interface_cast_base<I, noaddref_release<I>, ignore_interface_cast_exception> parent_class_type;
public:
/// The type of the current parameterisation
typedef interface_cast_tester<I> class_type;
/// The interface pointer type
typedef ss_typename_type_k parent_class_type::interface_pointer_type interface_pointer_type;
#ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
/// The interface type
typedef ss_typename_type_k parent_class_type::interface_type interface_type;
typedef protect_refcount<interface_type> *protected_interface_pointer_type;
#else /* ? STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
typedef interface_pointer_type protected_interface_pointer_type;
#endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
// Construction
public:
/// Constructor that attempts the speculative cast
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k J>
ss_explicit_k interface_cast_tester(J &j)
: parent_class_type(j, parent_class_type::allowNull)
{}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
ss_explicit_k interface_cast_tester(LPUNKNOWN punk)
: parent_class_type(punk, parent_class_type::allowNull)
{}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
/// Constructor that directly casts (without calling QueryInterface())
ss_explicit_k interface_cast_tester(interface_pointer_type pi)
: parent_class_type(pi, parent_class_type::allowNull)
{}
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
~interface_cast_tester() stlsoft_throw_0()
{} // We need to provide this to persuade VC6 to call the parent class dtor
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/// \name State
/// @{
private:
STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, operator_bool_generator_type, operator_bool_type);
public:
/// Represents whether the cast succeeded
///
/// \retval true The interface cast succeeded
/// \retval false The interface cast failed
operator operator_bool_type() const
{
return operator_bool_generator_type::translate(NULL != this->parent_class_type::get_pointer_());
}
/// Represents whether the cast failed
///
/// \retval true The interface cast failed
/// \retval false The interface cast succeeded
cs_bool_t operator !() const
{
return NULL == this->parent_class_type::get_pointer_();
}
/// @}
// Not to be implemented
private:
interface_cast_tester(class_type const& rhs);
class_type const& operator =(class_type const& rhs);
// These are defined to placate Borland C/C++
void *operator new(cs_size_t /* si */) { return 0; }
void operator delete(void * /* pv */) {}
};
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/** \brief Determines whether an interface is available on an object
*
* \ingroup group__library__conversion
*
* \param src The object whose capabilities will be tested. May be NULL
*
\code
IStream *stm = . . .
if(comstl::interface_cast_test<IStorage*>(stm))
{
printf("Object has IStorage interface\n");
}
else
{
printf("Object does not have IStorage interface\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -