From 696289dbf2c15fd9f3e4a66e80ba622b412d0a3d Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 7 Jan 2024 23:05:39 +0100 Subject: [PATCH] Made sure that we can get field names from structs declared inside functions or unnamed namespaces; #46 --- include/rfl/internal/bind_to_tuple.hpp | 24 +++++----- include/rfl/internal/fake_object.hpp | 14 ------ include/rfl/internal/get_fake_object.hpp | 46 ++++++++++++++++++ include/rfl/internal/get_field_names.hpp | 46 +++++++++++------- include/rfl/internal/to_ptr_tuple.hpp | 4 +- tests/json/test_inside_function.cpp | 55 ++++++++++++++++++++++ tests/json/test_inside_function.hpp | 4 ++ tests/json/test_unnamed_namespace.cpp | 60 ++++++++++++++++++++++++ tests/json/test_unnamed_namespace.hpp | 4 ++ tests/json/tests.cpp | 4 ++ 10 files changed, 215 insertions(+), 46 deletions(-) delete mode 100644 include/rfl/internal/fake_object.hpp create mode 100644 include/rfl/internal/get_fake_object.hpp create mode 100644 tests/json/test_inside_function.cpp create mode 100644 tests/json/test_inside_function.hpp create mode 100644 tests/json/test_unnamed_namespace.cpp create mode 100644 tests/json/test_unnamed_namespace.hpp diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index 7bb5970e..c2ec8fc5 100644 --- a/include/rfl/internal/bind_to_tuple.hpp +++ b/include/rfl/internal/bind_to_tuple.hpp @@ -1,5 +1,5 @@ -#ifndef RFL_INTERNAL_BIND_TO_TUPLE_HELPER_HPP_ -#define RFL_INTERNAL_BIND_TO_TUPLE_HELPER_HPP_ +#ifndef RFL_INTERNAL_BIND_TO_TUPLE_HPP_ +#define RFL_INTERNAL_BIND_TO_TUPLE_HPP_ #include #include @@ -13,17 +13,6 @@ namespace rfl { namespace internal { -template -constexpr auto tuple_view(T&); - -template -constexpr auto bind_to_tuple(T& _t, const F& _f) { - auto view = tuple_view(_t); - return [&](std::index_sequence) { - return std::make_tuple(_f(std::get(view))...); - }(std::make_index_sequence>()); -} - template struct tuple_view_helper { template @@ -598,6 +587,15 @@ constexpr auto tuple_view(T& t) { return tuple_view_helper>::tuple_view(t); } +template +constexpr auto bind_to_tuple(T& _t, const F& _f) { + auto view = tuple_view(_t); + return [&](std::index_sequence) { + return std::make_tuple(_f(std::get(view))...); + } + (std::make_index_sequence>()); +} + } // namespace internal } // namespace rfl diff --git a/include/rfl/internal/fake_object.hpp b/include/rfl/internal/fake_object.hpp deleted file mode 100644 index ec1026ba..00000000 --- a/include/rfl/internal/fake_object.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef RFL_INTERNAL_FAKEOBJECT_HPP_ -#define RFL_INTERNAL_FAKEOBJECT_HPP_ - -namespace rfl { -namespace internal { - -/// This is required to automatically retrieve the names. -template -extern T fake_object; - -} // namespace internal -} // namespace rfl - -#endif diff --git a/include/rfl/internal/get_fake_object.hpp b/include/rfl/internal/get_fake_object.hpp new file mode 100644 index 00000000..27f7fc5b --- /dev/null +++ b/include/rfl/internal/get_fake_object.hpp @@ -0,0 +1,46 @@ +#ifndef RFL_INTERNAL_GETFAKEOBJECT_HPP_ +#define RFL_INTERNAL_GETFAKEOBJECT_HPP_ + +namespace rfl { +namespace internal { + +#if __GNUC__ +#ifndef __clang__ +#pragma GCC system_header +#endif +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wundefined-var-template" +#pragma clang diagnostic ignored "-Wundefined-internal" +#endif + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 7631) +#endif + +template +struct wrapper { + const T value; + static const wrapper report_if_you_see_a_link_error_with_this_object; +}; + +template +consteval const T& get_fake_object() noexcept { + return wrapper::report_if_you_see_a_link_error_with_this_object.value; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 96fa05f8..85aef956 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -11,11 +11,11 @@ #include #include "../Literal.hpp" -#include "fake_object.hpp" +#include "bind_fake_object_to_tuple.hpp" +#include "get_fake_object.hpp" #include "is_flatten_field.hpp" #include "is_rename.hpp" #include "num_fields.hpp" -#include "to_ptr_tuple.hpp" #if __GNUC__ #ifndef __clang__ @@ -41,19 +41,19 @@ constexpr auto wrap(const T& arg) noexcept { return Wrapper{arg}; } -template +template consteval auto get_field_name_str_view() { const auto func_name = std::string_view{std::source_location::current().function_name()}; #if defined(__clang__) - const auto split = func_name.substr(0, func_name.find("}]")); + const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(".") + 1); #elif defined(__GNUC__) - const auto split = func_name.substr(0, func_name.find(")}")); + const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(":") + 1); #elif defined(_MSC_VER) - const auto split = func_name.substr(0, func_name.find_last_of("}")); - return split.substr(split.find_last_of(">") + 1); + const auto split = func_name.substr(0, func_name.size() - 7); + return split.substr(split.find("value->") + 7); #else static_assert(false, "You are using an unsupported compiler. Please use GCC, Clang " @@ -61,9 +61,9 @@ consteval auto get_field_name_str_view() { #endif } -template +template consteval auto get_field_name_str_lit() { - constexpr auto name = get_field_name_str_view(); + constexpr auto name = get_field_name_str_view(); const auto to_str_lit = [&](std::index_sequence) { return StringLiteral{name[Ns]...}; }; @@ -73,17 +73,21 @@ consteval auto get_field_name_str_lit() { template auto get_field_names(); -template +template auto get_field_name() { +#if defined(__clang__) using Type = std::remove_cvref_t::Type>>; +#else + using Type = std::remove_cvref_t>; +#endif if constexpr (is_rename_v) { using Name = typename Type::Name; return Name(); } else if constexpr (is_flatten_field_v) { return get_field_names>(); } else { - return rfl::Literal()>(); + return rfl::Literal()>(); } } @@ -114,16 +118,24 @@ template #endif #endif auto get_field_names() { - if constexpr (std::is_pointer_v>) { + using Type = std::remove_cvref_t; + if constexpr (std::is_pointer_v) { return get_field_names>(); } else { - constexpr auto ptr_tuple = to_ptr_tuple(fake_object); - const auto get = [&](std::index_sequence) { +#if defined(__clang__) + const auto get = [](std::index_sequence) { return concat_literals( - get_field_name(ptr_tuple))>()...); + get_field_name( + bind_fake_object_to_tuple()))>()...); }; - return get( - std::make_index_sequence>()); +#else + const auto get = [](std::index_sequence) { + return concat_literals( + get_field_name(bind_fake_object_to_tuple())>()...); + }; +#endif + return get(std::make_index_sequence>()); } } diff --git a/include/rfl/internal/to_ptr_tuple.hpp b/include/rfl/internal/to_ptr_tuple.hpp index 34f04fb8..9b360d45 100644 --- a/include/rfl/internal/to_ptr_tuple.hpp +++ b/include/rfl/internal/to_ptr_tuple.hpp @@ -1,5 +1,5 @@ -#ifndef RFL_INTERNAL_BIND_TO_TUPLE_HPP_ -#define RFL_INTERNAL_BIND_TO_TUPLE_HPP_ +#ifndef RFL_INTERNAL_TO_PTR_TUPLE_HPP_ +#define RFL_INTERNAL_TO_PTR_TUPLE_HPP_ #include #include diff --git a/tests/json/test_inside_function.cpp b/tests/json/test_inside_function.cpp new file mode 100644 index 00000000..76a11cee --- /dev/null +++ b/tests/json/test_inside_function.cpp @@ -0,0 +1,55 @@ +#include "test_inside_function.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_inside_function { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Age = rfl::Validator, rfl::Maximum<130>>; + + struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; + }; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = + Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})"); +} +} // namespace test_inside_function diff --git a/tests/json/test_inside_function.hpp b/tests/json/test_inside_function.hpp new file mode 100644 index 00000000..e7cf82f6 --- /dev/null +++ b/tests/json/test_inside_function.hpp @@ -0,0 +1,4 @@ +namespace test_inside_function { +void test(); +} + diff --git a/tests/json/test_unnamed_namespace.cpp b/tests/json/test_unnamed_namespace.cpp new file mode 100644 index 00000000..9347925f --- /dev/null +++ b/tests/json/test_unnamed_namespace.cpp @@ -0,0 +1,60 @@ + +#include "test_unnamed_namespace.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unnamed_namespace { + +namespace { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +} // namespace + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = + Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})"); +} +} // namespace test_unnamed_namespace diff --git a/tests/json/test_unnamed_namespace.hpp b/tests/json/test_unnamed_namespace.hpp new file mode 100644 index 00000000..8f64abd1 --- /dev/null +++ b/tests/json/test_unnamed_namespace.hpp @@ -0,0 +1,4 @@ +namespace test_unnamed_namespace { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 93e1bd30..4b9eda91 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -29,6 +29,7 @@ #include "test_flatten.hpp" #include "test_flatten_anonymous.hpp" #include "test_forward_list.hpp" +#include "test_inside_function.hpp" #include "test_list.hpp" #include "test_literal.hpp" #include "test_literal_map.hpp" @@ -61,6 +62,7 @@ #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" +#include "test_unnamed_namespace.hpp" #include "test_unordered_map.hpp" #include "test_unordered_multimap.hpp" #include "test_unordered_multiset.hpp" @@ -111,6 +113,8 @@ int main() { test_result::test(); test_anonymous_fields::test(); test_monster_example::test(); + test_unnamed_namespace::test(); + test_inside_function::test(); test_custom_class1::test(); test_custom_class2::test();