libUTL++
TBidIt.h
1 UTL_NS_BEGIN;
2 
4 
16 
18 template <class T = Object>
19 class TBidIt final : public BidIt
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 T*
64  get() const
65  {
66  return utl::cast<T>(_it->get());
67  }
68 
69  virtual void
70  set(const Object* object)
71  {
72  _it->set(object);
73  }
74 
79  BidIt*
80  iterator() const
81  {
82  return _it;
83  }
84 
89  void
90  setIt(BidIt* it)
91  {
92  if (it != _it)
93  {
94  delete _it;
95  _it = it;
96  }
97  }
98 
103  T* operator*() const
104  {
105  return utl::cast<T>(_it->get());
106  }
107 
113  {
114  setIt(it);
115  return *this;
116  }
117 
118  // for STL
119  typedef T* value_type;
120  typedef T*& reference;
121  typedef T** pointer;
122  typedef std::bidirectional_iterator_tag iterator_category;
123  typedef std::ptrdiff_t difference_type;
124 
125 private:
126  void
127  init()
128  {
129  _it = nullptr;
130  }
131  void
132  deInit()
133  {
134  delete _it;
135  }
136 
137 private:
138  BidIt* _it;
139 };
140 
142 
143 template <class T>
144 int
145 TBidIt<T>::compare(const Object& rhs) const
146 {
147  int res;
148  if (rhs.isA(TBidIt<T>))
149  {
150  auto& it = utl::cast<TBidIt<T>>(rhs);
151  res = compareNullable(_it, it._it, nullptr);
152  }
153  else
154  {
155  auto& it = utl::cast<BidIt>(rhs);
156  res = compareNullable(_it, &it, nullptr);
157  }
158  return res;
159 }
160 
162 
163 template <class T>
164 void
166 {
167  if (rhs.isA(TBidIt<T>))
168  {
169  auto& it = utl::cast<TBidIt<T>>(rhs);
170  setIt(utl::clone(it._it));
171  }
172  else
173  {
174  auto& it = utl::cast<BidIt>(rhs);
175  setIt(it.clone());
176  }
177 }
178 
180 
181 template <class T>
182 void
184 {
185  if (rhs_.isA(TBidIt<T>))
186  {
187  auto& rhs = utl::cast<TBidIt<T>>(rhs_);
188  if (_it != nullptr) delete _it;
189  _it = rhs._it;
190  rhs._it = nullptr;
191  }
192  else
193  {
194  auto& rhs = utl::cast<BidIt>(rhs_);
195  if (_it == nullptr)
196  {
197  _it = rhs.clone();
198  }
199  else
200  {
201  _it->steal(rhs);
202  }
203  }
204 }
205 
207 
208 UTL_NS_END;
209 
211 
#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
T * operator*() const
Pointer dereference operator – return a (T*) instead of an (Object*).
Definition: TBidIt.h:103
void deInit()
De-initialize UTL++.
virtual const Object & getProxiedObject() const
Get the proxied object (= self if none).
Definition: TBidIt.h:40
virtual void reverse(size_t dist=1)
Move backward the given number of objects.
Definition: TBidIt.h:58
TBidIt(BidIt *it)
Constructor.
Definition: TBidIt.h:28
Templated proxy for BidIt.
Definition: TBidIt.h:19
TBidIt & operator=(BidIt *it)
Set the iterator.
Definition: TBidIt.h:112
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
int compareNullable(const Object *lhs, const Object *rhs, const Ordering *ordering=nullptr)
Compare two objects.
Definition: util_inl.h:466
Bi-directional iterator abstraction.
Definition: BidIt.h:25
#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 void forward(size_t dist=1)
Move forward the given number of objects.
Definition: TBidIt.h:52
void setIt(BidIt *it)
Set the iterator.
Definition: TBidIt.h:90
virtual Object & getProxiedObject()
Get the proxied object (= self if none).
Definition: TBidIt.h:46
BidIt * iterator() const
Get the proxied iterator.
Definition: TBidIt.h:80
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.