CPN
Computational Process Networks
IteratorRef.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
29 #ifndef ITERATORREF_H
30 #define ITERATORREF_H
31 #pragma once
32 #include <cpn/common.h>
33 #include <iterator>
34 #include <typeinfo>
35 #include <memory>
45 template<typename T>
46 class IteratorRef {
47 private:
48 
49  class ItrRef {
50  public:
51  virtual ~ItrRef() {}
52  virtual void Increment() = 0;
53  virtual void Decrement() = 0;
54  virtual T &Dereference() = 0;
55  virtual ItrRef *Clone() const = 0;
56  virtual bool Equals(const ItrRef *rhs) const = 0;
57 
58  };
59 
60  template<typename iterator_type>
61  class ItrRefImpl : public ItrRef {
62  public:
63  typedef typename std::iterator_traits<iterator_type>::value_type value_type;
64  ItrRefImpl(iterator_type itr_) : itr(itr_) {}
65  void Increment() { ++itr; }
66  void Decrement() { --itr; }
67  value_type &Dereference() { return *itr; }
68  ItrRef *Clone() const { return new ItrRefImpl<iterator_type>(itr); }
69  bool Equals(const ItrRef *rhs) const {
70  try {
71  const ItrRefImpl<iterator_type> *o = dynamic_cast<const ItrRefImpl<iterator_type> *>(rhs);
72  if (o) {
73  return itr == o->itr;
74  }
75  } catch (const std::bad_cast&) {}
76  return false;
77  }
78  private:
79  iterator_type itr;
80  };
81 
82 public:
83 
84  template<typename iter_type>
85  IteratorRef(iter_type itr)
86  : itrref(std::auto_ptr<ItrRef>(new ItrRefImpl<iter_type>(itr)))
87  {}
88 
90  :itrref(itr.itrref->Clone())
91  {}
92 
94  itrref.reset(itr.itrref->Clone());
95  return *this;
96  }
97 
98  T &operator*() {
99  return itrref->Dereference();
100  }
101 
102  T *operator->() {
103  return &itrref->Dereference();
104  }
105 
107  IteratorRef copy = *this;
108  itrref->Increment();
109  return copy;
110  }
111 
113  itrref->Increment();
114  return *this;
115  }
116 
118  IteratorRef copy = *this;
119  itrref->Decrement();
120  return copy;
121  }
122 
124  itrref->Decrement();
125  return *this;
126  }
127 
128  bool operator==(const IteratorRef<T> &rhs) const {
129  return itrref->Equals(rhs.itrref.get());
130  }
131 
132  bool operator!=(const IteratorRef<T> &rhs) const {
133  return !itrref->Equals(rhs.itrref.get());
134  }
135 
136 private:
137  std::auto_ptr<ItrRef> itrref;
138 };
139 
140 
141 #endif
virtual void Increment()=0
IteratorRef(const IteratorRef &itr)
Definition: IteratorRef.h:89
bool Equals(const ItrRef *rhs) const
Definition: IteratorRef.h:69
bool operator!=(const IteratorRef< T > &rhs) const
Definition: IteratorRef.h:132
virtual ItrRef * Clone() const =0
virtual void Decrement()=0
virtual T & Dereference()=0
ItrRefImpl(iterator_type itr_)
Definition: IteratorRef.h:64
std::iterator_traits< iterator_type >::value_type value_type
Definition: IteratorRef.h:63
IteratorRef(iter_type itr)
Definition: IteratorRef.h:85
IteratorRef & operator=(const IteratorRef &itr)
Definition: IteratorRef.h:93
IteratorRef operator++()
Definition: IteratorRef.h:112
bool operator==(const IteratorRef< T > &rhs) const
Definition: IteratorRef.h:128
ItrRef * Clone() const
Definition: IteratorRef.h:68
value_type & Dereference()
Definition: IteratorRef.h:67
A reference to an iterator.
Definition: IteratorRef.h:46
IteratorRef operator--(int)
Definition: IteratorRef.h:117
virtual bool Equals(const ItrRef *rhs) const =0
IteratorRef operator--()
Definition: IteratorRef.h:123
T & operator*()
Definition: IteratorRef.h:98
IteratorRef operator++(int)
Definition: IteratorRef.h:106
std::auto_ptr< ItrRef > itrref
Definition: IteratorRef.h:137
T * operator->()
Definition: IteratorRef.h:102