2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/introspection/name.hpp>
|
|
|
|
#include <cruft/util/introspection/type.hpp>
|
|
|
|
#include <cruft/util/std.hpp>
|
|
|
|
#include <cruft/util/view.hpp>
|
2021-04-07 12:53:49 +10:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
2019-04-16 10:17:53 +10:00
|
|
|
|
|
|
|
#include <iostream>
|
2015-04-20 17:51:00 +10:00
|
|
|
|
2019-04-16 10:17:53 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-04-20 17:51:00 +10:00
|
|
|
// simple test struct of scalars
|
|
|
|
struct foo
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
float b;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2019-04-16 10:17:53 +10:00
|
|
|
namespace bar {
|
|
|
|
struct qux {
|
|
|
|
short c;
|
|
|
|
};
|
2020-09-24 14:40:56 +10:00
|
|
|
|
|
|
|
template <typename>
|
|
|
|
struct templated_qux { };
|
2019-04-16 10:17:53 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-28 13:48:45 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-02-27 08:00:18 +11:00
|
|
|
template <typename ...ValueT>
|
|
|
|
struct templated_with_type { };
|
2019-05-28 13:48:45 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <int ValueV>
|
|
|
|
struct templated_with_value { };
|
|
|
|
|
|
|
|
|
|
|
|
enum enumeration_t { FOO };
|
|
|
|
|
|
|
|
template <enumeration_t EnumV>
|
|
|
|
struct templated_with_enum { };
|
|
|
|
|
|
|
|
|
2019-04-16 10:17:53 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-04-20 17:51:00 +10:00
|
|
|
// define introspection data
|
2020-09-24 15:41:58 +10:00
|
|
|
namespace cruft::introspection
|
2015-04-20 17:51:00 +10:00
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct type<foo>
|
|
|
|
{
|
|
|
|
typedef std::tuple<
|
|
|
|
field<foo,int,&foo::a>,
|
|
|
|
field<foo,float,&foo::b>
|
|
|
|
> fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> const std::string field<foo,int,&foo::a>::name = "a";
|
|
|
|
template <> const std::string field<foo,float,&foo::b>::name = "b";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-24 14:40:56 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename A, typename B>
|
|
|
|
static
|
|
|
|
bool
|
|
|
|
equal (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return std::equal (
|
|
|
|
std::begin (a), std::end (a),
|
|
|
|
std::begin (b), std::end (b)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-22 09:38:59 +11:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// Returns true if the two strings are equal except for any embedded
|
|
|
|
/// whitespace.
|
|
|
|
static bool
|
|
|
|
whitespace_equal (std::string_view a, std::string_view b)
|
|
|
|
{
|
|
|
|
auto const a_size = std::ssize (a);
|
|
|
|
auto const b_size = std::ssize (b);
|
|
|
|
|
|
|
|
int a_cursor = 0;
|
|
|
|
int b_cursor = 0;
|
|
|
|
|
|
|
|
while (a_cursor < a_size && b_cursor < b_size) {
|
|
|
|
if (a[a_cursor] == ' ') {
|
|
|
|
++a_cursor;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b[b_cursor] == ' ') {
|
|
|
|
++b_cursor;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a[a_cursor] != b[b_cursor])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++a_cursor;
|
|
|
|
++b_cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a_cursor == a_size && b_cursor == b_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-20 17:51:00 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-05-01 02:49:19 +10:00
|
|
|
int main ()
|
2015-04-20 17:51:00 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2015-04-20 17:51:00 +10:00
|
|
|
|
2015-05-01 02:49:19 +10:00
|
|
|
// Ensure tuples are mapped to themselves with type_tuple::type
|
|
|
|
{
|
|
|
|
using src_t = std::tuple<int>;
|
2020-09-24 15:41:58 +10:00
|
|
|
using dst_t = typename cruft::introspection::type_tuple<src_t>::type;
|
2015-05-01 02:49:19 +10:00
|
|
|
|
|
|
|
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 };
|
2020-09-24 15:41:58 +10:00
|
|
|
auto f_tuple = cruft::introspection::as_tuple (d_foo);
|
2015-05-01 02:49:19 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (cruft::equal (d_foo.a, std::get<0> (f_tuple)) &&
|
|
|
|
cruft::equal (d_foo.b, std::get<1> (f_tuple)),
|
2015-05-01 02:49:19 +10:00
|
|
|
"dynamic member access after conversion to tuple");
|
|
|
|
}
|
2017-05-22 13:55:21 +10:00
|
|
|
|
2020-09-24 15:41:58 +10:00
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<i08> ()), "i08", "i08 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<i16> ()), "i16", "i16 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<i32> ()), "i32", "i32 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<i64> ()), "i64", "i64 type_name");
|
2019-04-16 10:17:53 +10:00
|
|
|
|
2020-09-24 15:41:58 +10:00
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<u08> ()), "u08", "u08 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<u16> ()), "u16", "u16 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<u32> ()), "u32", "u32 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<u64> ()), "u64", "u64 type_name");
|
2019-04-16 10:17:53 +10:00
|
|
|
|
2020-09-24 15:41:58 +10:00
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<f32> ()), "f32", "f32 type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<f64> ()), "f64", "f64 type_name");
|
2019-04-16 10:17:53 +10:00
|
|
|
|
2020-09-24 15:41:58 +10:00
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<foo> ()), "foo", "struct type_name");
|
|
|
|
tap.expect_eq (cruft::view (cruft::introspection::name::bare<bar::qux> ()), "qux", "de-namespaced struct type_name");
|
2020-09-24 14:40:56 +10:00
|
|
|
|
|
|
|
tap.expect (
|
|
|
|
equal (
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::name::full<bar::qux> (),
|
2020-09-24 14:40:56 +10:00
|
|
|
std::string_view ("bar::qux")
|
|
|
|
),
|
|
|
|
"namespaced struct"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect (
|
|
|
|
equal (
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::name::full<bar::templated_qux<int>> (),
|
2020-09-24 14:40:56 +10:00
|
|
|
std::string_view ("bar::templated_qux<int>")
|
|
|
|
),
|
|
|
|
"namespaced templated struct"
|
|
|
|
);
|
2019-04-16 10:17:53 +10:00
|
|
|
|
2019-05-28 13:48:45 +10:00
|
|
|
tap.expect_eq (
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::name::full<templated_with_type<int>> (),
|
2019-05-28 13:48:45 +10:00
|
|
|
std::string_view {"templated_with_type<int>"},
|
|
|
|
"templated_with_type"
|
|
|
|
);
|
|
|
|
|
2020-02-27 08:00:18 +11:00
|
|
|
tap.expect_eq (
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::name::full<templated_with_type<int, int>> (),
|
2020-02-27 08:00:18 +11:00
|
|
|
std::string_view {"templated_with_type<int, int>"},
|
|
|
|
"templated_with_type"
|
|
|
|
);
|
|
|
|
|
2020-10-22 09:38:59 +11:00
|
|
|
// The trailing '>' characters are separated by whitespace in GCC, and
|
|
|
|
// Clang<11, but aren't meaningfully different in user interpretation.
|
|
|
|
tap.expect (
|
|
|
|
whitespace_equal (
|
|
|
|
cruft::introspection::name::full<templated_with_type<templated_with_type<int>>> (),
|
|
|
|
std::string_view {"templated_with_type<templated_with_type<int>>"}
|
|
|
|
),
|
2020-02-27 07:05:57 +11:00
|
|
|
"templated_with_type"
|
|
|
|
);
|
|
|
|
|
2019-05-28 13:48:45 +10:00
|
|
|
tap.expect_eq (
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::name::full<templated_with_value<-1>> (),
|
2019-05-28 13:48:45 +10:00
|
|
|
std::string_view {"templated_with_value<-1>"},
|
|
|
|
"templated_with_value"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect_eq (
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::name::full<templated_with_enum<FOO>> (),
|
2019-05-28 13:48:45 +10:00
|
|
|
std::string_view {"templated_with_enum<FOO>"},
|
|
|
|
"templated_with_enum"
|
|
|
|
);
|
|
|
|
|
2021-03-26 16:24:31 +11:00
|
|
|
tap.expect_eq (
|
|
|
|
cruft::introspection::name::member<&foo::a> (),
|
|
|
|
std::string_view ("a"),
|
|
|
|
"pointer-to-member name"
|
|
|
|
);
|
|
|
|
|
2017-05-22 13:55:21 +10:00
|
|
|
return tap.status ();
|
2015-04-20 17:51:00 +10:00
|
|
|
}
|