Comparator_T.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #ifndef COMPARATOR_H
00009 #define COMPARATOR_H
00010 
00011 #include /**/ "ace/pre.h"
00012 
00013 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00014 # pragma once
00015 #endif /* ACE_LACKS_PRAGMA_ONCE */
00016 
00017 #include "ace/OS_NS_string.h"
00018 
00019 #include "RcHandle_T.h"
00020 #include "RcObject.h"
00021 #include "RakeData.h"
00022 
00023 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00024 
00025 namespace OpenDDS {
00026 namespace DCPS {
00027 
00028 class ComparatorBase : public RcObject {
00029 public:
00030   typedef RcHandle<ComparatorBase> Ptr;
00031 
00032   ComparatorBase(){}
00033   explicit ComparatorBase(Ptr next) : next_(next) {}
00034 
00035   virtual ~ComparatorBase() {}
00036 
00037   virtual bool less(void* lhs, void* rhs) const = 0;
00038   virtual bool equal(void* lhs, void* rhs) const = 0;
00039 
00040   bool compare(void* lhs, void* rhs) const {
00041     if (next_.in() && equal(lhs, rhs)) return next_->compare(lhs, rhs);
00042 
00043     return less(lhs, rhs);
00044   }
00045 
00046 private:
00047   Ptr next_;
00048 };
00049 
00050 template <class Sample, class Field>
00051 class FieldComparator : public ComparatorBase {
00052 public:
00053   typedef Field Sample::* MemberPtr;
00054   FieldComparator(MemberPtr mp, ComparatorBase::Ptr next)
00055   : ComparatorBase(next)
00056   , mp_(mp) {}
00057 
00058   bool less(void* lhs_void, void* rhs_void) const {
00059     Sample* lhs = static_cast<Sample*>(lhs_void);
00060     Sample* rhs = static_cast<Sample*>(rhs_void);
00061     using ::operator<; // TAO::String_Manager's operator< is in global NS
00062     return lhs->*mp_ < rhs->*mp_;
00063   }
00064 
00065   bool equal(void* lhs_void, void* rhs_void) const {
00066     Sample* lhs = static_cast<Sample*>(lhs_void);
00067     Sample* rhs = static_cast<Sample*>(rhs_void);
00068     const Field& field_l = lhs->*mp_;
00069     const Field& field_r = rhs->*mp_;
00070     using ::operator<; // TAO::String_Manager's operator< is in global NS
00071     return !(field_l < field_r) && !(field_r < field_l);
00072   }
00073 
00074 private:
00075   MemberPtr mp_;
00076 };
00077 
00078 template <class Sample, class Field>
00079 ComparatorBase::Ptr make_field_cmp(Field Sample::* mp,
00080                                    ComparatorBase::Ptr next)
00081 {
00082   return make_rch<FieldComparator<Sample, Field> >(mp, next);
00083 }
00084 
00085 /** deal with nested structs, for example:
00086  *  struct A { long x; };
00087  *  struct B { A theA; };
00088  *  B's query string has "ORDER BY theA.x"
00089  *  The generated code will create a StructComparator with
00090  *  Sample = B and Field = A which in turn has a "delegate" which is
00091  *  a FieldComparator (see above) with Sample = A and Field = CORBA::Long
00092  */
00093 template <class Sample, class Field>
00094 class StructComparator : public ComparatorBase {
00095 public:
00096   typedef Field Sample::* MemberPtr;
00097   StructComparator(MemberPtr mp, ComparatorBase::Ptr delegate,
00098                    ComparatorBase::Ptr next)
00099   : ComparatorBase(next)
00100   , mp_(mp)
00101   , delegate_(delegate) {}
00102 
00103   bool less(void* lhs_void, void* rhs_void) const {
00104     Sample* lhs = static_cast<Sample*>(lhs_void);
00105     Sample* rhs = static_cast<Sample*>(rhs_void);
00106     return delegate_->less(&(lhs->*mp_), &(rhs->*mp_));
00107   }
00108 
00109   bool equal(void* lhs_void, void* rhs_void) const {
00110     Sample* lhs = static_cast<Sample*>(lhs_void);
00111     Sample* rhs = static_cast<Sample*>(rhs_void);
00112     return delegate_->equal(&(lhs->*mp_), &(rhs->*mp_));
00113   }
00114 
00115 private:
00116   MemberPtr mp_;
00117   ComparatorBase::Ptr delegate_;
00118 };
00119 
00120 template <class Sample, class Field>
00121 ComparatorBase::Ptr make_struct_cmp(Field Sample::* mp,
00122                                     ComparatorBase::Ptr delegate,
00123                                     ComparatorBase::Ptr next)
00124 {
00125   return make_rch<StructComparator<Sample, Field> >(mp, delegate, next);
00126 }
00127 
00128 } // namespace DCPS
00129 } // namespace OpenDDS
00130 
00131 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00132 
00133 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1