Scopira 20080306

iterator.h

00001 
00002 /*
00003  *  Copyright (c) 2002    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED__SCOPIRA_TOOL_ITERATOR_H__
00015 #define __INCLUDED__SCOPIRA_TOOL_ITERATOR_H__
00016 
00017 #include <assert.h>
00018 
00019 #include <scopira/tool/object.h>
00020 
00021 namespace scopira
00022 {
00023   namespace tool
00024   {
00025     // general interface
00026     template <class T> class iterator_g;
00027     // empty iterator
00028     template <class T> class null_iterator_imp_g;
00029 
00030     // imp
00031     template <class T> class iterator_imp_g;
00032   }
00033 }
00034 
00040 template <class T> class scopira::tool::iterator_imp_g : public virtual scopira::tool::object
00041 {
00042   public:
00043     typedef T data_type;
00044 
00046     virtual T current(void) = 0;
00048     virtual bool valid(void) const = 0;
00050     virtual void next(void) = 0;
00051 };
00052 
00058 template <class T> class scopira::tool::null_iterator_imp_g : public scopira::tool::iterator_imp_g<T>
00059 {
00060   public:
00062     virtual T current(void) { return 0; } //hopefully, a valid-compile time cast (should never be executed at runtime)
00064     virtual bool valid(void) const { return false; }
00066     virtual void next(void) { }
00067 };
00068 
00078 template <class T> class scopira::tool::iterator_g
00079 {
00080   public:
00081     typedef T data_type;
00082     typedef null_iterator_imp_g<T> null_type;
00083 
00085     iterator_g(void)
00086       { }
00088     iterator_g(iterator_imp_g<T> *imp)
00089       : m_imp(imp) { }
00091     iterator_g(const iterator_g &src)
00092       : m_imp(src.m_imp) { }
00094     ~iterator_g() { }
00095 
00097     T operator *(void) {
00098       assert(m_imp.get());
00099       return m_imp->current();
00100     }
00102     bool valid(void) const {
00103       if (m_imp.get())
00104         return m_imp->valid();
00105       else  
00106         return 0;
00107     }
00109     bool operator ==(const iterator_g &rhs) const {
00110       assert(!rhs.m_imp.get());
00111       return !valid();
00112     }
00114     bool operator ==(int x) const {
00115       assert(x == 0);
00116       return !valid();
00117     }
00119     bool operator !=(const iterator_g &rhs) const {
00120       assert(!rhs.m_imp.get());
00121       return valid();
00122     }
00124     bool operator !=(int x) const {
00125       assert(x == 0);
00126       return valid();
00127     }
00128     iterator_g& operator ++(void) {
00129       assert(m_imp.get());
00130       assert(m_imp->valid());
00131       m_imp->next();
00132       return *this;
00133     }
00134     iterator_g& operator =(const iterator_g &rhs) {
00135       m_imp = rhs.m_imp;
00136       return *this;
00137     }
00138 
00139   private:
00140     scopira::tool::count_ptr< iterator_imp_g<T> > m_imp;
00141 };
00142 
00143 #endif
00144