list.h

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C头文件 代码 · 共 1,117 行 · 第 1/5 页

H
1,117
字号
/////////////////////////////////////////////////////////////////////////////
// Name:        list.h
// Purpose:     wxList, wxStringList classes
// Author:      Julian Smart
// Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added
// Created:     29/01/98
// RCS-ID:      $Id: list.h,v 1.93.2.2 2006/01/18 08:33:59 JS Exp $
// Copyright:   (c) 1998 Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/*
  All this is quite ugly but serves two purposes:
    1. Be almost 100% compatible with old, untyped, wxList class
    2. Ensure compile-time type checking for the linked lists

  The idea is to have one base class (wxListBase) working with "void *" data,
  but to hide these untyped functions - i.e. make them protected, so they
  can only be used from derived classes which have inline member functions
  working with right types. This achieves the 2nd goal. As for the first one,
  we provide a special derivation of wxListBase called wxList which looks just
  like the old class.
*/

#ifndef _WX_LISTH__
#define _WX_LISTH__

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && \
    !(defined(__MINGW32__) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
#pragma interface "list.h"
#endif

// -----------------------------------------------------------------------------
// headers
// -----------------------------------------------------------------------------

#include "wx/defs.h"
#include "wx/object.h"
#include "wx/string.h"

#if wxUSE_STL
    #include "wx/beforestd.h"
    #include <algorithm>
    #include <iterator>
    #include <list>
    #include "wx/afterstd.h"
#endif

// ----------------------------------------------------------------------------
// types
// ----------------------------------------------------------------------------

// type of compare function for list sort operation (as in 'qsort'): it should
// return a negative value, 0 or positive value if the first element is less
// than, equal or greater than the second

extern "C"
{
typedef int (* LINKAGEMODE wxSortCompareFunction)(const void *elem1, const void *elem2);
}

class WXDLLIMPEXP_BASE wxObjectListNode;
typedef wxObjectListNode wxNode;

//
typedef int (* LINKAGEMODE wxListIterateFunction)(void *current);

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

#if !defined(wxENUM_KEY_TYPE_DEFINED)
#define wxENUM_KEY_TYPE_DEFINED

enum wxKeyType
{
    wxKEY_NONE,
    wxKEY_INTEGER,
    wxKEY_STRING
};

#endif

#if wxUSE_STL

#define wxLIST_COMPATIBILITY

#define WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl) \
    WX_DECLARE_LIST_WITH_DECL(elT, liT, decl)
#define WX_DECLARE_LIST_PTR_3(elT, dummy1, liT, dummy2, decl) \
    WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl)

#define WX_DECLARE_LIST_2(elT, liT, dummy, decl) \
    WX_DECLARE_LIST_WITH_DECL(elT, liT, decl)
#define WX_DECLARE_LIST_PTR_2(elT, liT, dummy, decl) \
    WX_DECLARE_LIST_2(elT, liT, dummy, decl) \

#define WX_DECLARE_LIST_WITH_DECL(elT, liT, decl) \
    WX_DECLARE_LIST_XO(elT*, liT, decl)

#if !defined( __VISUALC__ )

template<class T>
class WXDLLIMPEXP_BASE wxList_SortFunction
{
public:
    wxList_SortFunction(wxSortCompareFunction f) : m_f(f) { }
    bool operator()(const T& i1, const T& i2)
      { return m_f((T*)&i1, (T*)&i2) < 0; }
private:
    wxSortCompareFunction m_f;
};

#define WX_LIST_SORTFUNCTION( elT, f ) wxList_SortFunction<elT>(f)
#define VC6_WORKAROUND(elT, liT, decl)

#else // if defined( __VISUALC__ )

