1 /* Copyright 2017-2021 PaGMO development team
2 
3 This file is part of the PaGMO library.
4 
5 The PaGMO library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8   * the GNU Lesser General Public License as published by the Free
9     Software Foundation; either version 3 of the License, or (at your
10     option) any later version.
11 
12 or
13 
14   * the GNU General Public License as published by the Free Software
15     Foundation; either version 3 of the License, or (at your option) any
16     later version.
17 
18 or both in parallel, as here.
19 
20 The PaGMO library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the PaGMO library.  If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PAGMO_DETAIL_TYPE_NAME_HPP
30 #define PAGMO_DETAIL_TYPE_NAME_HPP
31 
32 #include <string>
33 #include <type_traits>
34 #include <typeinfo>
35 
36 #include <pagmo/detail/visibility.hpp>
37 
38 namespace pagmo
39 {
40 
41 namespace detail
42 {
43 
44 PAGMO_DLL_PUBLIC std::string demangle_from_typeid(const char *);
45 
46 // Determine the name of the type T at runtime.
47 template <typename T>
type_name()48 inline std::string type_name()
49 {
50     // Get the demangled name without cvref.
51     auto ret
52         = demangle_from_typeid(typeid(typename std::remove_cv<typename std::remove_reference<T>::type>::type).name());
53 
54     // Redecorate it with cv qualifiers.
55     constexpr unsigned flag = unsigned(std::is_const<typename std::remove_reference<T>::type>::value)
56                               + (unsigned(std::is_volatile<typename std::remove_reference<T>::type>::value) << 1);
57     switch (flag) {
58         case 0u:
59             // NOTE: handle this explicitly to keep compiler warnings at bay.
60             break;
61         case 1u:
62             ret += " const";
63             break;
64         case 2u:
65             ret += " volatile";
66             break;
67         case 3u:
68             ret += " const volatile";
69     }
70 
71     // Re-add the reference, if necessary.
72     if (std::is_lvalue_reference<T>::value) {
73         ret += " &";
74     } else if (std::is_rvalue_reference<T>::value) {
75         ret += " &&";
76     }
77 
78     return ret;
79 }
80 
81 } // namespace detail
82 
83 } // namespace pagmo
84 
85 #endif
86