Skip to content
Snippets Groups Projects
Commit f91b6003 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify call on nth element of tuple

parent db2062a6
No related branches found
No related tags found
No related merge requests found
...@@ -505,20 +505,20 @@ struct tuple_element < I, ext::ptr_tuple < Types... > > { ...@@ -505,20 +505,20 @@ struct tuple_element < I, ext::ptr_tuple < Types... > > {
typedef typename std::tuple_element < I, std::tuple < Types ... > >::type type; typedef typename std::tuple_element < I, std::tuple < Types ... > >::type type;
}; };
   
template < class Result, unsigned I, class Callable, class ... Types > template < class Result, unsigned I, class ... Types >
struct call_on_nth_helper < Result, I, ext::ptr_tuple < Types ... >, Callable > { struct call_on_nth_helper < Result, I, ext::ptr_tuple < Types ... > > {
template < class Tuple > template < class Tuple, class Callable >
static Result call_on_nth_fn ( Tuple && t, unsigned index, Callable callback ) { static Result call_on_nth_fn ( Tuple && t, unsigned index, Callable callback ) {
if ( index == 0 ) if ( index == 0 )
return callback ( std::get < ext::tuple_size < Tuple >::value - I > ( std::forward < ext::ptr_tuple < Types ... > > ( t ) ) ); return callback ( std::get < ext::tuple_size < Tuple >::value - I > ( std::forward < Tuple && > ( t ) ) );
else else
return call_on_nth_helper < Result, I - 1, ext::ptr_tuple < Types ... >, Callable >::call_on_nth_fn ( std::forward < Tuple && > ( t ), index - 1, callback ); return call_on_nth_helper < Result, I - 1, ext::ptr_tuple < Types ... > >::call_on_nth_fn ( std::forward < Tuple && > ( t ), index - 1, callback );
} }
}; };
   
template < class Result, class Callable, class ... Types > template < class Result, class ... Types >
struct call_on_nth_helper < Result, 0, ext::ptr_tuple < Types ... >, Callable > { struct call_on_nth_helper < Result, 0, ext::ptr_tuple < Types ... > > {
template < class Tuple > template < class Tuple, class Callable >
static Result call_on_nth_fn ( Tuple &&, unsigned, Callable ) { static Result call_on_nth_fn ( Tuple &&, unsigned, Callable ) {
throw std::out_of_range ( "Not enough elements in tuple." ); throw std::out_of_range ( "Not enough elements in tuple." );
} }
......
...@@ -91,23 +91,23 @@ struct tuple_element < I, const volatile T > { ...@@ -91,23 +91,23 @@ struct tuple_element < I, const volatile T > {
typedef typename std::add_cv < typename ext::tuple_element < I, T >::type >::type type; typedef typename std::add_cv < typename ext::tuple_element < I, T >::type >::type type;
}; };
   
template < class Result, unsigned I, class Tuple, class Callable > template < class Result, unsigned I, class Tuple >
struct call_on_nth_helper; struct call_on_nth_helper;
   
template < class Result, unsigned I, class Callable, class ... Types > template < class Result, unsigned I, class ... Types >
struct call_on_nth_helper < Result, I, ext::tuple < Types ... >, Callable > { struct call_on_nth_helper < Result, I, ext::tuple < Types ... > > {
template < class Tuple > template < class Tuple, class Callable >
static Result call_on_nth_fn ( Tuple && t, unsigned index, Callable callback ) { static Result call_on_nth_fn ( Tuple && t, unsigned index, Callable callback ) {
if ( index == 0 ) if ( index == 0 )
return callback ( std::get < ext::tuple_size < Tuple >::value - I > ( std::forward < Tuple && > ( t ) ) ); return callback ( std::get < ext::tuple_size < Tuple >::value - I > ( std::forward < Tuple && > ( t ) ) );
else else
return call_on_nth_helper < Result, I - 1, ext::tuple < Types ... >, Callable >::call_on_nth_fn ( std::forward < Tuple && > ( t ), index - 1, callback ); return call_on_nth_helper < Result, I - 1, ext::tuple < Types ... > >::call_on_nth_fn ( std::forward < Tuple && > ( t ), index - 1, callback );
} }
}; };
   
template < class Result, class Callable, class ... Types > template < class Result, class ... Types >
struct call_on_nth_helper < Result, 0, ext::tuple < Types ... >, Callable > { struct call_on_nth_helper < Result, 0, ext::tuple < Types ... > > {
template < class Tuple > template < class Tuple, class Callable >
static Result call_on_nth_fn ( Tuple &&, unsigned, Callable ) { static Result call_on_nth_fn ( Tuple &&, unsigned, Callable ) {
throw std::out_of_range ( "Not enough elements in tuple." ); throw std::out_of_range ( "Not enough elements in tuple." );
} }
...@@ -115,7 +115,7 @@ struct call_on_nth_helper < Result, 0, ext::tuple < Types ... >, Callable > { ...@@ -115,7 +115,7 @@ struct call_on_nth_helper < Result, 0, ext::tuple < Types ... >, Callable > {
   
template < class Result, class Tuple, class Callable > template < class Result, class Tuple, class Callable >
Result call_on_nth ( Tuple && t, unsigned index, Callable callback ) { Result call_on_nth ( Tuple && t, unsigned index, Callable callback ) {
return call_on_nth_helper < Result, ext::tuple_size < Tuple >::value, typename std::decay < Tuple >::type, Callable >::call_on_nth_fn ( std::forward < Tuple && > ( t ), index, callback ); return call_on_nth_helper < Result, ext::tuple_size < Tuple >::value, typename std::decay < Tuple >::type >::call_on_nth_fn ( std::forward < Tuple && > ( t ), index, callback );
} }
   
   
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment