Flow123d  DF_patch_fe_data_tables-1a16803
general_iterator.hh
Go to the documentation of this file.
1 // --------------------------------------------------- GeneralIterator ---------------------------------
2 /*!
3  *
4  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License version 3 as published by the
8  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13  *
14  *
15  * @file general_iterator.hh
16  * @brief Template Iter serves as general template for internal iterators.
17  */
18 
19 #ifndef GENERAL_ITERATOR_HH_
20 #define GENERAL_ITERATOR_HH_
21 
22 /** @brief General iterator template.
23  * Provides iterator over objects of type Object in some container.
24  *
25  * Operators '*' and '->' returns objects of type ObjectOut
26  *
27  * Requires the template object to implement:
28  * - comparison operator==()
29  * - increment operator++()
30  */
31 template<class Object>
32 class Iter
33 {
34 public:
35 // IterConvert();
36 
37  Iter(const Object& object);
38 
39  /// equal operator
40  bool operator==(const Iter& other);
41  /// non-equal operator
42  bool operator!=(const Iter& other);
43 
44  /// * dereference operator
45  const Object& operator*() const;
46 
47  /// -> dereference operator
48  const Object* operator->() const;
49 
50  /// prefix increment
52 
53 protected:
54  Object object_;
55 };
56 
57 
58 /** @brief General iterator template.
59  * Provides iterator over objects of type ObjectIn in some container.
60  * Same as previous but allows conversion of output to type ObjectOut.
61  */
62 template<class ObjectIn, class ObjectOut>
63 class IterConvert : public Iter<ObjectIn>
64 {
65 public:
66 // IterConvert();
67 
68  IterConvert(const ObjectIn& object);
69 
70  /// * dereference operator
71  const ObjectOut& operator*() const;
72 
73  /// -> dereference operator
74  const ObjectOut* operator->() const;
75 
76 private:
77  mutable ObjectOut out_;
78 };
79 
80 
81 /**
82  * Create iterator from object
83  */
84 template<class Object>
85 Iter<Object> make_iter(Object obj) {
86  return Iter<Object>(obj);
87 }
88 
89 /**
90  * Create convertible iterator from object
91  */
92 template<class ObjectIn, class ObjectOut>
95 }
96 
97 
98 // --------------------------------------------------- Iter INLINE implementation -----------
99 // inline IterConvert::IterConvert()
100 // {}
101 
102 template<class Object>
103 inline Iter<Object>::Iter(const Object& object)
104 : object_(object)
105 {}
106 
107 template<class Object>
108 inline bool Iter<Object>::operator==(const Iter& other)
109 {
110  return (object_ == other.object_);
111 }
112 
113 template<class Object>
114 inline bool Iter<Object>::operator!=(const Iter& other)
115 {
116  return !( *this == other);
117 }
118 
119 template<class Object>
120 inline const Object& Iter<Object>::operator*() const
121 {
122  return object_;
123 }
124 
125 template<class Object>
126 inline const Object* Iter<Object>::operator->() const
127 {
128  return &object_;
129 }
130 
131 template<class Object>
133 {
134  object_.inc();
135  return (*this);
136 }
137 
138 template<class ObjectIn, class ObjectOut>
139 inline IterConvert<ObjectIn, ObjectOut>::IterConvert(const ObjectIn& object)
140 : Iter<ObjectIn>(object)
141 {}
142 
143 template<class ObjectIn, class ObjectOut>
144 inline const ObjectOut& IterConvert<ObjectIn, ObjectOut>::operator*() const
145 {
146  out_ = (ObjectOut)this->object_;
147  return out_;
148 }
149 
150 template<class ObjectIn, class ObjectOut>
151 inline const ObjectOut* IterConvert<ObjectIn, ObjectOut>::operator->() const
152 {
153  out_ = (ObjectOut)this->object_;
154  return &out_;
155 }
156 
157 #endif // GENERAL_ITERATOR_HH_
General iterator template. Provides iterator over objects of type ObjectIn in some container....
const ObjectOut & operator*() const
ObjectOut out_
const ObjectOut * operator->() const
-> dereference operator
IterConvert(const ObjectIn &object)
General iterator template. Provides iterator over objects of type Object in some container.
Iter & operator++()
prefix increment
bool operator!=(const Iter &other)
non-equal operator
bool operator==(const Iter &other)
equal operator
const Object * operator->() const
-> dereference operator
const Object & operator*() const
Object object_
Iter(const Object &object)
Iter< Object > make_iter(Object obj)