more parameterisation

This commit is contained in:
jez_higgins 2005-08-23 19:19:17 +00:00
parent b0033ea027
commit 2b61986c48
5 changed files with 68 additions and 68 deletions

View file

@ -232,7 +232,7 @@ public:
virtual XPathValue<string_type>* evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return new StringValue<string_type, string_adaptor>((baseT::argCount() > 0) ? baseT::argAsString(0, context, executionContext) : nodeStringValue(context));
return new StringValue<string_type, string_adaptor>((baseT::argCount() > 0) ? baseT::argAsString(0, context, executionContext) : nodeStringValue<string_type, string_adaptor>(context));
} // evaluate
}; // class StringFn
@ -375,7 +375,7 @@ public:
virtual XPathValue<string_type>* evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return new NumericValue<string_type, string_adaptor>(((baseT::argCount() > 0) ? baseT::argAsString(0, context, executionContext) : nodeStringValue(context)).length());
return new NumericValue<string_type, string_adaptor>(((baseT::argCount() > 0) ? baseT::argAsString(0, context, executionContext) : nodeStringValue<string_type, string_adaptor>(context)).length());
} // evaluate
}; // StringLengthFn
@ -390,7 +390,7 @@ public:
virtual XPathValue<string_type>* evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
string_type value = ((baseT::argCount() > 0) ? baseT::argAsString(0, context, executionContext) : nodeStringValue(context));
string_type value = ((baseT::argCount() > 0) ? baseT::argAsString(0, context, executionContext) : nodeStringValue<string_type, string_adaptor>(context));
size_t i = 0, ie = value.length();
// string leading space
@ -523,7 +523,7 @@ public:
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
double result = (baseT::argCount() > 0) ? baseT::argAsNumber(0, context, executionContext) :
StringValue<string_type, string_adaptor>(nodeStringValue(context)).asNumber();
StringValue<string_type, string_adaptor>(nodeStringValue<string_type, string_adaptor>(context)).asNumber();
return new NumericValue<string_type, string_adaptor>(result);
} // evaluate
}; // NumberFn
@ -542,7 +542,7 @@ public:
double sum = 0;
NodeSet<string_type> ns = baseT::argAsNodeSet(0, context, executionContext);
for(typename NodeSet<string_type>::const_iterator n = ns.begin(), end = ns.end(); n != end; ++n)
sum += nodeNumberValue(*n);
sum += nodeNumberValue<string_type, string_adaptor>(*n);
return new NumericValue<string_type, string_adaptor>(sum);
} // evaluate
}; // class SumFn

View file

