CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
TypeName.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
13#if defined(__clang__) || defined(__GNUC__)
14#define CE_META_FUNCTION_NAME __PRETTY_FUNCTION__
15#elif defined(_MSC_VER)
16#define CE_META_FUNCTION_NAME __FUNCSIG__
17#else
18 # error Unsupported compiler
19#endif
20
21namespace CeresEngine {
22
23 namespace internal {
24
25 template <std::size_t... Idxs>
26 consteval auto makeTypeNameArray(StringView str, std::index_sequence<Idxs...>) {
27 return std::array{str[Idxs]..., (char)0x00};
28 }
29
30 template <typename T>
31 consteval auto getTypeNameArrayPrefix() {
32 constexpr StringView function = CE_META_FUNCTION_NAME;
33 return function.find("double") - 6;
34 }
35
36 template <typename T>
37 consteval auto getTypeNameArraySuffix() {
38 constexpr StringView function = CE_META_FUNCTION_NAME;
39 return function.size() - function.rfind("double") - 6;
40 }
41
42 template <typename T>
43 consteval auto getTypeNameArray() {
44 constexpr StringView function = CE_META_FUNCTION_NAME;
45
46 constexpr auto start = getTypeNameArrayPrefix<double>();
47 constexpr auto end = function.size() - getTypeNameArraySuffix<double>();
48
49 static_assert(start < end);
50
51 constexpr auto name = function.substr(start, (end - start));
52 if constexpr(name.find("class ") == 0) {
53 return makeTypeNameArray(name.substr(6), std::make_index_sequence<name.size() - 6>{});
54 } else if constexpr(name.find("struct ") == 0) {
55 return makeTypeNameArray(name.substr(7), std::make_index_sequence<name.size() - 7>{});
56 } else {
57 return makeTypeNameArray(name, std::make_index_sequence<name.size()>{});
58 }
59 }
60
61 template<typename T> static inline constexpr auto TypeNameHolder = getTypeNameArray<T>();
62
63 template<class T> constexpr StringView getTypeName() {
64 constexpr auto& value = TypeNameHolder<T>;
65 return StringView{value.data(), value.size() - 1};
66 }
67 } // namespace internal
68
69 // More precise version!
70 // C++11 states that initialization of local statics is thread-safe
71 // and constructor is called only once. Using __PRETTY_FUNCTION__ hack.
72 template<typename T> [[nodiscard]] constexpr StringView getTypeName() { return internal::getTypeName<T>(); }
73
74} // namespace CeresEngine
consteval auto getTypeNameArrayPrefix()
Definition TypeName.hpp:31
consteval auto makeTypeNameArray(StringView str, std::index_sequence< Idxs... >)
Definition TypeName.hpp:26
constexpr StringView getTypeName()
Definition TypeName.hpp:63
consteval auto getTypeNameArray()
Definition TypeName.hpp:43
consteval auto getTypeNameArraySuffix()
Definition TypeName.hpp:37
Definition Application.hpp:19
constexpr StringView getTypeName()
Definition TypeName.hpp:72
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25