libUTL++
TCollection.h
1 #pragma once
2 
4 
5 #include <libutl/Collection.h>
6 #include <libutl/AutoPtr.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
26 
28 template <class T = Object>
29 class TCollection : public Object
30 {
32 
33 public:
36  {
37  _col = nullptr;
38  setCollection(col);
39  }
40 
41  virtual int
42  compare(const Object& rhs) const
43  {
44  auto rhsCol = findRHS(rhs);
45  if (_col == rhsCol) return 0;
46  if (rhsCol == nullptr) return 1;
47  if (_col == nullptr) return -1;
48  return _col->compare(*rhsCol);
49  }
50 
51  virtual void
52  copy(const Object& rhs)
53  {
54  auto rhsCol = findRHS(rhs);
55  if (_col == rhsCol) return;
56  setCollection(nullptr);
57  if (rhsCol == nullptr) return;
58  _col = rhsCol->clone();
59  }
60 
61  void
62  assertOwner()
63  {
64  col()->assertOwner();
65  }
66 
67  virtual void
68  dump(Stream& os, uint_t level = uint_t_max) const
69  {
70  col()->dump(os, level);
71  }
72 
73  virtual void
74  serialize(Stream& stream, uint_t io, uint_t mode = ser_default)
75  {
76  col()->serialize(stream, io, mode);
77  }
78 
79  void
80  serialize(const RunTimeClass* rtc, Stream& stream, uint_t io, uint_t mode = ser_default)
81  {
82  col()->serialize(rtc, stream, io, mode);
83  }
84 
85  bool
86  update(const Object* object)
87  {
88  return col()->update(object);
89  }
90 
91  bool
92  isOwner() const
93  {
94  return col()->isOwner();
95  }
96 
97  void
98  setOwner(bool owner)
99  {
100  col()->setOwner(owner);
101  }
102 
103  const Ordering*
104  ordering() const
105  {
106  return col()->ordering();
107  }
108 
109  Ordering*
110  ordering()
111  {
112  return col()->ordering();
113  }
114 
115  void
116  setOrdering(const Ordering& ordering, uint_t algorithm = sort_quickSort)
117  {
118  col()->setOrdering(ordering, algorithm);
119  }
120 
121  void
122  setOrdering(Ordering* ordering, uint_t algorithm = sort_quickSort)
123  {
124  col()->setOrdering(ordering, algorithm);
125  }
126 
127  bool
128  empty() const
129  {
130  return col()->empty();
131  }
132 
133  bool
134  isMultiSet() const
135  {
136  return col()->isMultiSet();
137  }
138 
139  void
140  setMultiSet(bool multiSet)
141  {
142  col()->setMultiSet(multiSet);
143  }
144 
145  size_t
146  items() const
147  {
148  return col()->items();
149  }
150 
151  size_t
152  size() const
153  {
154  return col()->size();
155  }
156 
157  bool
158  add(const Object& object)
159  {
160  return col()->add(object);
161  }
162 
163  bool
164  add(const Object* object)
165  {
166  return col()->add(object);
167  }
168 
169  void
170  add(const Collection& collection)
171  {
172  col()->add(collection);
173  }
174 
175  TBidIt<T>
176  addIt(const Object* object)
177  {
178  return TBidIt<T>(col()->addIt(object));
179  }
180 
181  void
182  addOrUpdate(const Object& object)
183  {
184  col()->addOrUpdate(object);
185  }
186 
187  void
188  addOrUpdate(const Object* object)
189  {
190  col()->addOrUpdate(object);
191  }
192 
194  copyItems(const Collection* src, const Predicate* pred = nullptr, bool predVal = true)
195  {
196  col()->copyItems(src, pred, predVal);
197  return self;
198  }
199 
201  copyItems(const ListNode* src, const Predicate* pred = nullptr, bool predVal = true)
202  {
203  col()->copyItems(src, pred, predVal);
204  return self;
205  }
206 
208  copyItems(const SlistNode* src, const Predicate* pred = nullptr, bool predVal = true)
209  {
210  col()->copyItems(src, pred, predVal);
211  return self;
212  }
213 
214  TCollection<T>& operator+=(const Object& rhs)
215  {
216  col()->operator+=(rhs);
217  return self;
218  }
219 
220  TCollection<T>& operator+=(const Object* rhs)
221  {
222  col()->operator+=(rhs);
223  return self;
224  }
225 
226  TCollection<T>& operator+=(const Collection& rhs)
227  {
228  col()->operator+=(rhs);
229  return self;
230  }
231 
232  TCollection<T>& operator-=(const Object& rhs)
233  {
234  col()->operator-=(rhs);
235  return self;
236  }
237 
238  void
239  dump(Stream& os,
240  bool key,
241  bool printClassName,
242  uint_t indent,
243  const char* separator,
244  uint_t level) const
245  {
246  col()->dump(os, level, key, printClassName, indent, separator);
247  }
248 
249  virtual String
250  toString() const
251  {
252  return col()->toString();
253  }
254 
255  String
256  toString(bool key) const
257  {
258  return col()->toString(key);
259  }
260 
261  String
262  toString(const char* sep, bool key = false) const
263  {
264  return col()->toString(sep, key);
265  }
266 
267  String
268  toString(const String& sep, bool key = false) const
269  {
270  return col()->toString(sep, key);
271  }
272 
273  TBidIt<T>
274  begin() const
275  {
276  return TBidIt<T>(col()->beginNew());
277  }
278 
279  TBidIt<T>
280  begin()
281  {
282  return TBidIt<T>(col()->beginNew());
283  }
284 
285  BidIt*
286  beginNew() const
287  {
288  return col()->beginNew();
289  }
290 
291  BidIt*
292  beginNew()
293  {
294  return col()->beginNew();
295  }
296 
297  BidIt*
298  createIt() const
299  {
300  return col()->createIt();
301  }
302 
303  BidIt*
304  createIt()
305  {
306  return col()->createIt();
307  }
308 
309  TBidIt<T>
310  end() const
311  {
312  return TBidIt<T>(col()->endNew());
313  }
314 
315  TBidIt<T>
316  end()
317  {
318  return TBidIt<T>(col()->endNew());
319  }
320 
321  BidIt*
322  endNew() const
323  {
324  return col()->endNew();
325  }
326 
327  BidIt*
328  endNew()
329  {
330  return col()->endNew();
331  }
332 
333  bool
334  contains(const Object* key) const
335  {
336  return col()->contains(key);
337  }
338 
339  bool
340  contains(const Object& key) const
341  {
342  return col()->contains(key);
343  }
344 
345  size_t
346  count(const Predicate* pred = nullptr, bool predVal = true) const
347  {
348  return col()->count(pred, predVal);
349  }
350 
351  T*
352  find(const Object* key) const
353  {
354  return utl::cast<T>(col()->find(key));
355  }
356 
357  T*
358  find(const Object& key) const
359  {
360  return utl::cast<T>(col()->find(key));
361  }
362 
363  void
364  findIt(const Object& key, BidIt& it) const
365  {
366  return col()->findIt(key, it);
367  }
368 
369  void
370  findIt(const Object& key, BidIt& it)
371  {
372  return col()->findIt(key, it);
373  }
374 
375  T*
376  first() const
377  {
378  return utl::cast<T>(col()->first());
379  }
380 
381  bool
382  has(const Object* key) const
383  {
384  return col()->has(key);
385  }
386 
387  bool
388  has(const Object& key) const
389  {
390  return col()->has(key);
391  }
392 
393  T*
394  last() const
395  {
396  return utl::cast<T>(col()->last());
397  }
398 
399  void
400  clear()
401  {
402  col()->clear();
403  }
404 
405  bool
406  remove(const Object* key)
407  {
408  ASSERTD(key != nullptr);
409  return remove(*key);
410  }
411 
412  virtual bool
413  remove(const Object& key)
414  {
415  return col()->remove(key);
416  }
417 
418  virtual void
419  removeIt(BidIt& it)
420  {
421  col()->removeIt(it);
422  }
423 
424  T*
425  take(BidIt& it)
426  {
427  return (T*)(col()->take(it));
428  }
429 
430  operator Collection&()
431  {
432  return *col();
433  }
434 
435  operator const Collection&() const
436  {
437  return *col();
438  }
439 
440  operator Collection*()
441  {
442  return _col;
443  }
444 
445  operator const Collection*() const
446  {
447  return _col;
448  }
449 
450 public:
451  typedef T type;
452  typedef TBidIt<T> iterator;
453  typedef T* value_type; // for STL
454 protected:
455  const Collection*
456  col() const
457  {
458  ASSERTD(_col != nullptr);
459  return _col;
460  }
461 
462  Collection*
463  col()
464  {
465  ASSERTD(_col != nullptr);
466  return _col;
467  }
468 
469  void
470  setCollection(Collection* col)
471  {
472  if (col == _col) return;
473  delete _col;
474  _col = col;
475  }
476 
477  static const Collection*
478  findRHS(const Object& rhs)
479  {
480  if (rhs.isA(TCollection<T>))
481  {
482  const TCollection<T>& rhsTCol = (const TCollection<T>&)rhs;
483  return rhsTCol._col;
484  }
485  else
486  {
487  return utl::cast<Collection>(&rhs);
488  }
489  }
490 
491 private:
492  void
493  init()
494  {
495  _col = nullptr;
496  }
497  void
498  deInit()
499  {
500  delete _col;
501  }
502 
503 private:
504  Collection* _col;
505 };
506 
508 
509 UTL_NS_END;
510 
512 
List node.
Definition: ListNode.h:28
#define UTL_CLASS_IMPL_TPL(className, T)
Implementation of standard UTL++ functionality for a template class.
Definition: macros.h:906
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
void deInit()
De-initialize UTL++.
Object comparison abstraction.
Definition: Ordering.h:30
default representation (via getSerializeMode())
Definition: util.h:75
Store information about a class.
Definition: RunTimeClass.h:23
quick sort
Definition: algorithms.h:25
Character string.
Definition: String.h:31
Graph vertex.
Definition: Vertex.h:32
Templated proxy for BidIt.
Definition: TBidIt.h:19
virtual void dump(Stream &os, uint_t level=uint_t_max) const
Dump a human-readable representation of self to the given output stream.
Definition: TCollection.h:68
virtual void copy(const Object &rhs)
Copy another instance.
Definition: TCollection.h:52
Logical predicate abstraction.
Definition: Predicate.h:26
virtual void serialize(Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize to or from a stream.
Definition: TCollection.h:74
Templated proxy for Collection.
Definition: TCollection.h:29
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
const uint_t uint_t_max
Maximum uint_t value.
Stream I/O abstraction.
Definition: Stream.h:68
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 int compare(const Object &rhs) const
Compare with another object.
Definition: TCollection.h:42
virtual String toString() const
Return a string representation of self.
Definition: TCollection.h:250
Singly-linked list node.
Definition: SlistNode.h:26
Root of UTL++ class hierarchy.
Definition: Object.h:52
void dump(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t level=uint_t_max, bool key=false, bool printClassName=false, uint_t indent=0, const char *separator=nullptr)
Dump objects to the given stream (with Object::dump()).
void init()
Initialize UTL++.
virtual String toString() const
Return a string representation of self.
TCollection(Collection *col)
Constructor.
Definition: TCollection.h:35
#define ASSERTD
Do an assertion in DEBUG mode only.
size_t count(const FwdIt &begin, const FwdIt &end, const Predicate *pred=nullptr, bool predVal=true)
Count objects that satisfy a Predicate.
A collection of objects.
Definition: Collection.h:62