@ -295,13 +295,13 @@ double stringAsNumber(const string_type& str)
} // catch
} // stringAsNumber
template<class string_type>
template<class string_type, class string_adaptor>
double nodeNumberValue(const DOM::Node<string_type>& node)
{
return stringAsNumber(nodeStringValue(node));
return stringAsNumber(nodeStringValue<string_type, string_adaptor>(node));
} // nodeNumberValue
template<class string_type>
template<class string_type, class string_adaptor>
string_type nodeStringValue(const DOM::Node<string_type>& node)
{
switch(node.getNodeType())
@ -311,7 +311,7 @@ string_type nodeStringValue(const DOM::Node<string_type>& node)
case DOM::Node_base::ELEMENT_NODE:
{
std::ostringstream os;
AxisEnumerator<string_type, Arabica::default_string_adaptor<string_type> > ae(node, DESCENDANT);
AxisEnumerator<string_type, string_adaptor> ae(node, DESCENDANT);
while(*ae != 0)
{
if((ae->getNodeType() == DOM::Node_base::TEXT_NODE) ||
@ -340,14 +340,14 @@ string_type nodeStringValue(const DOM::Node<string_type>& node)
////////////////////////////////////////////////////////////////////
namespace impl {
template<typename RT, typename string_type> struct value_of_node {
RT operator()(const DOM::Node<string_type>& node) { return nodeStringValue(node); }
template<typename RT, typename string_type, typename string_adaptor> struct value_of_node {
RT operator()(const DOM::Node<string_type>& node) { return nodeStringValue<string_type, string_adaptor>(node); }
};
template<typename string_type> struct value_of_node<double, string_type> {
double operator()(const DOM::Node<string_type>& node) { return nodeNumberValue(node); }
template<typename string_type, typename string_adaptor> struct value_of_node<double, string_type, string_adaptor> {
double operator()(const DOM::Node<string_type>& node) { return nodeNumberValue<string_type, string_adaptor>(node); }
};
template<class Op, class string_type>
template<class Op, class string_type, class string_adaptor>
class compareNodeWith
{
typedef typename Op::first_argument_type T;
@ -359,7 +359,7 @@ public:
bool operator()(const DOM::Node<string_type>& node)
{
value_of_node<T, string_type> nv;
value_of_node<T, string_type, string_adaptor> nv;
return Op()(nv(node), value_);
} // operator()
@ -369,7 +369,7 @@ private:
compareNodeWith& operator=(const compareNodeWith&);
}; // class compareNodeWith
template<class string_type>
template<class string_type, class string_adaptor>
bool nodeSetsEqual(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
const NodeSet<string_type>& lns = lhs->asNodeSet();
@ -380,11 +380,11 @@ bool nodeSetsEqual(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<st
std::set<string_type> values;
typename NodeSet<string_type>::const_iterator l = lns.begin();
string_type lvalue = nodeStringValue(*l);
string_type lvalue = nodeStringValue<string_type, string_adaptor>(*l);
for(typename NodeSet<string_type>::const_iterator r = rns.begin(), rend = rns.end(); r != rend; ++r)
{
string_type rvalue = nodeStringValue(*r);
string_type rvalue = nodeStringValue<string_type, string_adaptor>(*r);
if(lvalue == rvalue)
return true;
values.insert(rvalue);
@ -392,13 +392,13 @@ bool nodeSetsEqual(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<st
++l;
for(typename NodeSet<string_type>::const_iterator lend = lns.end(); l != lend; ++l)
if(values.find(nodeStringValue(*l)) != values.end())
if(values.find(nodeStringValue<string_type, string_adaptor>(*l)) != values.end())
return true;
return false;
} // nodeSetsEqual
template<class string_type>
template<class string_type, class string_adaptor>
bool nodeSetAndValueEqual(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
const NodeSet<string_type>& lns = lhs->asNodeSet();
@ -415,25 +415,25 @@ bool nodeSetAndValueEqual(const XPathValuePtr<string_type>& lhs, const XPathValu
case STRING:
return std::find_if(lns.begin(),
lns.end(),
compareNodeWith<std::equal_to<string_type>, string_type>(rhs->asString())) != lns.end();
compareNodeWith<std::equal_to<string_type>, string_type, string_adaptor>(rhs->asString())) != lns.end();
case NUMBER:
return std::find_if(lns.begin(),
lns.end(),
compareNodeWith<std::equal_to<double>, string_type>(rhs->asNumber())) != lns.end();
compareNodeWith<std::equal_to<double>, string_type, string_adaptor>(rhs->asNumber())) != lns.end();
default:
throw std::runtime_error("Node set == not yet implemented for type " + boost::lexical_cast<std::string>(rhs->type()));
} // switch
} // nodeSetAndValueEqual
template<class string_type>
template<class string_type, class string_adaptor>
double minValue(const NodeSet<string_type>& ns)
{
double v = nodeNumberValue(ns[0]);
double v = nodeNumberValue<string_type, string_adaptor>(ns[0]);
for(typename NodeSet<string_type>::const_iterator i = ns.begin(), ie = ns.end(); i != ie; ++i)
{
double vt = nodeNumberValue(*i);
double vt = nodeNumberValue<string_type, string_adaptor>(*i);
if(isNaN(vt))
continue;
if(!(vt > v)) // looks weird, but should account for infinity
@ -442,13 +442,13 @@ double minValue(const NodeSet<string_type>& ns)
return v;
} // minValue
template<class string_type>
template<class string_type, class string_adaptor>
double maxValue(const NodeSet<string_type>& ns)
{
double v = nodeNumberValue(ns[0]);
double v = nodeNumberValue<string_type, string_adaptor>(ns[0]);
for(typename NodeSet<string_type>::const_iterator i = ns.begin(), ie = ns.end(); i != ie; ++i)
{
double vt = nodeNumberValue(*i);
double vt = nodeNumberValue<string_type, string_adaptor>(*i);
if(isNaN(vt))
continue;
if(!(vt < v))
@ -457,34 +457,34 @@ double maxValue(const NodeSet<string_type>& ns)
return v;
} // maxValue
template<class Op, class string_type>
template<class Op, class string_type, class string_adaptor>
bool compareNodeSets(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
return Op()(minValue(lhs->asNodeSet()), maxValue(rhs->asNodeSet()));
return Op()(minValue<string_type, string_adaptor>(lhs->asNodeSet()), maxValue<string_type, string_adaptor>(rhs->asNodeSet()));
} // compareNodeSets
template<class Op, class string_type>
template<class Op, class string_type, class string_adaptor>
bool compareNodeSetWith(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
const NodeSet<string_type>& lns = lhs->asNodeSet();
return std::find_if(lns.begin(),
lns.end(),
compareNodeWith<Op, string_type>(rhs->asNumber())) != lns.end();
compareNodeWith<Op, string_type, string_adaptor>(rhs->asNumber())) != lns.end();
} // compareNodeSetAndValue
template<class string_type>
template<class string_type, class string_adaptor>
bool areEqual(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
ValueType lt = lhs->type();
ValueType rt = rhs->type();
if((lt == NODE_SET) && (rt == NODE_SET))
return nodeSetsEqual(lhs, rhs);
return nodeSetsEqual<string_type, string_adaptor>(lhs, rhs);
if(lt == NODE_SET)
return nodeSetAndValueEqual(lhs, rhs);
return nodeSetAndValueEqual<string_type, string_adaptor>(lhs, rhs);
if(rt == NODE_SET)
return nodeSetAndValueEqual(rhs, lhs);
return nodeSetAndValueEqual<string_type, string_adaptor>(rhs, lhs);
if((lt == BOOL) || (rt == BOOL))
return lhs->asBool() == rhs->asBool();
@ -498,52 +498,52 @@ bool areEqual(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_
return false;
} // areEquals
template<class string_type>
template<class string_type, class string_adaptor>
bool isLessThan(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
ValueType lt = lhs->type();
ValueType rt = rhs->type();
if((lt == NODE_SET) && (rt == NODE_SET))
return compareNodeSets<std::less<double> >(lhs, rhs);
return compareNodeSets<std::less<double>, string_type, string_adaptor>(lhs, rhs);
if(lt == NODE_SET)
return compareNodeSetWith<std::less<double> >(lhs, rhs);
return compareNodeSetWith<std::less<double>, string_type, string_adaptor>(lhs, rhs);
if(rt == NODE_SET)
return compareNodeSetWith<std::greater<double> >(rhs, lhs);
return compareNodeSetWith<std::greater<double>, string_type, string_adaptor>(rhs, lhs);
return lhs->asNumber() < rhs->asNumber();
} // isLessThan
template<class string_type>
template<class string_type, class string_adaptor>
bool isLessThanEquals(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
ValueType lt = lhs->type();
ValueType rt = rhs->type();
if((lt == NODE_SET) && (rt == NODE_SET))
return compareNodeSets<std::less_equal<double> >(lhs, rhs);
return compareNodeSets<std::less_equal<double>, string_type, string_adaptor>(lhs, rhs);
if(lt == NODE_SET)
return compareNodeSetWith<std::less_equal<double> >(lhs, rhs);
return compareNodeSetWith<std::less_equal<double>, string_type, string_adaptor>(lhs, rhs);
if(rt == NODE_SET)
return compareNodeSetWith<std::greater_equal<double> >(rhs, lhs);
return compareNodeSetWith<std::greater_equal<double>, string_type, string_adaptor>(rhs, lhs);
return lhs->asNumber() <= rhs->asNumber();
} // isLessThanEquals
template<class string_type>
template<class string_type, class string_adaptor>
bool isGreaterThan(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
return isLessThan(rhs, lhs);
return isLessThan<string_type, string_adaptor>(rhs, lhs);
} // isGreaterThan
template<class string_type>
template<class string_type, class string_adaptor>
bool isGreaterThanEquals(const XPathValuePtr<string_type>& lhs, const XPathValuePtr<string_type>& rhs)
{
return isLessThanEquals(rhs, lhs);
return isLessThanEquals<string_type, string_adaptor>(rhs, lhs);
} // isGreaterThanEquals
} // namespace impl

View file

@ -23,7 +23,7 @@ public:
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return BoolValue<string_type, string_adaptor>::createValue(areEqual(baseT::lhs()->evaluate(context, executionContext),
return BoolValue<string_type, string_adaptor>::createValue(areEqual<string_type, string_adaptor>(baseT::lhs()->evaluate(context, executionContext),
baseT::rhs()->evaluate(context, executionContext)));
} // evaluate
}; // class EqualsOperator
@ -41,7 +41,7 @@ public:
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return BoolValue<string_type, string_adaptor>::createValue(!areEqual(baseT::lhs()->evaluate(context, executionContext),
return BoolValue<string_type, string_adaptor>::createValue(!areEqual<string_type, string_adaptor>(baseT::lhs()->evaluate(context, executionContext),
baseT::rhs()->evaluate(context, executionContext)));
} // evaluate
}; // class NotEqualsOperator
@ -59,7 +59,7 @@ public:
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return BoolValue<string_type, string_adaptor>::createValue(isLessThan(baseT::lhs()->evaluate(context, executionContext),
return BoolValue<string_type, string_adaptor>::createValue(isLessThan<string_type, string_adaptor>(baseT::lhs()->evaluate(context, executionContext),
baseT::rhs()->evaluate(context, executionContext)));
} // evaluate
}; // class LessThanOperator
@ -77,7 +77,7 @@ public:
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return BoolValue<string_type, string_adaptor>::createValue(isLessThanEquals(baseT::lhs()->evaluate(context, executionContext),
return BoolValue<string_type, string_adaptor>::createValue(isLessThanEquals<string_type, string_adaptor>(baseT::lhs()->evaluate(context, executionContext),
baseT::rhs()->evaluate(context, executionContext)));
} // evaluate
}; // class LessThanEqualsOperator
@ -95,7 +95,7 @@ public:
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return BoolValue<string_type, string_adaptor>::createValue(isGreaterThan(baseT::lhs()->evaluate(context, executionContext),
return BoolValue<string_type, string_adaptor>::createValue(isGreaterThan<string_type, string_adaptor>(baseT::lhs()->evaluate(context, executionContext),
baseT::rhs()->evaluate(context, executionContext)));
} // evaluate
}; // class GreaterThanOperator
@ -113,7 +113,7 @@ public:
virtual XPathValuePtr<string_type> evaluate(const DOM::Node<string_type>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return BoolValue<string_type, string_adaptor>::createValue(isGreaterThanEquals(baseT::lhs()->evaluate(context, executionContext),
return BoolValue<string_type, string_adaptor>::createValue(isGreaterThanEquals<string_type, string_adaptor>(baseT::lhs()->evaluate(context, executionContext),
baseT::rhs()->evaluate(context, executionContext)));
} // evaluate
}; // class GreaterThanEqualsOperator

View file

@ -142,7 +142,7 @@ public:
} // asNumber
virtual string_type asString() const
{
return !set_.empty() ? impl::nodeStringValue(set_.top()) : string_adaptor().makeStringT("");
return !set_.empty() ? impl::nodeStringValue<string_type, string_adaptor>(set_.top()) : string_adaptor().makeStringT("");
} // asStringx
virtual const NodeSet<string_type>& asNodeSet() const { return set_; }

View file

@ -92,16 +92,16 @@ public:
XPathExpressionPtr<std::string> bf(new BoolValue<std::string, Arabica::default_string_adaptor<std::string> >(false));
XPathExpressionPtr<std::string> sf(new StringValue<std::string, Arabica::default_string_adaptor<std::string> >(""));
assertTrue(impl::areEqual(bt->evaluate(dummy_), (st->evaluate(dummy_))));
assertTrue(impl::areEqual(st->evaluate(dummy_), (bt->evaluate(dummy_))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(bt->evaluate(dummy_), (st->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(st->evaluate(dummy_), (bt->evaluate(dummy_)))));
assertTrue(impl::areEqual(sf->evaluate(dummy_), (bf->evaluate(dummy_))));
assertTrue(impl::areEqual(bf->evaluate(dummy_), (sf->evaluate(dummy_))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(sf->evaluate(dummy_), (bf->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(bf->evaluate(dummy_), (sf->evaluate(dummy_)))));
assertTrue(impl::areEqual(bt->evaluate(dummy_), (bt->evaluate(dummy_))));
assertTrue(impl::areEqual(bf->evaluate(dummy_), (bf->evaluate(dummy_))));
assertTrue(impl::areEqual(st->evaluate(dummy_), (st->evaluate(dummy_))));
assertTrue(impl::areEqual(sf->evaluate(dummy_), (sf->evaluate(dummy_))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(bt->evaluate(dummy_), (bt->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(bf->evaluate(dummy_), (bf->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(st->evaluate(dummy_), (st->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(sf->evaluate(dummy_), (sf->evaluate(dummy_)))));
} // test9
void test10()
@ -111,11 +111,11 @@ public:
XPathExpressionPtr<std::string> bf(new BoolValue<std::string, Arabica::default_string_adaptor<std::string> >(false));
XPathExpressionPtr<std::string> nf(new NumericValue<std::string, Arabica::default_string_adaptor<std::string> >(0.0));
assertTrue(impl::areEqual(bt->evaluate(dummy_), (nt->evaluate(dummy_))));
assertTrue(impl::areEqual(nt->evaluate(dummy_), (bt->evaluate(dummy_))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(bt->evaluate(dummy_), (nt->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(nt->evaluate(dummy_), (bt->evaluate(dummy_)))));
assertTrue(impl::areEqual(bf->evaluate(dummy_), (nf->evaluate(dummy_))));
assertTrue(impl::areEqual(nf->evaluate(dummy_), (bf->evaluate(dummy_))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(bf->evaluate(dummy_), (nf->evaluate(dummy_)))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(nf->evaluate(dummy_), (bf->evaluate(dummy_)))));
} // test10
void test11()
@ -123,7 +123,7 @@ public:
XPathExpressionPtr<std::string> nt(new NumericValue<std::string, Arabica::default_string_adaptor<std::string> >(1.0));
XPathValuePtr<std::string> ns = nt->evaluate(dummy_);
assertTrue(impl::areEqual(ns, (nt->evaluate(dummy_))));
assertTrue((impl::areEqual<std::string, Arabica::default_string_adaptor<std::string> >(ns, (nt->evaluate(dummy_)))));
} // test11
}; // ValueTest