Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boost PFR as implicit fallback #266

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
81ff974
[pfr_implicit_explicit] Docs [skip ci]
denzor200 Dec 24, 2022
45d9be9
[pfr_implicit_explicit] Add important notes for the docs [skip ci]
denzor200 Dec 30, 2022
d002a70
[pfr_implicit_explicit] Add Boost PFR implicit and explicit adapters
denzor200 Dec 30, 2022
85be88d
[pfr_implicit_explicit] Add regular test for Boost PFR adapter
denzor200 Dec 30, 2022
e26c017
[pfr_implicit_explicit] Add test for Boost PFR explicit adapter && fi…
denzor200 Dec 30, 2022
e5b2344
[pfr_implicit_explicit] Just more tests
denzor200 Dec 30, 2022
bc1415e
[pfr_implicit_explicit] Add pfr dependency
denzor200 Dec 30, 2022
cd54084
[pfr_implicit_explicit] Fix warning static_assert with no message
denzor200 Dec 30, 2022
aed0503
[pfr_implicit_explicit] Force pfr implicit reflection in cxx14 mode
denzor200 Jan 11, 2023
a7bb2c7
[pfr_implicit_explicit] Update copyright years
denzor200 Jan 15, 2023
40dfa11
[pfr_implicit_explicit] Fix for 'config.hpp' && Comment for 'adapted.…
denzor200 Jan 15, 2023
e016b09
[pfr_implicit_explicit] First successful step for regression tests
denzor200 Jan 22, 2023
7307754
[pfr_implicit_explicit] Regression tests for 'sequence' group
denzor200 Jan 22, 2023
ff05040
[pfr_implicit_explicit] Rest of regression tests
denzor200 Jan 22, 2023
aff77c4
[pfr_implicit_explicit] Fix for old compilers building
denzor200 Jan 23, 2023
01defe1
[pfr_implicit_explicit] Fix for C++11 build
denzor200 Jan 24, 2023
4686805
[pfr_implicit_explicit] Fix for clang-3.5 build
denzor200 Jan 26, 2023
4906f32
[pfr_implicit_explicit] One with_or_without_fallback.hpp
denzor200 Jan 27, 2023
403ae44
[pfr_implicit_explicit] Force macroes documented as acceptable in old…
denzor200 Jan 27, 2023
c4547ce
[pfr_implicit_explicit] Diagnostics improvement
denzor200 Jan 27, 2023
073e9b2
[pfr_implicit_explicit] Tests improved a lot
denzor200 Jan 28, 2023
62545d4
[pfr_implicit_explicit] Fix typename cannot be used outside a templat…
denzor200 Jan 28, 2023
8f0ab4b
[pfr_implicit_explicit] Fix invalid application of 'sizeof'
denzor200 Jan 28, 2023
aff0c59
[pfr_implicit_explicit] Lot of fixes
denzor200 Jan 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions include/boost/fusion/adapted/boost_pfr/tag_of_fallback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <boost/fusion/support/config.hpp>
#include <boost/fusion/support/tag_of_fwd.hpp>
#include <boost/type_index/ctti_type_index.hpp>
#include <boost/pfr/traits.hpp>
#include <type_traits>

Expand All @@ -24,15 +25,52 @@ namespace boost { namespace fusion

namespace detail
{
// Helper function that returns true if `name` starts with `substr`
template <std::size_t N>
constexpr bool starts_with(const char* name, const char (&substr)[N]) noexcept {
static_assert(N > 0, "non a zero-terminated string passed");
for (std::size_t i = 0; i<N-1; ++i) {
if (name[i] != substr[i]) {
return false;
}
}
return true;
}

// Function that returns true if `T` declared in namespace `ns`
// got from here: https://www.boost.org/doc/libs/1_81_0/doc/html/boost_typeindex/examples.html#boost_typeindex.examples.c_14_checking_namespace_at_compi
// TODO: test for it
template <class T, std::size_t N>
constexpr bool in_namespace(const char (&ns)[N]) noexcept {
denzor200 marked this conversation as resolved.
Show resolved Hide resolved
const char* name = boost::typeindex::ctti_type_index::type_id<T>().raw_name();

// Some compilers add `class ` or `struct ` before the namespace, so we need to skip those words first
if (detail::starts_with(name, "class ")) {
name += sizeof("class ") - 1;
} else if (detail::starts_with(name, "struct ")) {
name += sizeof("struct ") - 1;
}

return detail::starts_with(name, ns) && detail::starts_with(name + N - 1, "::");
}

template<typename T>
constexpr bool is_implicitly_reflectable_via_pfr() noexcept {
// TODO: check that T is complete
const auto possible_pfr = boost::pfr::is_implicitly_reflectable<
T, boost::pfr::boost_fusion_tag>::value;
const auto value = !std::is_array<T>::value
&& !detail::in_namespace<T>("boost::fusion")
&& !detail::in_namespace<T>("boost::mpl")
&& !detail::in_namespace<T>("mpl_")
&& possible_pfr;
return value;
}

template <typename T>
struct tag_of_fallback<T, std::enable_if_t<std::is_same<T, T>::value>>
struct tag_of_fallback<T, std::enable_if_t<detail::is_implicitly_reflectable_via_pfr<T>()>>
{
static constexpr auto possible_pfr = boost::pfr::is_implicitly_reflectable<
T, boost::pfr::boost_fusion_tag>::value;
using type = std::conditional_t<(!std::is_array<T>::value && possible_pfr)
, boost_pfr_tag
, non_fusion_tag
>;
using type = boost_pfr_tag;
};
}
}}
Expand Down
Loading