From 6118acf67da8072d08e4a17455290fc5cdffb7b2 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 25 Mar 2021 16:36:54 +0100 Subject: [PATCH] Long live kdToContainer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is some code I have had in my Qt build for a while now. Given the barrier to put this into QtCore is too high, maybe it's useful to publish elsewhere, so here it is. Change-Id: I519d360920539ee2048536b4e4eeb706fe03d64f Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/94929 Tested-by: Continuous Integration Reviewed-by: Ivan Čukić --- README.md | 2 + cpp/cpp.pro | 2 + cpp/toContainer/README.md | 64 ++++++ cpp/toContainer/testcpp14/testcpp14.pro | 6 + .../testcpplatest/testcpplatest.pro | 10 + cpp/toContainer/toContainer.h | 189 ++++++++++++++++++ cpp/toContainer/toContainer.pro | 7 + cpp/toContainer/tst_toContainer.cpp | 128 ++++++++++++ 8 files changed, 408 insertions(+) create mode 100644 cpp/toContainer/README.md create mode 100644 cpp/toContainer/testcpp14/testcpp14.pro create mode 100644 cpp/toContainer/testcpplatest/testcpplatest.pro create mode 100644 cpp/toContainer/toContainer.h create mode 100644 cpp/toContainer/toContainer.pro create mode 100644 cpp/toContainer/tst_toContainer.cpp diff --git a/README.md b/README.md index 78cb09b..2d8a97e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ C++ Tools - [DuplicateTracker](https://github.com/KDAB/KDToolBox/tree/master/cpp/duplicatetracker) A wrapper around `std::unordered_set`, that transparently uses `std::pmr::monotonic_buffer_resource`, where available, to improve the set's memory allocation pattern. +- [toContainer](https://github.com/KDAB/KDToolBox/tree/master/cpp/toContainer) + Helper functions to convert a container to another container, using a convenient pipeline style. Qt Tools ================= diff --git a/cpp/cpp.pro b/cpp/cpp.pro index 5ea9e9e..ebd3809 100644 --- a/cpp/cpp.pro +++ b/cpp/cpp.pro @@ -3,3 +3,5 @@ SUBDIRS += \ duplicatetracker \ future-backports \ +versionAtLeast(QT_VERSION, 5.15):SUBDIRS += \ + toContainer \ diff --git a/cpp/toContainer/README.md b/cpp/toContainer/README.md new file mode 100644 index 0000000..cd3f720 --- /dev/null +++ b/cpp/toContainer/README.md @@ -0,0 +1,64 @@ +kdToContainer +============= + +**A minimal reimplementation of `ranges::to` from ranges-v3.** + +Qt 5.15 deprecated (and then Qt 6 removed) most of the converting +`toContainer()` functions on Qt containers (e.g. `QList::toSet()`, +converting to a `QSet`, has been deprecated). + +The target type gained a range constructor instead. Now, while using +such constructor is indeed suggested as the *direct* replacement of +calling the conversion function, it's more verbose to use, and +requires an extra line in case of the source container is returned from +a function or so. + +`kdToContainer` tries to bring back the convenience of the conversion +function, while being more general, as it works between any range-like +type (as the source) and any container-like type (as the destination): + +``` +using namespace KDToolBox::Ranges; + +QList myList = ~~~; + +// pipeline style +auto set1 = myList | kdToContainer(); // converts the QList to QSet +auto set2 = myList | kdToContainer>(); // ditto + +// function call style +auto set3 = kdToContainer(myList); // ditto +auto set4 = kdToContainer>(myList); // ditto + + +auto vec = set1 | kdToContainer(); // converts the QSet to QVector + + +QStringList getNames(); +auto nameset = getNames() | kdToContainer(); // converts to std::unordered_set + +``` + +--- + +Note that the underlying reason for deprecating such functionality in +Qt containers is not merely because there's an equivalent, more general +one (so the "redundant", "limited" approach got removed). + +*The real reason* is because most of the time conversions between +containers are hiding a design problem: usage of containers instead of +algorithms, poorly designed interfaces, and similar. + +Therefore: before reaching for a conversion between containers, try to +understand if your _design_ is sound! For instance, if you want to remove +duplicates from a sequential container, use `std::sort` + `std::unique`; +if you want to process elements avoiding dupicate elements (without +altering the source container), then use +[DuplicateTracker](https://github.com/KDAB/KDToolBox/tree/master/cpp/duplicatetracker); +and so on. + +**It's highly recommended to use ranges-v3 instead of this.** +The implementation offered here is knowlingly incomplete in some cases. +Use this only if ranges-v3 doesn't work for you. + +Requires a C++14 capable compiler. diff --git a/cpp/toContainer/testcpp14/testcpp14.pro b/cpp/toContainer/testcpp14/testcpp14.pro new file mode 100644 index 0000000..daf59e5 --- /dev/null +++ b/cpp/toContainer/testcpp14/testcpp14.pro @@ -0,0 +1,6 @@ +CONFIG += testcase +QT = core testlib +SOURCES = ../tst_toContainer.cpp +TARGET = tst_toContainer_cpp14 + +CONFIG += c++14 strict_c++ diff --git a/cpp/toContainer/testcpplatest/testcpplatest.pro b/cpp/toContainer/testcpplatest/testcpplatest.pro new file mode 100644 index 0000000..5242a9d --- /dev/null +++ b/cpp/toContainer/testcpplatest/testcpplatest.pro @@ -0,0 +1,10 @@ +CONFIG += testcase +QT = core testlib +SOURCES = ../tst_toContainer.cpp +TARGET = tst_toContainer_cpplatest + +CONFIG += c++17 strict_c++ + +# not 100% ideal as it looks at Qt's config rather than compiler capabilities, +# but qmake is qmake... +contains(QT_CONFIG, c++2a):CONFIG *= c++2a diff --git a/cpp/toContainer/toContainer.h b/cpp/toContainer/toContainer.h new file mode 100644 index 0000000..dba1418 --- /dev/null +++ b/cpp/toContainer/toContainer.h @@ -0,0 +1,189 @@ +/**************************************************************************** +** MIT License +** +** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com +** Author: Giuseppe D'Angelo +** +** This file is part of KDToolBox (https://github.com/KDAB/KDToolBox). +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, ** and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice (including the next paragraph) +** shall be included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF ** CONTRACT, TORT OR OTHERWISE, +** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +** DEALINGS IN THE SOFTWARE. +****************************************************************************/ + +#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()) ), + + // begin() yields an input iterator + std::enable_if_t< + std::is_convertible< + typename std::iterator_traits< decltype( std::begin(std::declval()) ) >::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::is_convertible< + decltype( std::begin(std::declval()) != std::end(std::declval()) ), + bool + >::value + > + > +> : std::true_type {}; + +template +using RangeValueT = typename std::iterator_traits< decltype( std::begin(std::declval()) ) >::value_type; + +template +using IsContainerConstructibleFromRange = std::is_constructible< + Container, + decltype( std::begin(std::declval()) ), + 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 +{ + template , + IsContainerConstructibleFromRange + >::value, bool> = true + > + friend constexpr auto operator|(const Range &r, ToContainerDummyPipelineParameter) + { + return AContainer(std::begin(r), std::end(r)); + } +}; + +template