Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:38

0001 // Copyright (c) 2016-2023 Antony Polukhin
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 #ifndef BOOST_PFR_DETAIL_MAKE_FLAT_TUPLE_OF_REFERENCES_HPP
0007 #define BOOST_PFR_DETAIL_MAKE_FLAT_TUPLE_OF_REFERENCES_HPP
0008 #pragma once
0009 
0010 #include <boost/pfr/detail/config.hpp>
0011 
0012 #include <utility>      // metaprogramming stuff
0013 #include <boost/pfr/detail/sequence_tuple.hpp>
0014 #include <boost/pfr/detail/rvalue_t.hpp>
0015 #include <boost/pfr/detail/make_integer_sequence.hpp>
0016 
0017 
0018 namespace boost { namespace pfr { namespace detail {
0019 
0020 template <std::size_t Index>
0021 using size_t_ = std::integral_constant<std::size_t, Index >;
0022 
0023 // Helper: Make a "getter" object corresponding to built-in tuple::get
0024 // For user-defined structures, the getter should be "offset_based_getter"
0025 struct sequence_tuple_getter {
0026   template <std::size_t idx, typename TupleOfReferences>
0027   decltype(auto) get(TupleOfReferences&& t, size_t_<idx>) const noexcept {
0028     return sequence_tuple::get<idx>(std::forward<TupleOfReferences>(t));
0029   }
0030 };
0031 
0032 
0033 template <class TupleOrUserType, class Getter, std::size_t Begin, std::size_t Size>
0034 constexpr auto make_flat_tuple_of_references(TupleOrUserType&, const Getter&, size_t_<Begin>, size_t_<Size>) noexcept;
0035 
0036 template <class TupleOrUserType, class Getter, std::size_t Begin>
0037 constexpr sequence_tuple::tuple<> make_flat_tuple_of_references(TupleOrUserType&, const Getter&, size_t_<Begin>, size_t_<0>) noexcept;
0038 
0039 template <class TupleOrUserType, class Getter, std::size_t Begin>
0040 constexpr auto make_flat_tuple_of_references(TupleOrUserType&, const Getter&, size_t_<Begin>, size_t_<1>) noexcept;
0041 
0042 template <class... T>
0043 constexpr auto tie_as_tuple_with_references(T&... args) noexcept {
0044     return sequence_tuple::tuple<T&...>{ args... };
0045 }
0046 
0047 template <class... T>
0048 constexpr decltype(auto) tie_as_tuple_with_references(detail::sequence_tuple::tuple<T...>& t) noexcept {
0049     return detail::make_flat_tuple_of_references(t, sequence_tuple_getter{}, size_t_<0>{}, size_t_<sequence_tuple::tuple<T...>::size_v>{});
0050 }
0051 
0052 template <class... T>
0053 constexpr decltype(auto) tie_as_tuple_with_references(const detail::sequence_tuple::tuple<T...>& t) noexcept {
0054     return detail::make_flat_tuple_of_references(t, sequence_tuple_getter{}, size_t_<0>{}, size_t_<sequence_tuple::tuple<T...>::size_v>{});
0055 }
0056 
0057 template <class Tuple1, std::size_t... I1, class Tuple2, std::size_t... I2>
0058 constexpr auto my_tuple_cat_impl(const Tuple1& t1, std::index_sequence<I1...>, const Tuple2& t2, std::index_sequence<I2...>) noexcept {
0059     return detail::tie_as_tuple_with_references(
0060         sequence_tuple::get<I1>(t1)...,
0061         sequence_tuple::get<I2>(t2)...
0062     );
0063 }
0064 
0065 template <class Tuple1, class Tuple2>
0066 constexpr auto my_tuple_cat(const Tuple1& t1, const Tuple2& t2) noexcept {
0067     return detail::my_tuple_cat_impl(
0068         t1, detail::make_index_sequence< Tuple1::size_v >{},
0069         t2, detail::make_index_sequence< Tuple2::size_v >{}
0070     );
0071 }
0072 
0073 template <class TupleOrUserType, class Getter, std::size_t Begin, std::size_t Size>
0074 constexpr auto make_flat_tuple_of_references(TupleOrUserType& t, const Getter& g, size_t_<Begin>, size_t_<Size>) noexcept {
0075     constexpr std::size_t next_size = Size / 2;
0076     return detail::my_tuple_cat(
0077         detail::make_flat_tuple_of_references(t, g, size_t_<Begin>{}, size_t_<next_size>{}),
0078         detail::make_flat_tuple_of_references(t, g, size_t_<Begin + Size / 2>{}, size_t_<Size - next_size>{})
0079     );
0080 }
0081 
0082 template <class TupleOrUserType, class Getter, std::size_t Begin>
0083 constexpr sequence_tuple::tuple<> make_flat_tuple_of_references(TupleOrUserType&, const Getter&, size_t_<Begin>, size_t_<0>) noexcept {
0084     return {};
0085 }
0086 
0087 template <class TupleOrUserType, class Getter, std::size_t Begin>
0088 constexpr auto make_flat_tuple_of_references(TupleOrUserType& t, const Getter& g, size_t_<Begin>, size_t_<1>) noexcept {
0089     return detail::tie_as_tuple_with_references(
0090         g.get(t, size_t_<Begin>{})
0091     );
0092 }
0093 
0094 }}} // namespace boost::pfr::detail
0095 
0096 #endif // BOOST_PFR_DETAIL_MAKE_FLAT_TUPLE_OF_REFERENCES_HPP