2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
//$Id$
|
|
|
|
|
|
|
|
#ifndef MATH_EXPR_HPP
|
|
|
|
#define MATH_EXPR_HPP
|
|
|
|
|
|
|
|
#include "expression.hpp"
|
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
struct add
|
|
|
|
{
|
2006-09-11 11:48:27 +02:00
|
|
|
T operator () (T const& left, T const& right)
|
|
|
|
{
|
|
|
|
return left + right;
|
|
|
|
}
|
|
|
|
static std::string to_string()
|
|
|
|
{
|
|
|
|
return "+";
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct sub
|
|
|
|
{
|
2006-09-11 11:48:27 +02:00
|
|
|
T operator () (T const& left, T const& right)
|
|
|
|
{
|
|
|
|
return left - right;
|
|
|
|
}
|
|
|
|
static std::string to_string()
|
|
|
|
{
|
|
|
|
return "-";
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct mult
|
|
|
|
{
|
2006-09-11 11:48:27 +02:00
|
|
|
T operator () (T const& left, T const& right)
|
|
|
|
{
|
|
|
|
return left * right;
|
|
|
|
}
|
|
|
|
static std::string to_string()
|
|
|
|
{
|
|
|
|
return "*";
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct div
|
|
|
|
{
|
2006-09-11 11:48:27 +02:00
|
|
|
T operator () (T const& left, T const& right)
|
|
|
|
{
|
|
|
|
return left / right;
|
|
|
|
}
|
|
|
|
static std::string to_string()
|
|
|
|
{
|
|
|
|
return "/";
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename FeatureT,typename Op>
|
|
|
|
struct math_expr_b : public expression<FeatureT>
|
|
|
|
{
|
2006-09-11 11:48:27 +02:00
|
|
|
math_expr_b(expression<FeatureT> const& left,
|
|
|
|
expression<FeatureT> const& right)
|
|
|
|
: expression<FeatureT>(),
|
|
|
|
left_(left.clone()),
|
|
|
|
right_(right.clone()) {}
|
|
|
|
math_expr_b(math_expr_b const& other)
|
|
|
|
: expression<FeatureT>(),
|
|
|
|
left_(other.left_->clone()),
|
|
|
|
right_(other.right_->clone()) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-09-11 11:48:27 +02:00
|
|
|
value get_value(FeatureT const& feature) const
|
|
|
|
{
|
|
|
|
return Op ()(left_->get_value(feature),right_->get_value(feature));
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-09-11 11:48:27 +02:00
|
|
|
void accept(filter_visitor<FeatureT>& v)
|
|
|
|
{
|
|
|
|
left_->accept(v);
|
|
|
|
right_->accept(v);
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-09-11 11:48:27 +02:00
|
|
|
expression<FeatureT>* clone() const
|
|
|
|
{
|
|
|
|
return new math_expr_b<FeatureT,Op>(*this);
|
|
|
|
}
|
|
|
|
std::string to_string() const
|
|
|
|
{
|
|
|
|
return "("+left_->to_string() + Op::to_string() + right_->to_string()+")";
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-09-11 11:48:27 +02:00
|
|
|
~math_expr_b()
|
|
|
|
{
|
|
|
|
delete left_;
|
|
|
|
delete right_;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
private:
|
2006-09-11 11:48:27 +02:00
|
|
|
expression<FeatureT>* left_;
|
|
|
|
expression<FeatureT>* right_;
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
2006-03-09 00:02:28 +01:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
#endif //
|