Refureku v2.2.0
C++17 runtime reflection library.
CodeGenerationHelpers.h
1
8#pragma once
9
10#include <array>
11#include <cstddef> //std::size_t, std::ptrdiff_t
12
13#include "Refureku/Config.h"
14#include "Refureku/Misc/TypeTraitsMacros.h"
15#include "Refureku/TypeInfo/Archetypes/GetArchetype.h"
16#include "Refureku/TypeInfo/Archetypes/Struct.h"
17#include "Refureku/Misc/SharedPtr.h"
18
19#ifndef _RFK_UNPACK_IF_NOT_PARSING
20
21//This macro "KODGEN_PARSING" must match the name defined by the kodgen parser when parsing source files
22//It is used by the generated code to hide some portions of code to the parser
23#ifdef KODGEN_PARSING
24#define RFK_UNPACK_IF_NOT_PARSING(...)
25#else
26#define RFK_UNPACK_IF_NOT_PARSING(...) __VA_ARGS__
27#endif
28
29#endif
30
31namespace rfk::internal
32{
33 RFK_GENERATE_IMPLEMENTS_TEMPLATE1_METHOD_TRAITS(_rfk_registerChildClass);
34
35 class CodeGenerationHelpers
36 {
37 public:
38 CodeGenerationHelpers() = delete;
39 ~CodeGenerationHelpers() = delete;
40
47 template <typename ParentClass, typename ChildClass>
48 static constexpr void registerChildClass(rfk::Struct& childClass) noexcept;
49
56 template <typename Derived, typename Base>
57 RFK_NODISCARD static constexpr std::ptrdiff_t computeClassPointerOffset() noexcept;
58
66 template <typename ClassType>
67 RFK_NODISCARD static std::size_t getReflectedFieldsCount() noexcept;
68
76 template <typename ClassType>
77 RFK_NODISCARD static std::size_t getReflectedStaticFieldsCount() noexcept;
78
87#if defined(__GNUC__) && !defined (__clang__) && __GNUC__ <= 9
88 //Handle pre GCC 9 internal compiler error when using type traits in noexcept
89 template <typename T>
90 RFK_NODISCARD static rfk::SharedPtr<T> defaultSharedInstantiator();
91#else
92 template <typename T>
93 RFK_NODISCARD static rfk::SharedPtr<T> defaultSharedInstantiator() noexcept(!std::is_default_constructible_v<T> || std::is_nothrow_constructible_v<T>);
94#endif
95
104#if defined(__GNUC__) && !defined (__clang__) && __GNUC__ <= 9
105 //Handle pre GCC 9 internal compiler error when using type traits in noexcept
106 template <typename T>
107 RFK_NODISCARD static rfk::UniquePtr<T> defaultUniqueInstantiator();
108#else
109 template <typename T>
110 RFK_NODISCARD static rfk::UniquePtr<T> defaultUniqueInstantiator() noexcept(!std::is_default_constructible_v<T> || std::is_nothrow_constructible_v<T>);
111#endif
112 };
113
114 template <auto>
115 struct ForceGenerateSymbol
116 {
117 };
118
119 struct RawTypenameFormat
120 {
122 std::size_t leadingCharsCount = 0u;
123
125 std::size_t trailingCharsCount = 0u;
126 };
127
135 template <typename T>
136 static constexpr auto const& getRawTypename() noexcept
137 {
138__RFK_DISABLE_WARNING_PUSH
139__RFK_DISABLE_WARNING_LANGUAGE_EXTENSION_TOKEN
140
141#ifdef _MSC_VER
142 return __FUNCSIG__;
143#else
144 return __PRETTY_FUNCTION__;
145#endif
146
147__RFK_DISABLE_WARNING_POP
148 }
149
157 static constexpr bool getCompilerRawTypenameFormat(RawTypenameFormat* out_format) noexcept
158 {
159 constexpr auto const& rawTypename = getRawTypename<char>();
160
161 for (std::size_t i = 0u;; i++)
162 {
163 //Detect the "int" chars in the raw type name
164 if (rawTypename[i] == 'c' && rawTypename[i+1] == 'h' && rawTypename[i+2] == 'a' && rawTypename[i+3] == 'r')
165 {
166 if (out_format != nullptr)
167 {
168 out_format->leadingCharsCount = i;
169 out_format->trailingCharsCount = sizeof(rawTypename) - i - 4 - 1; // 4 to consume the "char" part, 1 for the string null terminator.
170 }
171
172 return true;
173 }
174 }
175
176 return false;
177 }
178
180 inline static constexpr RawTypenameFormat typenameFormat = []
181 {
182 //The below line is not supported in g++8, so drop it
183 //static_assert(getCompilerRawTypenameFormat(nullptr), "Unable to figure out how to generate type names on this compiler.");
184
185 RawTypenameFormat format;
186 getCompilerRawTypenameFormat(&format);
187
188 return format;
189 }();
190
198 template <typename T>
199 constexpr auto getTypenameAsArray() noexcept
200 {
201 constexpr std::size_t typenameLength = sizeof(getRawTypename<T>()) - typenameFormat.leadingCharsCount - typenameFormat.trailingCharsCount;
202 std::array<char, typenameLength> typename_{};
203
204 for (std::size_t i = 0; i < typenameLength - 1; i++)
205 {
206 typename_[i] = getRawTypename<T>()[i + typenameFormat.leadingCharsCount];
207 }
208
209 return typename_;
210 }
211
219 template <typename T>
220 char const* getTypename() noexcept
221 {
222 static constexpr auto name = getTypenameAsArray<T>();
223
224 return name.data();
225 }
226
227 #include "Refureku/Misc/CodeGenerationHelpers.inl"
228}
Definition: Struct.h:40