/**************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 as published by the Free Software ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTBASE_GENERATION_HELPERS_H #define QTBASE_GENERATION_HELPERS_H #include "qglobal.h" #include struct tag_input { }; struct tag_mapped { }; struct tag_reduction { }; template struct SequenceItem { SequenceItem() = default; // bool as a stronger "explicit": should never be called inside of QtConcurrent SequenceItem(int val, bool) : value(val) { } bool operator==(const SequenceItem &other) const { return value == other.value; } bool isOdd() const { return value & 1; } void multiplyByTwo() { value *= 2; } int value = 0; }; template struct NoConstructSequenceItem { NoConstructSequenceItem() = delete; // bool as a stronger "explicit": should never be called inside of QtConcurrent NoConstructSequenceItem(int val, bool) : value(val) { } bool operator==(const NoConstructSequenceItem &other) const { return value == other.value; } bool isOdd() const { return value & 1; } void multiplyByTwo() { value *= 2; } int value = 0; }; template struct MoveOnlySequenceItem { MoveOnlySequenceItem() = default; ~MoveOnlySequenceItem() = default; MoveOnlySequenceItem(const MoveOnlySequenceItem &) = delete; MoveOnlySequenceItem(MoveOnlySequenceItem &&other) : value(other.value) { other.value = -1; } MoveOnlySequenceItem &operator=(const MoveOnlySequenceItem &) = delete; MoveOnlySequenceItem &operator=(MoveOnlySequenceItem &&other) { value = other.value; other.value = -1; } // bool as a stronger "explicit": should never be called inside of QtConcurrent MoveOnlySequenceItem(int val, bool) : value(val) { } bool operator==(const MoveOnlySequenceItem &other) const { return value == other.value; } bool isOdd() const { return value & 1; } void multiplyByTwo() { value *= 2; } int value = 0; }; template struct MoveOnlyNoConstructSequenceItem { MoveOnlyNoConstructSequenceItem() = delete; ~MoveOnlyNoConstructSequenceItem() = default; MoveOnlyNoConstructSequenceItem(const MoveOnlyNoConstructSequenceItem &) = delete; MoveOnlyNoConstructSequenceItem(MoveOnlyNoConstructSequenceItem &&other) : value(other.value) { other.value = -1; } MoveOnlyNoConstructSequenceItem &operator=(const MoveOnlyNoConstructSequenceItem &) = delete; MoveOnlyNoConstructSequenceItem &operator=(MoveOnlyNoConstructSequenceItem &&other) { value = other.value; other.value = -1; } // bool as a stronger "explicit": should never be called inside of QtConcurrent MoveOnlyNoConstructSequenceItem(int val, bool) : value(val) { } bool operator==(const MoveOnlyNoConstructSequenceItem &other) const { return value == other.value; } bool isOdd() const { return value & 1; } void multiplyByTwo() { value *= 2; } int value = 0; }; template bool myfilter(const T &el) { return el.isOdd(); } template class MyFilter { public: bool operator()(const T &el) { return el.isOdd(); } }; template class MyMoveOnlyFilter { bool movedFrom = false; public: MyMoveOnlyFilter() = default; MyMoveOnlyFilter(const MyMoveOnlyFilter &) = delete; MyMoveOnlyFilter &operator=(const MyMoveOnlyFilter &) = delete; MyMoveOnlyFilter(MyMoveOnlyFilter &&other) { other.movedFrom = true; } MyMoveOnlyFilter &operator=(MyMoveOnlyFilter &&other) { other.movedFrom = true; } bool operator()(const T &el) { if (!movedFrom) return el.isOdd(); else return -1; } }; template To myMap(const From &f) { return To(f.value * 2, true); } template void myInplaceMap(T &el) { el.multiplyByTwo(); } template class MyMap { public: To operator()(const From &el) { return To(el.value * 2, true); } }; template class MyInplaceMap { public: void operator()(T &el) { el.multiplyByTwo(); } }; template class MyMoveOnlyMap { bool movedFrom = false; public: MyMoveOnlyMap() = default; MyMoveOnlyMap(const MyMoveOnlyMap &) = delete; MyMoveOnlyMap &operator=(const MyMoveOnlyMap &) = delete; MyMoveOnlyMap(MyMoveOnlyMap &&other) { other.movedFrom = true; } MyMoveOnlyMap &operator=(MyMoveOnlyMap &&other) { other.movedFrom = true; } To operator()(const From &el) { if (!movedFrom) return To(el.value * 2, true); else return To(-1, true); } }; template class MyMoveOnlyInplaceMap { bool movedFrom = false; public: MyMoveOnlyInplaceMap() = default; MyMoveOnlyInplaceMap(const MyMoveOnlyInplaceMap &) = delete; MyMoveOnlyInplaceMap &operator=(const MyMoveOnlyInplaceMap &) = delete; MyMoveOnlyInplaceMap(MyMoveOnlyInplaceMap &&other) { other.movedFrom = true; } MyMoveOnlyInplaceMap &operator=(MyMoveOnlyInplaceMap &&other) { other.movedFrom = true; } void operator()(T &el) { if (!movedFrom) el.multiplyByTwo(); } }; template void myReduce(To &sum, const From &val) { sum.value += val.value; } template class MyReduce { public: void operator()(To &sum, const From &val) { sum.value += val.value; } }; template class MyMoveOnlyReduce { bool movedFrom = false; public: MyMoveOnlyReduce() = default; MyMoveOnlyReduce(const MyMoveOnlyReduce &) = delete; MyMoveOnlyReduce &operator=(const MyMoveOnlyReduce &) = delete; MyMoveOnlyReduce(MyMoveOnlyReduce &&other) { other.movedFrom = true; } MyMoveOnlyReduce &operator=(MyMoveOnlyReduce &&other) { other.movedFrom = true; } void operator()(To &sum, const From &val) { if (!movedFrom) sum.value += val.value; } }; QT_BEGIN_NAMESPACE // pretty printing template char *toString(const SequenceItem &i) { using QTest::toString; return toString(QString::number(i.value)); } // pretty printing template char *toString(const std::vector &vec) { using QTest::toString; QString result(""); for (const auto &i : vec) { result.append(QString::number(i.value) + ", "); } if (result.size()) result.chop(2); return toString(result); } QT_END_NAMESPACE #endif // QTBASE_GENERATION_HELPERS_H