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

View File

@ -29,15 +29,25 @@ namespace util
//-----------------------------------------------------------------------------
int
main ()
int main ()
{
util::TAP::logger tap;
// Ensure tuples are mapped to themselves with type_tuple::type
{
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)),
"member access after conversion to tuple");
"dynamic member access after conversion to tuple");
}
}