efactgoring to avoid creating so many temporary objects when evaluation expressions and values

This commit is contained in:
jez 2009-05-11 08:55:52 +01:00
parent dbed132ae5
commit 50fbb691cd
2 changed files with 79 additions and 32 deletions

View file

@ -48,30 +48,58 @@ public:
XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context) const XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context) const
{ {
ExecutionContext<string_type, string_adaptor> executionContext; return evaluate(context, StaticExecutionContext());
return evaluate(context, executionContext);
} // evaluate } // evaluate
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) const
{
return evaluateAsBool(context, StaticExecutionContext());
}
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) const
{
return evaluateAsNumber(context, StaticExecutionContext());
}
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) const
{
return evaluateAsString(context, StaticExecutionContext());
}
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context) const
{
return evaluateAsNodeSet(context, StaticExecutionContext());
}
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) const { return evaluate(context).asBool(); } virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) const { return evaluate(context).asNumber(); }
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) const { return evaluate(context).asString(); }
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context) const { return evaluate(context).asNodeSet(); }
virtual XPathValue<string_type, string_adaptor> evaluate(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const = 0; const ExecutionContext<string_type, string_adaptor>& executionContext) const = 0;
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context, virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const { return evaluate(context, executionContext).asBool(); } const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return evaluate(context, executionContext).asBool();
}
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context, virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const { return evaluate(context, executionContext).asNumber(); } const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return evaluate(context, executionContext).asNumber();
}
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context, virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const { return evaluate(context, executionContext).asString(); } const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return evaluate(context, executionContext).asString();
}
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context, virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const { return evaluate(context, executionContext).asNodeSet(); } const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return evaluate(context, executionContext).asNodeSet();
}
virtual void scan(impl::Expression_scanner<string_type, string_adaptor>& scanner) const { scanner.scan(this); } virtual void scan(impl::Expression_scanner<string_type, string_adaptor>& scanner) const { scanner.scan(this); }
private: private:
static const ExecutionContext<string_type, string_adaptor>& StaticExecutionContext()
{
static ExecutionContext<string_type, string_adaptor> executionContext;
return executionContext;
} // StaticExecutionContext
XPathExpression_impl(const XPathExpression_impl&); XPathExpression_impl(const XPathExpression_impl&);
bool operator==(const XPathExpression_impl&); bool operator==(const XPathExpression_impl&);
XPathExpression_impl& operator=(const XPathExpression_impl&); XPathExpression_impl& operator=(const XPathExpression_impl&);

View file

@ -14,8 +14,43 @@ namespace Arabica
namespace XPath namespace XPath
{ {
template<class string_type, class string_adaptor>
class Value_base : public XPathValue_impl<string_type, string_adaptor>, public XPathExpression_impl<string_type, string_adaptor>
{
protected:
Value_base() { }
~Value_base() { }
public:
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) { return asBool(); }
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) { return asNumber(); }
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) { return asString(); }
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context) { return asNodeSet(); }
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return asBool();
}
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return asNumber();
}
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return asString();
}
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context,
const ExecutionContext<string_type, string_adaptor>& executionContext) const
{
return asNodeSet();
}
}; // class Value_base
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> > template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
class BoolValue : public XPathValue_impl<string_type, string_adaptor>, public XPathExpression_impl<string_type, string_adaptor> class BoolValue : public Value_base<string_type, string_adaptor>
{ {
public: public:
BoolValue(bool value) : BoolValue(bool value) :
@ -28,10 +63,6 @@ public:
{ {
return XPathValue<string_type, string_adaptor>(new BoolValue(value_)); return XPathValue<string_type, string_adaptor>(new BoolValue(value_));
} // evaluate } // evaluate
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) { return asBool(); }
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) { return asNumber(); }
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) { return asString(); }
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context) { return asNodeSet(); }
virtual bool asBool() const { return value_; } virtual bool asBool() const { return value_; }
virtual double asNumber() const { return value_ ? 1 : 0; } virtual double asNumber() const { return value_ ? 1 : 0; }
@ -45,7 +76,7 @@ private:
}; // class BoolValue }; // class BoolValue
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> > template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
class NumericValue : public XPathValue_impl<string_type, string_adaptor>, public XPathExpression_impl<string_type, string_adaptor> class NumericValue : public Value_base<string_type, string_adaptor>
{ {
public: public:
NumericValue(double value) : NumericValue(double value) :
@ -58,10 +89,6 @@ public:
{ {
return createValue(value_); return createValue(value_);
} // evaluate } // evaluate
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) { return asBool(); }
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) { return asNumber(); }
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) { return asString(); }
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet(const DOM::Node<string_type, string_adaptor>& context) { return asNodeSet(); }
virtual bool asBool() const virtual bool asBool() const
{ {
@ -89,7 +116,7 @@ private:
}; // class NumberValue }; // class NumberValue
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> > template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
class StringValue : public XPathValue_impl<string_type, string_adaptor>, public XPathExpression_impl<string_type, string_adaptor> class StringValue : public Value_base<string_type, string_adaptor>
{ {
public: public:
StringValue(const char* value) : StringValue(const char* value) :
@ -104,10 +131,6 @@ public:
{ {
return XPathValue<string_type, string_adaptor>(new StringValue(value_)); return XPathValue<string_type, string_adaptor>(new StringValue(value_));
} // evaluate } // evaluate
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) { return asBool(); }
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) { return asNumber(); }
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) { return asString(); }
virtual NodeSet<string_type, string_adaptor> evaluateAsNodeSet() const { return asNodeSet(); }
virtual bool asBool() const { return !string_adaptor::empty(value_); } virtual bool asBool() const { return !string_adaptor::empty(value_); }
virtual double asNumber() const virtual double asNumber() const
@ -124,7 +147,7 @@ private:
}; // class StringValue }; // class StringValue
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> > template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
class NodeSetValue : public XPathValue_impl<string_type, string_adaptor>, public XPathExpression_impl<string_type, string_adaptor> class NodeSetValue : public Value_base<string_type, string_adaptor>
{ {
public: public:
NodeSetValue(const NodeSet<string_type, string_adaptor>& set) : set_(set) { } NodeSetValue(const NodeSet<string_type, string_adaptor>& set) : set_(set) { }
@ -139,10 +162,6 @@ public:
{ {
return XPathValue<string_type, string_adaptor>(this); return XPathValue<string_type, string_adaptor>(this);
} // evaluate } // evaluate
virtual bool evaluateAsBool(const DOM::Node<string_type, string_adaptor>& context) const{ return asBool(); }
virtual double evaluateAsNumber(const DOM::Node<string_type, string_adaptor>& context) const { return asNumber(); }
virtual string_type evaluateAsString(const DOM::Node<string_type, string_adaptor>& context) const { return asString(); }
virtual const NodeSet<string_type, string_adaptor>& evaluateAsNodeSet() const { return asNodeSet(); }
virtual bool asBool() const virtual bool asBool() const
{ {