From 04de102789935c512f64da9d0cb957be7fa953c0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 24 Sep 2020 15:41:58 +1000 Subject: [PATCH] introspection: split the header into functional groups --- CMakeLists.txt | 8 +- cmdopt.hpp | 14 +- introspection.cpp => introspection/enum.cpp | 4 +- introspection.hpp => introspection/enum.hpp | 405 ++++---------------- introspection/name.cpp | 9 + introspection/name.hpp | 124 ++++++ introspection/type.cpp | 0 introspection/type.hpp | 164 ++++++++ parse/enum.hpp | 7 +- registrar.hpp | 14 +- test/hash/table.cpp | 6 +- test/introspection.cpp | 47 +-- test/rand/buckets.cpp | 4 +- 13 files changed, 426 insertions(+), 380 deletions(-) rename introspection.cpp => introspection/enum.cpp (72%) rename introspection.hpp => introspection/enum.hpp (52%) create mode 100644 introspection/name.cpp create mode 100644 introspection/name.hpp create mode 100644 introspection/type.cpp create mode 100644 introspection/type.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a4a85f8c..72a3ff41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -398,8 +398,12 @@ list ( hash/xxhash.hpp init.cpp init.hpp - introspection.cpp - introspection.hpp + introspection/enum.cpp + introspection/enum.hpp + introspection/name.cpp + introspection/name.hpp + introspection/type.cpp + introspection/type.hpp io.cpp io.hpp iterator/cast.hpp diff --git a/cmdopt.hpp b/cmdopt.hpp index 94141d88..34886a74 100644 --- a/cmdopt.hpp +++ b/cmdopt.hpp @@ -6,10 +6,10 @@ * Copyright 2013-2016 Danny Robson */ -#ifndef CRUFT_UTIL_CMDLINE_HPP -#define CRUFT_UTIL_CMDLINE_HPP +#pragma once -#include "introspection.hpp" +#include "introspection/name.hpp" +#include "introspection/enum.hpp" #include "iterator/infix.hpp" #include @@ -156,7 +156,7 @@ namespace cruft::cmdopt { { static const std::string EXAMPLE = std::string {"<"} + - std::string {type_name ()} + + std::string {cruft::introspection::name::bare ()} + std::string {">"}; return EXAMPLE; @@ -168,8 +168,8 @@ namespace cruft::cmdopt { { static const std::string EXAMPLE = [] (void) { std::ostringstream os; - std::copy (std::cbegin (enum_traits::names), - std::cend (enum_traits::names), + std::copy (std::cbegin (introspection::enum_traits::names), + std::cend (introspection::enum_traits::names), iterator::infix_iterator (os, "|")); return os.str (); } (); @@ -360,5 +360,3 @@ namespace cruft::cmdopt { std::vector m_options; }; } - -#endif diff --git a/introspection.cpp b/introspection/enum.cpp similarity index 72% rename from introspection.cpp rename to introspection/enum.cpp index d3a88e6c..2e012cf9 100644 --- a/introspection.cpp +++ b/introspection/enum.cpp @@ -3,7 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * - * Copyright 2017 Danny Robson + * Copyright 2015-2017 Danny Robson */ -#include "introspection.hpp" +#include "./enum.hpp" diff --git a/introspection.hpp b/introspection/enum.hpp similarity index 52% rename from introspection.hpp rename to introspection/enum.hpp index 99ab70bb..d3fea86f 100644 --- a/introspection.hpp +++ b/introspection/enum.hpp @@ -8,128 +8,11 @@ #pragma once -#include "std.hpp" -#include "algo/search.hpp" - +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -namespace cruft { - template - constexpr - std::string_view type_name_with_namespace (void) - { - std::string_view pretty_function (__PRETTY_FUNCTION__); - -#ifdef __clang__ - // PRETTY_FUNCTION = std::string_view cruft::type_name() [T = std::__1::vector >] - - std::string_view const prefix = "[T = "; -#elif defined(__GNUC__) - // PRETTY_FUNCTION = "constexpr std::string_view cruft::type_name() [with T = std::__debug::vector; std::string_view = std::basic_string_view]" - - std::string_view prefix = "[with T = "; -#else -#error "Unsupported compiler" -#endif - - // Find the location where the type begins. - auto const type_begin = std::search ( - pretty_function.begin (), - pretty_function.end (), - prefix.begin (), - prefix.end () - ) + prefix.size (); - - // Find the point the type name ends. Both use ']', but gcc lists - // std::string_view in the function signature too and so requires the - // delimiting ';' as a suffix. - char const suffixes[] = ";]"; - auto const type_end = std::find_first_of ( - type_begin, - pretty_function.end (), - std::begin (suffixes), - std::end (suffixes) - ); - - // Find the start of the first template parameter so we can cut it out. - // If this isn't present we end up with a pointer to the end of the - // type string which is the end of the type anyway. - auto const template_start = std::find (type_begin, type_end, '<'); - auto const template_end = cruft::search::balanced (template_start, type_end, '<', '>'); - - return std::string_view ( - type_begin, - std::distance (type_begin, template_end) - ); - } - - - template - constexpr - std::string_view type_name (void) - { - - auto const fullname = type_name_with_namespace (); - - constexpr char const namespace_symbol[] = "::"; - auto const last_namespace_pos = std::search ( - std::rbegin (fullname), - std::rend (fullname), - namespace_symbol + 0, - namespace_symbol + 2 - ); - - auto const length = std::distance (std::rbegin (fullname), last_namespace_pos); - auto const offset = fullname.size () - length; - return std::string_view (fullname.data () + offset, length); - } - - - template <> constexpr std::string_view type_name (void) { return "i08"; } - template <> constexpr std::string_view type_name (void) { return "i16"; } - template <> constexpr std::string_view type_name (void) { return "i32"; } - template <> constexpr std::string_view type_name (void) { return "i64"; } - - template <> constexpr std::string_view type_name (void) { return "u08"; } - template <> constexpr std::string_view type_name (void) { return "u16"; } - template <> constexpr std::string_view type_name (void) { return "u32"; } - template <> constexpr std::string_view type_name (void) { return "u64"; } - - template <> constexpr std::string_view type_name (void) { return "f32"; } - template <> constexpr std::string_view type_name (void) { return "f64"; } - - - /////////////////////////////////////////////////////////////////////////// - template - struct type_char; - - template <> struct type_char { static constexpr char value = 'f'; }; - template <> struct type_char { static constexpr char value = 'd'; }; - - template <> struct type_char< int8_t> { static constexpr char value = 'i'; }; - template <> struct type_char< int16_t> { static constexpr char value = 'i'; }; - template <> struct type_char< int32_t> { static constexpr char value = 'i'; }; - template <> struct type_char< int64_t> { static constexpr char value = 'i'; }; - - template <> struct type_char< uint8_t> { static constexpr char value = 'u'; }; - template <> struct type_char { static constexpr char value = 'u'; }; - template <> struct type_char { static constexpr char value = 'u'; }; - template <> struct type_char { static constexpr char value = 'u'; }; - - template - constexpr auto type_char_v = type_char::value; +namespace cruft::introspection { /////////////////////////////////////////////////////////////////////////// /// Lists valid values of an enumeration /// @@ -141,7 +24,7 @@ namespace cruft { /// values: static const std::array template < typename E - > + > struct enum_traits; /////////////////////////////////////////////////////////////////////////// @@ -156,7 +39,7 @@ namespace cruft { /// from it we can avoid this problem. Revist this solution at clang-4.0. #define INTROSPECTION_ENUM_DECL(NS,E, ...) \ - namespace cruft { \ + namespace cruft::introspection { \ struct PASTE(__enum_traits_,E) { \ using value_type = ::NS::E; \ \ @@ -186,17 +69,17 @@ namespace cruft { #define INTROSPECTION_ENUM_IMPL(NS,E, ...) \ const \ std::array< \ - cruft::enum_traits<::NS::E>::value_type, \ - cruft::enum_traits<::NS::E>::value_count \ - > PASTE(cruft::__enum_traits_,E)::values = { \ + cruft::introspection::enum_traits<::NS::E>::value_type, \ + cruft::introspection::enum_traits<::NS::E>::value_count \ + > PASTE(cruft::introspection::__enum_traits_,E)::values = { \ MAP1(NAMESPACE_LIST, ::NS::E, __VA_ARGS__) \ }; \ \ const \ std::array< \ const char*, \ - cruft::enum_traits<::NS::E>::value_count \ - > PASTE(cruft::__enum_traits_,E)::names = { \ + cruft::introspection::enum_traits<::NS::E>::value_count \ + > PASTE(cruft::introspection::__enum_traits_,E)::names = { \ MAP0(STRINGIZE_LIST, __VA_ARGS__) \ }; @@ -213,38 +96,38 @@ namespace cruft { /// /// For trivial enumerations INTROSPECTION_ENUM may be easier to use. - #define INTROSPECTION_ENUM_ISTREAM(NS,E) \ - std::istream& \ - ::NS::operator>> (std::istream &is, ::NS::E &e) \ - { \ - using traits = ::cruft::enum_traits<::NS::E>; \ - \ - std::string name; \ - is >> name; \ - \ - std::transform (std::begin (name), \ - std::end (name), \ - std::begin (name), \ - ::toupper); \ - \ - auto name_pos = std::find ( \ - std::cbegin (traits::names), \ - std::cend (traits::names), \ - name \ - ); \ - \ - if (name_pos == std::cend (traits::names)) { \ - is.setstate (std::istream::failbit); \ - } else { \ - auto d = std::distance ( \ - std::begin (traits::names), \ - name_pos \ - ); \ - \ - e = traits::values[d]; \ - } \ - \ - return is; \ + #define INTROSPECTION_ENUM_ISTREAM(NS,E) \ + std::istream& \ + ::NS::operator>> (std::istream &is, ::NS::E &e) \ + { \ + using traits = ::cruft::introspection::enum_traits<::NS::E>; \ + \ + std::string name; \ + is >> name; \ + \ + std::transform (std::begin (name), \ + std::end (name), \ + std::begin (name), \ + ::toupper); \ + \ + auto name_pos = std::find ( \ + std::cbegin (traits::names), \ + std::cend (traits::names), \ + name \ + ); \ + \ + if (name_pos == std::cend (traits::names)) { \ + is.setstate (std::istream::failbit); \ + } else { \ + auto d = std::distance ( \ + std::begin (traits::names), \ + name_pos \ + ); \ + \ + e = traits::values[d]; \ + } \ + \ + return is; \ } @@ -253,35 +136,36 @@ namespace cruft { /// /// Expects to be called from outside all namespaces. /// - /// The user is responsible for specialising the ::cruft::enum_traits - /// parameters which are used to drive the implementation (eg, through - /// INTROSPECTION_ENUM_DECL, and INTROSPECTION_ENUM_IMPL). + /// The user is responsible for specialising the + /// ::cruft::introspection:;enum_traits parameters which are used + /// to drive the implementation (eg, through INTROSPECTION_ENUM_DECL, + /// and INTROSPECTION_ENUM_IMPL). /// /// For trivial enumerations INTROSPECTION_ENUM may be easier to use. - #define INTROSPECTION_ENUM_OSTREAM(NS,E) \ - std::ostream& \ - ::NS::operator<< (std::ostream &os, ::NS::E e) \ - { \ - using traits = ::cruft::enum_traits<::NS::E>; \ - \ - auto val_pos = std::find ( \ - std::cbegin (traits::values), \ - std::cend (traits::values), \ - e \ - ); \ - \ - if (val_pos == std::cend (traits::values)) { \ - os.setstate (std::ostream::failbit); \ - } else { \ - auto d = std::distance ( \ - std::cbegin (traits::values), \ - val_pos \ - ); \ - \ - os << traits::names[d]; \ - } \ - \ - return os; \ + #define INTROSPECTION_ENUM_OSTREAM(NS,E) \ + std::ostream& \ + ::NS::operator<< (std::ostream &os, ::NS::E e) \ + { \ + using traits = ::cruft::introspection::enum_traits<::NS::E>;\ + \ + auto val_pos = std::find ( \ + std::cbegin (traits::values), \ + std::cend (traits::values), \ + e \ + ); \ + \ + if (val_pos == std::cend (traits::values)) { \ + os.setstate (std::ostream::failbit); \ + } else { \ + auto d = std::distance ( \ + std::cbegin (traits::values), \ + val_pos \ + ); \ + \ + os << traits::names[d]; \ + } \ + \ + return os; \ } @@ -318,153 +202,4 @@ namespace cruft { INTROSPECTION_ENUM_IMPL(detail_intr_enum,E,__VA_ARGS__) \ INTROSPECTION_ENUM_ISTREAM(detail_intr_enum,E) \ INTROSPECTION_ENUM_OSTREAM(detail_intr_enum,E) - - - /////////////////////////////////////////////////////////////////////////// - /// Describes a single member variable in a type availabe for introspection - /// - /// K: target class - /// R: member type - /// M: pointer-to-member - template < - class K, - typename R, - R K::*M - > - struct field - { - typedef K klass; - typedef R type; - - static const std::string name; - - static const R& get (const K &k) { return k.*M; } - static R& get ( K &k) { return k.*M; } - static R& get ( K &&) = delete; - }; - - /////////////////////////////////////////////////////////////////////////// - /// Holds the fields of a type available for introspection - /// - /// Specialise the following type struct with a 'fields' tuple of the - /// members that should be accessed like below: - /// - /// struct foo { int a; int b; }; - /// - /// template <> struct type - /// { - /// typedef std::tuple< - /// field, - /// field - /// > fields; - /// }; - /// - /// template <> const std::string field::name = "a"; - /// template <> const std::string field::name = "b"; - - template - struct type { }; - - - /////////////////////////////////////////////////////////////////////////// - /// traits class which converts an introspected type to a tuple - /// - /// K: target class - - template - struct type_tuple; - - template < - typename ...T - > struct type_tuple< - std::tuple - > { - typedef std::tuple type; - }; - - - template < - typename K, - typename I = typename std::make_index_sequence< - std::tuple_size< - typename type::fields - >::value - > - > struct _type_tuple; - - template < - typename K, - size_t ...I - > struct _type_tuple < - K, - std::index_sequence - > { - typedef std::tuple< - typename std::tuple_element< - I, - typename type::fields - >::type::type... - > type; - }; - - - template < - typename K - > struct type_tuple { - typedef typename _type_tuple::type type; - }; - - - /////////////////////////////////////////////////////////////////////////// - namespace detail { - template < - typename K, - typename I = typename std::make_index_sequence< - std::tuple_size< - typename type::fields - >::value - > - > - struct _as_tuple; - - template < - typename K, - size_t ...I - > - struct _as_tuple < - K, - std::index_sequence - > - { - static typename type_tuple::type - make (const K &k) - { - return std::make_tuple ( - std::tuple_element::fields>::type::get (k)... - ); - } - - static auto make (K&&) = delete; - }; - } - - /// Convert an introspection capable class instance into a tuple instance - /// - /// K: source class - template - auto - as_tuple (const K &k) - { - return detail::_as_tuple::make (k); - } - - template - auto as_tuple (K &_k) - { - const K &k = _k; - return as_tuple (k); - } - - template - auto as_tuple (K&&) = delete; -} +} \ No newline at end of file diff --git a/introspection/name.cpp b/introspection/name.cpp new file mode 100644 index 00000000..9bc6de54 --- /dev/null +++ b/introspection/name.cpp @@ -0,0 +1,9 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019-2020 Danny Robson + */ + +#include "./name.hpp" diff --git a/introspection/name.hpp b/introspection/name.hpp new file mode 100644 index 00000000..2be0d128 --- /dev/null +++ b/introspection/name.hpp @@ -0,0 +1,124 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019-2020 Danny Robson + */ + +#pragma once + +#include "../algo/search.hpp" +#include "../std.hpp" + +#include +#include + + +/////////////////////////////////////////////////////////////////////////////// +namespace cruft::introspection::name { + template + constexpr + std::string_view full (void) + { + std::string_view pretty_function (__PRETTY_FUNCTION__); + +#ifdef __clang__ + // PRETTY_FUNCTION = std::string_view cruft::type_name() [T = std::__1::vector >] + + std::string_view const prefix = "[T = "; +#elif defined(__GNUC__) + // PRETTY_FUNCTION = "constexpr std::string_view cruft::type_name() [with T = std::__debug::vector; std::string_view = std::basic_string_view]" + + std::string_view prefix = "[with T = "; +#else +#error "Unsupported compiler" +#endif + + // Find the location where the type begins. + auto const type_begin = std::search ( + pretty_function.begin (), + pretty_function.end (), + prefix.begin (), + prefix.end () + ) + prefix.size (); + + // Find the point the type name ends. Both use ']', but gcc lists + // std::string_view in the function signature too and so requires the + // delimiting ';' as a suffix. + char const suffixes[] = ";]"; + auto const type_end = std::find_first_of ( + type_begin, + pretty_function.end (), + std::begin (suffixes), + std::end (suffixes) + ); + + // Find the start of the first template parameter so we can cut it out. + // If this isn't present we end up with a pointer to the end of the + // type string which is the end of the type anyway. + auto const template_start = std::find (type_begin, type_end, '<'); + auto const template_end = cruft::search::balanced (template_start, type_end, '<', '>'); + + return std::string_view ( + type_begin, + std::distance (type_begin, template_end) + ); + } + + + template + constexpr + std::string_view bare (void) + { + + auto const fullname = full (); + + constexpr char const namespace_symbol[] = "::"; + auto const last_namespace_pos = std::search ( + std::rbegin (fullname), + std::rend (fullname), + namespace_symbol + 0, + namespace_symbol + 2 + ); + + auto const length = std::distance (std::rbegin (fullname), last_namespace_pos); + auto const offset = fullname.size () - length; + return std::string_view (fullname.data () + offset, length); + } + + + template <> constexpr std::string_view bare (void) { return "i08"; } + template <> constexpr std::string_view bare (void) { return "i16"; } + template <> constexpr std::string_view bare (void) { return "i32"; } + template <> constexpr std::string_view bare (void) { return "i64"; } + + template <> constexpr std::string_view bare (void) { return "u08"; } + template <> constexpr std::string_view bare (void) { return "u16"; } + template <> constexpr std::string_view bare (void) { return "u32"; } + template <> constexpr std::string_view bare (void) { return "u64"; } + + template <> constexpr std::string_view bare (void) { return "f32"; } + template <> constexpr std::string_view bare (void) { return "f64"; } + + + /////////////////////////////////////////////////////////////////////////// + template + struct type_char; + + template <> struct type_char { static constexpr char value = 'f'; }; + template <> struct type_char { static constexpr char value = 'd'; }; + + template <> struct type_char< int8_t> { static constexpr char value = 'i'; }; + template <> struct type_char< int16_t> { static constexpr char value = 'i'; }; + template <> struct type_char< int32_t> { static constexpr char value = 'i'; }; + template <> struct type_char< int64_t> { static constexpr char value = 'i'; }; + + template <> struct type_char< uint8_t> { static constexpr char value = 'u'; }; + template <> struct type_char { static constexpr char value = 'u'; }; + template <> struct type_char { static constexpr char value = 'u'; }; + template <> struct type_char { static constexpr char value = 'u'; }; + + template + constexpr auto type_char_v = type_char::value; +} diff --git a/introspection/type.cpp b/introspection/type.cpp new file mode 100644 index 00000000..e69de29b diff --git a/introspection/type.hpp b/introspection/type.hpp new file mode 100644 index 00000000..83305cbd --- /dev/null +++ b/introspection/type.hpp @@ -0,0 +1,164 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2015-2017 Danny Robson + */ + +#pragma once + +#include +#include +#include + + +namespace cruft::introspection { + /////////////////////////////////////////////////////////////////////////// + /// Describes a single member variable in a type availabe for introspection + /// + /// K: target class + /// R: member type + /// M: pointer-to-member + template < + class K, + typename R, + R K::*M + > + struct field + { + typedef K klass; + typedef R type; + + static const std::string name; + + static const R& get (const K &k) { return k.*M; } + static R& get ( K &k) { return k.*M; } + static R& get ( K &&) = delete; + }; + + /////////////////////////////////////////////////////////////////////////// + /// Holds the fields of a type available for introspection + /// + /// Specialise the following type struct with a 'fields' tuple of the + /// members that should be accessed like below: + /// + /// struct foo { int a; int b; }; + /// + /// template <> struct type + /// { + /// typedef std::tuple< + /// field, + /// field + /// > fields; + /// }; + /// + /// template <> const std::string field::name = "a"; + /// template <> const std::string field::name = "b"; + + template + struct type { }; + + + /////////////////////////////////////////////////////////////////////////// + /// traits class which converts an introspected type to a tuple + /// + /// K: target class + + template + struct type_tuple; + + template < + typename ...T + > struct type_tuple< + std::tuple + > { + typedef std::tuple type; + }; + + + template < + typename K, + typename I = typename std::make_index_sequence< + std::tuple_size< + typename type::fields + >::value + > + > struct _type_tuple; + + template < + typename K, + size_t ...I + > struct _type_tuple < + K, + std::index_sequence + > { + typedef std::tuple< + typename std::tuple_element< + I, + typename type::fields + >::type::type... + > type; + }; + + + template < + typename K + > struct type_tuple { + typedef typename _type_tuple::type type; + }; + + + /////////////////////////////////////////////////////////////////////////// + namespace detail { + template < + typename K, + typename I = typename std::make_index_sequence< + std::tuple_size< + typename type::fields + >::value + > + > + struct _as_tuple; + + template < + typename K, + size_t ...I + > + struct _as_tuple < + K, + std::index_sequence + > + { + static typename type_tuple::type + make (const K &k) + { + return std::make_tuple ( + std::tuple_element::fields>::type::get (k)... + ); + } + + static auto make (K&&) = delete; + }; + } + + /// Convert an introspection capable class instance into a tuple instance + /// + /// K: source class + template + auto + as_tuple (const K &k) + { + return detail::_as_tuple::make (k); + } + + template + auto as_tuple (K &_k) + { + const K &k = _k; + return as_tuple (k); + } + + template + auto as_tuple (K&&) = delete; +} \ No newline at end of file diff --git a/parse/enum.hpp b/parse/enum.hpp index 63378000..1338f1c7 100644 --- a/parse/enum.hpp +++ b/parse/enum.hpp @@ -11,7 +11,7 @@ #include "fwd.hpp" #include "../view.hpp" -#include "../introspection.hpp" +#include "../introspection/name.hpp" #include "../log.hpp" #include @@ -181,7 +181,10 @@ namespace cruft::parse::enumeration { }); if (!success) - LOG_WARN ("duplicate parse setup for %! was ignored", cruft::type_name ()); + LOG_WARN ( + "duplicate parse setup for %! was ignored", + cruft::introspection::name::bare () + ); return cookie {}; } diff --git a/registrar.hpp b/registrar.hpp index a434bb91..b890e842 100644 --- a/registrar.hpp +++ b/registrar.hpp @@ -9,7 +9,7 @@ #pragma once #include "log/log.hpp" -#include "introspection.hpp" +#include "introspection/name.hpp" #include #include @@ -58,10 +58,18 @@ namespace cruft { ); if (success) { - LOG_INFO ("Registered %! for %!", key, cruft::type_name ()); + LOG_INFO ( + "Registered %! for %!", + key, + cruft::introspection::name::full () + ); return cookie { key }; } { - LOG_ERROR ("Unable to register %! for %!", key, cruft::type_name ()); + LOG_ERROR ( + "Unable to register %! for %!", + key, + cruft::introspection::name::full () + ); throw std::runtime_error ("Unable to register type"); } } diff --git a/test/hash/table.cpp b/test/hash/table.cpp index 115eb213..a0ceaf81 100644 --- a/test/hash/table.cpp +++ b/test/hash/table.cpp @@ -1,7 +1,7 @@ #include "hash/table.hpp" #include "tap.hpp" -#include "introspection.hpp" +#include "introspection/name.hpp" /////////////////////////////////////////////////////////////////////////////// @@ -36,7 +36,7 @@ void test_type (cruft::TAP::logger &tap) tap.expect ( pos == std::end (results), "no equal elements, %!", - cruft::type_name () + cruft::introspection::name::bare () ); } @@ -63,7 +63,7 @@ void test_type (cruft::TAP::logger &tap) tap.expect ( success, "equal counts at bit positions, %!", - cruft::type_name () + cruft::introspection::name::bare () ); } } diff --git a/test/introspection.cpp b/test/introspection.cpp index ef3920a6..216862f1 100644 --- a/test/introspection.cpp +++ b/test/introspection.cpp @@ -1,4 +1,5 @@ -#include "introspection.hpp" +#include "introspection/name.hpp" +#include "introspection/type.hpp" #include "std.hpp" #include "tap.hpp" @@ -42,7 +43,7 @@ struct templated_with_enum { }; /////////////////////////////////////////////////////////////////////////////// // define introspection data -namespace cruft +namespace cruft::introspection { template <> struct type @@ -79,7 +80,7 @@ int main () // Ensure tuples are mapped to themselves with type_tuple::type { using src_t = std::tuple; - using dst_t = typename cruft::type_tuple::type; + using dst_t = typename cruft::introspection::type_tuple::type; tap.expect (std::is_same::value, "static identity type_tuple"); } @@ -87,32 +88,32 @@ int main () // Check member extraction from a simple POD structure. { foo d_foo { 7, 42.0 }; - auto f_tuple = cruft::as_tuple (d_foo); + 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::type_name ()), "i08", "i08 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "i16", "i16 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "i32", "i32 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "i64", "i64 type_name"); + 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::type_name ()), "u08", "u08 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "u16", "u16 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "u32", "u32 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "u64", "u64 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::type_name ()), "f32", "f32 type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "f64", "f64 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::type_name ()), "foo", "struct type_name"); - tap.expect_eq (cruft::view (cruft::type_name ()), "qux", "de-namespaced struct 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::type_name_with_namespace (), + cruft::introspection::name::full (), std::string_view ("bar::qux") ), "namespaced struct" @@ -120,38 +121,38 @@ int main () tap.expect ( equal ( - cruft::type_name_with_namespace> (), + cruft::introspection::name::full> (), std::string_view ("bar::templated_qux") ), "namespaced templated struct" ); tap.expect_eq ( - cruft::type_name> (), + cruft::introspection::name::full> (), std::string_view {"templated_with_type"}, "templated_with_type" ); tap.expect_eq ( - cruft::type_name> (), + cruft::introspection::name::full> (), std::string_view {"templated_with_type"}, "templated_with_type" ); tap.expect_eq ( - cruft::type_name>> (), + cruft::introspection::name::full>> (), std::string_view {"templated_with_type >"}, "templated_with_type" ); tap.expect_eq ( - cruft::type_name> (), + cruft::introspection::name::full> (), std::string_view {"templated_with_value<-1>"}, "templated_with_value" ); tap.expect_eq ( - cruft::type_name> (), + cruft::introspection::name::full> (), std::string_view {"templated_with_enum"}, "templated_with_enum" ); diff --git a/test/rand/buckets.cpp b/test/rand/buckets.cpp index a136aa5e..5526148a 100644 --- a/test/rand/buckets.cpp +++ b/test/rand/buckets.cpp @@ -10,7 +10,7 @@ #include "tap.hpp" #include "maths.hpp" #include "types/string.hpp" -#include "introspection.hpp" +#include "introspection/name.hpp" /// ////////////////////////////////////////////////////////////////////////////// @@ -40,7 +40,7 @@ test_buckets (cruft::TAP::logger &tap, Args&& ...args) std::find_if (std::cbegin (buckets), std::cend (buckets), [] (auto v) { return v < EXPECTED * 7 / 8; }) == std::cend (buckets), - "bucket counts for %s", cruft::type_name () + "bucket counts for %s", cruft::introspection::name::full () ); }