Short live the Qt implementation of the Detection Idiom

These never made it into the IS, but are still available in the
Library Fundamentals TSs.

Use them to simplify detection code in qcontainertools_impl.h.

Change-Id: Iee83bc93c2abca34446d31912a2e04f6e3fe71de
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2019-12-13 19:32:06 +01:00 committed by Fabian Kosmale
parent d47061f62f
commit 29e0cca0ed
3 changed files with 77 additions and 29 deletions

View File

@ -104,6 +104,7 @@ qt_internal_add_module(Core
global/q23functional.h
global/q20iterator.h
global/qxpfunctional.h
global/qxptype_traits.h
io/qabstractfileengine.cpp io/qabstractfileengine_p.h
io/qbuffer.cpp io/qbuffer.h
io/qdataurl.cpp io/qdataurl_p.h

View File

@ -0,0 +1,63 @@
// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QXPTYPE_TRAITS_H
#define QXPTYPE_TRAITS_H
#include <QtCore/qtconfigmacros.h>
#include <type_traits>
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. Types and functions defined in this
// file can reliably be replaced by their std counterparts, once available.
// You may use these definitions in your own code, but be aware that we
// will remove them once Qt depends on the C++ version that supports
// them in namespace std. There will be NO deprecation warning, the
// definitions will JUST go away.
//
// If you can't agree to these terms, don't use these definitions!
//
// We mean it.
//
QT_BEGIN_NAMESPACE
// like std::experimental::{nonesuch,is_detected/_v}(LFTSv2)
namespace qxp {
struct nonesuch {
~nonesuch() = delete;
nonesuch(const nonesuch&) = delete;
void operator=(const nonesuch&) = delete;
};
namespace _detail {
template <typename T, typename Void, template <typename...> class Op, typename...Args>
struct detector {
using value_t = std::false_type;
using type = T;
};
template <typename T, template <typename...> class Op, typename...Args>
struct detector<T, std::void_t<Op<Args...>>, Op, Args...> {
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace _detail
template <template <typename...> class Op, typename...Args>
using is_detected = typename _detail::detector<qxp::nonesuch, void, Op, Args...>::value_t;
template <template <typename...> class Op, typename...Args>
constexpr inline bool is_detected_v = is_detected<Op, Args...>::value;
} // namespace qxp
QT_END_NAMESPACE
#endif // QXPTYPE_TRAITS_H

View File

@ -14,6 +14,8 @@
#include <QtCore/qglobal.h>
#include <QtCore/qtypeinfo.h>
#include <QtCore/qxptype_traits.h>
#include <cstring>
#include <iterator>
#include <memory>
@ -203,43 +205,25 @@ void reserveIfForwardIterator(Container *c, ForwardIterator f, ForwardIterator l
c->reserve(static_cast<typename Container::size_type>(std::distance(f, l)));
}
template <typename Iterator, typename = std::void_t<>>
struct AssociativeIteratorHasKeyAndValue : std::false_type
{
};
template <typename Iterator>
using KeyAndValueTest = decltype(
std::declval<Iterator &>().key(),
std::declval<Iterator &>().value()
);
template <typename Iterator>
struct AssociativeIteratorHasKeyAndValue<
Iterator,
std::void_t<decltype(std::declval<Iterator &>().key()),
decltype(std::declval<Iterator &>().value())>
>
: std::true_type
{
};
template <typename Iterator, typename = std::void_t<>, typename = std::void_t<>>
struct AssociativeIteratorHasFirstAndSecond : std::false_type
{
};
template <typename Iterator>
struct AssociativeIteratorHasFirstAndSecond<
Iterator,
std::void_t<decltype(std::declval<Iterator &>()->first),
decltype(std::declval<Iterator &>()->second)>
>
: std::true_type
{
};
using FirstAndSecondTest = decltype(
std::declval<Iterator &>()->first,
std::declval<Iterator &>()->second
);
template <typename Iterator>
using IfAssociativeIteratorHasKeyAndValue =
typename std::enable_if<AssociativeIteratorHasKeyAndValue<Iterator>::value, bool>::type;
std::enable_if_t<qxp::is_detected_v<KeyAndValueTest, Iterator>, bool>;
template <typename Iterator>
using IfAssociativeIteratorHasFirstAndSecond =
typename std::enable_if<AssociativeIteratorHasFirstAndSecond<Iterator>::value, bool>::type;
std::enable_if_t<qxp::is_detected_v<FirstAndSecondTest, Iterator>, bool>;
template <typename T, typename U>
using IfIsNotSame =