From f231736bf5abba18adf9a14b4008c08723135823 Mon Sep 17 00:00:00 2001 From: jez_higgins <> Date: Tue, 16 Aug 2005 16:23:10 +0000 Subject: [PATCH] parameterised everything in xpath_arithmetic.hpp --- XPath/impl/xpath_arithmetic.hpp | 72 +++++++++++++++++++-------------- XPath/src/xpath_parser.cpp | 12 +++--- test/XPath/arithmetic_test.cpp | 30 +++++++------- 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/XPath/impl/xpath_arithmetic.hpp b/XPath/impl/xpath_arithmetic.hpp index d3cc8e74..f362f249 100644 --- a/XPath/impl/xpath_arithmetic.hpp +++ b/XPath/impl/xpath_arithmetic.hpp @@ -8,81 +8,93 @@ namespace Arabica namespace XPath { -class PlusOperator : private BinaryExpression, public XPathExpression +template +class PlusOperator : private BinaryExpression, public XPathExpression { + typedef BinaryExpression baseT; public: - PlusOperator(XPathExpression* lhs, XPathExpression* rhs) : - BinaryExpression(lhs, rhs) { } + PlusOperator(XPathExpression* lhs, XPathExpression* rhs) : + BinaryExpression(lhs, rhs) { } - virtual XPathValuePtr evaluate(const DOM::Node& context, + virtual XPathValuePtr evaluate(const DOM::Node& context, 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 }; // class PlusOperator -class MinusOperator : private BinaryExpression, public XPathExpression +template +class MinusOperator : private BinaryExpression, public XPathExpression { + typedef BinaryExpression baseT; public: - MinusOperator(XPathExpression* lhs, XPathExpression* rhs) : - BinaryExpression(lhs, rhs) { } + MinusOperator(XPathExpression* lhs, XPathExpression* rhs) : + BinaryExpression(lhs, rhs) { } - virtual XPathValuePtr evaluate(const DOM::Node& context, + virtual XPathValuePtr evaluate(const DOM::Node& context, 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 }; // class MinusOperator -class MultiplyOperator : private BinaryExpression, public XPathExpression +template +class MultiplyOperator : private BinaryExpression, public XPathExpression { + typedef BinaryExpression baseT; public: - MultiplyOperator(XPathExpression* lhs, XPathExpression* rhs) : - BinaryExpression(lhs, rhs) { } + MultiplyOperator(XPathExpression* lhs, XPathExpression* rhs) : + BinaryExpression(lhs, rhs) { } - virtual XPathValuePtr evaluate(const DOM::Node& context, + virtual XPathValuePtr evaluate(const DOM::Node& context, 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 }; // class MultiplyOperator -class DivideOperator : private BinaryExpression, public XPathExpression +template +class DivideOperator : private BinaryExpression, public XPathExpression { + typedef BinaryExpression baseT; public: - DivideOperator(XPathExpression* lhs, XPathExpression* rhs) : - BinaryExpression(lhs, rhs) { } + DivideOperator(XPathExpression* lhs, XPathExpression* rhs) : + BinaryExpression(lhs, rhs) { } - virtual XPathValuePtr evaluate(const DOM::Node& context, + virtual XPathValuePtr evaluate(const DOM::Node& context, 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 }; // class DivideOperator -class ModOperator : private BinaryExpression, public XPathExpression +template +class ModOperator : private BinaryExpression, public XPathExpression { + typedef BinaryExpression baseT; public: - ModOperator(XPathExpression* lhs, XPathExpression* rhs) : - BinaryExpression(lhs, rhs) { } + ModOperator(XPathExpression* lhs, XPathExpression* rhs) : + BinaryExpression(lhs, rhs) { } - virtual XPathValuePtr evaluate(const DOM::Node& context, + virtual XPathValuePtr evaluate(const DOM::Node& context, const ExecutionContext& executionContext) const { - return NumericValue::createValue(static_cast(lhs()->evaluateAsNumber(context)) % static_cast(rhs()->evaluateAsNumber(context))); + return NumericValue::createValue(static_cast(baseT::lhs()->evaluateAsNumber(context)) % static_cast(baseT::rhs()->evaluateAsNumber(context))); } // evaluate }; // class ModOperator -class UnaryNegative : private UnaryExpression, public XPathExpression +template +class UnaryNegative : private UnaryExpression, public XPathExpression { + typedef UnaryExpression baseT; public: - UnaryNegative(XPathExpression* expr) : - UnaryExpression(expr) { } + UnaryNegative(XPathExpression* expr) : + UnaryExpression(expr) { } - virtual XPathValuePtr evaluate(const DOM::Node& context, + virtual XPathValuePtr evaluate(const DOM::Node& context, const ExecutionContext& executionContext) const { - return NumericValue::createValue(-expr()->evaluate(context, executionContext)->asNumber()); + return NumericValue::createValue(-baseT::expr()->evaluate(context, executionContext)->asNumber()); } // evaluate }; // class UnaryNegative diff --git a/XPath/src/xpath_parser.cpp b/XPath/src/xpath_parser.cpp index b7ee8502..1b41a4a8 100644 --- a/XPath/src/xpath_parser.cpp +++ b/XPath/src/xpath_parser.cpp @@ -118,19 +118,19 @@ XPathExpression* createBinaryExpression(node_iter_t const& i, Compi switch(op) { case PlusOperator_id: - p1 = new PlusOperator(p1, p2); + p1 = new PlusOperator(p1, p2); break; case MinusOperator_id: - p1 = new MinusOperator(p1, p2); + p1 = new MinusOperator(p1, p2); break; case MultiplyOperator_id: - p1 = new MultiplyOperator(p1, p2); + p1 = new MultiplyOperator(p1, p2); break; case DivOperator_id: - p1 = new DivideOperator(p1, p2); + p1 = new DivideOperator(p1, p2); break; case ModOperator_id: - p1 = new ModOperator(p1, p2); + p1 = new ModOperator(p1, p2); break; case EqualsOperator_id: p1 = new EqualsOperator(p1, p2); @@ -230,7 +230,7 @@ XPathExpression* createUnaryExpression(node_iter_t const& i, Compil XPathExpression* createUnaryNegativeExpr(node_iter_t const& i, CompilationContext& context) { - return new UnaryNegative(compile_expression(i+1, context)); + return new UnaryNegative(compile_expression(i+1, context)); } // createUnaryNegativeExpr XPathExpressionPtr XPath::compile(const std::string& xpath) const diff --git a/test/XPath/arithmetic_test.cpp b/test/XPath/arithmetic_test.cpp index a135835c..6601dde3 100644 --- a/test/XPath/arithmetic_test.cpp +++ b/test/XPath/arithmetic_test.cpp @@ -25,7 +25,7 @@ public: XPathExpression* p1 = new NumericValue(1); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr add(new PlusOperator(p1, p2)); + XPathExpressionPtr add(new PlusOperator(p1, p2)); assertEquals(1, add.use_count()); add->evaluate(dummy_); @@ -40,7 +40,7 @@ public: XPathExpression* p1 = new NumericValue(1); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr minus(new MinusOperator(p1, p2)); + XPathExpressionPtr minus(new MinusOperator(p1, p2)); assertEquals(-1.0, minus->evaluateAsNumber(dummy_), 0.0); } // test2 @@ -50,16 +50,16 @@ public: XPathExpression* p1 = new NumericValue(3); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr mult(new MultiplyOperator(p1, p2)); + XPathExpressionPtr mult(new MultiplyOperator(p1, p2)); assertEquals(6, mult->evaluateAsNumber(dummy_), 0.0); } // test3 void test4() { - XPathExpression* mult = new MultiplyOperator(new NumericValue(4), new NumericValue(2)); + XPathExpression* mult = new MultiplyOperator(new NumericValue(4), new NumericValue(2)); - XPathExpressionPtr minus(new MinusOperator(mult, new NumericValue(2))); + XPathExpressionPtr minus(new MinusOperator(mult, new NumericValue(2))); assertEquals(8, mult->evaluateAsNumber(dummy_), 0.0); assertEquals(6, minus->evaluateAsNumber(dummy_), 0.0); @@ -70,7 +70,7 @@ public: XPathExpression* p1 = new NumericValue(12); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr div(new DivideOperator(p1, p2)); + XPathExpressionPtr div(new DivideOperator(p1, p2)); assertEquals(6, div->evaluateAsNumber(dummy_), 0.0); } // test5 @@ -80,7 +80,7 @@ public: XPathExpression* p1 = new NumericValue(12); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr mod(new ModOperator(p1, p2)); + XPathExpressionPtr mod(new ModOperator(p1, p2)); assertEquals(0, mod->evaluateAsNumber(dummy_), 0.0); } // test6 @@ -90,7 +90,7 @@ public: XPathExpression* p1 = new NumericValue(11); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr div(new DivideOperator(p1, p2)); + XPathExpressionPtr div(new DivideOperator(p1, p2)); assertEquals(5.5, div->evaluateAsNumber(dummy_), 0.0); } // test7 @@ -100,7 +100,7 @@ public: XPathExpression* p1 = new NumericValue(11); XPathExpression* p2 = new NumericValue(4); - XPathExpressionPtr mod(new ModOperator(p1, p2)); + XPathExpressionPtr mod(new ModOperator(p1, p2)); assertEquals(3, mod->evaluateAsNumber(dummy_), 0.0); } // test8 @@ -110,7 +110,7 @@ public: XPathExpression* p1 = new NumericValue(5); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr mod(new ModOperator(p1, p2)); + XPathExpressionPtr mod(new ModOperator(p1, p2)); assertEquals(1.0, mod->evaluateAsNumber(dummy_), 0.0); } // test9 @@ -120,7 +120,7 @@ public: XPathExpression* p1 = new NumericValue(5); XPathExpression* p2 = new NumericValue(-2); - XPathExpressionPtr mod(new ModOperator(p1, p2)); + XPathExpressionPtr mod(new ModOperator(p1, p2)); assertEquals(1.00, mod->evaluateAsNumber(dummy_), 0.0); } // test10 @@ -130,7 +130,7 @@ public: XPathExpression* p1 = new NumericValue(-5); XPathExpression* p2 = new NumericValue(2); - XPathExpressionPtr mod(new ModOperator(p1, p2)); + XPathExpressionPtr mod(new ModOperator(p1, p2)); assertEquals(-1.0, mod->evaluateAsNumber(dummy_), 0.0); } // test11 @@ -140,7 +140,7 @@ public: XPathExpression* p1 = new NumericValue(-5); XPathExpression* p2 = new NumericValue(-2); - XPathExpressionPtr mod(new ModOperator(p1, p2)); + XPathExpressionPtr mod(new ModOperator(p1, p2)); assertEquals(-1.0, mod->evaluateAsNumber(dummy_), 0.0); } // test12 @@ -148,7 +148,7 @@ public: void test13() { XPathExpression* p1 = new NumericValue(5); - XPathExpressionPtr p2(new UnaryNegative(p1)); + XPathExpressionPtr p2(new UnaryNegative(p1)); assertEquals(-5.0, p2->evaluateAsNumber(dummy_), 0.0); } // test13 @@ -156,7 +156,7 @@ public: void test14() { XPathExpression* p1 = new NumericValue(-5); - XPathExpressionPtr p2(new UnaryNegative(p1)); + XPathExpressionPtr p2(new UnaryNegative(p1)); assertEquals(5.0, p2->evaluateAsNumber(dummy_), 0.0); } // test14