From 78554708dc50fe0a91854862d7e940c5f5cb1bb6 Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 1 Apr 2024 12:16:17 +0200 Subject: [PATCH 1/6] Use BUILD_SHARED_LIBS as default value for REFLECTCPP_BUILD_SHARED --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65a58e00..0192c9b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15) -option(REFLECTCPP_BUILD_SHARED "Build shared library" OFF) +option(REFLECTCPP_BUILD_SHARED "Build shared library" ${BUILD_SHARED_LIBS}) option(REFLECTCPP_BSON "Enable BSON support" OFF) option(REFLECTCPP_CBOR "Enable CBOR support" OFF) option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF) From 8c1f9d1ee332775f7bf82dc24dd3e54cbaeab8da Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 1 Apr 2024 14:38:48 +0200 Subject: [PATCH 2/6] Add cmake config module --- CMakeLists.txt | 48 ++++++++++++++++++++++++++++++++++++-- reflectcpp-config.cmake.in | 38 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 reflectcpp-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 65a58e00..a5f2e36f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,15 @@ option(REFLECTCPP_YAML "Enable YAML support" OFF) option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) -# enable vcpkg if require features other than JSON +set(REFLECTCPP_USE_VCPKG_DEFAULT OFF) if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) + # enable vcpkg per default if require features other than JSON + set(REFLECTCPP_USE_VCPKG_DEFAULT ON) +endif() + +option(REFLECTCPP_USE_VCPKG "Use VCPKG to download and build dependencies" ${REFLECTCPP_USE_VCPKG_DEFAULT}) + +if (REFLECTCPP_USE_VCPKG) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file") endif () @@ -25,7 +32,9 @@ else () add_library(reflectcpp STATIC src/yyjson.c) endif () -target_include_directories(reflectcpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_compile_features(reflectcpp PUBLIC cxx_std_20) + +target_include_directories(reflectcpp PUBLIC $ $ ) if (REFLECTCPP_BSON) find_package(bson-1.0 CONFIG REQUIRED) @@ -66,3 +75,38 @@ target_compile_options(reflectcpp PRIVATE -Wall) if (REFLECTCPP_BUILD_TESTS) add_subdirectory(tests) endif () + + +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + +configure_package_config_file(reflectcpp-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp + ) + +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp" +) + +file(GLOB_RECURSE RFL_HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${CMAKE_CURRENT_LIST_DIR}/include/*" ) + +target_sources(reflectcpp + PUBLIC + FILE_SET reflectcpp_headers + TYPE HEADERS + BASE_DIRS $ $ + FILES ${RFL_HEADERS}) + +install( + TARGETS reflectcpp + EXPORT reflectcpp-exports + FILE_SET reflectcpp_headers DESTINATION ${INCLUDE_INSTALL_DIR} + ) + +install( + EXPORT reflectcpp-exports + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp + NAMESPACE reflectcpp:: +) \ No newline at end of file diff --git a/reflectcpp-config.cmake.in b/reflectcpp-config.cmake.in new file mode 100644 index 00000000..1d8d7c06 --- /dev/null +++ b/reflectcpp-config.cmake.in @@ -0,0 +1,38 @@ +@PACKAGE_INIT@ + +set(REFLECTCPP_BSON @REFLECTCPP_BSON@) +set(REFLECTCPP_FLEXBUFFERS @REFLECTCPP_FLEXBUFFERS@) +set(REFLECTCPP_TOML @REFLECTCPP_TOML@) +set(REFLECTCPP_XML @REFLECTCPP_XML@) +set(REFLECTCPP_YAML @REFLECTCPP_YAML@) + +if(REFLECTCPP_BSON OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML) + include(CMakeFindDependencyMacro) +endif() + +include(${CMAKE_CURRENT_LIST_DIR}/reflectcpp-exports.cmake) + + +if (REFLECTCPP_BSON) + find_dependency(bson-1.0) +endif () + +if (REFLECTCPP_FLEXBUFFERS) + find_dependency(flatbuffers) +endif () + +if (REFLECTCPP_TOML) + find_package(PkgConfig REQUIRED) + pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus) +endif() + +if (REFLECTCPP_XML) + find_dependency(pugixml) +endif () + +if (REFLECTCPP_YAML) + find_dependency(yaml-cpp) +endif () + +check_required_components(reflect-cpp) + From 52cf3ed7d4a5d400db9fc4f38e1b33b989392047 Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Fri, 5 Apr 2024 08:33:55 +0200 Subject: [PATCH 3/6] reflectcpp-config.cmake.in: Fix typo in check_required_components --- reflectcpp-config.cmake.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reflectcpp-config.cmake.in b/reflectcpp-config.cmake.in index 1d8d7c06..702cbc4a 100644 --- a/reflectcpp-config.cmake.in +++ b/reflectcpp-config.cmake.in @@ -34,5 +34,4 @@ if (REFLECTCPP_YAML) find_dependency(yaml-cpp) endif () -check_required_components(reflect-cpp) - +check_required_components(reflectcpp) From 0b33c92c9b0362cf2e1320aaf973a56db08720e0 Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 8 Apr 2024 10:42:42 +0200 Subject: [PATCH 4/6] Fix names of inherited members being prefixed with the base-class's name when using clang --- include/rfl/internal/get_field_names.hpp | 5 ++++- tests/json/CMakeLists.txt | 2 +- tests/json/test_inheritance.cpp | 26 ++++++++++++++++++++++++ tests/json/test_inheritance.hpp | 3 +++ tests/json/tests.cpp | 3 +++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/json/test_inheritance.cpp create mode 100644 tests/json/test_inheritance.hpp diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 8f5bd3c8..52efbd2e 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -52,7 +52,10 @@ consteval auto get_field_name_str_view() { #endif #if defined(__clang__) const auto split = func_name.substr(0, func_name.size() - 2); - return split.substr(split.find_last_of(".") + 1); + const auto possibly_prefixed = split.substr(split.find_last_of(".") + 1); + const auto separator_index = possibly_prefixed.find_last_of(":"); + if (separator_index == std::string_view::npos) return possibly_prefixed; + return possibly_prefixed.substr(separator_index + 1); #elif defined(__GNUC__) const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(":") + 1); diff --git a/tests/json/CMakeLists.txt b/tests/json/CMakeLists.txt index 8a32cc38..1e077f6a 100644 --- a/tests/json/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -1,6 +1,6 @@ project(reflect-cpp-json-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") add_executable(reflect-cpp-json-tests ${SOURCES}) diff --git a/tests/json/test_inheritance.cpp b/tests/json/test_inheritance.cpp new file mode 100644 index 00000000..5f480970 --- /dev/null +++ b/tests/json/test_inheritance.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +namespace test_inheritance { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + struct S { + int x; + }; + + struct T : S {}; + + const auto name = get<0>(rfl::fields()).name(); + if (name == "x") { + std::cout << "OK" << std::endl; + } else { + std::cout << "FAIL\n" + << "Expected member name 'x', got '" << name << "'" << std::endl; + } +} + +} // namespace test_inheritance \ No newline at end of file diff --git a/tests/json/test_inheritance.hpp b/tests/json/test_inheritance.hpp new file mode 100644 index 00000000..31d51d1a --- /dev/null +++ b/tests/json/test_inheritance.hpp @@ -0,0 +1,3 @@ +namespace test_inheritance{ +void test(); +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index ebcffe3b..1246c887 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -86,6 +86,7 @@ #include "test_variant.hpp" #include "test_view.hpp" #include "test_wstring.hpp" +#include "test_inheritance.hpp" int main() { test_readme_example::test(); @@ -187,5 +188,7 @@ int main() { test_wstring::test(); test_json_schema::test(); + test_inheritance::test(); + return 0; } From 1146f76bded466f60a34918f3bf5be3bb65a848d Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 8 Apr 2024 16:02:16 +0200 Subject: [PATCH 5/6] More elegant formulation --- include/rfl/internal/get_field_names.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 52efbd2e..ebb3e8ae 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -52,10 +52,7 @@ consteval auto get_field_name_str_view() { #endif #if defined(__clang__) const auto split = func_name.substr(0, func_name.size() - 2); - const auto possibly_prefixed = split.substr(split.find_last_of(".") + 1); - const auto separator_index = possibly_prefixed.find_last_of(":"); - if (separator_index == std::string_view::npos) return possibly_prefixed; - return possibly_prefixed.substr(separator_index + 1); + return split.substr(split.find_last_of(":.") + 1); #elif defined(__GNUC__) const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(":") + 1); From fbeca9b095fae31365b4d2fda48ea849c0d46aca Mon Sep 17 00:00:00 2001 From: Lixin Wei Date: Thu, 11 Apr 2024 22:54:17 +0800 Subject: [PATCH 6/6] Support one-level inheritance cases (#83) * Substract base class param from num of fields * Fix comments from @schaumb * Fix CI * fix comments from @liuzicheng1987 * fix some comments form @schaumb * rename some funcs * one-level inheritance fixed * fix CI --- .gitignore | 4 ++ include/rfl/internal/bind_to_tuple.hpp | 1 + include/rfl/internal/num_fields.hpp | 97 ++++++++++++++++++++++++-- tests/json/test_inheritance2.cpp | 52 ++++++++++++++ tests/json/test_inheritance2.hpp | 3 + tests/json/test_view.cpp | 1 + tests/json/tests.cpp | 2 + 7 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 tests/json/test_inheritance2.cpp create mode 100644 tests/json/test_inheritance2.hpp diff --git a/.gitignore b/.gitignore index f05d838f..d57c7e6f 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,7 @@ *.yaml !vcpkg.json + +# clangd +compile_flags.txt +compile_commands.json diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index c2ec8fc5..16eb6755 100644 --- a/include/rfl/internal/bind_to_tuple.hpp +++ b/include/rfl/internal/bind_to_tuple.hpp @@ -9,6 +9,7 @@ #include "../always_false.hpp" #include "is_named_tuple.hpp" +#include "num_fields.hpp" namespace rfl { namespace internal { diff --git a/include/rfl/internal/num_fields.hpp b/include/rfl/internal/num_fields.hpp index fb88454a..a70397b5 100644 --- a/include/rfl/internal/num_fields.hpp +++ b/include/rfl/internal/num_fields.hpp @@ -29,6 +29,7 @@ the potential array as we can without missing variables in subsequent fields. This is the purpose of get_nested_array_size(). */ +#include #include #include #include @@ -48,6 +49,29 @@ This is the purpose of get_nested_array_size(). namespace rfl { namespace internal { +template +struct any_empty_base { + any_empty_base(std::size_t); + template + requires( + std::is_empty_v> && + std::is_base_of_v, + std::remove_cv_t> && + !std::is_same_v, std::remove_cv_t>) + constexpr operator Base&() const noexcept; +}; + +template +struct any_base { + any_base(std::size_t); + template + requires( + std::is_base_of_v, + std::remove_cv_t> && + !std::is_same_v, std::remove_cv_t>) + constexpr operator Base&() const noexcept; +}; + struct any { any(std::size_t); template @@ -76,12 +100,12 @@ struct CountFieldsHelper { } template - static consteval std::size_t count_max_fields() { + static consteval std::size_t count_max_args_in_agg_init() { static_assert(n <= static_cast(sizeof(T))); if constexpr (constructible() && !constructible()) { return n; } else { - return count_max_fields(); + return count_max_args_in_agg_init(); } } @@ -97,20 +121,83 @@ struct CountFieldsHelper { } } + template + static consteval std::size_t find_the_sole_non_empty_base_index() { + static_assert(index < max_args); + constexpr auto check = []( + std::index_sequence, + std::index_sequence) { + return requires { + T{any_empty_base(l)..., any_base(0), any_empty_base(r)...}; + }; + }; + + if constexpr (check(std::make_index_sequence(), + std::make_index_sequence())) { + return index; + } else { + return find_the_sole_non_empty_base_index(); + } + } + + template + static consteval std::size_t get_nested_base_field_count() { + static_assert(size <= sizeof(T)); + if constexpr (constructible_with_nested() && + !constructible_with_nested()) { + return size; + } else { + return get_nested_base_field_count(); + } + } + + template + static consteval bool has_n_base_param() { + constexpr auto right_len = max_arg_num>=n ? max_arg_num-n : 0; + return [](std::index_sequence, + std::index_sequence) { + return requires { T{any_base(l)..., any(r)...}; }; + }(std::make_index_sequence(), std::make_index_sequence()); + } + + template + static consteval std::size_t base_param_num() { + if constexpr (!has_n_base_param()) { + return index; + } else { + return base_param_num(); + } + } + template - static consteval std::size_t count_fields_impl() { + static consteval std::size_t constructible_no_brace_elision() { static_assert(index <= max); if constexpr (index == max) { return 0; } else { return 1 + - count_fields_impl< + constructible_no_brace_elision< index + get_nested_array_size(), max>(); } } static consteval std::size_t count_fields() { - return count_fields_impl<0, count_max_fields()>(); + constexpr std::size_t max_agg_args = count_max_args_in_agg_init(); + constexpr std::size_t no_brace_ellison_args = + constructible_no_brace_elision<0, max_agg_args>(); + constexpr std::size_t base_args = base_param_num(); + if constexpr (no_brace_ellison_args == 0 && base_args == 0) { + // Empty struct + return 0; + } else if constexpr (base_args == no_brace_ellison_args) { + // Special case when the derived class is empty. + // In such cases the filed number is the fields in base class. + // Note that there should be only one base class in this case. + return get_nested_base_field_count< + find_the_sole_non_empty_base_index()>(); + } else { + return no_brace_ellison_args - base_args; + } } }; diff --git a/tests/json/test_inheritance2.cpp b/tests/json/test_inheritance2.cpp new file mode 100644 index 00000000..25e62b25 --- /dev/null +++ b/tests/json/test_inheritance2.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +#include "rfl/internal/num_fields.hpp" + +namespace test_inheritance2 { + +struct EmptyBase1 {}; +struct EmptyBase2 {}; +struct Derived1 : public EmptyBase1 { + int x; + int y; +}; +struct Derived2 : public EmptyBase1, public EmptyBase2 { + int x; + int y; + int z; +}; + +struct BaseX { + int x; + int y; +}; +struct EmptyDerived0 : BaseX, EmptyBase1 {}; +struct EmptyDerived1 : EmptyBase1, BaseX {}; +struct EmptyDerived2 : EmptyBase1, EmptyBase2, BaseX {}; + +void test() { + Derived1 derived1; + const auto derived1_view = rfl::to_view(derived1); + static_assert(derived1_view.size() == 2); + Derived2 derived2; + const auto derived2_view = rfl::to_view(derived2); + static_assert(derived2_view.size() == 3); + + EmptyDerived1 empty_derived0; + auto empty_derived0_view = rfl::to_view(empty_derived0); + static_assert(empty_derived0_view.size() == 2); + + EmptyDerived1 empty_derived1; + auto empty_derived1_view = rfl::to_view(empty_derived1); + static_assert(empty_derived1_view.size() == 2); + + EmptyDerived1 empty_derived2; + auto empty_derived2_view = rfl::to_view(empty_derived2); + static_assert(empty_derived0_view.size() == 2); + + std::cout << "OK\n"; +} + +} // namespace test_inheritance \ No newline at end of file diff --git a/tests/json/test_inheritance2.hpp b/tests/json/test_inheritance2.hpp new file mode 100644 index 00000000..c347aac6 --- /dev/null +++ b/tests/json/test_inheritance2.hpp @@ -0,0 +1,3 @@ +namespace test_inheritance2 { +void test(); +} diff --git a/tests/json/test_view.cpp b/tests/json/test_view.cpp index e1de18de..f4b4b042 100644 --- a/tests/json/test_view.cpp +++ b/tests/json/test_view.cpp @@ -7,6 +7,7 @@ #include #include +#include "rfl/internal/num_fields.hpp" #include "test_replace.hpp" #include "write_and_read.hpp" diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 1246c887..fd415c17 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -87,6 +87,7 @@ #include "test_view.hpp" #include "test_wstring.hpp" #include "test_inheritance.hpp" +#include "test_inheritance2.hpp" int main() { test_readme_example::test(); @@ -189,6 +190,7 @@ int main() { test_json_schema::test(); test_inheritance::test(); + test_inheritance2::test(); return 0; }