Class documentation of Concepts

Loading...
Searching...
No Matches
scannerConnectors.hh
Go to the documentation of this file.
1
6#ifndef scannerConnectors_hh
7#define scannerConnectors_hh
8
9#include <vector>
10#include "basics/typedefs.hh"
12#include "basics/debug.hh"
13#include "basics/exceptions.hh"
14#include "toolbox/array.hh"
15
16// debugging Joiner
17#define JoinerConstr_D 0
18#define JoinerDestr_D 0
19
20namespace concepts {
21
22 // ****************************************************************** Scan **
23
28 template <class T>
29 class Scan {
30 public:
31 virtual ~Scan() {};
32
34 operator int() { return !eos(); }
35
37 virtual bool eos() const = 0;
39 virtual T& operator++(int) = 0;
41 virtual Scan* clone() const = 0;
42 };
43
44 // ************************************************************* Joiner **
45
46 template<class T, unsigned nlnk>
47 class Joiner;
48
49 template<class T, unsigned nlnk>
50 std::ostream& operator<<(std::ostream& os,
51 const Joiner<T, nlnk>& j);
52
72 template<class T, unsigned nlnk>
73 class Joiner {
74 friend std::ostream& operator<< <>(std::ostream& os,
75 const Joiner<T, nlnk>& j);
76 public:
83 inline Joiner(const T& val, Joiner* lnk = 0);
84
88 inline ~Joiner() {}
89
95 static void destructor(Joiner<T, nlnk>*& j, bool values = true);
96
101 inline Joiner*& operator[](uint i) {
103 return lnk_[i];
104 }
105
107 inline T& value() { return val_; }
108
109 private:
111 Joiner<T, nlnk>* lnk_[nlnk];
112
114 T val_;
115
117 Joiner(const Joiner&);
118 void operator =(const Joiner&);
119
121 static void destructor_(Joiner<T, nlnk>* j, bool values = true);
122 };
123
124 template <class T, unsigned nlnk>
126 val_(val) {
127 DEBUGL(JoinerConstr_D, "start.");
128 for(uint i = 0; i < nlnk - 1; ++i)
129 lnk_[i] = 0;
130 lnk_[nlnk - 1] = lnk;
131 DEBUGL(JoinerConstr_D, "done.");
132 }
133
134 template <class T, unsigned nlnk>
136 DEBUGL(JoinerDestr_D, "start.");
137 if (nlnk > 1) {
138 if (j) {destructor_(j); j = 0;}
139 }
140 else {
141 while (j) {
142 Joiner<T, 1>* foo = j;
143 j = j->lnk_[0];
144 if (values) delete foo->val_;
145 delete foo;
146 }
147 }
148 DEBUGL(JoinerDestr_D, "done.");
149 }
150
151 template <class T, unsigned nlnk>
152 void Joiner<T, nlnk>::destructor_(Joiner<T, nlnk>* j, bool values) {
153 for(uint i = 0; i < nlnk; i++) {
154 if (j->lnk_[i]) destructor_(j->lnk_[i]);
155 }
156 if (values) delete j->val_;
157 delete j;
158 }
159
160 template<class T, unsigned nlnk>
161 std::ostream& operator<<(std::ostream& os, const Joiner<T, nlnk>& j) {
162 os << "J(" << j.val_ << " -> [" << std::flush;
163 for(uint i = 0; i < nlnk-1; ++i)
164 if (j.lnk_[i])
165 os << *j.lnk_[i] << ", " << std::flush;
166 if (j.lnk_[nlnk-1])
167 os << *j.lnk_[nlnk-1] << std::flush;
168 return os << "])" << std::flush;
169 }
170
171 // ************************************************************* PListScan **
172
175 template <class T>
176 class PListScan : public Scan<T> {
178 Joiner<T*, 1>* head_;
179
181 Joiner<T*, 1>* current_;
182
184 bool eos_;
185 public:
190 head_(&cnr), current_(&cnr), eos_(head_ ? false : true) {}
191
192 bool eos() const { return eos_; }
193
194 PListScan<T>* clone() const { return new PListScan<T>(*this); }
195
196 T& operator++(int);
197 };
198
199 template <class T>
201
202 T* tmp = 0;
203 if (!eos_) {
204 tmp = current_->value();
205 current_ = (*current_)[0];
206 if ((eos_ = current_ == 0)) current_ = head_;
207 }
208 return *tmp;
209 }
210
211 // ************************************************************** ListScan **
212
215 template <class T>
216 class ListScan : public Scan<T> {
218 Joiner<T, 1>* head_;
219
221 Joiner<T, 1>* current_;
222
224 bool eos_;
225 public:
230 head_(&cnr), current_(&cnr), eos_(head_ ? false : true) {}
231
232 bool eos() const { return eos_; }
233
234 ListScan<T>* clone() const { return new ListScan<T>(*this); }
235
236 T& operator++(int);
237 };
238
239 template <class T>
241
242 T& tmp = current_->value();
243
244 if (!eos_) {
245 current_ = (*current_)[0];
246 if ((eos_ = current_ == 0)) current_ = head_;
247 }
248
249 return tmp;
250 }
251
252 // ******************************************************** PStlVectorScan **
253
256 template<class T>
257 class PStlVectorScan : public Scan<T> {
258 public:
259 inline PStlVectorScan(typename std::vector<T*>& v)
260 : i_(v.begin()), last_(v.end()) {}
261 inline PStlVectorScan(typename std::vector<T*>::iterator first,
262 typename std::vector<T*>::const_iterator last)
263 : i_(first), last_(last) {}
264 inline PStlVectorScan(const PStlVectorScan<T> &scan)
265 : i_(scan.i_), last_(scan.last_) {}
266 inline bool eos() const { return i_ == last_; }
267 inline T& operator++(int) { return **i_++; }
268 inline PStlVectorScan<T>* clone() const { return new PStlVectorScan(*this); }
269 private:
270 typename std::vector<T*>::iterator i_;
271 typename std::vector<T*>::const_iterator last_;
272 };
273
274 // ********************************************************* StlVectorScan **
275
281 class StlVectorScan : public Scan<T> {
282 typedef std::vector<T*> Vector;
283
284 const Vector& vec;
285 ItType it;
286
287
288 public:
293 StlVectorScan(const Vector& vec, ItType it)
294 : vec(vec), it(it)
295 {}
296
300 StlVectorScan(Vector& vec)
301 : vec(vec), it(vec.begin())
302 {}
303
304 bool eos() const { return it == vec.end(); }
305
306 StlVectorScan* clone() const { return new StlVectorScan(*this); }
307
308 T& operator*() {
309 return **it;
310 }
311
313 T& operator++(int) {
314 T* old = *it;
315 ++it;
316 return *old;
317 }
318
321 return *(++it);
322 }
323
324 };
325
326 // ************************************************************* ArrayScan **
327
332 template<class T>
333 class ArrayScan : public Scan<T> {
334 public:
336 : idx_(0), container_(container) {}
337 inline ArrayScan(const ArrayScan& scan) :
338 idx_(scan.idx_), container_(scan.container_) {}
339 inline bool eos() const { return idx_ == container_.size(); }
340 inline T& operator++(int) { return container_[idx_++]; }
341 inline Scan<T>* clone() const { return new ArrayScan(*this); }
342 private:
343 uint idx_;
344 Array<T>& container_;
345 };
346
347 // *********************************************************** SingletonScan **
348
351 template<class T>
352 class SingletonScan : public concepts::Scan<T> {
353 public:
355 singleton_(singleton), eos_(false) {}
356
358 singleton_(other.singleton_), eos_(other.eos_) {}
359
360 bool eos() const /*override*/ { return eos_; }
361
362 T& operator++ (int) /*override*/ {
364 eos_ = true;
365 return singleton_;
366 }
367
368 SingletonScan* clone () const /*override*/ {
369 return new SingletonScan(*this);
370 }
371
372 private:
373 T& singleton_;
374 bool eos_;
375 };
376
377} // namespace concepts
378
379#endif // scannerConnectors_hh
bool eos() const
Returns true if the end of the scanned set is reached.
Scan< T > * clone() const
Returns a clone of the scanner.
T & operator++(int)
Returns the next element in the scanned set.
uint size() const
Returns the requested size of the array.
Definition array.hh:259
Joiner *& operator[](uint i)
T & value()
Returns the content of the container.
Joiner(const T &val, Joiner *lnk=0)
static void destructor(Joiner< T, nlnk > *&j, bool values=true)
T & operator++(int)
Returns the next element in the scanned set.
ListScan(Joiner< T, 1 > &cnr)
ListScan< T > * clone() const
Returns a clone of the scanner.
bool eos() const
Returns true if the end of the scanned set is reached.
PListScan(Joiner< T *, 1 > &cnr)
bool eos() const
Returns true if the end of the scanned set is reached.
T & operator++(int)
Returns the next element in the scanned set.
PListScan< T > * clone() const
Returns a clone of the scanner.
bool eos() const
Returns true if the end of the scanned set is reached.
T & operator++(int)
Returns the next element in the scanned set.
PStlVectorScan< T > * clone() const
Returns a clone of the scanner.
virtual T & operator++(int)=0
Returns the next element in the scanned set.
virtual bool eos() const =0
Returns true if the end of the scanned set is reached.
virtual Scan * clone() const =0
Returns a clone of the scanner.
T & operator++(int)
Returns the next element in the scanned set.
bool eos() const
Returns true if the end of the scanned set is reached.
SingletonScan * clone() const
Returns a clone of the scanner.
StlVectorScan(const Vector &vec, ItType it)
StlVectorScan * clone() const
Returns a clone of the scanner.
T & operator++()
pre-increment operator
bool eos() const
Returns true if the end of the scanned set is reached.
T & operator++(int)
post-increment operator
#define conceptsAssert(cond, exc)
#define DEBUGL(doit, msg)
Definition debug.hh:40
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320