aboutsummaryrefslogtreecommitdiff
path: root/source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md
diff options
context:
space:
mode:
authorAdrian Kummerlaender2014-09-11 13:08:41 +0200
committerAdrian Kummerlaender2014-09-11 13:08:41 +0200
commit78020547dae1fd46c330be60fe869fb33477b5e4 (patch)
treedae83ea73daa5585f5793e5be2d57a97c3bd6700 /source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md
parentded69be933abdb3ff69e3bf1991586a70fd13d63 (diff)
parentdffe5e060399eea7d8a6985664786cc2a5d933ff (diff)
downloadblog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.tar
blog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.tar.gz
blog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.tar.bz2
blog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.tar.lz
blog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.tar.xz
blog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.tar.zst
blog.kummerlaender.eu-78020547dae1fd46c330be60fe869fb33477b5e4.zip
Merge commit 'dffe5e060399eea7d8a6985664786cc2a5d933ff' as 'source/00_content'
Diffstat (limited to 'source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md')
-rw-r--r--source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md63
1 files changed, 63 insertions, 0 deletions
diff --git a/source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md b/source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md
new file mode 100644
index 0000000..99a8dce
--- /dev/null
+++ b/source/00_content/articles/2014-07-11_mapping_arrays_using_tuples_in_cpp11.md
@@ -0,0 +1,63 @@
+# Mapping arrays using tuples in C++11
+
+During my proof-of-concept implementation of external functions enabling [XSLT based static site generation](https://github.com/KnairdA/InputXSLT) I came upon the problem of calling a template method specialized on the Nth type of a `std::tuple` specialization using the Nth element of a array-like type instance as input. This was needed to implement a generic template-based interface for implementing [Apache Xalan](http://xalan.apache.org/xalan-c/index.html) external functions. This article aims to explain the particular approach taken to solve this problem.
+
+While it is possible to unpack a `std::tuple` instance into individual predefined objects using `std::tie` the standard library offers no such helper template for `unpacking` an array into individual objects and calling appropriate casting methods defined by a `std::tuple` mapping type. Sadly exactly this functionality is needed so that we are able to call a `constructDocument` member method of a class derived from the [`FunctionBase`](https://github.com/KnairdA/InputXSLT/blob/master/src/function/base.h) external function interface template class using static polymorphism provided through the [curiously recurring template pattern](https://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern). This interface template accepts the desired external function arguments as variadic template types and aims to provide the required validation and conversion boilerplate implementation. While we could recursively generate a `std::tuple` specialization instance from an array-like type using a approach simmilar to the one detailed in my article on [mapping binary structures as tuples using template metaprogramming](/article/mapping_binary_structures_as_tuples_using_template_metaprogramming) this wouldn't solve the problem of passing on the resulting values as individual objects. This is why I had to take the new approach of directly calling a template method on individual array elements using a `std::tuple` specialization as a kind of blueprint and passing the result values of this method to the `constructDocument` method as separate arguments.
+
+Extracting array elements obviously requires some way of defining the appropriate indexes and mapping the elements using a tuple blueprint additionally requires this way to be statically resolvable as one can not pass a dynamic index value to `std::tuple_element`. So the first step to fullfilling the defined requirements involved the implementation of a template based index or sequence type.
+
+~~~
+template <std::size_t...>
+struct Sequence {
+ typedef Sequence type;
+};
+
+template <
+ std::size_t Size,
+ std::size_t Index = 0,
+ std::size_t... Current
+>
+struct IndexSequence {
+ typedef typename std::conditional<
+ Index < Size,
+ IndexSequence<Size, Index + 1, Current..., Index>,
+ Sequence<Current...>
+ >::type::type type;
+};
+~~~
+{: .language-cpp}
+
+This is achieved by the [`IndexSequence` template](https://github.com/KnairdA/InputXSLT/blob/master/src/support/type/sequence.h) above by recursively specializing the `Sequence` template using static recursion controlled by the standard libraries template metaprogramming utility template `std::conditional`. This means that e.g. the type `Sequence<0, 1, 2, 3>` can also be defined as `IndexSequence<4>::type`.
+
+Now all that is required to accomplish the goal is instantiating the sequence type and passing it to a variadic member template as [follows](https://github.com/KnairdA/InputXSLT/blob/master/src/function/base.h):
+
+~~~
+[...]
+this->callConstructDocument(
+ parameters,
+ locator,
+ typename IndexSequence<parameter_count>::type()
+)
+[...]
+template <std::size_t... Index>
+inline xalan::XalanDocument* callConstructDocument(
+ const XObjectArgVectorType& parameters,
+ const xalan::Locator* locator,
+ Sequence<Index...>
+) const {
+ [...]
+ return this->document_cache_->create(
+ static_cast<const Implementation*>(this)->constructDocument(
+ valueGetter.get<typename std::tuple_element<
+ Index,
+ std::tuple<Types...>
+ >::type>(parameters[Index])...
+ )
+ );
+}
+~~~
+{: .language-cpp}
+
+As we can see a `IndexSequence` template specialization instance is passed to the variadic `callConstructDocument` method to expose the actual sequence values as `Index`. This method then resolves the `Index` parameter pack as both the array and `std::tuple` index inside the calls to the `valueGetter.get` template method which is called for every sequence element because of this. What this means is that we are now able to implement non-template `constructDocument` methods inside XSLT external function implementations such as [FunctionTransform](https://github.com/KnairdA/InputXSLT/blob/master/src/function/transform.h). The values passed to these methods are automatically extracted from the argument array and converted into their respective types as required.
+
+While this article only provided a short overview of mapping arrays using tuples in C++11 one may view the full implementation on [Github](https://github.com/KnairdA/InputXSLT/blob/master/src/function/base.h) or [cgit](http://code.kummerlaender.eu/InputXSLT/tree/src/function/base.h).