libUTL++
Predicate.h
1 #pragma once
2 
4 
5 #include <libutl/FlagsMI.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 // Predicate ///////////////////////////////////////////////////////////////////////////////////////
14 
24 
26 class Predicate : public Object, protected FlagsMI
27 {
30 
31 public:
33  bool
34  isOwner() const
35  {
36  return getFlag(flg_owner);
37  }
38 
40  void
41  setOwner(bool owner)
42  {
43  setFlag(flg_owner, owner);
44  }
45 
51  virtual int compare(const Object& rhs) const;
52 
58  virtual bool eval(const Object* object) const = 0;
59 
75  bool operator()(const Object* object) const
76  {
77  return eval(object);
78  }
79 
80 private:
81  enum flg_t
82  {
83  flg_owner
84  };
85 };
86 
88 // IsA /////////////////////////////////////////////////////////////////////////////////////////////
90 
101 
103 class IsA : public Predicate
104 {
106 
107 public:
112  IsA(const RunTimeClass* rtc)
113  {
114  _rtc = rtc;
115  }
116 
122  virtual bool
123  eval(const Object* object) const
124  {
125  if ((_rtc == nullptr) || (object == nullptr)) return false;
126  return object->_isA(_rtc);
127  }
128 
129 private:
130  void
131  init()
132  {
133  _rtc = nullptr;
134  }
135  void
136  deInit()
137  {
138  }
139  const RunTimeClass* _rtc;
140 };
141 
143 // UnaryPredicate //////////////////////////////////////////////////////////////////////////////////
145 
153 
155 class UnaryPredicate : public Predicate
156 {
158 
159 public:
165  UnaryPredicate(const Object& object, bool owner = true);
166 
172  UnaryPredicate(const Object* object, bool owner = true);
173 
175  void
177  {
178  setObject(nullptr);
179  }
180 
182  virtual void copy(const Object& rhs);
183 
184  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
185 
187  const Object*
188  getObject() const
189  {
190  return _object;
191  }
192 
194  void
195  setObject(const Object& object)
196  {
197  if (isOwner())
198  setObject(object.clone());
199  else
200  setObject(&object);
201  }
202 
204  void
205  setObject(const Object* object)
206  {
207  if (isOwner()) delete _object;
208  _object = const_cast<Object*>(object);
209  }
210 
211 protected:
212  Object* _object;
213 
214 private:
215  void
216  init()
217  {
218  _object = nullptr;
219  setOwner(true);
220  }
221  void
222  deInit()
223  {
224  clear();
225  }
226 };
227 
229 // BinaryPredicate /////////////////////////////////////////////////////////////////////////////////
231 
239 
242 {
244 
245 public:
252  BinaryPredicate(const Object& lhs, const Object& rhs, bool owner = true);
253 
260  BinaryPredicate(const Object* lhs, const Object* rhs, bool owner = true);
261 
263  void
265  {
266  setLHS(nullptr);
267  setRHS(nullptr);
268  }
269 
270  virtual void copy(const Object& rhs);
271 
272  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
273 
275  const Object*
276  getLHS() const
277  {
278  return _lhs;
279  }
280 
282  const Object*
283  getRHS() const
284  {
285  return _rhs;
286  }
287 
289  void
290  setLHS(const Object& lhs)
291  {
292  if (isOwner())
293  setLHS(lhs.clone());
294  else
295  setLHS(&lhs);
296  }
297 
299  void
300  setLHS(const Object* lhs)
301  {
302  if (isOwner()) delete _lhs;
303  _lhs = const_cast<Object*>(lhs);
304  }
305 
307  void
308  setRHS(const Object& lhs)
309  {
310  if (isOwner())
311  setRHS(lhs.clone());
312  else
313  setRHS(&lhs);
314  }
315 
317  void
318  setRHS(const Object* rhs)
319  {
320  if (isOwner()) delete _rhs;
321  _rhs = const_cast<Object*>(rhs);
322  }
323 
324 protected:
325  Object* _lhs;
326  Object* _rhs;
327 
328 private:
329  void
330  init()
331  {
332  _lhs = _rhs = nullptr;
333  setOwner(true);
334  }
335  void
336  deInit()
337  {
338  clear();
339  }
340 };
341 
343 // ComparisonPredicate /////////////////////////////////////////////////////////////////////////////
345 
353 
356 {
358 
359 public:
366  ComparisonPredicate(const Object& object, bool owner = true, const Ordering* ordering = nullptr)
367  : UnaryPredicate(object, owner)
368  {
369  _ordering = utl::clone(ordering);
370  }
371 
378  ComparisonPredicate(const Object* object, bool owner = true, const Ordering* ordering = nullptr)
379  : UnaryPredicate(object, owner)
380  {
381  _ordering = utl::clone(ordering);
382  }
383 
389  int
390  cmp(const Object* object) const
391  {
392  return compareNullable(object, _object, _ordering);
393  }
394 
396  const Ordering*
397  ordering() const
398  {
399  return _ordering;
400  }
401 
403  void
404  setOrdering(const Ordering& ordering)
405  {
406  delete _ordering;
407  _ordering = ordering.clone();
408  }
409 
411  void
413  {
414  delete _ordering;
415  _ordering = ordering;
416  }
417 
418 private:
419  void
420  init()
421  {
422  _ordering = nullptr;
423  }
424  void
425  deInit()
426  {
427  delete _ordering;
428  }
429 
430 private:
431  Ordering* _ordering;
432 };
433 
435 // Not /////////////////////////////////////////////////////////////////////////////////////////////
437 
448 
450 class Not : public UnaryPredicate
451 {
454 
455 public:
456  Not(const Predicate* pred, bool owner = true)
457  : UnaryPredicate(pred, owner)
458  {
459  }
460 
461  virtual bool
462  eval(const Object* object) const
463  {
464  if ((_object == nullptr) || (object == nullptr)) return false;
465  return !utl::cast<Predicate>(_object)->eval(object);
466  }
467 };
468 
470 // And /////////////////////////////////////////////////////////////////////////////////////////////
472 
483 
485 class And : public BinaryPredicate
486 {
489 
490 public:
497  And(const Predicate& lhs, const Predicate& rhs, bool owner = true)
498  : BinaryPredicate(lhs, rhs, owner)
499  {
500  }
501 
508  And(const Predicate* lhs, const Predicate* rhs, bool owner = true)
509  : BinaryPredicate(lhs, rhs, owner)
510  {
511  }
512 
517  virtual bool
518  eval(const Object* object) const
519  {
520  return lhs()->eval(object) && rhs()->eval(object);
521  }
522 
524  const Predicate*
525  lhs() const
526  {
527  return utl::cast<Predicate>(_lhs);
528  }
529 
531  const Predicate*
532  rhs() const
533  {
534  return utl::cast<Predicate>(_rhs);
535  }
536 };
537 
539 // Or //////////////////////////////////////////////////////////////////////////////////////////////
541 
552 
554 class Or : public BinaryPredicate
555 {
558 
559 public:
566  Or(const Predicate& lhs, const Predicate& rhs, bool owner = true)
567  : BinaryPredicate(lhs, rhs, owner)
568  {
569  }
570 
577  Or(const Predicate* lhs, const Predicate* rhs, bool owner = true)
578  : BinaryPredicate(lhs, rhs, owner)
579  {
580  }
581 
586  virtual bool
587  eval(const Object* object) const
588  {
589  return lhs()->eval(object) || rhs()->eval(object);
590  }
591 
593  const Predicate*
594  lhs() const
595  {
596  return utl::cast<Predicate>(_lhs);
597  }
598 
600  const Predicate*
601  rhs() const
602  {
603  return utl::cast<Predicate>(_rhs);
604  }
605 };
606 
608 // EqualTo /////////////////////////////////////////////////////////////////////////////////////////
610 
621 
624 {
627 
628 public:
636  EqualTo(const Object& object, bool owner = true, const Ordering* ordering = nullptr)
637  : ComparisonPredicate(object, owner, ordering)
638  {
639  }
640 
647  EqualTo(const Object* object, bool owner = true, const Ordering* ordering = nullptr)
648  : ComparisonPredicate(object, owner, ordering)
649  {
650  }
651 
656  virtual bool
657  eval(const Object* object) const
658  {
659  return (cmp(object) == 0);
660  }
661 };
662 
664 // LessThan ////////////////////////////////////////////////////////////////////////////////////////
666 
677 
680 {
683 
684 public:
692  LessThan(const Object& object, bool owner = true, const Ordering* ordering = nullptr)
693  : ComparisonPredicate(object, owner, ordering)
694  {
695  }
696 
703  LessThan(const Object* object, bool owner = true, const Ordering* ordering = nullptr)
704  : ComparisonPredicate(object, owner, ordering)
705  {
706  }
707 
712  virtual bool
713  eval(const Object* object) const
714  {
715  return (cmp(object) < 0);
716  }
717 };
718 
720 // LessThanOrEqualTo ///////////////////////////////////////////////////////////////////////////////
722 
734 
737 {
740 
741 public:
749  LessThanOrEqualTo(const Object& object, bool owner = true, const Ordering* ordering = nullptr)
750  : ComparisonPredicate(object, owner, ordering)
751  {
752  }
753 
760  LessThanOrEqualTo(const Object* object, bool owner = true, const Ordering* ordering = nullptr)
761  : ComparisonPredicate(object, owner, ordering)
762  {
763  }
764 
769  virtual bool
770  eval(const Object* object) const
771  {
772  return (cmp(object) <= 0);
773  }
774 };
775 
777 // GreaterThan /////////////////////////////////////////////////////////////////////////////////////
779 
790 
793 {
796 
797 public:
805  GreaterThan(const Object& object, bool owner = true, const Ordering* ordering = nullptr)
806  : ComparisonPredicate(object, owner, ordering)
807  {
808  }
809 
816  GreaterThan(const Object* object, bool owner = true, const Ordering* ordering = nullptr)
817  : ComparisonPredicate(object, owner, ordering)
818  {
819  }
820 
825  virtual bool
826  eval(const Object* object) const
827  {
828  return (cmp(object) > 0);
829  }
830 };
831 
833 // GreaterThanOrEqualTo ////////////////////////////////////////////////////////////////////////////
835 
847 
850 {
853 
854 public:
863  bool owner = true,
864  const Ordering* ordering = nullptr)
865  : ComparisonPredicate(object, owner, ordering)
866  {
867  }
868 
876  bool owner = true,
877  const Ordering* ordering = nullptr)
878  : ComparisonPredicate(object, owner, ordering)
879  {
880  }
881 
886  virtual bool
887  eval(const Object* object) const
888  {
889  return (cmp(object) >= 0);
890  }
891 };
892 
894 
895 UTL_NS_END;
896 
898 
904 inline utl::Not operator!(const utl::Predicate& pred)
905 {
906  return utl::Not(pred, false);
907 }
908 
910 
916 inline utl::And operator&&(const utl::Predicate& lhs, const utl::Predicate& rhs)
917 {
918  return utl::And(lhs, rhs, false);
919 }
920 
922 
928 inline utl::Or operator||(const utl::Predicate& lhs, const utl::Predicate& rhs)
929 {
930  return utl::Or(lhs, rhs, false);
931 }
bool isOwner() const
Get the ownership flag.
Definition: Predicate.h:34
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
bool operator()(const Object *object) const
Another way of writing eval.
Definition: Predicate.h:75
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
EqualTo(const Object *object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:647
Or(const Predicate *lhs, const Predicate *rhs, bool owner=true)
Constructor.
Definition: Predicate.h:577
LessThan(const Object *object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:703
void deInit()
De-initialize UTL++.
const Object * getLHS() const
Get the left-hand-side parameter.
Definition: Predicate.h:276
Logical or predicate.
Definition: Predicate.h:554
Object comparison abstraction.
Definition: Ordering.h:30
const Predicate * rhs() const
Get the right-hand-side predicate.
Definition: Predicate.h:532
Less-Than-Or-Equal-To predicate.
Definition: Predicate.h:736
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it is greater than the associated object.
Definition: Predicate.h:826
LessThanOrEqualTo(const Object &object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:749
Abstraction for two-parameter predicates.
Definition: Predicate.h:241
void setOrdering(Ordering *ordering)
Set the ordering.
Definition: Predicate.h:412
default representation (via getSerializeMode())
Definition: util.h:75
const Predicate * lhs() const
Get the left-hand-side predicate.
Definition: Predicate.h:594
Store information about a class.
Definition: RunTimeClass.h:23
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
void setRHS(const Object *rhs)
Set the right-hand-side parameter.
Definition: Predicate.h:318
Less-Than predicate.
Definition: Predicate.h:679
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
void clear()
See Object::clear().
Definition: Predicate.h:176
GreaterThan(const Object &object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:805
utl::Or operator||(const utl::Predicate &lhs, const utl::Predicate &rhs)
(Predicate || Predicate) constructs an instance of the Or predicate.
Definition: Predicate.h:928
virtual bool eval(const Object *object) const
Determine whether the given object is derived from the class.
Definition: Predicate.h:123
Logical not predicate.
Definition: Predicate.h:450
const Predicate * rhs() const
Get the right-hand-side predicate.
Definition: Predicate.h:601
ComparisonPredicate(const Object &object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:366
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
void setRHS(const Object &lhs)
Set the right-hand-side parameter (make a copy if isOwner() = true).
Definition: Predicate.h:308
Abstraction for comparison predicates.
Definition: Predicate.h:355
Abstraction for single-parameter predicates.
Definition: Predicate.h:155
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it satisfies both the sub-predicates.
Definition: Predicate.h:518
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it is less than or equal to the associated object...
Definition: Predicate.h:770
utl::Not operator!(const utl::Predicate &pred)
(! Predicate) constructs an instance of the Not predicate.
Definition: Predicate.h:904
void setOrdering(const Ordering &ordering)
Set the ordering to a copy of the given ordering.
Definition: Predicate.h:404
const Object * getObject() const
Get the parameter.
Definition: Predicate.h:188
void setObject(const Object *object)
Set the parameter.
Definition: Predicate.h:205
const Ordering * ordering() const
Get the ordering.
Definition: Predicate.h:397
LessThanOrEqualTo(const Object *object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:760
Logical predicate abstraction.
Definition: Predicate.h:26
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it is equal to the associated object.
Definition: Predicate.h:657
void clear()
See Object::clear.
Definition: Predicate.h:264
Logical and predicate.
Definition: Predicate.h:485
GreaterThanOrEqualTo(const Object *object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:875
GreaterThan(const Object *object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:816
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
EqualTo(const Object &object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:636
int compareNullable(const Object *lhs, const Object *rhs, const Ordering *ordering=nullptr)
Compare two objects.
Definition: util_inl.h:466
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
void setOwner(bool owner)
Set the ownership flag.
Definition: Predicate.h:41
Or(const Predicate &lhs, const Predicate &rhs, bool owner=true)
Constructor.
Definition: Predicate.h:566
const Predicate * lhs() const
Get the left-hand-side predicate.
Definition: Predicate.h:525
Stream I/O abstraction.
Definition: Stream.h:68
GreaterThanOrEqualTo(const Object &object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:862
Greater-Than predicate.
Definition: Predicate.h:792
IsA(const RunTimeClass *rtc)
Constructor.
Definition: Predicate.h:112
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it is less than the associated object.
Definition: Predicate.h:713
void setObject(const Object &object)
Set the parameter (make a copy if isOwner() = true).
Definition: Predicate.h:195
void setLHS(const Object *lhs)
Set the left-hand-side parameter.
Definition: Predicate.h:300
int cmp(const Object *object) const
Compare the given object with self&#39;s object.
Definition: Predicate.h:390
void setLHS(const Object &lhs)
Set the left-hand-side parameter (make a copy if isOwner() = true).
Definition: Predicate.h:290
ComparisonPredicate(const Object *object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:378
utl::And operator &&(const utl::Predicate &lhs, const utl::Predicate &rhs)
(Predicate && Predicate) constructs an instance of the And predicate.
Definition: Predicate.h:916
Greater-Than-Or-Equal-To predicate.
Definition: Predicate.h:849
Equal-To predicate.
Definition: Predicate.h:623
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it satisfies at least one of the sub-predicates.
Definition: Predicate.h:587
virtual bool eval(const Object *object) const
The object satisfies the predicate iff it is greater than or equal to the associated object...
Definition: Predicate.h:887
void init()
Initialize UTL++.
const Object * getRHS() const
Get the right-hand-side parameter.
Definition: Predicate.h:283
And(const Predicate *lhs, const Predicate *rhs, bool owner=true)
Constructor.
Definition: Predicate.h:508
is-A predicate.
Definition: Predicate.h:103
And(const Predicate &lhs, const Predicate &rhs, bool owner=true)
Constructor.
Definition: Predicate.h:497
LessThan(const Object &object, bool owner=true, const Ordering *ordering=nullptr)
Constructor.
Definition: Predicate.h:692
virtual bool eval(const Object *object) const
Evaluate the predicate for the given object.
Definition: Predicate.h:462