From abe24475d8608ca2fe73f452d95294e10d4da25c Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 10 Feb 2009 15:46:54 +0000 Subject: [PATCH] Add a modulus operator to the filter language. --- include/mapnik/filter_parser.hpp | 3 ++- include/mapnik/math_expr.hpp | 13 +++++++++ include/mapnik/value.hpp | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/include/mapnik/filter_parser.hpp b/include/mapnik/filter_parser.hpp index 65c7d2841..a3a69beae 100644 --- a/include/mapnik/filter_parser.hpp +++ b/include/mapnik/filter_parser.hpp @@ -436,7 +436,8 @@ namespace mapnik ; term = factor >> *((L'*' >> factor) [compose_expression >(self.exprs)] - | (L'/' >> factor) [compose_expression >(self.exprs)]); + | (L'/' >> factor) [compose_expression >(self.exprs)] + | (L'%' >> factor) [compose_expression >(self.exprs)]); expression = term >> *((L'+' >> term) [compose_expression >(self.exprs)] | (L'-' >> term) [compose_expression >(self.exprs)]); diff --git a/include/mapnik/math_expr.hpp b/include/mapnik/math_expr.hpp index 6f8cbeb37..c95f1c91d 100644 --- a/include/mapnik/math_expr.hpp +++ b/include/mapnik/math_expr.hpp @@ -80,6 +80,19 @@ namespace mapnik } }; + template + struct mod + { + T operator () (T const& left, T const& right) + { + return left % right; + } + static std::string to_string() + { + return "%"; + } + }; + template struct math_expr_b : public expression { diff --git a/include/mapnik/value.hpp b/include/mapnik/value.hpp index ff37341c5..73e730787 100644 --- a/include/mapnik/value.hpp +++ b/include/mapnik/value.hpp @@ -36,6 +36,7 @@ #include #include #include +#include // uci #include #include @@ -434,6 +435,44 @@ namespace mapnik { return lhs / rhs; } }; + + template + struct mod: public boost::static_visitor + { + typedef V value_type; + template + value_type operator() (T1 const& lhs, T2 const&) const + { + return lhs; + } + + template + value_type operator() (T lhs, T rhs) const + { + return lhs % rhs; + } + + value_type operator() (UnicodeString const& lhs, + UnicodeString const&) const + { + return lhs; + } + + value_type operator() (double lhs, int rhs) const + { + return fmod(lhs, rhs); + } + + value_type operator() (int lhs, double rhs) const + { + return fmod(lhs, rhs); + } + + value_type operator() (double lhs, double rhs) const + { + return fmod(lhs, rhs); + } + }; struct to_bool : public boost::static_visitor { @@ -553,6 +592,7 @@ namespace mapnik { friend const value operator-(value const&,value const&); friend const value operator*(value const&,value const&); friend const value operator/(value const&,value const&); + friend const value operator%(value const&,value const&); public: value () @@ -641,6 +681,12 @@ namespace mapnik { return value(boost::apply_visitor(impl::div(),p1.base_, p2.base_)); } + inline const value operator%(value const& p1,value const& p2) + { + + return value(boost::apply_visitor(impl::mod(),p1.base_, p2.base_)); + } + template inline std::basic_ostream& operator << (std::basic_ostream& out,