Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:41:01

0001 // Copyright (c) 2023 Bela Schaum, X-Ryl669, Denis Mikhailov.
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 
0007 // Initial implementation by Bela Schaum, https://github.com/schaumb
0008 // The way to make it union and UB free by X-Ryl669, https://github.com/X-Ryl669
0009 //
0010 
0011 #ifndef BOOST_PFR_CORE_NAME_HPP
0012 #define BOOST_PFR_CORE_NAME_HPP
0013 #pragma once
0014 
0015 #include <boost/pfr/detail/config.hpp>
0016 
0017 #include <boost/pfr/detail/core_name.hpp>
0018 
0019 #include <boost/pfr/detail/sequence_tuple.hpp>
0020 #include <boost/pfr/detail/stdarray.hpp>
0021 #include <boost/pfr/detail/make_integer_sequence.hpp>
0022 
0023 #include <cstddef> // for std::size_t
0024 
0025 #include <boost/pfr/tuple_size.hpp>
0026 
0027 /// \file boost/pfr/core_name.hpp
0028 /// Contains functions \forcedlink{get_name} and \forcedlink{names_as_array} to know which names each field of any \aggregate has.
0029 ///
0030 /// \fnrefl for details.
0031 ///
0032 /// \b Synopsis:
0033 
0034 namespace boost { namespace pfr {
0035 
0036 BOOST_PFR_BEGIN_MODULE_EXPORT
0037 
0038 /// \brief Returns name of a field with index `I` in \aggregate `T`.
0039 ///
0040 /// \b Example:
0041 /// \code
0042 ///     struct my_struct { int i, short s; };
0043 ///
0044 ///     assert(boost::pfr::get_name<0, my_struct>() == "i");
0045 ///     assert(boost::pfr::get_name<1, my_struct>() == "s");
0046 /// \endcode
0047 template <std::size_t I, class T>
0048 constexpr
0049 #ifdef BOOST_PFR_DOXYGEN_INVOKED
0050 std::string_view
0051 #else
0052 auto
0053 #endif
0054 get_name() noexcept {
0055     return detail::get_name<T, I>();
0056 }
0057 
0058 // FIXME: implement this
0059 // template<class U, class T>
0060 // constexpr auto get_name() noexcept {
0061 //     return detail::sequence_tuple::get_by_type_impl<U>( detail::tie_as_names_tuple<T>() );
0062 // }
0063 
0064 /// \brief Creates a `std::array` from names of fields of an \aggregate `T`.
0065 ///
0066 /// \b Example:
0067 /// \code
0068 ///     struct my_struct { int i, short s; };
0069 ///     std::array<std::string_view, 2> a = boost::pfr::names_as_array<my_struct>();
0070 ///     assert(a[0] == "i");
0071 /// \endcode
0072 template <class T>
0073 constexpr
0074 #ifdef BOOST_PFR_DOXYGEN_INVOKED
0075 std::array<std::string_view, boost::pfr::tuple_size_v<T>>
0076 #else
0077 auto
0078 #endif
0079 names_as_array() noexcept {
0080     return detail::make_stdarray_from_tietuple(
0081         detail::tie_as_names_tuple<T>(),
0082         detail::make_index_sequence< tuple_size_v<T> >(),
0083         1L
0084     );
0085 }
0086 
0087 
0088 /// Calls `func` for each field with its name of a `value`
0089 ///
0090 /// \param func must have one of the following signatures:
0091 ///     * any_return_type func(std::string_view name, U&& field)                // field of value is perfect forwarded to function
0092 ///     * any_return_type func(std::string_view name, U&& field, std::size_t i)
0093 ///     * any_return_type func(std::string_view name, U&& value, I i)           // Here I is an `std::integral_constant<size_t, field_index>`
0094 ///
0095 /// \param value To each field of this variable will be the `func` applied.
0096 ///
0097 /// \b Example:
0098 /// \code
0099 ///     struct Toto { int a; char c; };
0100 ///     Toto t {5, 'c'};
0101 ///     auto print = [](std::string_view name, const auto& value){ std::cout << "Name: " << name << " Value: " << value << std::endl; };
0102 ///     for_each_field_with_name(t, print);
0103 /// \endcode
0104 template <class T, class F>
0105 constexpr void for_each_field_with_name(T&& value, F&& func) {
0106     return boost::pfr::detail::for_each_field_with_name(std::forward<T>(value), std::forward<F>(func));
0107 }
0108 
0109 BOOST_PFR_END_MODULE_EXPORT
0110 
0111 }} // namespace boost::pfr
0112 
0113 #endif // BOOST_PFR_CORE_NAME_HPP