#define WX_LIST_SORTFUNCTION( elT, f ) std::greater<elT>( f )
#define VC6_WORKAROUND(elT, liT, decl)                                        \
    decl liT;                                                                 \
                                                                              \
    /* Workaround for broken VC6 STL incorrectly requires a std::greater<> */ \
    /* to be passed into std::list::sort() */                                 \
    template <>                                                               \
    struct std::greater<elT>                                                  \
    {                                                                         \
        private:                                                              \
            wxSortCompareFunction m_CompFunc;                                 \
        public:                                                               \
            greater( wxSortCompareFunction compfunc = NULL )                  \
                : m_CompFunc( compfunc ) {}                                   \
            bool operator()(const elT X, const elT Y) const                   \
                {                                                             \
                    return m_CompFunc ?                                       \
                        ( m_CompFunc( X, Y ) < 0 ) :                          \
                        ( X > Y );                                            \
                }                                                             \
    };

#endif // defined( __VISUALC__ )

// Visual C++ 2005 complains about the const
#if (defined(__VISUALC__) && __VISUALC__ >= 1400) || defined(__MWERKS__)
#define _WX_DELETEFUNCTIONCONST
#else
#define _WX_DELETEFUNCTIONCONST const
#endif

#define WX_DECLARE_LIST_XO(elT, liT, decl)                                    \
    VC6_WORKAROUND(elT, liT, decl)                                            \
    decl liT : public std::list<elT>                                          \
    {                                                                         \
    private:                                                                  \
        bool m_destroy;                                                       \
    private:                                                                  \
        typedef elT _WX_LIST_ITEM_TYPE_##liT;                                 \
        static void DeleteFunction( _WX_DELETEFUNCTIONCONST _WX_LIST_ITEM_TYPE_##liT X );       \
    public:                                                                   \
        class compatibility_iterator                                          \
        {                                                                     \
        private:                                                              \
          /* Workaround for broken VC6 nested class name resolution */        \
          typedef std::list<elT>::iterator iterator;                          \
          friend class liT;                                                   \
        private:                                                              \
            iterator m_iter;                                                  \
            liT * m_list;                                                     \
        public:                                                               \
            compatibility_iterator()                                          \
                : m_iter(), m_list( NULL ) {}                                 \
            compatibility_iterator( liT* li, iterator i )                     \
                : m_iter( i ), m_list( li ) {}                                \
            compatibility_iterator( const liT* li, iterator i )               \
                : m_iter( i ), m_list( const_cast< liT* >( li ) ) {}          \
                                                                              \
            compatibility_iterator* operator->() { return this; }             \
            const compatibility_iterator* operator->() const { return this; } \
                                                                              \
            bool operator==(const compatibility_iterator& i) const            \
                { return (m_list == i.m_list) && (m_iter == i.m_iter); }      \
            bool operator!=(const compatibility_iterator& i) const            \
                { return !( operator==( i ) ); }                              \
            operator bool() const                                             \
                { return m_list ? m_iter != m_list->end() : false; }          \
            bool operator !() const                                           \
                { return !( operator bool() ); }                              \
                                                                              \
            elT GetData() const                                               \
                { return *m_iter; }                                           \
            void SetData( elT e )                                             \
                { *m_iter = e; }                                              \
                                                                              \
            compatibility_iterator GetNext() const                            \
            {                                                                 \
                iterator i = m_iter;                                          \
                return compatibility_iterator( m_list, ++i );                 \
            }                                                                 \
            compatibility_iterator GetPrevious() const                        \
            {                                                                 \
                iterator i = m_iter;                                          \
                return compatibility_iterator( m_list, --i );                 \
            }                                                                 \
            int IndexOf() const                                               \
            {                                                                 \
                return m_list ?                                               \
                    m_iter != m_list->end() ?                                 \
                        std::distance( m_list->begin(), m_iter ) :            \
                            wxNOT_FOUND :                                     \
                        wxNOT_FOUND;                                          \
            }                                                                 \
        };                                                                    \
    public:                                                                   \
        liT() : m_destroy( false ) {}                                         \
                                                                              \
        compatibility_iterator Find( const elT e ) const                      \
        {                                                                     \
          liT* _this = const_cast< liT* >( this );                            \
          return compatibility_iterator( _this,                               \
                     std::find( _this->begin(), _this->end(), e ) );          \
        }                                                                     \
                                                                              \
        bool IsEmpty() const                                                  \
            { return empty(); }                                               \

⌨️ 快捷键说明

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