Template class to make IEnumerator type-safe in C++
This is my current version. Comments are welcome.
(yes, I removed the previous posts as I did not want to fill this blog with “ Here is verssio n“... Sorry.)
class EnumeratorBase
{
public:
bool MoveNext() { return m_e->MoveNext() ; }
void Reset() { return m_e->Reset() ; }
protected:
EnumeratorBase( System::Object * o ) : m_e(static_cast<System::Collections::IEnumerable *>(o)->GetEnumerator()) {}
protected:
gcroot< System::Collections::IEnumerator * > m_e ;
} ;
template <typename T>
class Enumerator : public EnumeratorBase
{
public:
Enumerator( System::Object * o ) : EnumeratorBase(o) {}
T * get_Current() { return __try_cast<T*>(m_e->get_Current()) ; }
} ;
template <typename T>
class ValueEnumerator : public EnumeratorBase
{
public:
ValueEnumerator( System::Object * o ) : EnumeratorBase(o) {}
T __box * get_Current() { return dynamic_cast<T __box *>(m_e->get_Current()) ; }
} ;