libUTL++
TRandIt.h
1 UTL_NS_BEGIN;
2 
4 
16 
18 template <class T = Object>
19 class TRandIt final : public RandIt
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 void
58  reverse(size_t dist = 1)
59  {
60  _it->reverse(dist);
61  }
62 
63  virtual void
64  seek(size_t offset)
65  {
66  _it->seek(offset);
67  }
68 
69  virtual T*
70  get() const
71  {
72  return utl::cast<T>(_it->get());
73  }
74 
75  virtual T*
76  get(size_t offset) const
77  {
78  return _it->get(offset);
79  }
80 
81  virtual size_t
82  offset() const
83  {
84  return _it->offset();
85  }
86 
87  virtual void
88  set(size_t offset, const Object* object)
89  {
90  _it->set(offset, object);
91  }
92 
93  virtual void
94  set(const Object* object)
95  {
96  _it->set(object);
97  }
98 
99  virtual size_t
100  size() const
101  {
102  return _it->size();
103  }
104 
105  virtual size_t
106  subtract(const RandIt& it) const
107  {
108  return _it->subtract(it);
109  }
110 
115  RandIt*
116  iterator() const
117  {
118  return _it;
119  }
120 
125  void
127  {
128  if (it != _it)
129  {
130  delete _it;
131  _it = it;
132  }
133  }
134 
139  T* operator*() const
140  {
141  return utl::cast<T>(_it->get());
142  }
143 
149  {
150  setIt(it);
151  return *this;
152  }
153 
154  // for STL
155  typedef T* value_type;
156  typedef T*& reference;
157  typedef T** pointer;
158  typedef std::random_access_iterator_tag iterator_category;
159  typedef std::ptrdiff_t difference_type;
160 
161 private:
162  void
163  init()
164  {
165  _it = nullptr;
166  }
167  void
168  deInit()
169  {
170  delete _it;
171  }
172 
173 private:
174  RandIt* _it;
175 };
176 
178 
179 template <class T>
180 int
181 TRandIt<T>::compare(const Object& rhs) const
182 {
183  int res;
184  if (rhs.isA(TRandIt<T>))
185  {
186  auto& it = utl::cast<TRandIt<T>>(rhs);
187  res = compareNullable(_it, it._it, nullptr);
188  }
189  else
190  {
191  auto& it = utl::cast<RandIt>(rhs);
192  res = compareNullable(_it, &it, nullptr);
193  }
194  return res;
195 }
196 
198 
199 template <class T>
200 void
202 {
203  if (rhs.isA(TRandIt<T>))
204  {
205  auto& it = utl::cast<TRandIt<T>>(rhs);
206  setIt(utl::clone(it._it));
207  }
208  else
209  {
210  auto& it = utl::cast<RandIt>(rhs);
211  setIt(it.clone());
212  }
213 }
214 
216 
217 template <class T>
218 void
220 {
221  if (rhs_.isA(TRandIt<T>))
222  {
223  auto& rhs = utl::cast<TRandIt<T>>(rhs_);
224  if (_it != nullptr) delete _it;
225  _it = rhs._it;
226  rhs._it = nullptr;
227  }
228  else
229  {
230  auto& rhs = utl::cast<RandIt>(rhs_);
231  if (_it == nullptr)
232  {
233  _it = rhs.clone();
234  }
235  else
236  {
237  _it->steal(rhs);
238  }
239  }
240 }
241 
243 
244 UTL_NS_END;
245 
247 
#define UTL_CLASS_IMPL_TPL(className, T)
Implementation of standard UTL++ functionality for a template class.
Definition: macros.h:906
virtual size_t offset() const
Get the current offset.
Definition: TRandIt.h:82
virtual size_t size() const
Return the current size of the sequence.
Definition: TRandIt.h:100
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
void deInit()
De-initialize UTL++.
TRandIt & operator=(RandIt *it)
Set the iterator.
Definition: TRandIt.h:148
virtual const Object & getProxiedObject() const
Get the proxied object (= self if none).
Definition: TRandIt.h:40
Random-access iterator abstraction.
Definition: RandIt.h:25
RandIt * iterator() const
Get the proxied iterator.
Definition: TRandIt.h:116
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
void setIt(RandIt *it)
Set the iterator.
Definition: TRandIt.h:126
virtual size_t subtract(const RandIt &it) const
Determine the distance between self and another random-access iterator for the same sequence...
Definition: TRandIt.h:106
Templated proxy for RandIt.
Definition: TRandIt.h:19
int compareNullable(const Object *lhs, const Object *rhs, const Ordering *ordering=nullptr)
Compare two objects.
Definition: util_inl.h:466
TRandIt(RandIt *it)
Constructor.
Definition: TRandIt.h:28
virtual void seek(size_t offset)
Seek to the given offset.
Definition: TRandIt.h:64
#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.
virtual Object & getProxiedObject()
Get the proxied object (= self if none).
Definition: TRandIt.h:46
virtual void reverse(size_t dist=1)
Move backward the given number of objects.
Definition: TRandIt.h:58
virtual void forward(size_t dist=1)
Move forward the given number of objects.
Definition: TRandIt.h:52
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.
T * operator*() const
Pointer dereference operator – return a T* instead of an Object*.
Definition: TRandIt.h:139