Skip to content

Commit

Permalink
Added: basic reflection, MSVC isn't perfect yet
Browse files Browse the repository at this point in the history
  • Loading branch information
WillisMedwell committed Jan 31, 2024
1 parent 1eb3e9f commit e0f1a67
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
47 changes: 39 additions & 8 deletions include/Utily/Reflection.hpp
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;
}
};
}
8 changes: 7 additions & 1 deletion include/Utily/Simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#if defined(__SSE__) && defined(__SSE2__) && defined(__SSE3__)
#define UTY_SUPPORTS_128 1
#include <emmintrin.h>
#include <xmmintrin.h>
#include <pmmintrin.h>
#include <xmmintrin.h>
#else
#define UTY_SUPPORTS_128 0
#endif
Expand Down Expand Up @@ -275,10 +275,16 @@ namespace Utily::Simd {
template <std::contiguous_iterator Iter, typename Value>
UTY_ALWAYS_INLINE auto find(Iter begin, Iter end, Value value) noexcept -> Iter {
if constexpr (sizeof(std::iter_value_t<Iter>) == 1 && sizeof(Value) == 1) {
if (begin == end) {
return begin;
}
auto data = reinterpret_cast<const char*>(&(*begin));
auto& val = *reinterpret_cast<const char*>(&value);
return begin + Utily::Simd::Details::find(data, static_cast<size_t>(std::distance(begin, end)), val);
} else if constexpr (sizeof(std::iter_value_t<Iter>) == 4 && sizeof(Value) == 4) {
if (begin == end) {
return begin;
}
auto data = reinterpret_cast<const int32_t*>(&(*begin));
auto& val = *reinterpret_cast<const int32_t*>(&value);
return begin + Utily::Simd::Details::find(data, static_cast<size_t>(std::distance(begin, end)), val);
Expand Down
1 change: 1 addition & 0 deletions include/Utily/Utily.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "Utily/Reflection.hpp"
#include "Utily/Concepts.hpp"
#include "Utily/Simd.hpp"
#include "Utily/TupleAlgo.hpp"
Expand Down
23 changes: 23 additions & 0 deletions test/UnitReflection.cpp
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");
}
}

0 comments on commit e0f1a67

Please sign in to comment.