00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef COMPARISON_HPP
00025 #define COMPARISON_HPP
00026
00027 #include <mapnik/filter.hpp>
00028 #include <mapnik/expression.hpp>
00029 #include <mapnik/attribute.hpp>
00030
00031 namespace mapnik {
00032 template <typename T>
00033 struct greater_than
00034 {
00035 bool operator() (T const& left, T const& right) const
00036 {
00037 return left > right;
00038 }
00039 static std::string to_string()
00040 {
00041 return ">";
00042 }
00043 };
00044
00045 template <typename T>
00046 struct greater_than_or_equal
00047 {
00048 bool operator() (T const& left, T const& right) const
00049 {
00050 return left >= right;
00051 }
00052 static std::string to_string()
00053 {
00054 return ">=";
00055 }
00056 };
00057 template <typename T>
00058 struct less_than
00059 {
00060 bool operator() (T const& left, T const& right) const
00061 {
00062 return left < right;
00063 }
00064 static std::string to_string()
00065 {
00066 return "<";
00067 }
00068 };
00069 template <typename T>
00070 struct less_than_or_equal
00071 {
00072 bool operator() (T const& left, T const& right) const
00073 {
00074 return left <= right;
00075 }
00076 static std::string to_string()
00077 {
00078 return "<=";
00079 }
00080 };
00081 template <typename T>
00082 struct equals
00083 {
00084 bool operator() (T const& left, T const& right) const
00085 {
00086 return left == right;
00087 }
00088 static std::string to_string()
00089 {
00090 return "=";
00091 }
00092 };
00093
00094 template <typename T>
00095 struct not_equals
00096 {
00097 bool operator() (T const& left, T const& right) const
00098 {
00099 return left != right;
00100 }
00101 static std::string to_string()
00102 {
00103 return "<>";
00104 }
00105 };
00106
00107 template <typename FeatureT,typename Op>
00108 struct compare_filter : public filter<FeatureT>
00109 {
00110 compare_filter(expression<FeatureT> const& left,
00111 expression<FeatureT> const& right)
00112 : filter<FeatureT>(),
00113 left_(left.clone()), right_(right.clone()) {}
00114
00115 compare_filter(compare_filter const& other)
00116 : filter<FeatureT>(),
00117 left_(other.left_->clone()),right_(other.right_->clone()) {}
00118
00119 bool pass(const FeatureT& feature) const
00120 {
00121 return Op()(left_->get_value(feature),right_->get_value(feature));
00122 }
00123 void accept(filter_visitor<FeatureT>& v)
00124 {
00125 left_->accept(v);
00126 right_->accept(v);
00127 v.visit(*this);
00128 }
00129 std::string to_string() const
00130 {
00131 return "("+left_->to_string()+Op::to_string()+right_->to_string()+")";
00132 }
00133
00134 filter<FeatureT>* clone() const
00135 {
00136 return new compare_filter<FeatureT,Op>(*this);
00137 }
00138 virtual ~compare_filter()
00139 {
00140 delete left_;
00141 delete right_;
00142 }
00143 private:
00144 expression<FeatureT>* left_;
00145 expression<FeatureT>* right_;
00146 };
00147 }
00148
00149 #endif //COMPARISON_HPP