FilterEvaluator.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 OPENDDS_DCPS_FILTER_EVALUATOR_H
00009 #define OPENDDS_DCPS_FILTER_EVALUATOR_H
00010 
00011 #include "dds/DCPS/Definitions.h"
00012 
00013 #ifndef OPENDDS_NO_CONTENT_SUBSCRIPTION_PROFILE
00014 
00015 #include "dds/DdsDcpsInfrastructureC.h"
00016 #include "dds/DCPS/PoolAllocator.h"
00017 #include "Comparator_T.h"
00018 #include "RcObject.h"
00019 
00020 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00021 
00022 namespace OpenDDS {
00023 namespace DCPS {
00024 
00025 class MetaStruct;
00026 
00027 template<typename T>
00028 const MetaStruct& getMetaStruct();
00029 
00030 struct OpenDDS_Dcps_Export Value {
00031   Value(bool b, bool conversion_preferred = false);
00032   Value(int i, bool conversion_preferred = false);
00033   Value(unsigned int u, bool conversion_preferred = false);
00034   Value(ACE_INT64 l, bool conversion_preferred = false);
00035   Value(ACE_UINT64 m, bool conversion_preferred = false);
00036   Value(char c, bool conversion_preferred = false);
00037   Value(double f, bool conversion_preferred = false);
00038   Value(ACE_CDR::LongDouble ld, bool conversion_preferred = false);
00039   Value(const char* s, bool conversion_preferred = false);
00040   Value(const TAO::String_Manager& s, bool conversion_preferred = false);
00041   Value(const TAO::WString_Manager& s, bool conversion_preferred = false);
00042 
00043   ~Value();
00044   Value(const Value& v);
00045   Value& operator=(const Value& v);
00046   void swap(Value& other);
00047 
00048   bool operator==(const Value& v) const;
00049   bool operator<(const Value& v) const;
00050   bool like(const Value& v) const;
00051   Value operator%(const Value& v) const;
00052 
00053   enum Type {VAL_BOOL, VAL_INT, VAL_UINT, VAL_I64, VAL_UI64, VAL_FLOAT,
00054              VAL_LNGDUB, VAL_LARGEST_NUMERIC = VAL_LNGDUB,
00055              VAL_CHAR, VAL_STRING};
00056   bool convert(Type t);
00057   static void conversion(Value& lhs, Value& rhs);
00058   template<typename T> T& get();
00059   template<typename T> const T& get() const;
00060 
00061   Type type_;
00062   union {
00063     bool b_;
00064     int i_;
00065     unsigned int u_;
00066     ACE_INT64 l_;
00067     ACE_UINT64 m_;
00068     char c_;
00069     double f_;
00070     ACE_CDR::LongDouble ld_;
00071     const char* s_;
00072   };
00073   bool conversion_preferred_;
00074 };
00075 
00076 class OpenDDS_Dcps_Export FilterEvaluator : public RcObject {
00077 public:
00078 
00079   struct AstNodeWrapper;
00080 
00081   FilterEvaluator(const char* filter, bool allowOrderBy);
00082 
00083   explicit FilterEvaluator(const AstNodeWrapper& yardNode);
00084 
00085   ~FilterEvaluator();
00086 
00087   OPENDDS_VECTOR(OPENDDS_STRING) getOrderBys() const;
00088 
00089   bool hasFilter() const;
00090 
00091   bool usesExtendedGrammar() const { return extended_grammar_; }
00092 
00093   size_t number_parameters() const { return number_parameters_; }
00094 
00095   template<typename T>
00096   bool eval(const T& sample, const DDS::StringSeq& params) const
00097   {
00098     DeserializedForEval data(&sample, getMetaStruct<T>(), params);
00099     return eval_i(data);
00100   }
00101 
00102   bool eval(ACE_Message_Block* serializedSample, bool swap_bytes,
00103             bool cdr_encap, const MetaStruct& meta,
00104             const DDS::StringSeq& params) const
00105   {
00106     SerializedForEval data(serializedSample, meta, params,
00107                            swap_bytes, cdr_encap);
00108     return eval_i(data);
00109   }
00110 
00111   class EvalNode;
00112   class Operand;
00113 
00114   struct OpenDDS_Dcps_Export DataForEval {
00115     DataForEval(const MetaStruct& meta, const DDS::StringSeq& params)
00116       : meta_(meta), params_(params) {}
00117     virtual ~DataForEval();
00118     virtual Value lookup(const char* field) const = 0;
00119     const MetaStruct& meta_;
00120     const DDS::StringSeq& params_;
00121   private:
00122     DataForEval(const DataForEval&);
00123     DataForEval& operator=(const DataForEval&);
00124   };
00125 
00126 private:
00127   FilterEvaluator(const FilterEvaluator&);
00128   FilterEvaluator& operator=(const FilterEvaluator&);
00129 
00130   EvalNode* walkAst(const AstNodeWrapper& node);
00131   Operand* walkOperand(const AstNodeWrapper& node);
00132 
00133   struct OpenDDS_Dcps_Export DeserializedForEval : DataForEval {
00134     DeserializedForEval(const void* data, const MetaStruct& meta,
00135                         const DDS::StringSeq& params)
00136       : DataForEval(meta, params), deserialized_(data) {}
00137     virtual ~DeserializedForEval();
00138     Value lookup(const char* field) const;
00139     const void* const deserialized_;
00140   };
00141 
00142   struct SerializedForEval : DataForEval {
00143     SerializedForEval(ACE_Message_Block* data, const MetaStruct& meta,
00144                       const DDS::StringSeq& params, bool swap, bool cdr)
00145       : DataForEval(meta, params), serialized_(data), swap_(swap), cdr_(cdr) {}
00146     Value lookup(const char* field) const;
00147     ACE_Message_Block* serialized_;
00148     bool swap_, cdr_;
00149     mutable OPENDDS_MAP(OPENDDS_STRING, Value) cache_;
00150   };
00151 
00152   bool eval_i(DataForEval& data) const;
00153 
00154   bool extended_grammar_;
00155   EvalNode* filter_root_;
00156   OPENDDS_VECTOR(OPENDDS_STRING) order_bys_;
00157   /// Number of parameter used in the filter, this should
00158   /// match the number of values passed when evaluating the filter
00159   size_t number_parameters_;
00160 };
00161 
00162 class OpenDDS_Dcps_Export MetaStruct {
00163 public:
00164   virtual ~MetaStruct();
00165 
00166   virtual Value getValue(const void* stru, const char* fieldSpec) const = 0;
00167   virtual Value getValue(Serializer& ser, const char* fieldSpec) const = 0;
00168 
00169   virtual ComparatorBase::Ptr create_qc_comparator(const char* fieldSpec,
00170     ComparatorBase::Ptr next) const = 0;
00171 
00172   ComparatorBase::Ptr create_qc_comparator(const char* fieldSpec) const
00173   { return create_qc_comparator(fieldSpec, ComparatorBase::Ptr()); }
00174 
00175   virtual bool compare(const void* lhs, const void* rhs,
00176                        const char* fieldSpec) const = 0;
00177 
00178 #ifndef OPENDDS_NO_MULTI_TOPIC
00179   virtual size_t numDcpsKeys() const = 0;
00180 
00181   virtual const char** getFieldNames() const = 0;
00182 
00183   virtual void assign(void* lhs, const char* lhsFieldSpec,
00184                       const void* rhs, const char* rhsFieldSpec,
00185                       const MetaStruct& rhsMeta) const = 0;
00186 
00187   virtual const void* getRawField(const void* stru,
00188                                   const char* fieldSpec) const = 0;
00189 
00190   virtual void* allocate() const = 0;
00191   virtual void deallocate(void* stru) const = 0;
00192 #endif /* OPENDDS_NO_MULTI_TOPIC */
00193 };
00194 
00195 /// Each user-defined struct type will have an instantiation of this template
00196 /// generated by opendds_idl.
00197 template<typename T>
00198 struct MetaStructImpl;
00199 
00200 }  }
00201 
00202 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00203 
00204 #endif // OPENDDS_NO_CONTENT_SUBSCRIPTION_PROFILE
00205 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1