欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

interface_cast.hpp

用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
HPP
第 1 页 / 共 2 页
字号:
private:
    interface_pointer_type const    m_pi;

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


/// Interface cast that does not add a net reference count
///
/// 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
// [[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)
    {}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
    ss_explicit_k interface_cast_noaddref(LPUNKNOWN punk)
        : parent_class_type(punk)
    {}
#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)
    {}

#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 */) {}
};

/// Interface cast that does add a net reference count
///
/// 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)
    {}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
    ss_explicit_k interface_cast_addref(LPUNKNOWN punk)
        : parent_class_type(punk)
    {}
#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)
    {}

// 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 */) {}
};


/// Interface cast that tests whether a given interface pointer can be successfully queried
///
/// This class performs a cast to ascertain whether the requested interface is available, but
/// then releases the resultant interface, so no net changes are made to the reference count
/// on the tested object.
///
/// \param I The interface pointer type
// [[synesis:class:cast: comstl::interface_cast_test]]
template<   ss_typename_param_k I
        >
class interface_cast_test
    : 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_test<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_test(J &j)
        : parent_class_type(j)
    {}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
    ss_explicit_k interface_cast_test(LPUNKNOWN punk)
        : parent_class_type(punk)
    {}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */

    /// Constructor that directly casts (without calling QueryInterface())
    ss_explicit_k interface_cast_test(interface_pointer_type pi)
        : parent_class_type(pi)
    {}

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
    ~interface_cast_test() 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_test(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 */) {}
};

/* /////////////////////////////////////////////////////////////////////////
 * Shims
 */

#if !defined(STLSOFT_COMPILER_IS_COMO) && \
    !defined(STLSOFT_COMPILER_IS_GCC)
/// Attribute shim to retrieve the interface pointer of the given cast instance
///
/// \param p The cast instance
template<   ss_typename_param_k I
        ,   ss_typename_param_k X
        >
inline I get_ptr(comstl_ns_qual(interface_cast_noaddref)<I, X> &p)
{
    return p.operator -> ();
}
#endif /* compiler */

/// Attribute shim to retrieve the interface pointer of the given cast instance
///
/// \param p The cast instance
template<   ss_typename_param_k I
        ,   ss_typename_param_k X
        >
inline I get_ptr(comstl_ns_qual(interface_cast_noaddref)<I, X> const &p)
{
    return p.operator -> ();
}

/// Attribute shim to retrieve the interface pointer of the given cast instance
///
/// \param p The cast instance
template<   ss_typename_param_k I
        ,   ss_typename_param_k X
        >
inline I get_ptr(comstl_ns_qual(interface_cast_addref)<I, X> &p)
{
    return p;
}

/// Attribute shim to retrieve the interface pointer of the given cast instance
///
/// \param p The cast instance
template<   ss_typename_param_k I
        ,   ss_typename_param_k X
        >
inline I const get_ptr(comstl_ns_qual(interface_cast_addref)<I, X> const &p)
{
    return p;
}

/// Attribute shim to determine whether the interface pointer is empty, or not
///
/// \param p The cast instance
template<   ss_typename_param_k I
        ,   ss_typename_param_k X
        >
inline cs_bool_t is_empty(comstl_ns_qual(interface_cast_noaddref)<I, X> const &p)
{
    return NULL != get_ptr(p);
}

/// Attribute shim to determine whether the interface pointer is empty, or not
///
/// \param p The cast instance
template<   ss_typename_param_k I
        ,   ss_typename_param_k X
        >
inline cs_bool_t is_empty(comstl_ns_qual(interface_cast_addref)<I, X> const &p)
{
    return NULL != get_ptr(p);
}

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

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

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

#ifndef _COMSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace comstl
# else
} // namespace comstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_COMSTL_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 _COMSTL_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 ::comstl::get_ptr;

using ::comstl::is_empty;

# 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 /* !_COMSTL_NO_NAMESPACE */

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

#endif /* !COMSTL_INCL_COMSTL_HPP_INTERFACE_CAST */

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

⌨️ 快捷键说明

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