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

Generated on Fri Feb 12 20:05:23 2016 for OpenDDS by  doxygen 1.4.7