#pragma once /* * Copyright (C) 2024 Brett Terpstra * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLT_ITERATOR_FLATTEN_H #define BLT_ITERATOR_FLATTEN_H #include #include #include namespace blt::iterator { namespace detail { template static auto flatten(Tuple&& tuple) -> decltype(auto) { using Decay = std::decay_t; if constexpr (meta::is_tuple_v || meta::is_pair_v) { return std::apply([](auto&&... args) { return std::tuple_cat(flatten(std::forward(args))...); }, std::forward(tuple)); } else { if constexpr (std::is_lvalue_reference_v) { return std::forward_as_tuple(std::forward(tuple)); } else { return std::make_tuple(std::forward(tuple)); } } } } template class flatten_wrapper : public deref_only_wrapper> { public: using iterator_category = typename std::iterator_traits::iterator_category; using value_type = std::remove_reference_t()))>; using difference_type = ptrdiff_t; using pointer = value_type*; using reference = value_type&; explicit flatten_wrapper(Iter iter): deref_only_wrapper(std::move(iter)) { } auto operator*() const -> decltype(auto) { return detail::flatten(*this->iter); } }; } #endif //BLT_ITERATOR_FLATTEN_H