MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TypeInfo.hpp
Go to the documentation of this file.
1#pragma once
2
3#if (defined(MAYAFLUX_COMPILER_CLANG) || defined(MAYAFLUX_COMPILER_GCC)) && !defined(MAYAFLUX_PLATFORM_WINDOWS)
4#include <cxxabi.h>
5#endif
6
7namespace MayaFlux::Reflect {
8
9namespace detail {
10
11 [[nodiscard]] inline std::string_view strip_namespaces(std::string_view name) noexcept
12 {
13 auto pos = name.rfind(':');
14 return (pos != std::string_view::npos) ? name.substr(pos + 1) : name;
15 }
16
17} // namespace detail
18
19/**
20 * @brief Returns the fully qualified compile-time type name of @p T.
21 *
22 * Zero allocation. Returns a string_view into static storage valid for the
23 * process lifetime. Replaces the local live_type_name<T>() in LiveArena.hpp.
24 *
25 * @tparam T Any type.
26 */
27template <typename T>
28[[nodiscard]] constexpr std::string_view type_name() noexcept
29{
30#if defined(MAYAFLUX_COMPILER_CLANG)
31 constexpr std::string_view fn = __PRETTY_FUNCTION__;
32 constexpr auto s = fn.find("T = ") + 4;
33 constexpr auto e = fn.rfind(']');
34 return fn.substr(s, e - s);
35#elif defined(MAYAFLUX_COMPILER_GCC)
36 constexpr std::string_view fn = __PRETTY_FUNCTION__;
37 constexpr auto s = fn.find("T = ") + 4;
38 constexpr auto e = fn.find(';', s);
39 return (e != std::string_view::npos) ? fn.substr(s, e - s) : fn.substr(s);
40#elif defined(MAYAFLUX_COMPILER_MSVC)
41 constexpr std::string_view fn = __FUNCSIG__;
42 constexpr auto s = fn.find("type_name<") + 10;
43 constexpr auto e = fn.rfind(">(");
44 return (e != std::string_view::npos && e > s) ? fn.substr(s, e - s) : fn.substr(s);
45#else
46 return "unknown";
47#endif
48}
49
50/**
51 * @brief Returns the unqualified compile-time type name of @p T.
52 *
53 * Strips all namespace prefixes. Equivalent to the old live_type_name<T>()
54 * in LiveArena.hpp, which can now delegate here.
55 *
56 * @tparam T Any type.
57 * @return e.g. "Phasor" for "MayaFlux::Nodes::Phasor".
58 */
59template <typename T>
60[[nodiscard]] constexpr std::string_view short_type_name() noexcept
61{
62 return detail::strip_namespaces(type_name<T>());
63}
64
65/**
66 * @brief Returns the demangled fully qualified dynamic type name of @p obj.
67 *
68 * Allocates once per call via abi::__cxa_demangle on GCC/Clang. On MSVC
69 * typeid().name() is already human-readable. Intended for display and
70 * introspection paths, not hot loops.
71 *
72 * @param obj Any polymorphic object.
73 */
74template <typename T>
75[[nodiscard]] inline std::string dynamic_type_name(const T& obj) noexcept
76{
77 const char* mangled = typeid(obj).name();
78#if (defined(MAYAFLUX_COMPILER_CLANG) || defined(MAYAFLUX_COMPILER_GCC)) && !defined(MAYAFLUX_PLATFORM_WINDOWS)
79 int status {};
80 char* buf = abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
81 std::string result = (status == 0 && buf) ? buf : mangled;
82 std::free(buf);
83 return result;
84#else
85 return mangled;
86#endif
87}
88
89/**
90 * @brief Returns the unqualified dynamic type name of @p obj.
91 * @param obj Any polymorphic object.
92 * @return e.g. "Phasor".
93 */
94template <typename T>
95[[nodiscard]] inline std::string short_dynamic_type_name(const T& obj) noexcept
96{
97 auto full = dynamic_type_name(obj);
98 return std::string(detail::strip_namespaces(full));
99}
100
101/**
102 * @brief Overload for shared_ptr: dereferences before querying dynamic type.
103 * @param ptr shared_ptr to a polymorphic object.
104 * @return Unqualified dynamic type name, or "null" if ptr is empty.
105 */
106template <typename T>
107[[nodiscard]] inline std::string short_dynamic_type_name(const std::shared_ptr<T>& ptr) noexcept
108{
109 if (!ptr)
110 return "null";
112}
113
114} // namespace MayaFlux::Reflect
const uint8_t * ptr
std::string_view strip_namespaces(std::string_view name) noexcept
Definition TypeInfo.hpp:11
constexpr std::string_view type_name() noexcept
Returns the fully qualified compile-time type name of T.
Definition TypeInfo.hpp:28
constexpr std::string_view short_type_name() noexcept
Returns the unqualified compile-time type name of T.
Definition TypeInfo.hpp:60
std::string dynamic_type_name(const T &obj) noexcept
Returns the demangled fully qualified dynamic type name of obj.
Definition TypeInfo.hpp:75
std::string short_dynamic_type_name(const T &obj) noexcept
Returns the unqualified dynamic type name of obj.
Definition TypeInfo.hpp:95