CastableIterator.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #include <typeinfo>
32 #include <stdexcept>
33 #include <iostream>
34 #include <vector>
35 #include <string>
36 
37 #include "Castable.h"
38 
39 #ifndef CASTABLE_ITERATOR
40 #define CASTABLE_ITERATOR
41 
42 // // using namespace std;
43 
44 namespace drain {
45 
46 
47 
48 
50 
55 // Consider inheriting Caster directly now that it has a pointer
56 // Consider adding iterators directly to members or begin/end methods of Castable.
57 class CastableIterator : protected Castable { //private Castable {
58 
59 public:
60 
62  setPtr(NULL, typeid(void));
63  }
64 
66  setPtr(it.caster.ptr, it.getType());
67  }
68 
69  CastableIterator(void *p, const std::type_info & t){
70  setPtr(p, t);
71  }
72 
73 
74  template <class T>
75  CastableIterator(T *p) : Castable(p) {
76  //std::cerr << "CastableIterator(T *p), T:"<< typeid(T).name() << "\n";
77  }
78 
79 
80 
81  CastableIterator(std::string *p){
82  throw std::runtime_error("CastableIterator(std::string *p): std::string iterators not implemented");
83  }
84 
85 
86 
87  CastableIterator(void *p){
88  throw std::runtime_error("CastableIterator(void *p): void iterators not allowed");
89  }
90 
91  virtual inline
92  ~CastableIterator(){};
93 
95  inline
97  if (&it != this){
98  // std::cerr << "CastableIterator operator=(const & CastableIterator) \n";
99  setPtr(it.caster.ptr, it.getType());
100  }
101  return *this;
102  }
103 
104 
105  // Problematic. Compare with (void *p).
107  template <class T>
108  inline
110  //std::cerr << "CastableIterator::operator=, type:"<< typeid(T).name() << "\n";
111  if (typeid(T) == typeid(void)){
112  // Does not change type.
113  // What about elementCount?
114  caster.ptr = p;
115  elementCount = 1;
116  }
117  else {
118  if (typeIsSet() && (getType() != typeid(T))){
119  // Type info provided, set also type.
120  setPtr(p, typeid(T));
121  //std::cerr << "Warning"
122  throw std::runtime_error("CastableIterator operator=(T *p): implicit type change requested");
123  }
124  else { // type unset, or no change
125  setPtr(p, typeid(T));
126  }
127  }
128  return *this;
129  }
130 
131 
133  /*
134  inline
135  CastableIterator & operator=(void *p){
136  //std::cerr << "CastableIterator::operator=(void *):\n";
137  //setPtr(p,getType());
138  ptr = p;
139  return *this;
140  }
141  */
142 
143  inline
144  void setType(const std::type_info &type){
145  caster.setType(type);
146  };
147 
148  inline
149  const std::type_info & getType() const {
150  return caster.getType();
151  };
152 
153 
154  inline
155  bool operator==(const CastableIterator &it) const {
156  return (caster.ptr == it.caster.ptr);
157  };
158 
159  inline
160  bool operator!=(const CastableIterator &it) const {
161  return (caster.ptr != it.caster.ptr);
162  };
163 
164  inline
165  bool operator<(const CastableIterator &it) const {
166  return (caster.ptr < it.caster.ptr);
167  };
168 
169  inline
170  bool operator>(const CastableIterator &it) const {
171  return (caster.ptr > it.caster.ptr);
172  };
173 
174  inline
175  CastableIterator & operator++(){
176  char *cptr = (char *)caster.ptr;
177  caster.ptr = (cptr + caster.getElementSize());
178  return *this;
179  };
180 
182  inline
184  CastableIterator tmp = *this;
185  char *cptr = (char *)caster.ptr;
186  caster.ptr = (cptr + caster.getElementSize());
187  return tmp;
188  };
189 
190  inline
191  CastableIterator & operator--(){
192  char *cptr = (char *)caster.ptr;
193  caster.ptr = (cptr - caster.getElementSize());
194  return *this;
195  };
196 
198  inline
200  CastableIterator tmp = *this;
201  char *cptr = (char *)caster.ptr;
202  caster.ptr = (cptr - caster.getElementSize());
203  return tmp;
204  };
205 
206 
207  inline
208  const Castable & operator*() const {
209  return (const Castable &) *this;
210  };
211 
212 
213  inline
214  Castable & operator*(){
215  return (Castable &)*this;
216  };
217 
218  inline
219  operator const void*() const {
220  return caster.ptr;
221  };
222 
224  /*
225  inline
226  operator long() const {
227  return (long)caster.ptr;
228  };
229  */
230 
231 
233  template <class T>
234  operator T() const {
235  return (T)(caster.ptr);
236  }
237 
238 
239 
240 };
241 
242 inline
243 std::ostream & operator<<(std::ostream & ostr, const CastableIterator & it){
244  ostr << (long)it;
245  return ostr;
246 }
247 /*
248 class CastableIterator : public CastableIteratorBase {
249 
250 public:
251 
252  template <class T>
253  CastableIterator(T *p) : CastableIteratorBase(p) {
254  }
255 
256 
257  inline
258  Castable & operator*(){
259  return (Castable &)*this;
260  };
261 
262 
263 };
264 */
265 
266 /*
267 class CastableConstIterator : public CastableIteratorBase {
268 
269 public:
270 
271  template <class T>
272  CastableConstIterator(T *p) : CastableIteratorBase(p) {
273  }
274 
275 };
276 */
277 
278 
279 } // namespace drain
280 
281 
282 #endif
283 
284 
285 // Drain
Definition: CastableIterator.h:57
CastableIterator operator--(int)
Not recommended, use prefix operator--() instead.
Definition: CastableIterator.h:199
const std::type_info & getType() const
Return the storage type (base type) of the referenced variable.
Definition: CastableIterator.h:149
CastableIterator & operator=(const CastableIterator &it)
Moves iterator to address pointed by &it. Changes the internal type accordingly.
Definition: CastableIterator.h:96
void setType(const std::type_info &type)
Moves iterator to address *p.
Definition: CastableIterator.h:144
CastableIterator operator++(int)
Not recommended, use prefix operator++() instead.
Definition: CastableIterator.h:183
CastableIterator & operator=(T *p)
Moves iterator to address *p.
Definition: CastableIterator.h:109
Definition: Castable.h:76
size_t elementCount
Pointer to the data variable.
Definition: Castable.h:1209
void setPtr(void *p)
Stores the pointer and its storage type F. Assumes elementCount=1.
Definition: Castable.h:875
const std::type_info & getType() const
Returns type_info of the current type.
Definition: Caster.h:144
size_t getElementSize() const
Returns the size of the base type (size of an element, not of element array).
Definition: Caster.h:151
void setType(const std::type_info &t)
Calls setType<T>() for which typeid(T) = t.
Definition: Caster.h:119
void * ptr
Future member: enables setting Caster type.
Definition: Caster.h:247
Definition: DataSelector.cpp:1277