// Copyright (c) 2016-2024 Antony Polukhin // // 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) #ifndef BOOST_PFR_CORE_HPP #define BOOST_PFR_CORE_HPP #pragma once #include #include #include #include #include #include #include #include #include // metaprogramming stuff #include /// \file boost/pfr/core.hpp /// Contains all the basic tuple-like interfaces \forcedlink{get}, \forcedlink{tuple_size}, \forcedlink{tuple_element_t}, and others. /// /// \b Synopsis: namespace boost { namespace pfr { /// \brief Returns reference or const reference to a field with index `I` in \aggregate `val`. /// Overload taking the type `U` returns reference or const reference to a field /// with provided type `U` in \aggregate `val` if there's only one field of such type in `val`. /// /// \b Example: /// \code /// struct my_struct { int i, short s; }; /// my_struct s {10, 11}; /// /// assert(boost::pfr::get<0>(s) == 10); /// boost::pfr::get<1>(s) = 0; /// /// assert(boost::pfr::get(s) == 10); /// boost::pfr::get(s) = 11; /// \endcode template constexpr decltype(auto) get(const T& val) noexcept { return detail::sequence_tuple::get( detail::tie_as_tuple(val) ); } /// \overload get template constexpr decltype(auto) get(T& val #if !BOOST_PFR_USE_CPP17 , std::enable_if_t::value>* = nullptr #endif ) noexcept { return detail::sequence_tuple::get( detail::tie_as_tuple(val) ); } #if !BOOST_PFR_USE_CPP17 /// \overload get template constexpr auto get(T&, std::enable_if_t::value>* = nullptr) noexcept { static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::get on non const non assignable type is allowed only in C++17"); return 0; } #endif /// \overload get template constexpr auto get(T&& val, std::enable_if_t< std::is_rvalue_reference::value>* = nullptr) noexcept { return std::move(detail::sequence_tuple::get( detail::tie_as_tuple(val) )); } /// \overload get template constexpr const U& get(const T& val) noexcept { return detail::sequence_tuple::get_by_type_impl( detail::tie_as_tuple(val) ); } /// \overload get template constexpr U& get(T& val #if !BOOST_PFR_USE_CPP17 , std::enable_if_t::value>* = nullptr #endif ) noexcept { return detail::sequence_tuple::get_by_type_impl( detail::tie_as_tuple(val) ); } #if !BOOST_PFR_USE_CPP17 /// \overload get template constexpr U& get(T&, std::enable_if_t::value>* = nullptr) noexcept { static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::get on non const non assignable type is allowed only in C++17"); return 0; } #endif /// \overload get template constexpr U&& get(T&& val, std::enable_if_t< std::is_rvalue_reference::value>* = nullptr) noexcept { return std::move(detail::sequence_tuple::get_by_type_impl( detail::tie_as_tuple(val) )); } /// \brief `tuple_element` has a member typedef `type` that returns the type of a field with index I in \aggregate T. /// /// \b Example: /// \code /// std::vector< boost::pfr::tuple_element<0, my_structure>::type > v; /// \endcode template using tuple_element = detail::sequence_tuple::tuple_element()) ) >; /// \brief Type of a field with index `I` in \aggregate `T`. /// /// \b Example: /// \code /// std::vector< boost::pfr::tuple_element_t<0, my_structure> > v; /// \endcode template using tuple_element_t = typename tuple_element::type; /// \brief Creates a `std::tuple` from fields of an \aggregate `val`. /// /// \b Example: /// \code /// struct my_struct { int i, short s; }; /// my_struct s {10, 11}; /// std::tuple t = boost::pfr::structure_to_tuple(s); /// assert(get<0>(t) == 10); /// \endcode template constexpr auto structure_to_tuple(const T& val) { return detail::make_stdtuple_from_tietuple( detail::tie_as_tuple(val), detail::make_index_sequence< tuple_size_v >() ); } /// \brief std::tie` like function that ties fields of a structure. /// /// \returns a `std::tuple` with lvalue and const lvalue references to fields of an \aggregate `val`. /// /// \b Example: /// \code /// void foo(const int&, const short&); /// struct my_struct { int i, short s; }; /// /// const my_struct const_s{1, 2}; /// std::apply(foo, boost::pfr::structure_tie(const_s)); /// /// my_struct s; /// boost::pfr::structure_tie(s) = std::tuple{10, 11}; /// assert(s.s == 11); /// \endcode template constexpr auto structure_tie(const T& val) noexcept { return detail::make_conststdtiedtuple_from_tietuple( detail::tie_as_tuple(const_cast(val)), detail::make_index_sequence< tuple_size_v >() ); } /// \overload structure_tie template constexpr auto structure_tie(T& val #if !BOOST_PFR_USE_CPP17 , std::enable_if_t::value>* = nullptr #endif ) noexcept { return detail::make_stdtiedtuple_from_tietuple( detail::tie_as_tuple(val), detail::make_index_sequence< tuple_size_v >() ); } #if !BOOST_PFR_USE_CPP17 /// \overload structure_tie template constexpr auto structure_tie(T&, std::enable_if_t::value>* = nullptr) noexcept { static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::structure_tie on non const non assignable type is allowed only in C++17"); return 0; } #endif /// \overload structure_tie template constexpr auto structure_tie(T&&, std::enable_if_t< std::is_rvalue_reference::value>* = nullptr) noexcept { static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::structure_tie on rvalue references is forbidden"); return 0; } /// Calls `func` for each field of a `value`. /// /// \param func must have one of the following signatures: /// * any_return_type func(U&& field) // field of value is perfect forwarded to function /// * any_return_type func(U&& field, std::size_t i) /// * any_return_type func(U&& value, I i) // Here I is an `std::integral_constant` /// /// \param value To each field of this variable will be the `func` applied. /// /// \b Example: /// \code /// struct my_struct { int i, short s; }; /// int sum = 0; /// boost::pfr::for_each_field(my_struct{20, 22}, [&sum](const auto& field) { sum += field; }); /// assert(sum == 42); /// \endcode template constexpr void for_each_field(T&& value, F&& func) { constexpr std::size_t fields_count_val = boost::pfr::detail::fields_count>(); ::boost::pfr::detail::for_each_field_dispatcher( value, [f = std::forward(func)](auto&& t) mutable { // MSVC related workaround. Its lambdas do not capture constexprs. constexpr std::size_t fields_count_val_in_lambda = boost::pfr::detail::fields_count>(); ::boost::pfr::detail::for_each_field_impl( t, std::forward(f), detail::make_index_sequence{}, std::is_rvalue_reference{} ); }, detail::make_index_sequence{} ); } /// \brief std::tie-like function that allows assigning to tied values from aggregates. /// /// \returns an object with lvalue references to `args...`; on assignment of an \aggregate value to that /// object each field of an aggregate is assigned to the corresponding `args...` reference. /// /// \b Example: /// \code /// auto f() { /// struct { struct { int x, y } p; short s; } res { { 4, 5 }, 6 }; /// return res; /// } /// auto [p, s] = f(); /// boost::pfr::tie_from_structure(p, s) = f(); /// \endcode template constexpr detail::tie_from_structure_tuple tie_from_structure(Elements&... args) noexcept { return detail::tie_from_structure_tuple(args...); } }} // namespace boost::pfr #endif // BOOST_PFR_CORE_HPP