#ifndef ARABICA_XPATH_FUNCTION_HOLDER_HPP #define ARABICA_XPATH_FUNCTION_HOLDER_HPP #include #include "xpath_value.hpp" #include "xpath_function.hpp" namespace Arabica { namespace XPath { template XPathFunction* CreateFn(const std::vector >& argExprs) { return new function_type(argExprs); } typedef XPathFunction* (*CreateFnPtr)(const std::vector >& argExprs); struct NamedFunction { const char* name; CreateFnPtr creator; }; const NamedFunction FunctionLookupTable[] = { // node-set functions { "position", CreateFn }, { "last", CreateFn }, { "count", CreateFn }, { "local-name", CreateFn }, { "namespace-uri", CreateFn }, { "name", CreateFn }, // string functions {"string", CreateFn }, {"concat", CreateFn }, {"starts-with", CreateFn }, {"contains", CreateFn }, {"substring-before", CreateFn }, {"substring-after", CreateFn }, {"substring", CreateFn }, {"string-length", CreateFn }, {"normalize-space", CreateFn }, {"translate", CreateFn }, // boolean functions {"boolean", CreateFn }, {"not", CreateFn }, {"true", CreateFn }, {"false", CreateFn }, // number functions {"number", CreateFn }, {"sum", CreateFn }, {"floor", CreateFn }, {"ceiling", CreateFn }, {"round", CreateFn }, {0, 0} }; class FunctionHolder : public XPathExpression { public: FunctionHolder(XPathFunction* func) : func_(func) { } // FunctionHolder virtual ~FunctionHolder() { delete func_; } // ~FunctionHolder virtual XPathValuePtr evaluate(const DOM::Node& context, const ExecutionContext& executionContext) const { return XPathValuePtr(func_->evaluate(context, executionContext)); } // evaluate static FunctionHolder* createFunction(const std::string& name, const std::vector >& argExprs, const CompilationContext& context) { for(const NamedFunction* fn = FunctionLookupTable; fn->name != 0; ++fn) if(name == fn->name) return new FunctionHolder(fn->creator(argExprs)); XPathFunction* func = context.functionResolver().resolveFunction(name, argExprs); if(func == 0) throw std::runtime_error("Function " + name + " not implemented"); return new FunctionHolder(func); } // createFunction private: XPathFunction* func_; }; // class FunctionResolver } // namespace XPath } // namespace Arabica #endif