1/****************************************************************************
2**
3** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
4** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#if 0
42#pragma qt_sync_skip_header_check
43#pragma qt_sync_stop_processing
44#endif
45
46#ifndef QCONTAINERTOOLS_IMPL_H
47#define QCONTAINERTOOLS_IMPL_H
48
49#include <QtCore/qglobal.h>
50#include <iterator>
51
52QT_BEGIN_NAMESPACE
53
54namespace QtPrivate
55{
56template <typename Iterator>
57using IfIsInputIterator = typename std::enable_if<
58 std::is_convertible<typename std::iterator_traits<Iterator>::iterator_category, std::input_iterator_tag>::value,
59 bool>::type;
60
61template <typename Iterator>
62using IfIsForwardIterator = typename std::enable_if<
63 std::is_convertible<typename std::iterator_traits<Iterator>::iterator_category, std::forward_iterator_tag>::value,
64 bool>::type;
65
66template <typename Iterator>
67using IfIsNotForwardIterator = typename std::enable_if<
68 !std::is_convertible<typename std::iterator_traits<Iterator>::iterator_category, std::forward_iterator_tag>::value,
69 bool>::type;
70
71template <typename Container,
72 typename InputIterator,
73 IfIsNotForwardIterator<InputIterator> = true>
74void reserveIfForwardIterator(Container *, InputIterator, InputIterator)
75{
76}
77
78template <typename Container,
79 typename ForwardIterator,
80 IfIsForwardIterator<ForwardIterator> = true>
81void reserveIfForwardIterator(Container *c, ForwardIterator f, ForwardIterator l)
82{
83 c->reserve(static_cast<typename Container::size_type>(std::distance(f, l)));
84}
85
86// for detecting expression validity
87template <typename ... T>
88using void_t = void;
89
90template <typename Iterator, typename = void_t<>>
91struct AssociativeIteratorHasKeyAndValue : std::false_type
92{
93};
94
95template <typename Iterator>
96struct AssociativeIteratorHasKeyAndValue<
97 Iterator,
98 void_t<decltype(std::declval<Iterator &>().key()),
99 decltype(std::declval<Iterator &>().value())>
100 >
101 : std::true_type
102{
103};
104
105template <typename Iterator, typename = void_t<>, typename = void_t<>>
106struct AssociativeIteratorHasFirstAndSecond : std::false_type
107{
108};
109
110template <typename Iterator>
111struct AssociativeIteratorHasFirstAndSecond<
112 Iterator,
113 void_t<decltype(std::declval<Iterator &>()->first),
114 decltype(std::declval<Iterator &>()->second)>
115 >
116 : std::true_type
117{
118};
119
120template <typename Iterator>
121using IfAssociativeIteratorHasKeyAndValue =
122 typename std::enable_if<AssociativeIteratorHasKeyAndValue<Iterator>::value, bool>::type;
123
124template <typename Iterator>
125using IfAssociativeIteratorHasFirstAndSecond =
126 typename std::enable_if<AssociativeIteratorHasFirstAndSecond<Iterator>::value, bool>::type;
127
128} // namespace QtPrivate
129
130QT_END_NAMESPACE
131
132#endif // QCONTAINERTOOLS_IMPL_H
133