Made sure that we can get field names from structs declared inside functions or unnamed namespaces; #46

This commit is contained in:
Patrick Urbanke
2024-01-07 23:05:39 +01:00
parent 30adc5c614
commit 696289dbf2
10 changed files with 215 additions and 46 deletions
+11 -13
View File
@@ -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 <cstddef>
#include <iostream>
@@ -13,17 +13,6 @@
namespace rfl {
namespace internal {
template <class T>
constexpr auto tuple_view(T&);
template <class T, typename F>
constexpr auto bind_to_tuple(T& _t, const F& _f) {
auto view = tuple_view(_t);
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return std::make_tuple(_f(std::get<Is>(view))...);
}(std::make_index_sequence<std::tuple_size_v<decltype(view)>>());
}
template <std::size_t n>
struct tuple_view_helper {
template <class T>
@@ -598,6 +587,15 @@ constexpr auto tuple_view(T& t) {
return tuple_view_helper<num_fields<T>>::tuple_view(t);
}
template <class T, typename F>
constexpr auto bind_to_tuple(T& _t, const F& _f) {
auto view = tuple_view(_t);
return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
return std::make_tuple(_f(std::get<Is>(view))...);
}
(std::make_index_sequence<std::tuple_size_v<decltype(view)>>());
}
} // namespace internal
} // namespace rfl
-14
View File
@@ -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 <class T>
extern T fake_object;
} // namespace internal
} // namespace rfl
#endif
+46
View File
@@ -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 <class T>
struct wrapper {
const T value;
static const wrapper<T> report_if_you_see_a_link_error_with_this_object;
};
template <class T>
consteval const T& get_fake_object() noexcept {
return wrapper<T>::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
+29 -17
View File
@@ -11,11 +11,11 @@
#include <utility>
#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 <auto ptr>
template <class T, auto ptr>
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 <auto ptr>
template <class T, auto ptr>
consteval auto get_field_name_str_lit() {
constexpr auto name = get_field_name_str_view<ptr>();
constexpr auto name = get_field_name_str_view<T, ptr>();
const auto to_str_lit = [&]<auto... Ns>(std::index_sequence<Ns...>) {
return StringLiteral<sizeof...(Ns) + 1>{name[Ns]...};
};
@@ -73,17 +73,21 @@ consteval auto get_field_name_str_lit() {
template <class T>
auto get_field_names();
template <auto ptr>
template <class T, auto ptr>
auto get_field_name() {
#if defined(__clang__)
using Type = std::remove_cvref_t<std::remove_pointer_t<
typename std::remove_pointer_t<decltype(ptr)>::Type>>;
#else
using Type = std::remove_cvref_t<std::remove_pointer_t<decltype(ptr)>>;
#endif
if constexpr (is_rename_v<Type>) {
using Name = typename Type::Name;
return Name();
} else if constexpr (is_flatten_field_v<Type>) {
return get_field_names<std::remove_cvref_t<typename Type::Type>>();
} else {
return rfl::Literal<get_field_name_str_lit<ptr>()>();
return rfl::Literal<get_field_name_str_lit<T, ptr>()>();
}
}
@@ -114,16 +118,24 @@ template <class T>
#endif
#endif
auto get_field_names() {
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_pointer_v<Type>) {
return get_field_names<std::remove_pointer_t<T>>();
} else {
constexpr auto ptr_tuple = to_ptr_tuple(fake_object<T>);
const auto get = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
#if defined(__clang__)
const auto get = []<std::size_t... Is>(std::index_sequence<Is...>) {
return concat_literals(
get_field_name<wrap(std::get<Is>(ptr_tuple))>()...);
get_field_name<Type, wrap(std::get<Is>(
bind_fake_object_to_tuple<T>()))>()...);
};
return get(
std::make_index_sequence<std::tuple_size_v<decltype(ptr_tuple)>>());
#else
const auto get = []<std::size_t... Is>(std::index_sequence<Is...>) {
return concat_literals(
get_field_name<Type,
std::get<Is>(bind_fake_object_to_tuple<T>())>()...);
};
#endif
return get(std::make_index_sequence<num_fields<T>>());
}
}
+2 -2
View File
@@ -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 <iostream>
#include <tuple>
+55
View File
@@ -0,0 +1,55 @@
#include "test_inside_function.hpp"
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<unsigned int, rfl::Minimum<0>, 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<Person> 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<Person>({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
+4
View File
@@ -0,0 +1,4 @@
namespace test_inside_function {
void test();
}
+60
View File
@@ -0,0 +1,60 @@
#include "test_unnamed_namespace.hpp"
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_unnamed_namespace {
namespace {
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, 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<Person> 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<Person>({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
+4
View File
@@ -0,0 +1,4 @@
namespace test_unnamed_namespace {
void test();
}
+4
View File
@@ -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();