Skip to content

Commit

Permalink
meta: add typevals test
Browse files Browse the repository at this point in the history
  • Loading branch information
erlenner committed Nov 25, 2021
1 parent 9dacc71 commit a49edea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
21 changes: 1 addition & 20 deletions examples/meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ int main()
;
constexpr auto res = c.get<0>();
printf("c res: %s\n", typeid(res).name());
//tstruct<int>::Type t;
decltype(res)::Type t;
(void)sizeof(t);
}
Expand All @@ -64,28 +63,10 @@ int main()

iterate<c.size()>
([](auto index){
//printf("index: %d\n", index());
static_assert((index >= 0) && (index < c.size()));
printf("index: %s\n", typeid(index).name());
printf("itaration %d: %s\n", index(), typeid(index).name());
});
}
}

//if(tm.match_exec(
// [](auto& arg, const TLs& match) -> bool
// {
// return match.a == 4;
// }
// ,
// [](auto& arg, const TLs& match)
// {
// printf("match: %d, %f\n", match.a, match.b);
// printf("type: %s\n", typeid(arg).name());
// }
//) == 0)
// printf("tls success\n");
//else
// printf("no tls match\n");

return 0;
}
47 changes: 47 additions & 0 deletions tests/meta/typevals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <tyndall/meta/typevals.h>
#include <tyndall/meta/iterate.h>


int main()
{
{
constexpr struct A
{
int a; float b;
bool operator==(const A&) const = default;
} a{4,2};

constexpr struct B
{
char a;
double b;
long c;
bool operator==(const B&) const = default;
} b{6,6,6};

static constexpr auto tv =
typevals{}
+ 5
+ b
+ a
+ B{0,0,7};

static_assert(tv.size() == 4);
static_assert(tv.get<0>() == 5);
static_assert(tv.get<1>() == b);
static_assert(tv.get<2>() == a);
static_assert(tv.get<3>() == B{0,0,7});

static_assert(tv[std::integral_constant<int, 2>()] == a);

iterate<tv.size()>
([a, b](auto index){
static_assert((index >= 0) && (index < tv.size()));

if constexpr (index == 0) static_assert(tv[index] == 5);
else if constexpr (index == 1) static_assert(tv[index] == b);
else if constexpr (index == 2) static_assert(tv[index] == a);
else if constexpr (index == 3) static_assert(tv[index] == B{0,0,7});
});
}
}
2 changes: 2 additions & 0 deletions tyndall/meta/typevals.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct typevals<Type, Tail...> : public typevals<Tail...>
}

protected:

// get_type is a static helper for determining return type of get
template<int index>
static constexpr std::enable_if_t<index == sizeof...(Tail),
const Type&> get_type() noexcept
Expand Down

0 comments on commit a49edea

Please sign in to comment.