-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7dea57
commit 5dc9f5a
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#pragma once | ||
|
||
#include "fixed_containers/concepts.hpp" | ||
#include "fixed_containers/fixed_string.hpp" | ||
#include "fixed_containers/fixed_vector.hpp" | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <string_view> | ||
#include <tuple> | ||
|
||
// https://clang.llvm.org/docs/LanguageExtensions.html#builtin-dump-struct | ||
static_assert(__has_builtin(__builtin_dump_struct), | ||
"Static reflection requires __builtin_dump_struct() to be available"); | ||
|
||
namespace fixed_containers::reflection_detail | ||
{ | ||
template <typename FieldTypeNameStringType = fixed_string_detail::FixedString<128>, | ||
typename FieldNameStringType = fixed_string_detail::FixedString<64>> | ||
class FieldInfo | ||
{ | ||
private: | ||
FieldTypeNameStringType field_type_name_{}; | ||
FieldNameStringType field_name_{}; | ||
|
||
public: | ||
constexpr FieldInfo() noexcept | ||
: field_type_name_{} | ||
, field_name_{} | ||
{ | ||
} | ||
|
||
constexpr FieldInfo(const std::string_view& field_type_name, | ||
const std::string_view& field_name) noexcept | ||
: field_type_name_{field_type_name} | ||
, field_name_{field_name} | ||
{ | ||
} | ||
|
||
public: | ||
[[nodiscard]] constexpr std::string_view field_type_name() const { return field_type_name_; } | ||
[[nodiscard]] constexpr std::string_view field_name() const { return field_name_; } | ||
}; | ||
|
||
template <ConstexprDefaultConstructible T> | ||
constexpr std::size_t field_count_of(const T& instance = {}) | ||
{ | ||
auto converter = | ||
[]<typename... Args>(std::size_t& out, const char* const /*fmt*/, Args&&... args) | ||
{ | ||
if constexpr (sizeof...(args) >= 3) | ||
{ | ||
++out; | ||
} | ||
}; | ||
|
||
std::size_t counter = 0; | ||
__builtin_dump_struct(&instance, converter, counter); | ||
return counter; | ||
} | ||
|
||
template <ConstexprDefaultConstructible T> | ||
constexpr auto field_info_of(const T& instance = {}) -> std::array<FieldInfo<>, field_count_of<T>()> | ||
{ | ||
constexpr std::size_t FIELD_COUNT = field_count_of<T>(); | ||
|
||
auto converter = []<typename... Args>(FixedVector<FieldInfo<>, FIELD_COUNT>& out, | ||
const char* const /*fmt*/, | ||
Args&&... args) | ||
{ | ||
if constexpr (sizeof...(args) >= 3) | ||
{ | ||
auto as_tuple = std::tuple{std::forward<Args>(args)...}; | ||
out.emplace_back(/*field_type_name*/ std::get<1>(as_tuple), | ||
/*field_name*/ std::get<2>(as_tuple)); | ||
} | ||
}; | ||
|
||
FixedVector<FieldInfo<>, FIELD_COUNT> out_vec{}; | ||
__builtin_dump_struct(&instance, converter, out_vec); | ||
|
||
assert(out_vec.size() == FIELD_COUNT); | ||
|
||
std::array<FieldInfo<>, FIELD_COUNT> out{}; | ||
for (std::size_t i = 0; i < FIELD_COUNT; i++) | ||
{ | ||
out.at(i) = out_vec.at(i); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
} // namespace fixed_containers::reflection_detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#if __has_builtin(__builtin_dump_struct) | ||
|
||
#include "fixed_containers/reflection.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace fixed_containers | ||
{ | ||
namespace | ||
{ | ||
struct BaseStruct | ||
{ | ||
}; | ||
|
||
struct ChildStruct : public BaseStruct | ||
{ | ||
}; | ||
|
||
struct MyColors | ||
{ | ||
int yellow; | ||
double red; | ||
BaseStruct green; | ||
ChildStruct purple; | ||
}; | ||
} // namespace | ||
|
||
TEST(Reflection, Example) | ||
{ | ||
static_assert(consteval_compare::equal<4, reflection_detail::field_count_of<MyColors>()>); | ||
|
||
constexpr std::array FIELD_INFO = reflection_detail::field_info_of<MyColors>(); | ||
|
||
static_assert(FIELD_INFO.at(0).field_type_name() == "int"); | ||
static_assert(FIELD_INFO.at(0).field_name() == "yellow"); | ||
|
||
static_assert(FIELD_INFO.at(1).field_type_name() == "double"); | ||
static_assert(FIELD_INFO.at(1).field_name() == "red"); | ||
|
||
static_assert(FIELD_INFO.at(2).field_type_name() == "BaseStruct"); | ||
static_assert(FIELD_INFO.at(2).field_name() == "green"); | ||
|
||
static_assert(FIELD_INFO.at(3).field_type_name() == "ChildStruct"); | ||
static_assert(FIELD_INFO.at(3).field_name() == "purple"); | ||
} | ||
|
||
} // namespace fixed_containers | ||
|
||
#endif |