/**************************************************************************** ** ** Copyright (C) 2018 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt Creator. ** ** 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. ** ****************************************************************************/ #pragma once #include "algorithm.h" #include namespace Utils { template using ValueType = typename C::value_type; template using PointerType = typename C::value_type::element_type*; ////////////////// // anyOf ///////////////// template bool anyOf(const C &container, PointerType p) { return anyOf(container, [p](const ValueType &v) { return v.get() == p; }); } template bool anyOf(const C &container, std::nullptr_t) { return anyOf(container, static_cast>(nullptr)); } ////////////////// // count ///////////////// template int count(const C &container, PointerType p) { return count(container, [p](const ValueType &v) { return v.get() == p; }); } template int count(const C &container, std::nullptr_t) { return count(container, static_cast>(nullptr)); } ////////////////// // allOf ///////////////// template bool allOf(const C &container, PointerType p) { return allOf(container, [p](const ValueType &v) { return v.get() == p; }); } template int allOf(const C &container, std::nullptr_t) { return allOf(container, static_cast>(nullptr)); } ////////////////// // erase ///////////////// template void erase(C &container, PointerType p) { return erase(container, [p](const ValueType &v) { return v.get() == p; }); } template int erase(const C &container, std::nullptr_t) { return erase(container, static_cast>(nullptr)); } ////////////////// // contains ///////////////// template bool contains(const C &container, PointerType p) { return anyOf(container, p); } template bool contains(const C &container, std::nullptr_t) { return anyOf(container, nullptr); } ////////////////// // findOr ///////////////// template Q_REQUIRED_RESULT PointerType findOr(const C &container, PointerType other, F function) { typename C::const_iterator begin = std::begin(container); typename C::const_iterator end = std::end(container); typename C::const_iterator it = std::find_if(begin, end, function); return it == end ? other : it->get(); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, PointerType other, R (S::*function)() const) { return findOr(container, other, std::mem_fn(function)); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, PointerType other, R S::*member) { return findOr(container, other, std::mem_fn(member)); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, std::nullptr_t, F function) { return findOr(container, static_cast>(nullptr), function); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, std::nullptr_t, R (S::*function)() const) { return findOr(container, static_cast>(nullptr), function); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, std::nullptr_t, R S::*member) { return findOr(container, static_cast>(nullptr), member); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, PointerType other, PointerType p) { return findOr(container, other, [p](const ValueType &v) { return v.get() == p; }); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, PointerType other, std::nullptr_t) { return findOr(container, other, static_cast>(nullptr)); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, std::nullptr_t, PointerType p) { return findOr(container, static_cast>(nullptr), p); } template Q_REQUIRED_RESULT PointerType findOr(const C &container, std::nullptr_t, std::nullptr_t) { return findOr(container, static_cast>(nullptr), static_cast>(nullptr)); } ////////////////// // findOrDefault ///////////////// template Q_REQUIRED_RESULT PointerType findOrDefault(const C &container, F function) { return findOr(container, static_cast>(nullptr), function); } template Q_REQUIRED_RESULT PointerType findOrDefault(const C &container, R (S::*function)() const) { return findOr(container, static_cast>(nullptr), std::mem_fn(function)); } template Q_REQUIRED_RESULT PointerType findOrDefault(const C &container, R S::*member) { return findOr(container, static_cast>(nullptr), std::mem_fn(member)); } template Q_REQUIRED_RESULT PointerType findOrDefault(const C &container, PointerType p) { return findOr(container, static_cast>(nullptr), p); } template Q_REQUIRED_RESULT PointerType findOrDefault(const C &container, std::nullptr_t) { return findOr(container, static_cast>(nullptr), static_cast>(nullptr)); } ////////////////// // index of: ////////////////// template Q_REQUIRED_RESULT int indexOf(const C& container, PointerType p) { return indexOf(container, [p](const ValueType &v) { return v.get() == p; }); } template Q_REQUIRED_RESULT int indexOf(const C& container, std::nullptr_t) { return indexOf(container, static_cast>(nullptr)); } ////////////////// // toRawPointer ///////////////// template ResultContainer toRawPointer(const SourceContainer &sources) { return transform(sources, [] (const auto &pointer) { return pointer.get(); }); } template class ResultContainer, template class SourceContainer, typename... SCArgs> auto toRawPointer(const SourceContainer &sources) { return transform &>(sources, [] (const auto &pointer) { return pointer.get(); }); } template auto toRawPointer(const SourceContainer &sources) { return transform(sources, [] (const auto &pointer) { return pointer.get(); }); } ////////////////// // take: ///////////////// template Q_REQUIRED_RESULT optional> take(C &container, PointerType p) { return take(container, [p](const ValueType &v) { return v.get() == p; }); } template Q_REQUIRED_RESULT optional> take(C &container, std::nullptr_t) { return take(container, static_cast>(nullptr)); } ////////////////// // takeOrDefault: ///////////////// template Q_REQUIRED_RESULT ValueType takeOrDefault(C &container, PointerType p) { auto result = take(container, [p](const ValueType &v) { return v.get() == p; }); return bool(result) ? std::move(result.value()) : std::move(ValueType()); } template Q_REQUIRED_RESULT ValueType takeOrDefault(C &container, std::nullptr_t) { return takeOrDefault(container, static_cast>(nullptr)); } } // namespace Utils