Wrapper for LIST_ENTRY (draft)

template <class T> class CLinkedList
{
public:
    class CListEntry;
    class iterator;

    CLinkedList() : m_end(NULL, &m_list), m_nCount(0)
    {
        InitializeListHead(&m_list);
    }
    virtual ~CLinkedList()
    {
        clear();
    }

    void clear()
    {
        m_nCount = 0;
        while(!IsListEmpty(&m_list))
        {
            CListEntry *pEntry = CListEntry::FromPLISTENTRY(RemoveTailList(&m_list));
            if(NULL != pEntry)
                delete pEntry;
        }
    }

    ULONG count() const
    {
        return m_nCount;
    }

    BOOLEAN empty() const
    {
        return IsListEmpty(&m_list);
    }

    void push_back(T& t)
    {
        CListEntry *pEntry = new CListEntry(t);
        if(NULL != pEntry)
        {
            InsertTailList(&m_list, &pEntry->m_entry);
            ++m_nCount;
        }
    }

    class iterator
    {
    public:
        virtual ~iterator() {}

        iterator& operator ++ ()
        {
            if(m_pListEntry->m_entry.Flink == m_pHead)
                m_pListEntry = NULL;
            else
                m_pListEntry = CListEntry::FromPLISTENTRY(m_pListEntry->m_entry.Flink);
            return *this;
        }
        iterator operator ++ (int)
        {
            iterator itor(m_pListEntry);
            ++(*this);
            return itor;
        }
        bool operator == (const iterator& itor) const
        {
            return (m_pListEntry == itor.m_pListEntry);
        }
        bool operator != (const iterator& itor) const
        {
            return (m_pListEntry != itor.m_pListEntry);
        }
        T& operator -> () const
        {
            return m_pListEntry->m_t;
        }
        T& operator * () const
        {
            return m_pListEntry->m_t;
        }
       
    private:
        CListEntry *m_pListEntry;
        PLIST_ENTRY m_pHead;

        friend class CLinkedList;
        iterator(CListEntry *pEntry, PLIST_ENTRY pHead) : m_pListEntry(pEntry), m_pHead(pHead) {}
    };

    iterator begin()
    {
        if(empty())
            return end();
        return iterator(CListEntry::FromPLISTENTRY(m_list.Flink), &m_list);
    }

    const iterator& end() const
    {
        return m_end;
    }

    iterator erase(iterator itor)
    {
        if(itor != end())
        {
            –m_nCount;
            CListEntry *pListEntry = itor.m_pListEntry;
            ++itor;
            RemoveEntryList(&pListEntry->m_entry);
            delete pListEntry;
        }
        return itor;
    }

protected:
    class CListEntry
    {
    public:
        T m_t;
        LIST_ENTRY m_entry;

        CListEntry(T t) : m_t(t)
        {
            InitializeListHead(&m_entry);
        }
        ~CListEntry() {}

        static CListEntry* FromPLISTENTRY(PLIST_ENTRY pEntry)
        {
            if(NULL == pEntry)
                return NULL;
            return (CListEntry*)((PUINT8)pEntry – (UINT8)(&((CListEntry*)0)->m_entry));
        }
    };

    LIST_ENTRY m_list;
    iterator m_end;
    ULONG m_nCount;
};

Advertisements

Posted on January 20, 2011, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: