mirror of
https://github.com/jezhiggins/arabica
synced 2025-01-17 18:12:04 +01:00
parameterised everything in xpath_arithmetic.hpp
This commit is contained in:
parent
7f53e6cba4
commit
f231736bf5
3 changed files with 63 additions and 51 deletions
|
@ -8,81 +8,93 @@ namespace Arabica
|
||||||
namespace XPath
|
namespace XPath
|
||||||
{
|
{
|
||||||
|
|
||||||
class PlusOperator : private BinaryExpression<std::string>, public XPathExpression<std::string>
|
template<class string_type>
|
||||||
|
class PlusOperator : private BinaryExpression<string_type>, public XPathExpression<string_type>
|
||||||
{
|
{
|
||||||
|
typedef BinaryExpression<string_type> baseT;
|
||||||
public:
|
public:
|
||||||
PlusOperator(XPathExpression<std::string>* lhs, XPathExpression<std::string>* rhs) :
|
PlusOperator(XPathExpression<string_type>* lhs, XPathExpression<string_type>* rhs) :
|
||||||
BinaryExpression<std::string>(lhs, rhs) { }
|
BinaryExpression<string_type>(lhs, rhs) { }
|
||||||
|
|
||||||
virtual XPathValuePtr<std::string> evaluate(const DOM::Node<std::string>& context,
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
||||||
const ExecutionContext& executionContext) const
|
const ExecutionContext& executionContext) const
|
||||||
{
|
{
|
||||||
return NumericValue::createValue(lhs()->evaluateAsNumber(context) + rhs()->evaluateAsNumber(context));
|
return NumericValue::createValue(baseT::lhs()->evaluateAsNumber(context) + baseT::rhs()->evaluateAsNumber(context));
|
||||||
} // evaluate
|
} // evaluate
|
||||||
}; // class PlusOperator
|
}; // class PlusOperator
|
||||||
|
|
||||||
class MinusOperator : private BinaryExpression<std::string>, public XPathExpression<std::string>
|
template<class string_type>
|
||||||
|
class MinusOperator : private BinaryExpression<string_type>, public XPathExpression<string_type>
|
||||||
{
|
{
|
||||||
|
typedef BinaryExpression<string_type> baseT;
|
||||||
public:
|
public:
|
||||||
MinusOperator(XPathExpression<std::string>* lhs, XPathExpression<std::string>* rhs) :
|
MinusOperator(XPathExpression<string_type>* lhs, XPathExpression<string_type>* rhs) :
|
||||||
BinaryExpression<std::string>(lhs, rhs) { }
|
BinaryExpression<string_type>(lhs, rhs) { }
|
||||||
|
|
||||||
virtual XPathValuePtr<std::string> evaluate(const DOM::Node<std::string>& context,
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
||||||
const ExecutionContext& executionContext) const
|
const ExecutionContext& executionContext) const
|
||||||
{
|
{
|
||||||
return NumericValue::createValue(lhs()->evaluateAsNumber(context) - rhs()->evaluateAsNumber(context));
|
return NumericValue::createValue(baseT::lhs()->evaluateAsNumber(context) - baseT::rhs()->evaluateAsNumber(context));
|
||||||
} // evaluate
|
} // evaluate
|
||||||
}; // class MinusOperator
|
}; // class MinusOperator
|
||||||
|
|
||||||
class MultiplyOperator : private BinaryExpression<std::string>, public XPathExpression<std::string>
|
template<class string_type>
|
||||||
|
class MultiplyOperator : private BinaryExpression<string_type>, public XPathExpression<string_type>
|
||||||
{
|
{
|
||||||
|
typedef BinaryExpression<string_type> baseT;
|
||||||
public:
|
public:
|
||||||
MultiplyOperator(XPathExpression<std::string>* lhs, XPathExpression<std::string>* rhs) :
|
MultiplyOperator(XPathExpression<string_type>* lhs, XPathExpression<string_type>* rhs) :
|
||||||
BinaryExpression<std::string>(lhs, rhs) { }
|
BinaryExpression<string_type>(lhs, rhs) { }
|
||||||
|
|
||||||
virtual XPathValuePtr<std::string> evaluate(const DOM::Node<std::string>& context,
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
||||||
const ExecutionContext& executionContext) const
|
const ExecutionContext& executionContext) const
|
||||||
{
|
{
|
||||||
return NumericValue::createValue(lhs()->evaluateAsNumber(context) * rhs()->evaluateAsNumber(context));
|
return NumericValue::createValue(baseT::lhs()->evaluateAsNumber(context) * baseT::rhs()->evaluateAsNumber(context));
|
||||||
} // evaluate
|
} // evaluate
|
||||||
}; // class MultiplyOperator
|
}; // class MultiplyOperator
|
||||||
|
|
||||||
class DivideOperator : private BinaryExpression<std::string>, public XPathExpression<std::string>
|
template<class string_type>
|
||||||
|
class DivideOperator : private BinaryExpression<string_type>, public XPathExpression<string_type>
|
||||||
{
|
{
|
||||||
|
typedef BinaryExpression<string_type> baseT;
|
||||||
public:
|
public:
|
||||||
DivideOperator(XPathExpression<std::string>* lhs, XPathExpression<std::string>* rhs) :
|
DivideOperator(XPathExpression<string_type>* lhs, XPathExpression<string_type>* rhs) :
|
||||||
BinaryExpression<std::string>(lhs, rhs) { }
|
BinaryExpression<string_type>(lhs, rhs) { }
|
||||||
|
|
||||||
virtual XPathValuePtr<std::string> evaluate(const DOM::Node<std::string>& context,
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
||||||
const ExecutionContext& executionContext) const
|
const ExecutionContext& executionContext) const
|
||||||
{
|
{
|
||||||
return NumericValue::createValue(lhs()->evaluateAsNumber(context) / rhs()->evaluateAsNumber(context));
|
return NumericValue::createValue(baseT::lhs()->evaluateAsNumber(context) / baseT::rhs()->evaluateAsNumber(context));
|
||||||
} // evaluate
|
} // evaluate
|
||||||
}; // class DivideOperator
|
}; // class DivideOperator
|
||||||
|
|
||||||
class ModOperator : private BinaryExpression<std::string>, public XPathExpression<std::string>
|
template<class string_type>
|
||||||
|
class ModOperator : private BinaryExpression<string_type>, public XPathExpression<string_type>
|
||||||
{
|
{
|
||||||
|
typedef BinaryExpression<string_type> baseT;
|
||||||
public:
|
public:
|
||||||
ModOperator(XPathExpression<std::string>* lhs, XPathExpression<std::string>* rhs) :
|
ModOperator(XPathExpression<string_type>* lhs, XPathExpression<string_type>* rhs) :
|
||||||
BinaryExpression<std::string>(lhs, rhs) { }
|
BinaryExpression<string_type>(lhs, rhs) { }
|
||||||
|
|
||||||
virtual XPathValuePtr<std::string> evaluate(const DOM::Node<std::string>& context,
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
||||||
const ExecutionContext& executionContext) const
|
const ExecutionContext& executionContext) const
|
||||||
{
|
{
|
||||||
return NumericValue::createValue(static_cast<long>(lhs()->evaluateAsNumber(context)) % static_cast<long>(rhs()->evaluateAsNumber(context)));
|
return NumericValue::createValue(static_cast<long>(baseT::lhs()->evaluateAsNumber(context)) % static_cast<long>(baseT::rhs()->evaluateAsNumber(context)));
|
||||||
} // evaluate
|
} // evaluate
|
||||||
}; // class ModOperator
|
}; // class ModOperator
|
||||||
|
|
||||||
class UnaryNegative : private UnaryExpression<std::string>, public XPathExpression<std::string>
|
template<class string_type>
|
||||||
|
class UnaryNegative : private UnaryExpression<string_type>, public XPathExpression<string_type>
|
||||||
{
|
{
|
||||||
|
typedef UnaryExpression<string_type> baseT;
|
||||||
public:
|
public:
|
||||||
UnaryNegative(XPathExpression<std::string>* expr) :
|
UnaryNegative(XPathExpression<string_type>* expr) :
|
||||||
UnaryExpression<std::string>(expr) { }
|
UnaryExpression<string_type>(expr) { }
|
||||||
|
|
||||||
virtual XPathValuePtr<std::string> evaluate(const DOM::Node<std::string>& context,
|
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
|
||||||
const ExecutionContext& executionContext) const
|
const ExecutionContext& executionContext) const
|
||||||
{
|
{
|
||||||
return NumericValue::createValue(-expr()->evaluate(context, executionContext)->asNumber());
|
return NumericValue::createValue(-baseT::expr()->evaluate(context, executionContext)->asNumber());
|
||||||
} // evaluate
|
} // evaluate
|
||||||
}; // class UnaryNegative
|
}; // class UnaryNegative
|
||||||
|
|
||||||
|
|
|
@ -118,19 +118,19 @@ XPathExpression<std::string>* createBinaryExpression(node_iter_t const& i, Compi
|
||||||
switch(op)
|
switch(op)
|
||||||
{
|
{
|
||||||
case PlusOperator_id:
|
case PlusOperator_id:
|
||||||
p1 = new PlusOperator(p1, p2);
|
p1 = new PlusOperator<std::string>(p1, p2);
|
||||||
break;
|
break;
|
||||||
case MinusOperator_id:
|
case MinusOperator_id:
|
||||||
p1 = new MinusOperator(p1, p2);
|
p1 = new MinusOperator<std::string>(p1, p2);
|
||||||
break;
|
break;
|
||||||
case MultiplyOperator_id:
|
case MultiplyOperator_id:
|
||||||
p1 = new MultiplyOperator(p1, p2);
|
p1 = new MultiplyOperator<std::string>(p1, p2);
|
||||||
break;
|
break;
|
||||||
case DivOperator_id:
|
case DivOperator_id:
|
||||||
p1 = new DivideOperator(p1, p2);
|
p1 = new DivideOperator<std::string>(p1, p2);
|
||||||
break;
|
break;
|
||||||
case ModOperator_id:
|
case ModOperator_id:
|
||||||
p1 = new ModOperator(p1, p2);
|
p1 = new ModOperator<std::string>(p1, p2);
|
||||||
break;
|
break;
|
||||||
case EqualsOperator_id:
|
case EqualsOperator_id:
|
||||||
p1 = new EqualsOperator(p1, p2);
|
p1 = new EqualsOperator(p1, p2);
|
||||||
|
@ -230,7 +230,7 @@ XPathExpression<std::string>* createUnaryExpression(node_iter_t const& i, Compil
|
||||||
|
|
||||||
XPathExpression<std::string>* createUnaryNegativeExpr(node_iter_t const& i, CompilationContext& context)
|
XPathExpression<std::string>* createUnaryNegativeExpr(node_iter_t const& i, CompilationContext& context)
|
||||||
{
|
{
|
||||||
return new UnaryNegative(compile_expression(i+1, context));
|
return new UnaryNegative<std::string>(compile_expression(i+1, context));
|
||||||
} // createUnaryNegativeExpr
|
} // createUnaryNegativeExpr
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> XPath::compile(const std::string& xpath) const
|
XPathExpressionPtr<std::string> XPath::compile(const std::string& xpath) const
|
||||||
|
|
|
@ -25,7 +25,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(1);
|
XPathExpression<std::string>* p1 = new NumericValue(1);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> add(new PlusOperator(p1, p2));
|
XPathExpressionPtr<std::string> add(new PlusOperator<std::string>(p1, p2));
|
||||||
assertEquals(1, add.use_count());
|
assertEquals(1, add.use_count());
|
||||||
|
|
||||||
add->evaluate(dummy_);
|
add->evaluate(dummy_);
|
||||||
|
@ -40,7 +40,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(1);
|
XPathExpression<std::string>* p1 = new NumericValue(1);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> minus(new MinusOperator(p1, p2));
|
XPathExpressionPtr<std::string> minus(new MinusOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(-1.0, minus->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(-1.0, minus->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test2
|
} // test2
|
||||||
|
@ -50,16 +50,16 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(3);
|
XPathExpression<std::string>* p1 = new NumericValue(3);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mult(new MultiplyOperator(p1, p2));
|
XPathExpressionPtr<std::string> mult(new MultiplyOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(6, mult->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(6, mult->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test3
|
} // test3
|
||||||
|
|
||||||
void test4()
|
void test4()
|
||||||
{
|
{
|
||||||
XPathExpression<std::string>* mult = new MultiplyOperator(new NumericValue(4), new NumericValue(2));
|
XPathExpression<std::string>* mult = new MultiplyOperator<std::string>(new NumericValue(4), new NumericValue(2));
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> minus(new MinusOperator(mult, new NumericValue(2)));
|
XPathExpressionPtr<std::string> minus(new MinusOperator<std::string>(mult, new NumericValue(2)));
|
||||||
|
|
||||||
assertEquals(8, mult->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(8, mult->evaluateAsNumber(dummy_), 0.0);
|
||||||
assertEquals(6, minus->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(6, minus->evaluateAsNumber(dummy_), 0.0);
|
||||||
|
@ -70,7 +70,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(12);
|
XPathExpression<std::string>* p1 = new NumericValue(12);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> div(new DivideOperator(p1, p2));
|
XPathExpressionPtr<std::string> div(new DivideOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(6, div->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(6, div->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test5
|
} // test5
|
||||||
|
@ -80,7 +80,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(12);
|
XPathExpression<std::string>* p1 = new NumericValue(12);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mod(new ModOperator(p1, p2));
|
XPathExpressionPtr<std::string> mod(new ModOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(0, mod->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(0, mod->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test6
|
} // test6
|
||||||
|
@ -90,7 +90,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(11);
|
XPathExpression<std::string>* p1 = new NumericValue(11);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> div(new DivideOperator(p1, p2));
|
XPathExpressionPtr<std::string> div(new DivideOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(5.5, div->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(5.5, div->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test7
|
} // test7
|
||||||
|
@ -100,7 +100,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(11);
|
XPathExpression<std::string>* p1 = new NumericValue(11);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(4);
|
XPathExpression<std::string>* p2 = new NumericValue(4);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mod(new ModOperator(p1, p2));
|
XPathExpressionPtr<std::string> mod(new ModOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(3, mod->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(3, mod->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test8
|
} // test8
|
||||||
|
@ -110,7 +110,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(5);
|
XPathExpression<std::string>* p1 = new NumericValue(5);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mod(new ModOperator(p1, p2));
|
XPathExpressionPtr<std::string> mod(new ModOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(1.0, mod->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(1.0, mod->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test9
|
} // test9
|
||||||
|
@ -120,7 +120,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(5);
|
XPathExpression<std::string>* p1 = new NumericValue(5);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(-2);
|
XPathExpression<std::string>* p2 = new NumericValue(-2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mod(new ModOperator(p1, p2));
|
XPathExpressionPtr<std::string> mod(new ModOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(1.00, mod->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(1.00, mod->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test10
|
} // test10
|
||||||
|
@ -130,7 +130,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(-5);
|
XPathExpression<std::string>* p1 = new NumericValue(-5);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(2);
|
XPathExpression<std::string>* p2 = new NumericValue(2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mod(new ModOperator(p1, p2));
|
XPathExpressionPtr<std::string> mod(new ModOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(-1.0, mod->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(-1.0, mod->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test11
|
} // test11
|
||||||
|
@ -140,7 +140,7 @@ public:
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(-5);
|
XPathExpression<std::string>* p1 = new NumericValue(-5);
|
||||||
XPathExpression<std::string>* p2 = new NumericValue(-2);
|
XPathExpression<std::string>* p2 = new NumericValue(-2);
|
||||||
|
|
||||||
XPathExpressionPtr<std::string> mod(new ModOperator(p1, p2));
|
XPathExpressionPtr<std::string> mod(new ModOperator<std::string>(p1, p2));
|
||||||
|
|
||||||
assertEquals(-1.0, mod->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(-1.0, mod->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test12
|
} // test12
|
||||||
|
@ -148,7 +148,7 @@ public:
|
||||||
void test13()
|
void test13()
|
||||||
{
|
{
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(5);
|
XPathExpression<std::string>* p1 = new NumericValue(5);
|
||||||
XPathExpressionPtr<std::string> p2(new UnaryNegative(p1));
|
XPathExpressionPtr<std::string> p2(new UnaryNegative<std::string>(p1));
|
||||||
|
|
||||||
assertEquals(-5.0, p2->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(-5.0, p2->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test13
|
} // test13
|
||||||
|
@ -156,7 +156,7 @@ public:
|
||||||
void test14()
|
void test14()
|
||||||
{
|
{
|
||||||
XPathExpression<std::string>* p1 = new NumericValue(-5);
|
XPathExpression<std::string>* p1 = new NumericValue(-5);
|
||||||
XPathExpressionPtr<std::string> p2(new UnaryNegative(p1));
|
XPathExpressionPtr<std::string> p2(new UnaryNegative<std::string>(p1));
|
||||||
|
|
||||||
assertEquals(5.0, p2->evaluateAsNumber(dummy_), 0.0);
|
assertEquals(5.0, p2->evaluateAsNumber(dummy_), 0.0);
|
||||||
} // test14
|
} // test14
|
||||||
|
|
Loading…
Reference in a new issue