diff --git a/test/tuple.cpp b/test/tuple.cpp index ed4441c3..b1dcd054 100644 --- a/test/tuple.cpp +++ b/test/tuple.cpp @@ -4,6 +4,12 @@ #include +template +struct int_mapper +{ + typedef int type; +}; + int main () { @@ -14,7 +20,7 @@ main () std::vector expected {{ 1, 2, 3, 4 }}; std::vector actual; - util::for_each ([&actual] (auto i) { actual.push_back (i); }, tuple); + util::tuple::for_each ([&actual] (auto i) { actual.push_back (i); }, tuple); tap.expect_eq (actual, expected, "value iteration"); } @@ -29,10 +35,20 @@ main () }; std::vector actual; - util::for_type ([&actual] (auto i) { + util::tuple::for_type ([&actual] (auto i) { actual.push_back (typeid (typename decltype(i)::type)); }); tap.expect_eq (actual, expected, "type iteration"); } + + { + using tuple_t = std::tuple; + using mapped_t = typename util::tuple::map::type; + + bool tuple = std::is_same::type, int>::value; + bool mapped = std::is_same::type, int>::value; + + tap.expect (!tuple && mapped, "tuple type mapping"); + } } diff --git a/tuple.hpp b/tuple.hpp index 1e8c4e8e..0eaa3111 100644 --- a/tuple.hpp +++ b/tuple.hpp @@ -18,12 +18,13 @@ #define __UTIL_TUPLE_HPP #include "types.hpp" +#include "variadic.hpp" #include #include -namespace util { +namespace util { namespace tuple { /////////////////////////////////////////////////////////////////////////// /// call a provided object with type_tag for each type in a tuple template < @@ -94,7 +95,45 @@ namespace util { f (std::get (t)); for_each (f, t); } -} + + /////////////////////////////////////////////////////////////////////////////// + /// Statically map the member types of a tuple via F<>::type + /// + /// T: tuple type + /// F: type mapping object, conversion uses F<>::type + /// I: tuple indexing helper + template < + typename T, + template < + typename + > class F, + typename I = typename make_indices< + std::tuple_size::value + >::type + > + struct map; + + + //----------------------------------------------------------------------------- + template < + typename T, + template < + typename + > class F, + size_t ...I + > + struct map< + T, + F, + indices + > { + typedef std::tuple< + typename F< + typename std::tuple_element::type + >::type... + > type; + }; +} } #endif