/* This file is part of KDToolBox. SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company Author: Giuseppe D'Angelo SPDX-License-Identifier: MIT */ #ifndef KDTOOLBOX_RANGES_TOCONTAINER_H #define KDTOOLBOX_RANGES_TOCONTAINER_H #include #include namespace KDToolBox { namespace Ranges { namespace Private { // C++17 has these, but we limit ourselves to C++14 #ifdef __cpp_lib_void_t using std::void_t; #else template using void_t = void; #endif #ifdef __cpp_lib_logical_traits using std::conjunction; #else template struct conjunction : std::true_type { }; template struct conjunction : B { }; template struct conjunction : std::conditional_t, B> { }; #endif // Miscellanea type traits used below to SFINAE our functions/operators. // // WARNING. None of these traits are // 1. 100% accurate // 2. matching what std::ranges do // 3. particularly optimized to make compilation faster and reduce instantiations // // In a nutshell, they're "good enough". // If you want the real deal, use ranges-v3 and its to() function. template struct IsRange : std::false_type { }; template struct IsRange< T, void_t< // it's got begin() decltype(std::begin(std::declval())), // begin() yields an input iterator std::enable_if_t()))>::iterator_category, std::input_iterator_tag>::value>, // it's got end() decltype(std::end(std::declval())), // begin() != end() is OK and convertible to bool std::enable_if_t()) != std::end(std::declval())), bool>::value>>> : std::true_type { }; template using RangeValueT = typename std::iterator_traits()))>::value_type; template using IsContainerConstructibleFromRange = std::is_constructible())), decltype(std::end(std::declval()))>; // There are two implementations: one for container classes and one for // container class templates. The code is pretty much identical. // // We happily assume the container will reserve() in its // constructor if it detects a forward iterator. This wouldn't be // acceptable in general, but see above, we're aiming for "good enough" // here. // Pipeline operators template struct ToContainerDummyPipelineParameter; struct ToContainerDummyPipelineParameterBase { template, IsContainerConstructibleFromRange>::value, bool> = true> friend constexpr auto operator|(const Range &r, ToContainerDummyPipelineParameter) { return Container(std::begin(r), std::end(r)); } }; template struct ToContainerDummyPipelineParameter : ToContainerDummyPipelineParameterBase { }; template typename C> struct ToContainerTemplateDummyPipelineParameter; struct ToContainerTemplateDummyPipelineParameterBase { template class C, std::enable_if_t< conjunction, IsContainerConstructibleFromRange>, Range>>::value, bool> = true> friend constexpr auto operator|(const Range &r, ToContainerTemplateDummyPipelineParameter) { return C>(std::begin(r), std::end(r)); } }; template typename C> struct ToContainerTemplateDummyPipelineParameter : ToContainerTemplateDummyPipelineParameterBase { }; } // namespace Private // *** PUBLIC API *** // Pipeline objects template constexpr auto kdToContainer() noexcept { return Private::ToContainerDummyPipelineParameter{}; } template class C> constexpr auto kdToContainer() noexcept { return Private::ToContainerTemplateDummyPipelineParameter{}; } // Converting functions template::value, bool> = true, std::enable_if_t::value, bool> = true> constexpr auto kdToContainer(const Range &r) { return Container(std::begin(r), std::end(r)); } template class C, typename Range, std::enable_if_t::value, bool> = true, std::enable_if_t>, Range>::value, bool> = true> constexpr auto kdToContainer(const Range &r) { return C>(std::begin(r), std::end(r)); } } // namespace Ranges } // namespace KDToolBox #endif // KDTOOLBOX_RANGES_TOCONTAINER_H