-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2023 Bela Schaum, X-Ryl669, Denis Mikhailov. | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
// Initial implementation by Bela Schaum, https://github.com/schaumb | ||
// The way to make it union and UB free by X-Ryl669, https://github.com/X-Ryl669 | ||
// | ||
|
||
#ifndef BOOST_PFR_DETAIL_CORE_NAME14_DISABLED_HPP | ||
#define BOOST_PFR_DETAIL_CORE_NAME14_DISABLED_HPP | ||
#pragma once | ||
|
||
#include <boost/pfr/detail/config.hpp> | ||
#include <boost/pfr/detail/sequence_tuple.hpp> | ||
|
||
namespace boost { namespace pfr { namespace detail { | ||
|
||
// TODO: move it outside | ||
template <class... Args> | ||
constexpr auto make_sequence_tuple(Args... args) noexcept { | ||
return sequence_tuple::tuple<Args...>{ args... }; | ||
} | ||
|
||
|
||
template <class T> | ||
constexpr auto tie_as_names_tuple() noexcept { | ||
static_assert( | ||
sizeof(T) && false, | ||
"====================> Boost.PFR: Extraction of field's names is allowed only in C++20" | ||
); | ||
|
||
return detail::make_sequence_tuple(); | ||
} | ||
|
||
}}} | ||
|
||
#endif // BOOST_PFR_DETAIL_CORE_NAME14_DISABLED_HPP | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2023 Bela Schaum, X-Ryl669, Denis Mikhailov. | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
// Initial implementation by Bela Schaum, https://github.com/schaumb | ||
// The way to make it union and UB free by X-Ryl669, https://github.com/X-Ryl669 | ||
// | ||
|
||
#ifndef BOOST_PFR_DETAIL_CORE_NAME20_STATIC_HPP | ||
#define BOOST_PFR_DETAIL_CORE_NAME20_STATIC_HPP | ||
#pragma once | ||
|
||
#include <boost/pfr/detail/config.hpp> | ||
#include <boost/pfr/detail/core.hpp> | ||
#include <boost/pfr/detail/sequence_tuple.hpp> | ||
#include <boost/pfr/detail/make_integer_sequence.hpp> | ||
#include <boost/pfr/detail/fields_count.hpp> | ||
#include <type_traits> | ||
#include <string_view> | ||
#include <array> | ||
|
||
namespace boost { namespace pfr { namespace detail { | ||
|
||
// TODO: move it outside | ||
template <class... Args> | ||
constexpr auto make_sequence_tuple(Args... args) noexcept { | ||
return sequence_tuple::tuple<Args...>{ args... }; | ||
} | ||
|
||
template <auto& ptr> | ||
constexpr auto name_of_field_impl() noexcept { | ||
#ifdef _MSC_VER | ||
constexpr std::string_view sv = __FUNCSIG__; | ||
constexpr auto last = sv.find_last_not_of(" >(", sv.size() - 6); | ||
#else | ||
constexpr std::string_view sv = __PRETTY_FUNCTION__; | ||
constexpr auto last = sv.find_last_not_of(" ])"); | ||
#endif | ||
constexpr auto first = sv.find_last_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789", last); | ||
std::array<char, last - first + 1> res{}; | ||
auto it = res.data(); | ||
for (auto a = first+1; a <= last; ++a) | ||
*it++ = sv[a]; | ||
return res; | ||
} | ||
|
||
template <typename T> | ||
extern const T fake_object; | ||
|
||
template <class T, std::size_t I> | ||
constexpr auto stored_name_of_field = name_of_field_impl<detail::sequence_tuple::get<I>( | ||
detail::tie_as_tuple(fake_object<T>) | ||
)>(); | ||
|
||
template <class T, std::size_t... I> | ||
constexpr auto tie_as_names_tuple_impl(std::index_sequence<I...>) noexcept { | ||
return detail::make_sequence_tuple(std::string_view{stored_name_of_field<T, I>.data()}...); | ||
} | ||
|
||
template <class T> | ||
constexpr auto tie_as_names_tuple() noexcept { | ||
static_assert( | ||
!std::is_union<T>::value, | ||
"====================> Boost.PFR: For safety reasons it is forbidden to reflect unions. See `Reflection of unions` section in the docs for more info." | ||
); | ||
static_assert( | ||
sizeof(T) && BOOST_PFR_USE_CPP17, | ||
"====================> Boost.PFR: Extraction of field's names is allowed only when the BOOST_PFR_USE_CPP17 macro enabled." | ||
); | ||
|
||
return tie_as_names_tuple_impl<T>(detail::make_index_sequence<detail::fields_count<T>()>{}); | ||
} | ||
|
||
}}} | ||
|
||
#endif // BOOST_PFR_DETAIL_CORE_NAME20_STATIC_HPP | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters