introspection: map tuples to themselves in type_tuple

This commit is contained in:
Danny Robson 2015-05-01 02:49:19 +10:00
parent 358f2ddb60
commit 70c65c2bb2
2 changed files with 43 additions and 15 deletions

View File

@ -67,10 +67,24 @@ namespace util {
template <class K> template <class K>
struct type { }; struct type { };
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/// traits class which converts an introspected type to a tuple /// traits class which converts an introspected type to a tuple
/// ///
/// K: target class /// K: target class
template <typename K>
struct type_tuple;
template <
typename ...T
> struct type_tuple<
std::tuple<T...>
> {
typedef std::tuple<T...> type;
};
template < template <
typename K, typename K,
typename I = typename make_indices< typename I = typename make_indices<
@ -78,19 +92,15 @@ namespace util {
typename type<K>::fields typename type<K>::fields
>::value >::value
>::type >::type
> > struct _type_tuple;
struct type_tuple;
template < template <
typename K, typename K,
size_t ...I size_t ...I
> > struct _type_tuple <
struct type_tuple<
K, K,
indices<I...> indices<I...>
> > {
{
typedef std::tuple< typedef std::tuple<
typename std::tuple_element< typename std::tuple_element<
I, I,
@ -99,6 +109,14 @@ namespace util {
> type; > type;
}; };
template <
typename K
> struct type_tuple {
typedef typename _type_tuple<K>::type type;
};
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
namespace detail { namespace detail {
template < template <

View File

@ -29,15 +29,25 @@ namespace util
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int int main ()
main ()
{ {
util::TAP::logger tap; util::TAP::logger tap;
foo d_foo { 7, 42.0 };
auto f_tuple = util::as_tuple (d_foo);
tap.expect (exactly_equal (d_foo.a, std::get<0> (f_tuple)) && // Ensure tuples are mapped to themselves with type_tuple::type
exactly_equal (d_foo.b, std::get<1> (f_tuple)), {
"member access after conversion to tuple"); using src_t = std::tuple<int>;
using dst_t = typename util::type_tuple<src_t>::type;
tap.expect (std::is_same<src_t, dst_t>::value, "static identity type_tuple");
}
// Check member extraction from a simple POD structure.
{
foo d_foo { 7, 42.0 };
auto f_tuple = util::as_tuple (d_foo);
tap.expect (exactly_equal (d_foo.a, std::get<0> (f_tuple)) &&
exactly_equal (d_foo.b, std::get<1> (f_tuple)),
"dynamic member access after conversion to tuple");
}
} }