00001 00009 #ifndef COLLECTION_HH 00010 #define COLLECTION_HH 00011 00012 #include<list> 00013 00014 namespace my_lib { 00015 00022 template<typename T> 00023 struct AbstractIterator { 00027 virtual bool hasNext() = 0; 00031 virtual T next() = 0; 00037 virtual AbstractIterator* clone() const = 0; 00038 }; 00039 00050 template<typename T, typename Container = std::list<T> > 00051 struct Collection : private Container { 00058 void add(const T& t){ 00059 this->push_back(t); 00060 } 00061 00067 struct Iterator : AbstractIterator<T> { 00068 private : 00069 const typename Container::iterator end; 00070 typename Container::iterator current; 00071 public : 00072 Iterator(Container& c) : current(c.begin()), end(c.end()) {} 00073 bool hasNext() { 00074 return current != end; 00075 } 00076 00077 T next() { 00078 T& t=*current; 00079 current++; 00080 return t; 00081 } 00082 00083 Iterator* clone() const { 00084 return new Iterator(*this); 00085 } 00086 }; 00087 00092 Iterator iterator(){ 00093 return Iterator(*this); 00094 } 00095 }; 00096 00104 template<typename T> 00105 struct Iterator{ 00106 private: 00107 AbstractIterator<T> *it; 00108 public: 00110 Iterator() : it(NULL) {} 00116 Iterator(const AbstractIterator<T>& it) : it(it.clone()) {} 00117 00124 Iterator& operator=(const AbstractIterator<T>& it) { 00125 if(this->it!=NULL) 00126 delete this->it; 00127 this->it = it.clone(); 00128 } 00129 00131 ~Iterator() { 00132 if(this->it!=NULL) 00133 delete this->it; 00134 } 00135 00136 00137 bool hasNext() { 00138 return it->hasNext(); 00139 } 00140 00141 T next() { 00142 return it->next(); 00143 } 00144 00145 00146 }; 00147 00148 00149 00150 }//my_lib 00151 00152 #endif COLLECTION_HH 00153 00154