00001
00002 #ifndef __MIX_NODELIST_H_
00003 #define __MIX_NODELIST_H_
00004
00005 #include "classes.h"
00006
00007 #include <list>
00008
00009 namespace MiX{
00010
00011 template <class charT,class char_traits,class xml_traits>
00012 class NodeList : public std::list<Node<charT,char_traits,xml_traits>* >{
00013 public:
00014 typedef Node<charT,char_traits,xml_traits>* value_type;
00015 typedef NodeList<charT,char_traits,xml_traits> this_type;
00016 typedef typename std::list<value_type>::iterator iterator;
00017 typedef typename std::list<value_type>::const_iterator const_iterator;
00018
00028 template <class NodeT>
00029 class Iterator{
00030 typedef NodeT* pointer;
00031 typedef NodeT& reference;
00032
00033 typedef NodeList<charT,char_traits,xml_traits> container_type;
00034 typedef container_type::iterator basic_iterator;
00035 typedef Iterator<NodeT> this_type;
00036
00037 basic_iterator it_;
00038 container_type* container_;
00039
00041 Iterator(container_type* container,basic_iterator it){
00042 container_ = container;
00043 it_ = it;
00044 };
00045 public:
00051 Iterator(){
00052 container_ = NULL;
00053 };
00055 reference operator*(){
00056 return dynamic_cast<NodeT&>(**it_);
00057 };
00059 pointer operator->(){
00060 return dynamic_cast<NodeT*>(*it_);
00061 };
00063 this_type operator++(){
00064 basic_iterator itEnd=container_->end();
00065 ++it_;
00066 while(it_!=itEnd){
00067 if((*it_)->getType()==NodeT::type()) break;
00068 ++it_;
00069 }
00070 return *this;
00071 };
00073 this_type operator--(){
00074 basic_iterator itBegin=container_->begin();
00075 while(it_!=itBegin){
00076 --it_;
00077 if((*it_)->getType()==NodeT::type()) break;
00078 }
00079 return *this;
00080 };
00087 this_type operator++(int dmy){
00088 this_type ret = *this;
00089 ++(*this);
00090 return ret;
00091 };
00098 this_type operator--(int dmy){
00099 this_type ret = *this;
00100 --(*this);
00101 return ret;
00102 };
00103
00104 bool operator==(this_type& r)const{
00105 return it_==r.it_;
00106 };
00107
00108 bool operator!=(this_type& r)const{
00109 return !(*this==r);
00110 };
00111
00112 friend class NodeList<charT,char_traits,xml_traits>;
00113 };
00117 template <class NodeT>
00118 class ConstIterator{
00119 typedef const NodeT* pointer;
00120 typedef const NodeT& reference;
00121
00122 typedef NodeList<charT,char_traits,xml_traits> container_type;
00123 typedef container_type::const_iterator basic_iterator;
00124 typedef ConstIterator<NodeT> this_type;
00125
00126 basic_iterator it_;
00127 container_type* container_;
00128
00130 ConstIterator(container_type* container,basic_iterator it){
00131 container_ = container;
00132 it_ = it;
00133 };
00134 public:
00140 ConstIterator(){ container_ = NULL; };
00142 pointer operator->(){return dynamic_cast<pointer>(*it_); };
00144 reference operator*(){return dynamic_cast<reference>(**it_);};
00146 this_type operator++(){
00147 basic_iterator itEnd=container_->end();
00148 ++it_;
00149 while(it_!=itEnd){
00150 if((*it_)->getType()==NodeT::type()) break;
00151 ++it_;
00152 }
00153 return *this;
00154 };
00156 this_type operator--(){
00157 basic_iterator itBegin=container_->begin();
00158 while(it_!=itBegin){
00159 --it_;
00160 if((*it_)->getType()==NodeT::type()) break;
00161 }
00162 return *this;
00163 };
00170 this_type operator++(int dmy){
00171 this_type ret = *this;
00172 ++(*this);
00173 return ret;
00174 };
00181 this_type operator--(int dmy){
00182 this_type ret = *this;
00183 --(*this);
00184 return ret;
00185 };
00186
00187 bool operator==(this_type& r)const{
00188 return it_==r.it_;
00189 };
00190
00191 bool operator!=(this_type& r)const{
00192 return !(*this==r);
00193 };
00194
00195 friend class NodeList<charT,char_traits,xml_traits>;
00196 };
00197
00198 public:
00200 template <class NodeT>
00201 Iterator<NodeT> End(){
00202 return this_type::Iterator<NodeT>(this,end());
00203 }
00205 template <class NodeT>
00206 ConstIterator<NodeT> End()const{
00207 return this_type::ConstIterator<NodeT>(this,end());
00208 }
00209
00211 template <class NodeT>
00212 Iterator<NodeT> Begin(){
00213 if( empty() ) {
00214 return End<NodeT>();
00215 } else {
00216 this_type::Iterator<NodeT> ret = this_type::Iterator<NodeT>(this,begin());
00217 if((*(ret.it_))->getType()!=NodeT::type()) ++ret;
00218 return ret;
00219 }
00220 }
00222 template <class NodeT>
00223 ConstIterator<NodeT> Begin()const{
00224 if( empty() ) {
00225 return End<NodeT>();
00226 } else {
00227 this_type::ConstIterator<NodeT> ret = this_type::ConstIterator<NodeT>(this,begin());
00228 if((*(ret.it_))->getType()!=NodeT::type()) ++ret;
00229 return ret;
00230 }
00231 }
00232 };
00233 }
00234
00235
00236 #endif