Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:57

0001 /*
0002 @file
0003 Defines `boost::hana::experimental::type_name`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_EXPERIMENTAL_TYPE_NAME_HPP
0011 #define BOOST_HANA_EXPERIMENTAL_TYPE_NAME_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/string.hpp>
0015 
0016 #include <cstddef>
0017 #include <utility>
0018 
0019 
0020 namespace boost { namespace hana { namespace experimental {
0021     namespace detail {
0022         struct cstring {
0023             char const* ptr;
0024             std::size_t length;
0025         };
0026 
0027         // Note: We substract the null terminator from the string sizes below.
0028         template <typename T>
0029         constexpr auto type_name_impl2() {
0030         #if defined(BOOST_HANA_CONFIG_CLANG)
0031             constexpr char const* pretty_function = __PRETTY_FUNCTION__;
0032             constexpr std::size_t total_size = sizeof(__PRETTY_FUNCTION__) - 1;
0033             constexpr std::size_t prefix_size = sizeof("auto boost::hana::experimental::detail::type_name_impl2() [T = ") - 1;
0034             constexpr std::size_t suffix_size = sizeof("]") - 1;
0035         #elif defined(BOOST_HANA_CONFIG_GCC)
0036             constexpr char const* pretty_function = __PRETTY_FUNCTION__;
0037             constexpr std::size_t total_size = sizeof(__PRETTY_FUNCTION__) - 1;
0038             constexpr std::size_t prefix_size = sizeof("constexpr auto boost::hana::experimental::detail::type_name_impl2() [with T = ") - 1;
0039             constexpr std::size_t suffix_size = sizeof("]") - 1;
0040         #else
0041             #error "No support for this compiler."
0042         #endif
0043 
0044             cstring s{pretty_function + prefix_size, total_size - prefix_size - suffix_size};
0045             return s;
0046         }
0047 
0048         template <typename T, std::size_t ...i>
0049         auto type_name_impl1(std::index_sequence<i...>) {
0050             constexpr auto name = detail::type_name_impl2<T>();
0051             return hana::string<*(name.ptr + i)...>{};
0052         }
0053     } // end namespace detail
0054 
0055     //! @ingroup group-experimental
0056     //! Returns a `hana::string` representing the name of the given type, at
0057     //! compile-time.
0058     //!
0059     //! This only works on Clang (and apparently MSVC, but Hana does not work
0060     //! there as of writing this). Original idea taken from
0061     //! https://github.com/Manu343726/ctti.
0062     template <typename T>
0063     auto type_name() {
0064         constexpr auto name = detail::type_name_impl2<T>();
0065         return detail::type_name_impl1<T>(std::make_index_sequence<name.length>{});
0066     }
0067 } }} // end namespace boost::hana
0068 
0069 #endif // !BOOST_HANA_EXPERIMENTAL_TYPE_NAME_HPP