-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: basic reflection, MSVC isn't perfect yet
- Loading branch information
1 parent
1eb3e9f
commit e0f1a67
Showing
4 changed files
with
70 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
#pragma once | ||
|
||
#include <string_view> | ||
#include <algorithm> | ||
#include <cassert> | ||
#include <cstdint> | ||
#include <iostream> | ||
#include <optional> | ||
#include <source_location> | ||
#include <string_view> | ||
#include <type_traits> | ||
|
||
namespace Utily { | ||
class Reflection | ||
{ | ||
public: | ||
template <typename T> | ||
static auto type_name() -> std::string_view { | ||
|
||
struct Reflection { | ||
template <typename T> | ||
consteval static auto get_type_name() -> std::string_view { | ||
std::string_view name = std::source_location::current().function_name(); | ||
std::string_view t_equals = "T = "; | ||
auto begin = std::search(name.begin(), name.end(), t_equals.begin(), | ||
t_equals.end()); | ||
if (begin == name.end()) { | ||
// This is MSVC | ||
constexpr std::string_view function_sig = "Reflection::get_type_name<"; | ||
|
||
begin = std::search(name.begin(), name.end(), function_sig.begin(), function_sig.end()); | ||
if(begin == name.end()) { | ||
return "error"; | ||
} | ||
begin += function_sig.size(); | ||
std::string_view function_end = ">(void)"; | ||
|
||
auto end = std::search(begin, name.end(), function_end.begin(), | ||
function_end.end()); | ||
|
||
name = std::string_view{begin, end}; | ||
} else { | ||
// This is GCC and Clang | ||
begin += t_equals.size(); | ||
std::string_view end_delims = " ;]"; | ||
auto end = std::find_first_of(begin, name.end(), end_delims.begin(), | ||
end_delims.end()); | ||
name = std::string_view{begin, end}; | ||
} | ||
}; | ||
return name; | ||
} | ||
}; | ||
} |
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,23 @@ | ||
#include "Utily/Utily.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(Reflection, BaseTypes) { | ||
{ | ||
auto result = Utily::Reflection::get_type_name<float>(); | ||
auto expected = std::string_view { "float" }; | ||
EXPECT_EQ(result, expected); | ||
} | ||
|
||
{ | ||
auto result = Utily::Reflection::get_type_name<double>(); | ||
auto expected = std::string_view { "double" }; | ||
EXPECT_EQ(result, expected); | ||
} | ||
|
||
{ | ||
auto result = Utily::Reflection::get_type_name<Utily::Result<void, Utily::Error>>(); | ||
auto expected = std::string_view { "Utily::Result<void, Utily::Error>" }; | ||
EXPECT_NE(result, "error"); | ||
} | ||
} |