arabica/include/XPath/impl/xpath_arithmetic.hpp

125 lines
5.7 KiB
C++
Raw Normal View History

2005-08-04 22:42:30 +02:00
#ifndef ARABICA_XPATHIC_XPATH_ARITHMETIC_HPP
#define ARABICA_XPATHIC_XPATH_ARITHMETIC_HPP
#include "xpath_value.hpp"
namespace Arabica
{
namespace XPath
{
namespace impl
{
2005-08-04 22:42:30 +02:00
2005-08-17 09:01:27 +02:00
template<class string_type, class string_adaptor>
class PlusOperator : public BinaryExpression<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef BinaryExpression<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
PlusOperator(XPathExpression_impl<string_type, string_adaptor>* lhs, XPathExpression_impl<string_type, string_adaptor>* rhs) :
BinaryExpression<string_type, string_adaptor>(lhs, rhs) { }
2005-08-04 22:42:30 +02:00
virtual ValueType type() const { return NUMBER; }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
return NumericValue<string_type, string_adaptor>::createValue(baseT::lhs()->evaluateAsNumber(context, executionContext) + baseT::rhs()->evaluateAsNumber(context, executionContext));
2005-08-04 22:42:30 +02:00
} // evaluate
}; // class PlusOperator
2005-08-17 09:01:27 +02:00
template<class string_type, class string_adaptor>
class MinusOperator : public BinaryExpression<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef BinaryExpression<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
MinusOperator(XPathExpression_impl<string_type, string_adaptor>* lhs, XPathExpression_impl<string_type, string_adaptor>* rhs) :
BinaryExpression<string_type, string_adaptor>(lhs, rhs) { }
2005-08-04 22:42:30 +02:00
virtual ValueType type() const { return NUMBER; }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
return NumericValue<string_type, string_adaptor>::createValue(baseT::lhs()->evaluateAsNumber(context, executionContext) - baseT::rhs()->evaluateAsNumber(context, executionContext));
2005-08-04 22:42:30 +02:00
} // evaluate
}; // class MinusOperator
2005-08-17 09:01:27 +02:00
template<class string_type, class string_adaptor>
class MultiplyOperator : public BinaryExpression<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef BinaryExpression<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
MultiplyOperator(XPathExpression_impl<string_type, string_adaptor>* lhs, XPathExpression_impl<string_type, string_adaptor>* rhs) :
BinaryExpression<string_type, string_adaptor>(lhs, rhs) { }
2005-08-04 22:42:30 +02:00
virtual ValueType type() const { return NUMBER; }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
return NumericValue<string_type, string_adaptor>::createValue(baseT::lhs()->evaluateAsNumber(context, executionContext) * baseT::rhs()->evaluateAsNumber(context, executionContext));
2005-08-04 22:42:30 +02:00
} // evaluate
}; // class MultiplyOperator
2005-08-17 09:01:27 +02:00
template<class string_type, class string_adaptor>
class DivideOperator : public BinaryExpression<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef BinaryExpression<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
DivideOperator(XPathExpression_impl<string_type, string_adaptor>* lhs, XPathExpression_impl<string_type, string_adaptor>* rhs) :
BinaryExpression<string_type, string_adaptor>(lhs, rhs) { }
2005-08-04 22:42:30 +02:00
virtual ValueType type() const { return NUMBER; }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
return NumericValue<string_type, string_adaptor>::createValue(baseT::lhs()->evaluateAsNumber(context, executionContext) / baseT::rhs()->evaluateAsNumber(context, executionContext));
2005-08-04 22:42:30 +02:00
} // evaluate
}; // class DivideOperator
2005-08-17 09:01:27 +02:00
template<class string_type, class string_adaptor>
class ModOperator : public BinaryExpression<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef BinaryExpression<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
ModOperator(XPathExpression_impl<string_type, string_adaptor>* lhs, XPathExpression_impl<string_type, string_adaptor>* rhs) :
BinaryExpression<string_type, string_adaptor>(lhs, rhs) { }
2005-08-04 22:42:30 +02:00
virtual ValueType type() const { return NUMBER; }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
2005-08-04 22:42:30 +02:00
{
2007-07-19 19:01:31 +02:00
double l = baseT::lhs()->evaluateAsNumber(context, executionContext);
double r = baseT::rhs()->evaluateAsNumber(context, executionContext);
if(isNaN(l) || isNaN(r))
return NumericValue<string_type, string_adaptor>::createValue(NaN);
return NumericValue<string_type, string_adaptor>::createValue(static_cast<long>(l) % static_cast<long>(r));
2005-08-04 22:42:30 +02:00
} // evaluate
}; // class ModOperator
2005-08-17 09:01:27 +02:00
template<class string_type, class string_adaptor>
class UnaryNegative : public UnaryExpression<string_type, string_adaptor>
2005-08-04 22:42:30 +02:00
{
typedef UnaryExpression<string_type, string_adaptor> baseT;
2005-08-04 22:42:30 +02:00
public:
UnaryNegative(XPathExpression_impl<string_type, string_adaptor>* expr) :
UnaryExpression<string_type, string_adaptor>(expr) { }
2005-08-04 22:42:30 +02:00
virtual ValueType type() const { return NUMBER; }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
2005-08-04 22:42:30 +02:00
{
return NumericValue<string_type, string_adaptor>::createValue(-baseT::expr()->evaluate(context, executionContext).asNumber());
2005-08-04 22:42:30 +02:00
} // evaluate
}; // class UnaryNegative
} // namespace impl
} // namespace XPath
} // namespace Arabica
2005-08-04 22:42:30 +02:00
#endif