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 MATH_EXPR_HPP
00025 #define MATH_EXPR_HPP
00026
00027 #include <mapnik/expression.hpp>
00028
00029 namespace mapnik
00030 {
00031 template <typename T>
00032 struct add
00033 {
00034 T operator () (T const& left, T const& right)
00035 {
00036 return left + right;
00037 }
00038 static std::string to_string()
00039 {
00040 return "+";
00041 }
00042 };
00043
00044 template <typename T>
00045 struct sub
00046 {
00047 T operator () (T const& left, T const& right)
00048 {
00049 return left - right;
00050 }
00051 static std::string to_string()
00052 {
00053 return "-";
00054 }
00055 };
00056
00057 template <typename T>
00058 struct mult
00059 {
00060 T operator () (T const& left, T const& right)
00061 {
00062 return left * right;
00063 }
00064 static std::string to_string()
00065 {
00066 return "*";
00067 }
00068 };
00069
00070 template <typename T>
00071 struct div
00072 {
00073 T operator () (T const& left, T const& right)
00074 {
00075 return left / right;
00076 }
00077 static std::string to_string()
00078 {
00079 return "/";
00080 }
00081 };
00082
00083 template <typename FeatureT,typename Op>
00084 struct math_expr_b : public expression<FeatureT>
00085 {
00086 math_expr_b(expression<FeatureT> const& left,
00087 expression<FeatureT> const& right)
00088 : expression<FeatureT>(),
00089 left_(left.clone()),
00090 right_(right.clone()) {}
00091 math_expr_b(math_expr_b const& other)
00092 : expression<FeatureT>(),
00093 left_(other.left_->clone()),
00094 right_(other.right_->clone()) {}
00095
00096 value get_value(FeatureT const& feature) const
00097 {
00098 return Op ()(left_->get_value(feature),right_->get_value(feature));
00099 }
00100
00101 void accept(filter_visitor<FeatureT>& v)
00102 {
00103 left_->accept(v);
00104 right_->accept(v);
00105 v.visit(*this);
00106 }
00107
00108 expression<FeatureT>* clone() const
00109 {
00110 return new math_expr_b<FeatureT,Op>(*this);
00111 }
00112 std::string to_string() const
00113 {
00114 return "("+left_->to_string() + Op::to_string() + right_->to_string()+")";
00115 }
00116
00117 ~math_expr_b()
00118 {
00119 delete left_;
00120 delete right_;
00121 }
00122 private:
00123 expression<FeatureT>* left_;
00124 expression<FeatureT>* right_;
00125 };
00126 }
00127
00128 #endif //