OpenDDS  Snapshot(2023/04/28-20:55)
Comparator_T.h
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.opendds.org/license.html
4  */
5 
6 #ifndef OPENDDS_DCPS_COMPARATOR_T_H
7 #define OPENDDS_DCPS_COMPARATOR_T_H
8 
9 #include <ace/config-macros.h>
10 #ifndef ACE_LACKS_PRAGMA_ONCE
11 # pragma once
12 #endif
13 
14 #include "RcHandle_T.h"
15 #include "RcObject.h"
16 #include "RakeData.h"
17 
18 #include <ace/OS_NS_string.h>
19 
21 
22 namespace OpenDDS {
23 namespace DCPS {
24 
25 class ComparatorBase : public virtual RcObject {
26 public:
28 
30  explicit ComparatorBase(Ptr next) : next_(next) {}
31 
32  virtual ~ComparatorBase() {}
33 
34  virtual bool less(void* lhs, void* rhs) const = 0;
35  virtual bool equal(void* lhs, void* rhs) const = 0;
36 
37  bool compare(void* lhs, void* rhs) const {
38  if (next_.in() && equal(lhs, rhs)) return next_->compare(lhs, rhs);
39 
40  return less(lhs, rhs);
41  }
42 
43 private:
44  Ptr next_;
45 };
46 
47 template <class Sample, class Field>
49 public:
50  typedef Field Sample::* MemberPtr;
52  : ComparatorBase(next)
53  , mp_(mp) {}
54 
55  bool less(void* lhs_void, void* rhs_void) const {
56  Sample* lhs = static_cast<Sample*>(lhs_void);
57  Sample* rhs = static_cast<Sample*>(rhs_void);
58  using ::operator<; // TAO::String_Manager's operator< is in global NS
59  return lhs->*mp_ < rhs->*mp_;
60  }
61 
62  bool equal(void* lhs_void, void* rhs_void) const {
63  Sample* lhs = static_cast<Sample*>(lhs_void);
64  Sample* rhs = static_cast<Sample*>(rhs_void);
65  const Field& field_l = lhs->*mp_;
66  const Field& field_r = rhs->*mp_;
67  using ::operator<; // TAO::String_Manager's operator< is in global NS
68  return !(field_l < field_r) && !(field_r < field_l);
69  }
70 
71 private:
73 };
74 
75 template <class Sample, class Field>
78 {
79  return make_rch<FieldComparator<Sample, Field> >(mp, next);
80 }
81 
82 /** deal with nested structs, for example:
83  * struct A { long x; };
84  * struct B { A theA; };
85  * B's query string has "ORDER BY theA.x"
86  * The generated code will create a StructComparator with
87  * Sample = B and Field = A which in turn has a "delegate" which is
88  * a FieldComparator (see above) with Sample = A and Field = CORBA::Long
89  */
90 template <class Sample, class Field>
92 public:
93  typedef Field Sample::* MemberPtr;
96  : ComparatorBase(next)
97  , mp_(mp)
98  , delegate_(delegate) {}
99 
100  bool less(void* lhs_void, void* rhs_void) const {
101  Sample* lhs = static_cast<Sample*>(lhs_void);
102  Sample* rhs = static_cast<Sample*>(rhs_void);
103  return delegate_->less(&(lhs->*mp_), &(rhs->*mp_));
104  }
105 
106  bool equal(void* lhs_void, void* rhs_void) const {
107  Sample* lhs = static_cast<Sample*>(lhs_void);
108  Sample* rhs = static_cast<Sample*>(rhs_void);
109  return delegate_->equal(&(lhs->*mp_), &(rhs->*mp_));
110  }
111 
112 private:
115 };
116 
117 template <class Sample, class Field>
119  ComparatorBase::Ptr delegate,
120  ComparatorBase::Ptr next)
121 {
122  return make_rch<StructComparator<Sample, Field> >(mp, delegate, next);
123 }
124 
125 } // namespace DCPS
126 } // namespace OpenDDS
127 
129 
130 #endif
FieldComparator(MemberPtr mp, ComparatorBase::Ptr next)
Definition: Comparator_T.h:51
ComparatorBase::Ptr make_struct_cmp(Field Sample::*mp, ComparatorBase::Ptr delegate, ComparatorBase::Ptr next)
Definition: Comparator_T.h:118
StructComparator(MemberPtr mp, ComparatorBase::Ptr delegate, ComparatorBase::Ptr next)
Definition: Comparator_T.h:94
ComparatorBase::Ptr delegate_
Definition: Comparator_T.h:114
bool equal(void *lhs_void, void *rhs_void) const
Definition: Comparator_T.h:62
bool less(void *lhs_void, void *rhs_void) const
Definition: Comparator_T.h:100
virtual bool equal(void *lhs, void *rhs) const =0
ComparatorBase::Ptr make_field_cmp(Field Sample::*mp, ComparatorBase::Ptr next)
Definition: Comparator_T.h:76
bool compare(void *lhs, void *rhs) const
Definition: Comparator_T.h:37
bool less(void *lhs_void, void *rhs_void) const
Definition: Comparator_T.h:55
bool equal(void *lhs_void, void *rhs_void) const
Definition: Comparator_T.h:106
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
RcHandle< ComparatorBase > Ptr
Definition: Comparator_T.h:27
virtual bool less(void *lhs, void *rhs) const =0
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28