1 /*
2 @file
3 Defines `boost::hana::experimental::type_name`.
4 
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
8  */
9 
10 #ifndef BOOST_HANA_EXPERIMENTAL_TYPE_NAME_HPP
11 #define BOOST_HANA_EXPERIMENTAL_TYPE_NAME_HPP
12 
13 #include <boost/hana/config.hpp>
14 #include <boost/hana/string.hpp>
15 
16 #include <cstddef>
17 #include <utility>
18 
19 
20 BOOST_HANA_NAMESPACE_BEGIN namespace experimental {
21     namespace detail {
22         struct cstring {
23             char const* ptr;
24             std::size_t length;
25         };
26 
27         // Note: We substract the null terminator from the string sizes below.
28         template <typename T>
type_name_impl2()29         constexpr cstring type_name_impl2() {
30 
31         #if defined(__clang__)
32             constexpr char const* pretty_function = __PRETTY_FUNCTION__;
33             constexpr std::size_t total_size = sizeof(__PRETTY_FUNCTION__) - 1;
34             constexpr std::size_t prefix_size = sizeof("boost::hana::experimental::detail::cstring boost::hana::experimental::detail::type_name_impl2() [T = ") - 1;
35             constexpr std::size_t suffix_size = sizeof("]") - 1;
36         #else
37             #error "No support for this compiler."
38         #endif
39 
40             return {pretty_function + prefix_size, total_size - prefix_size - suffix_size};
41         }
42 
43         template <typename T, std::size_t ...i>
type_name_impl1(std::index_sequence<i...>)44         auto type_name_impl1(std::index_sequence<i...>) {
45             constexpr auto name = detail::type_name_impl2<T>();
46             return hana::string<*(name.ptr + i)...>{};
47         }
48     } // end namespace detail
49 
50     //! @ingroup group-experimental
51     //! Returns a `hana::string` representing the name of the given type, at
52     //! compile-time.
53     //!
54     //! This only works on Clang (and apparently MSVC, but Hana does not work
55     //! there as of writing this). Original idea taken from
56     //! https://github.com/Manu343726/ctti.
57     template <typename T>
type_name()58     auto type_name() {
59         constexpr auto name = detail::type_name_impl2<T>();
60         return detail::type_name_impl1<T>(std::make_index_sequence<name.length>{});
61     }
62 } BOOST_HANA_NAMESPACE_END
63 
64 #endif // !BOOST_HANA_EXPERIMENTAL_TYPE_NAME_HPP
65