#include "introspection/name.hpp" #include "introspection/type.hpp" #include "std.hpp" #include "view.hpp" #include "tap.hpp" #include /////////////////////////////////////////////////////////////////////////////// // simple test struct of scalars struct foo { int a; float b; }; //----------------------------------------------------------------------------- namespace bar { struct qux { short c; }; template struct templated_qux { }; } /////////////////////////////////////////////////////////////////////////////// template struct templated_with_type { }; template struct templated_with_value { }; enum enumeration_t { FOO }; template struct templated_with_enum { }; /////////////////////////////////////////////////////////////////////////////// // define introspection data namespace cruft::introspection { template <> struct type { typedef std::tuple< field, field > fields; }; template <> const std::string field::name = "a"; template <> const std::string field::name = "b"; } //----------------------------------------------------------------------------- template static bool equal (A &&a, B &&b) { return std::equal ( std::begin (a), std::end (a), std::begin (b), std::end (b) ); } ///---------------------------------------------------------------------------- /// 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; } //----------------------------------------------------------------------------- int main () { cruft::TAP::logger tap; // Ensure tuples are mapped to themselves with type_tuple::type { using src_t = std::tuple; using dst_t = typename cruft::introspection::type_tuple::type; tap.expect (std::is_same::value, "static identity type_tuple"); } // Check member extraction from a simple POD structure. { foo d_foo { 7, 42.0 }; auto f_tuple = cruft::introspection::as_tuple (d_foo); tap.expect (cruft::equal (d_foo.a, std::get<0> (f_tuple)) && cruft::equal (d_foo.b, std::get<1> (f_tuple)), "dynamic member access after conversion to tuple"); } tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "i08", "i08 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "i16", "i16 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "i32", "i32 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "i64", "i64 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "u08", "u08 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "u16", "u16 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "u32", "u32 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "u64", "u64 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "f32", "f32 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "f64", "f64 type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "foo", "struct type_name"); tap.expect_eq (cruft::view (cruft::introspection::name::bare ()), "qux", "de-namespaced struct type_name"); tap.expect ( equal ( cruft::introspection::name::full (), std::string_view ("bar::qux") ), "namespaced struct" ); tap.expect ( equal ( cruft::introspection::name::full> (), std::string_view ("bar::templated_qux") ), "namespaced templated struct" ); tap.expect_eq ( cruft::introspection::name::full> (), std::string_view {"templated_with_type"}, "templated_with_type" ); tap.expect_eq ( cruft::introspection::name::full> (), std::string_view {"templated_with_type"}, "templated_with_type" ); // 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>> (), std::string_view {"templated_with_type>"} ), "templated_with_type" ); tap.expect_eq ( cruft::introspection::name::full> (), std::string_view {"templated_with_value<-1>"}, "templated_with_value" ); tap.expect_eq ( cruft::introspection::name::full> (), std::string_view {"templated_with_enum"}, "templated_with_enum" ); tap.expect_eq ( cruft::introspection::name::member<&foo::a> (), std::string_view ("a"), "pointer-to-member name" ); return tap.status (); }