From 5f6fc45749b99e9013f04c95a525f2d627db01bf Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 7 Jun 2014 17:24:44 +0200 Subject: Improved FunctionBase constructDocument parameter propagation * replaced std::tuple constructing Mapper template methods with direct XObjectArgVectorType unpacking ** XObjectValue::get template method is applied directly using parameter pack unpacking * implemented custom IndexSequence / Sequence type to provide vector indexes * modified all external functions to provide matching constructDocument overloads --- CMakeLists.txt | 2 +- src/function/base.h | 55 +++++++++++++++++++---------- src/function/external_text_formatter.cc | 11 ++---- src/function/external_text_formatter.h | 3 +- src/function/read_directory.cc | 4 +-- src/function/read_directory.h | 2 +- src/function/read_file.cc | 3 +- src/function/read_file.h | 2 +- src/function/read_xml_file.cc | 3 +- src/function/read_xml_file.h | 2 +- src/function/transform.cc | 17 +++------ src/function/transform.h | 4 ++- src/support/tuple/mapper.h | 62 --------------------------------- src/support/tuple/xobject_value.cc | 25 ------------- src/support/tuple/xobject_value.h | 17 --------- src/support/type/xobject_value.cc | 25 +++++++++++++ src/support/type/xobject_value.h | 17 +++++++++ 17 files changed, 98 insertions(+), 156 deletions(-) delete mode 100644 src/support/tuple/mapper.h delete mode 100644 src/support/tuple/xobject_value.cc delete mode 100644 src/support/tuple/xobject_value.h create mode 100644 src/support/type/xobject_value.cc create mode 100644 src/support/type/xobject_value.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c0b5b97..1aeec10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ set( src/support/error/error_multiplexer.cc src/support/error/error_capacitor.cc src/support/error/warning_capacitor.cc - src/support/tuple/xobject_value.cc + src/support/type/xobject_value.cc src/support/dom/document_cache.cc src/support/dom/document_cache_item.cc src/support/dom/result_node_facade.cc diff --git a/src/function/base.h b/src/function/base.h index 3609201..56712bb 100644 --- a/src/function/base.h +++ b/src/function/base.h @@ -12,8 +12,9 @@ #include "support/xalan_string.h" #include "support/filesystem_context.h" #include "support/include_entity_resolver.h" -#include "support/tuple/mapper.h" #include "support/dom/document_cache.h" +#include "support/type/sequence.h" +#include "support/type/xobject_value.h" namespace InputXSLT { @@ -22,18 +23,18 @@ template < typename... Types > class FunctionBase : public xalan::Function { - public: - typedef std::tuple parameter_tuple; + static const std::size_t parameter_count = sizeof...(Types); + public: FunctionBase(IncludeEntityResolver* resolver): include_resolver_(resolver), document_cache_(std::make_shared()) { } virtual xalan::XObjectPtr execute( xalan::XPathExecutionContext& executionContext, - xalan::XalanNode* context, - const XObjectArgVectorType& parameters, - const xalan::Locator* locator + xalan::XalanNode* context, + const XObjectArgVectorType& parameters, + const xalan::Locator* locator ) const { this->validateParameters( parameters, @@ -43,25 +44,22 @@ class FunctionBase : public xalan::Function { ); xalan::XalanDocument* const domDocument( - this->document_cache_->create( - static_cast( - const_cast(this) - )->constructDocument( - FilesystemContext(locator), - Mapper::template construct(parameters) - ) + this->callConstructDocument( + parameters, + locator, + typename IndexSequence::type() ) ); - xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodeList( + xalan::XPathExecutionContext::BorrowReturnMutableNodeRefList nodes( executionContext ); - nodeList->addNodes( + nodes->addNodes( *domDocument->getDocumentElement()->getChildNodes() ); - return executionContext.getXObjectFactory().createNodeSet(nodeList); + return executionContext.getXObjectFactory().createNodeSet(nodes); } virtual FunctionBase* clone( @@ -84,14 +82,33 @@ class FunctionBase : public xalan::Function { const xalan::XalanDOMString& getError( xalan::XalanDOMString& result) const { result.assign(std::string( - "The function expects " + - std::to_string(std::tuple_size::value) + + "The function expects " + + std::to_string(parameter_count) + " parameter(s)" ).data()); return result; } + template + inline xalan::XalanDocument* callConstructDocument( + const XObjectArgVectorType& parameters, + const xalan::Locator* locator, + Sequence + ) const { + return this->document_cache_->create( + static_cast( + const_cast(this) + )->constructDocument( + FilesystemContext(locator), + XObjectValue::get + >::type>(parameters[Index])... + ) + ); + } + inline void validateParameters( const XObjectArgVectorType& parameters, xalan::XPathExecutionContext& executionContext, @@ -106,7 +123,7 @@ class FunctionBase : public xalan::Function { } ); - if ( parameters.size() != std::tuple_size::value || anyNull ) { + if ( parameters.size() != parameter_count || anyNull ) { xalan::XPathExecutionContext::GetAndReleaseCachedString guard( executionContext ); diff --git a/src/function/external_text_formatter.cc b/src/function/external_text_formatter.cc index 22f53c5..9fef439 100644 --- a/src/function/external_text_formatter.cc +++ b/src/function/external_text_formatter.cc @@ -44,16 +44,9 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionExternalTextFormatter::constructDocument( const InputXSLT::FilesystemContext&, - const FunctionBase::parameter_tuple& parameters + std::string formatterPath, + std::string stdinText ) { - const std::string& formatterPath( - std::get<0>(parameters) - ); - - const std::string& stdinText( - std::get<1>(parameters) - ); - xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( nullptr, diff --git a/src/function/external_text_formatter.h b/src/function/external_text_formatter.h index 877bac5..95ff127 100644 --- a/src/function/external_text_formatter.h +++ b/src/function/external_text_formatter.h @@ -18,7 +18,8 @@ class FunctionExternalTextFormatter : public FunctionBase< xercesc::DOMDocument* constructDocument( const FilesystemContext&, - const FunctionBase::parameter_tuple& + std::string, + std::string ); }; diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc index e54e146..cfbe302 100644 --- a/src/function/read_directory.cc +++ b/src/function/read_directory.cc @@ -11,10 +11,10 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionReadDirectory::constructDocument( const InputXSLT::FilesystemContext& fsContext, - const FunctionBase::parameter_tuple& parameters + std::string path ) { const boost::filesystem::path directoryPath( - fsContext.resolve(std::get<0>(parameters)) + fsContext.resolve(path) ); xercesc::DOMDocument* const domDocument( diff --git a/src/function/read_directory.h b/src/function/read_directory.h index 88fcf15..6366a09 100644 --- a/src/function/read_directory.h +++ b/src/function/read_directory.h @@ -17,7 +17,7 @@ class FunctionReadDirectory : public FunctionBase< xercesc::DOMDocument* constructDocument( const FilesystemContext&, - const FunctionBase::parameter_tuple& + std::string ); }; diff --git a/src/function/read_file.cc b/src/function/read_file.cc index 79e321d..22e96d7 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -26,9 +26,8 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionReadFile::constructDocument( const FilesystemContext& fsContext, - const FunctionBase::parameter_tuple& parameters + std::string rawPath ) { - const std::string& rawPath = std::get<0>(parameters); boost::filesystem::path filePath(fsContext.resolve(rawPath)); if ( !(boost::filesystem::exists(filePath) && diff --git a/src/function/read_file.h b/src/function/read_file.h index cc1b662..fec8fe2 100644 --- a/src/function/read_file.h +++ b/src/function/read_file.h @@ -17,7 +17,7 @@ class FunctionReadFile : public FunctionBase< xercesc::DOMDocument* constructDocument( const FilesystemContext&, - const FunctionBase::parameter_tuple& + std::string ); }; diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc index cfa216c..65989b2 100644 --- a/src/function/read_xml_file.cc +++ b/src/function/read_xml_file.cc @@ -34,9 +34,8 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionReadXmlFile::constructDocument( const FilesystemContext& fsContext, - const FunctionBase::parameter_tuple& parameters + std::string rawPath ) { - const std::string& rawPath = std::get<0>(parameters); boost::filesystem::path filePath(fsContext.resolve(rawPath)); if ( !(boost::filesystem::exists(filePath) && diff --git a/src/function/read_xml_file.h b/src/function/read_xml_file.h index 6061257..7fe949a 100644 --- a/src/function/read_xml_file.h +++ b/src/function/read_xml_file.h @@ -17,7 +17,7 @@ class FunctionReadXmlFile : public FunctionBase< xercesc::DOMDocument* constructDocument( const FilesystemContext&, - const FunctionBase::parameter_tuple& + std::string ); }; diff --git a/src/function/transform.cc b/src/function/transform.cc index 844dee9..7e6207c 100644 --- a/src/function/transform.cc +++ b/src/function/transform.cc @@ -30,19 +30,12 @@ namespace InputXSLT { xercesc::DOMDocument* FunctionTransform::constructDocument( const InputXSLT::FilesystemContext& fsContext, - const FunctionBase::parameter_tuple& parameters + std::string transformationPath, + std::string targetPath, + xalan::XObjectPtr parameterObject ) { - const std::string transformationPath( - fsContext.resolve(std::get<0>(parameters)).string() - ); - - const std::string targetPath( - fsContext.resolve(std::get<1>(parameters)).string() - ); - - const xalan::XObjectPtr& parameterObject( - std::get<2>(parameters) - ); + transformationPath = fsContext.resolve(transformationPath).string(); + targetPath = fsContext.resolve(targetPath).string(); xercesc::DOMDocument* const domDocument( xercesc::DOMImplementation::getImplementation()->createDocument( diff --git a/src/function/transform.h b/src/function/transform.h index 810738e..b841750 100644 --- a/src/function/transform.h +++ b/src/function/transform.h @@ -19,7 +19,9 @@ class FunctionTransform : public FunctionBase< xercesc::DOMDocument* constructDocument( const FilesystemContext&, - const FunctionBase::parameter_tuple& + std::string, + std::string, + xalan::XObjectPtr ); }; diff --git a/src/support/tuple/mapper.h b/src/support/tuple/mapper.h deleted file mode 100644 index 28c5f3b..0000000 --- a/src/support/tuple/mapper.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef INPUTXSLT_SRC_SUPPORT_TUPLE_MAPPER_H_ -#define INPUTXSLT_SRC_SUPPORT_TUPLE_MAPPER_H_ - -#include - -#include -#include - -#include "common.h" -#include "xobject_value.h" - -namespace InputXSLT { - -template -using enable_if = typename std::enable_if::type; - -namespace Mapper { - template < - typename Target, - std::size_t Index = 0, - typename Current = std::tuple<>, - enable_if::value> = 0 - > - inline Target construct( - const xalan::XPathExecutionContext::XObjectArgVectorType&, - Current&& current - ) { - return current; - } - - template < - typename Target, - std::size_t Index = 0, - typename Current = std::tuple<>, - enable_if::value> = 0 - > - inline Target construct( - const xalan::XPathExecutionContext::XObjectArgVectorType& source, - Current&& current = std::tuple<>() - ) { - return construct< - Target, - Index + 1 - >( - source, - std::tuple_cat( - current, - std::make_tuple( - XObjectValue::get< - typename std::tuple_element::type - >( - source[Index] - ) - ) - ) - ); - } -} - -} - -#endif // INPUTXSLT_SRC_SUPPORT_TUPLE_MAPPER_H_ diff --git a/src/support/tuple/xobject_value.cc b/src/support/tuple/xobject_value.cc deleted file mode 100644 index cdeb9c6..0000000 --- a/src/support/tuple/xobject_value.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include "xobject_value.h" - -#include - -#include - -#include "support/xalan_string.h" - -namespace InputXSLT { - -namespace XObjectValue { - -template <> -std::string get(const xalan::XObjectPtr& ptr) { - return boost::trim_copy(toString(ptr->str())); -} - -template <> -xalan::XObjectPtr get(const xalan::XObjectPtr& ptr) { - return ptr; -} - -} - -} diff --git a/src/support/tuple/xobject_value.h b/src/support/tuple/xobject_value.h deleted file mode 100644 index bb602a4..0000000 --- a/src/support/tuple/xobject_value.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef INPUTXSLT_SRC_SUPPORT_TUPLE_XOBJECT_VALUE_H_ -#define INPUTXSLT_SRC_SUPPORT_TUPLE_XOBJECT_VALUE_H_ - -#include - -#include "common.h" - -namespace InputXSLT { - -namespace XObjectValue { - template - Type get(const xalan::XObjectPtr&); -} - -} - -#endif // INPUTXSLT_SRC_SUPPORT_TUPLE_XOBJECT_VALUE_H_ diff --git a/src/support/type/xobject_value.cc b/src/support/type/xobject_value.cc new file mode 100644 index 0000000..cdeb9c6 --- /dev/null +++ b/src/support/type/xobject_value.cc @@ -0,0 +1,25 @@ +#include "xobject_value.h" + +#include + +#include + +#include "support/xalan_string.h" + +namespace InputXSLT { + +namespace XObjectValue { + +template <> +std::string get(const xalan::XObjectPtr& ptr) { + return boost::trim_copy(toString(ptr->str())); +} + +template <> +xalan::XObjectPtr get(const xalan::XObjectPtr& ptr) { + return ptr; +} + +} + +} diff --git a/src/support/type/xobject_value.h b/src/support/type/xobject_value.h new file mode 100644 index 0000000..bb602a4 --- /dev/null +++ b/src/support/type/xobject_value.h @@ -0,0 +1,17 @@ +#ifndef INPUTXSLT_SRC_SUPPORT_TUPLE_XOBJECT_VALUE_H_ +#define INPUTXSLT_SRC_SUPPORT_TUPLE_XOBJECT_VALUE_H_ + +#include + +#include "common.h" + +namespace InputXSLT { + +namespace XObjectValue { + template + Type get(const xalan::XObjectPtr&); +} + +} + +#endif // INPUTXSLT_SRC_SUPPORT_TUPLE_XOBJECT_VALUE_H_ -- cgit v1.2.3