libUTL++
TFwdIt.h
1 UTL_NS_BEGIN;
2 
4 
16 
18 template <class T = Object>
19 class TFwdIt final : public FwdIt
20 {
22 
23 public:
29  {
30  _it = it;
31  }
32 
33  virtual int compare(const Object& rhs) const;
34 
35  virtual void copy(const Object& rhs);
36 
37  virtual void steal(Object& rhs);
38 
39  virtual const Object&
41  {
42  return *_it;
43  }
44 
45  virtual Object&
47  {
48  return *_it;
49  }
50 
51  virtual void
52  forward(size_t dist = 1)
53  {
54  _it->forward(dist);
55  }
56 
57  virtual T*
58  get() const
59  {
60  return utl::cast<T>(_it->get());
61  }
62 
63  virtual void
64  set(const Object* object)
65  {
66  _it->set(object);
67  }
68 
73  FwdIt*
74  iterator() const
75  {
76  return _it;
77  }
78 
83  void
84  setIt(FwdIt* it)
85  {
86  if (it != _it)
87  {
88  delete _it;
89  _it = it;
90  }
91  }
92 
97  T* operator*() const
98  {
99  return utl::cast<T>(_it->get());
100  }
101 
107  {
108  setIt(it);
109  return *this;
110  }
111 
112  // for STL
113  typedef T* value_type;
114  typedef T*& reference;
115  typedef T** pointer;
116  typedef std::forward_iterator_tag iterator_category;
117  typedef std::ptrdiff_t difference_type;
118 
119 private:
120  void
121  init()
122  {
123  _it = nullptr;
124  }
125  void
126  deInit()
127  {
128  delete _it;
129  }
130 
131 private:
132  FwdIt* _it;
133 };
134 
136 
137 template <class T>
138 int
139 TFwdIt<T>::compare(const Object& rhs) const
140 {
141  int res;
142  if (rhs.isA(TFwdIt<T>))
143  {
144  auto& it = utl::cast<TFwdIt<T>>(rhs);
145  res = compareNullable(_it, it._it, nullptr);
146  }
147  else
148  {
149  auto& it = utl::cast<FwdIt>(rhs);
150  res = compareNullable(_it, &it, nullptr);
151  }
152  return res;
153 }
154 
156 
157 template <class T>
158 void
160 {
161  if (rhs.isA(TFwdIt<T>))
162  {
163  auto& it = utl::cast<TFwdIt<T>>(rhs);
164  setIt(utl::clone(it._it));
165  }
166  else
167  {
168  auto& it = utl::cast<FwdIt>(rhs);
169  setIt(it.clone());
170  }
171 }
172 
174 
175 template <class T>
176 void
178 {
179  if (rhs_.isA(TFwdIt<T>))
180  {
181  auto& rhs = utl::cast<TFwdIt<T>>(rhs_);
182  if (_it != nullptr) delete _it;
183  _it = rhs._it;
184  rhs._it = nullptr;
185  }
186  else
187  {
188  auto& rhs = utl::cast<FwdIt>(rhs_);
189  if (_it == nullptr)
190  {
191  _it = rhs.clone();
192  }
193  else
194  {
195  _it->steal(rhs);
196  }
197  }
198 }
199 
201 
202 UTL_NS_END;
203 
205 
#define UTL_CLASS_IMPL_TPL(className, T)
Implementation of standard UTL++ functionality for a template class.
Definition: macros.h:906
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
void deInit()
De-initialize UTL++.
T * operator*() const
Pointer dereference operator – return a (T*) instead of an (Object*).
Definition: TFwdIt.h:97
virtual void forward(size_t dist=1)
Move forward the given number of objects.
Definition: TFwdIt.h:52
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
virtual const Object & getProxiedObject() const
Get the proxied object (= self if none).
Definition: TFwdIt.h:40
Templated proxy for FwdIt.
Definition: TFwdIt.h:19
virtual Object & getProxiedObject()
Get the proxied object (= self if none).
Definition: TFwdIt.h:46
Forward iterator abstraction.
Definition: FwdIt.h:26
int compareNullable(const Object *lhs, const Object *rhs, const Ordering *ordering=nullptr)
Compare two objects.
Definition: util_inl.h:466
TFwdIt(FwdIt *it)
Constructor.
Definition: TFwdIt.h:28
FwdIt * iterator() const
Get the proxied iterator.
Definition: TFwdIt.h:74
#define UTL_CLASS_DECL_TPL(DC, T, BC)
Declaration of standard UTL++ functionality for a template class with one parameter.
Definition: macros.h:713
virtual void steal(Object &rhs)
"Steal" the internal representation from another instance.
TFwdIt & operator=(FwdIt *it)
Set the iterator.
Definition: TFwdIt.h:106
void setIt(FwdIt *it)
Set the iterator.
Definition: TFwdIt.h:84
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.