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

📄 enumerators.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        atlstl/enumerators.hpp (originally MAEnum.h)
 *
 * Purpose:     Enumerator classes.
 *
 * Created:     11th November 1998
 * Updated:     10th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 1998-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.
 *
 * ////////////////////////////////////////////////////////////////////// */


#ifndef ATLSTL_INCL_ATLSTL_HPP_ENUMERATORS
#define ATLSTL_INCL_ATLSTL_HPP_ENUMERATORS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define ATLSTL_VER_ATLSTL_HPP_ENUMERATORS_MAJOR    3
# define ATLSTL_VER_ATLSTL_HPP_ENUMERATORS_MINOR    0
# define ATLSTL_VER_ATLSTL_HPP_ENUMERATORS_REVISION 3
# define ATLSTL_VER_ATLSTL_HPP_ENUMERATORS_EDIT     49
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

////////////////////////////////////////////////////////////////////////////
// Includes

#ifndef ATLSTL_INCL_ATLSTL_HPP_ATLSTL
# include <atlstl/atlstl.hpp>
#endif /* !ATLSTL_INCL_ATLSTL_HPP_ATLSTL */
#ifndef STLSOFT_INCL_STLSOFT_HPP_REF_PTR
# include <stlsoft/ref_ptr.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_REF_PTR */
#ifndef STLSOFT_INCL_STLSOFT_FUNCTION_ADAPTORS_HPP_REF2PTR
# include <stlsoft/function_adaptors/ref2ptr.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_FUNCTION_ADAPTORS_HPP_REF2PTR */
#include <atlcom.h>
#include <algorithm>
#include <list>

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _ATLSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::atlstl */
namespace atlstl
{
# else
/* Define stlsoft::atlstl_project */

namespace stlsoft
{

namespace atlstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_ATLSTL_NO_NAMESPACE */

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

template<   ss_typename_param_k XV  // Enumerator interface value type (External value type)
        ,   ss_typename_param_k IV  // Internal value type
        >
struct copy_policy
{
public:
    typedef XV  external_value_type;
    typedef IV  internal_value_type;

public:
    static void init(external_value_type *xv);
#if 0
    {
        *xv = external_value_type();
    }
#endif /* 0 */
    static HRESULT copy(external_value_type *v, internal_value_type const &iv);
#if 0
    {
        *xv = iv;

        return S_OK;
    }
#endif /* 0 */
    /// This is only called when copy() has failed, and a number of copies must be undone
    static void clear(external_value_type *xv);
#if 0
    {
        STLSOFT_SUPPRESS_UNUSED(iv);
    }
#endif /* 0 */
};

////////////////////////////////////////////////////////////////////////////
// Classes
//
// copy_enumerator_impl is the analogue to CComEnumImpl, but the advantages over the
// ATL class is that it can be added to/removed from after initialisation, and
// it can be initialised/appended from a sequence (as defined by the start and
// end iterators), rather than assuming an array as source.
//
// copy_enumerator is the analogue to CComEnum, but uses the copy_enumerator_impl.
//
// Each ot the classes takes a number of template arguments, which are defined
// as follows:
//
// I            -   The interface, e.g. IEnumString
// piid         -   The address of the interface, e.g. &IID_IEnumString
// V            -   The value type of the enumeration, e.g. LPOLESTR
// It           -   The iterator class, e.g. LPOLESTR*
// Copy         -   A class that implements copy behaviour (see CComEnumImpl)
// ThreadModel  -   The threading model of the instances (see CComObjectRoot)
//
// Note: The copy_enumerator_impl class only supports copy semantics on the Init and
// Add methods, i.e. it always creates its enumeration contents by taking a
// copy of the contents of its source.

template<   ss_typename_param_k I                                   //!< Enumerator interface
        ,   IID const           *piid                               //!< Enumerator interface Id
        ,   ss_typename_param_k V                                   //!< Enumerator interface value type
        ,   ss_typename_param_k IV      =   V                       //!< Internal type. Must have value semantics
        ,   ss_typename_param_k I2ETx   =   copy_policy<IV, V>      //!< Internal to external transformation policy
        ,   ss_typename_param_k TM      =   CComObjectThreadModel   //!< Thread model
        >
class ATL_NO_VTABLE copy_enumerator;

// class copy_enumerator_impl
template<   ss_typename_param_k I
        ,   IID const           *piid
        ,   ss_typename_param_k V
        ,   ss_typename_param_k IV
        ,   ss_typename_param_k I2ETx
        >
class ATL_NO_VTABLE copy_enumerator_impl
    : public I
{
/// \name Member Types
/// @{
public:
    typedef I                                               interface_type;
    typedef V                                               value_type;
    typedef IV                                              internal_value_type;
    typedef I2ETx                                           internal_to_external_transformer_type;
//    typedef V const                                           *const_pointer;
    typedef copy_enumerator_impl<I, piid, V, IV, I2ETx>     class_type;
    typedef ss_size_t                                       size_type;
    typedef ss_ptrdiff_t                                    difference_type;
private:
    typedef stlsoft_ns_qual_std(list)<internal_value_type>  values_type;
public:
    typedef ss_typename_type_k values_type::iterator        iterator;
    typedef ss_typename_type_k values_type::const_iterator  const_iterator;
/// @}

/// \name Construction
/// @{
public:
    copy_enumerator_impl();
    ~copy_enumerator_impl() stlsoft_throw_0();
/// @}

/// \name Enumeration
/// @{
public:
    STDMETHOD(Next)(ULONG celt, V *rgelt, ULONG *pceltFetched);
    STDMETHOD(Skip)(ULONG celt);
    STDMETHOD(Reset)(void);
    STDMETHOD(Clone)(I **ppEnum);
/// @}

/// \name Operations
/// @{
public:
    template<ss_typename_param_k I, ss_typename_param_k F>
    HRESULT Init(I begin, I end, F fn)
    {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
        try
        {
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
            m_values.clear();

            for(; begin != end; ++begin)
            {
                m_values.push_back(fn(*begin));
            }

            m_current = m_values.begin();

            return S_OK;
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
        }
        catch(std::bad_alloc &)
        {
            return E_OUTOFMEMORY;
        }
        catch(std::exception &)
        {
            return E_UNEXPECTED;
        }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
    }
    template<ss_typename_param_k I>
    HRESULT Init(I begin, I end)
    {
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
        try
        {
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
            m_values.clear();

            for(; begin != end; ++begin)
            {
                m_values.push_back(*begin);
            }

            m_current = m_values.begin();

            return S_OK;
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
        }
        catch(std::bad_alloc &)
        {
            return E_OUTOFMEMORY;
        }
        catch(std::exception &)
        {
            return E_UNEXPECTED;
        }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
    }

    size_type       size() const;

    const_iterator  begin() const;
    iterator        begin();

    const_iterator  end() const;
    iterator        end();
/// @}

/// \name Implementation
/// @{
private:
    virtual class_type  *CreateEmptyClone() const = 0;

    template <typename T>
    static difference_type count_all(const T &b, const T &e)
    {
        difference_type d       =   0;
        T               begin   =   b;
        T               end     =   e;

        for(; begin != end; ++begin)
        {
            ++d;
        }

        return d;
    }

    template <typename T>
    static T increment_by(T it, difference_type by)
    {
        for(; by-- > 0; )
        {
            ++it;
        }

        return it;

⌨️ 快捷键说明

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