Skip to content

@sehe /Test.cpp
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
BaseMFCIter
#include "afxwin.h"
#include "afxtempl.h"
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
class myCObject : public CObject
{
public:
myCObject( int val )
{
x = val;
}
int x;
};
#include < iterator >
// Define BaseMFCIter as a standard input iterator.
//
// The template arguments are:
// Item: the contained element type
// Cont: the container type
// Key: the access key (defaults to POSITION)
template < class Item, class Cont, class Key = POSITION >
class BaseMFCIter : public std::iterator < std::input_iterator_tag, Item >
{
public:
// Define types for the 2 member functions to be used:
typedef Key (Cont::*GetFirstFunctionPtr) () const;
typedef Item (Cont::*GetNextFunctionPtr) (Key&) const;
// Default constructor, makes a null iterator, equal to BaseMFCIter::end()
BaseMFCIter() : m_pCont(0), m_Pos(0), m_GetFirstFunc(0), m_GetNextFunc(0), m_End(true) {}
// Constructor taking pointer to container and the iteration functions
BaseMFCIter(Cont* pCont, GetFirstFunctionPtr pFF, GetNextFunctionPtr pNF)
: m_pCont(pCont), m_Pos(0), m_GetFirstFunc(pFF), m_GetNextFunc(pNF)
{ init(); }
// Copy constructor, initialises iterator to first element
BaseMFCIter(const BaseMFCIter& vi) : m_pCont(vi.m_pCont), m_Pos(0),
m_GetFirstFunc(vi.m_GetFirstFunc), m_GetNextFunc(vi.m_GetNextFunc)
{ init(); }
// Assignment operator, initialises iterator to first element
BaseMFCIter& operator=(const BaseMFCIter& vi)
{
m_pCont = vi.m_pCont;
m_GetFirstFunc = vi.m_GetFirstFunc;
m_GetNextFunc = vi.m_GetNextFunc;
init();
return *this;
}
bool operator == (const BaseMFCIter& rhs) const
{ return (m_Pos == rhs.m_Pos && m_End == rhs.m_End); }
bool operator != (const BaseMFCIter& rhs) const
{ return !operator==(rhs); }
BaseMFCIter& operator ++ () { advance(); return *this; }
BaseMFCIter& operator ++ (int) { BaseMFCIter ret(*this); advance(); return ret; }
Item operator * () { return m_Item; }
Item operator -> () { return m_Item; }
static BaseMFCIter end () { return BaseMFCIter(); } // end() returns default null iterator
private:
Item m_Item; // Current item from container
Cont* m_pCont; // Pointer to container
Key m_Pos; // Key to item in container
bool m_End; // Flag to indicate end of container reached
// Pointers to container iteration functions
GetFirstFunctionPtr m_GetFirstFunc;
GetNextFunctionPtr m_GetNextFunc;
// Use container GetFirst & GetNext functions to set to first element, or end() if not found
void init()
{
m_Pos = 0;
m_End = true;
if (m_pCont && m_GetFirstFunc != 0)
{
m_Pos = (m_pCont->*m_GetFirstFunc)();
advance();
}
}
// Use container GetNext function to find next element in container
void advance()
{
m_End = m_Pos ? false : true;
m_Item = (m_Pos && m_pCont && m_GetNextFunc != 0) ?
(m_pCont->*m_GetNextFunc)(m_Pos) : Item();
}
};
//class ListIter : public BaseMFCIter < CObject, CObList >
//{
//public:
// ListIter( CObject* pObj = 0) : BaseMFCIter< CObject, CObList >
// (pObj, CObList::GetHeadPosition, CObList::GetNext)
// {}
//};
//
//class ListIter : public BaseMFCIter < myCObject, CObList >
//{
//public:
// ListIter( CObList* pObj = 0) : BaseMFCIter< myCObject, CObList >
// (pObj, &CObList::GetHeadPosition, &CObList::GetNext)
// {}
//};
//class ListIter : public BaseMFCIter < myCObject, CObList >
//{
//public:
// ListIter( CObList* pObj = 0) : BaseMFCIter< myCObject, CObList >
// (pObj, &CObList::GetHeadPosition, &CObList::GetNext )
// {}
//};
class ListIter : public BaseMFCIter < myCObject, CObList >
{
public:
typedef CObject*& (CObList::*GetNextPtr)(POSITION&);
typedef const CObject* (CObList::*GetNextConstPtr)(POSITION&) const;
ListIter( CObList* pObj = 0) : BaseMFCIter< myCObject, CObList >
(pObj, &CObList::GetHeadPosition, (GetNextConstPtr) ( &CObList::GetNext ) )
{}
};
class Container : public CObList
{
};
void main( )
{
myCObject* m = new myCObject( 1 );
myCObject* n = new myCObject( 2 );
myCObject* p = new myCObject( 3 );
myCObject* q = new myCObject( 4 );
Container cont;
cont.AddHead( m );
cont.AddHead( n );
cont.AddHead( p );
cont.AddHead( q );
for ( POSITION pos = cont.GetHeadPosition( ); pos != NULL; cont.GetNext( pos ) )
{
std::cout << ( ( myCObject* )(cont.GetAt( pos )) )->x << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.