1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QPAIR_H
41#define QPAIR_H
42
43#include <QtCore/qglobal.h>
44
45QT_BEGIN_NAMESPACE
46
47
48template <class T1, class T2>
49struct QPair
50{
51 typedef T1 first_type;
52 typedef T2 second_type;
53
54 Q_DECL_CONSTEXPR QPair()
55 noexcept((std::is_nothrow_default_constructible<T1>::value &&
56 std::is_nothrow_default_constructible<T2>::value))
57 : first(), second() {}
58 Q_DECL_CONSTEXPR QPair(const T1 &t1, const T2 &t2)
59 noexcept((std::is_nothrow_copy_constructible<T1>::value &&
60 std::is_nothrow_copy_constructible<T2>::value))
61 : first(t1), second(t2) {}
62 // compiler-generated copy/move ctor/assignment operators are fine!
63
64 template <typename TT1, typename TT2>
65 Q_DECL_CONSTEXPR QPair(const QPair<TT1, TT2> &p)
66 noexcept((std::is_nothrow_constructible<T1, TT1&>::value &&
67 std::is_nothrow_constructible<T2, TT2&>::value))
68 : first(p.first), second(p.second) {}
69 template <typename TT1, typename TT2>
70 Q_DECL_RELAXED_CONSTEXPR QPair &operator=(const QPair<TT1, TT2> &p)
71 noexcept((std::is_nothrow_assignable<T1, TT1&>::value &&
72 std::is_nothrow_assignable<T2, TT2&>::value))
73 { first = p.first; second = p.second; return *this; }
74 template <typename TT1, typename TT2>
75 Q_DECL_CONSTEXPR QPair(QPair<TT1, TT2> &&p)
76 noexcept((std::is_nothrow_constructible<T1, TT1>::value &&
77 std::is_nothrow_constructible<T2, TT2>::value))
78 // can't use std::move here as it's not constexpr in C++11:
79 : first(static_cast<TT1 &&>(p.first)), second(static_cast<TT2 &&>(p.second)) {}
80 template <typename TT1, typename TT2>
81 Q_DECL_RELAXED_CONSTEXPR QPair &operator=(QPair<TT1, TT2> &&p)
82 noexcept((std::is_nothrow_assignable<T1, TT1>::value &&
83 std::is_nothrow_assignable<T2, TT2>::value))
84 { first = std::move(p.first); second = std::move(p.second); return *this; }
85
86 Q_DECL_RELAXED_CONSTEXPR void swap(QPair &other)
87 noexcept(noexcept(qSwap(other.first, other.first)) && noexcept(qSwap(other.second, other.second)))
88 {
89 // use qSwap() to pick up ADL swaps automatically:
90 qSwap(first, other.first);
91 qSwap(second, other.second);
92 }
93
94 T1 first;
95 T2 second;
96};
97
98#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
99template<class T1, class T2>
100QPair(T1, T2) -> QPair<T1, T2>;
101#endif
102
103template <typename T1, typename T2>
104void swap(QPair<T1, T2> &lhs, QPair<T1, T2> &rhs) noexcept(noexcept(lhs.swap(rhs)))
105{ lhs.swap(rhs); }
106
107// mark QPair<T1,T2> as complex/movable/primitive depending on the
108// typeinfos of the constituents:
109template<class T1, class T2>
110class QTypeInfo<QPair<T1, T2> > : public QTypeInfoMerger<QPair<T1, T2>, T1, T2> {}; // Q_DECLARE_TYPEINFO
111
112template <class T1, class T2>
113Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator==(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
114 noexcept(noexcept(p1.first == p2.first && p1.second == p2.second))
115{ return p1.first == p2.first && p1.second == p2.second; }
116
117template <class T1, class T2>
118Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator!=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
119 noexcept(noexcept(!(p1 == p2)))
120{ return !(p1 == p2); }
121
122template <class T1, class T2>
123Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator<(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
124 noexcept(noexcept(p1.first < p2.first || (!(p2.first < p1.first) && p1.second < p2.second)))
125{
126 return p1.first < p2.first || (!(p2.first < p1.first) && p1.second < p2.second);
127}
128
129template <class T1, class T2>
130Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator>(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
131 noexcept(noexcept(p2 < p1))
132{
133 return p2 < p1;
134}
135
136template <class T1, class T2>
137Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator<=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
138 noexcept(noexcept(!(p2 < p1)))
139{
140 return !(p2 < p1);
141}
142
143template <class T1, class T2>
144Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator>=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
145 noexcept(noexcept(!(p1 < p2)))
146{
147 return !(p1 < p2);
148}
149
150template <class T1, class T2>
151Q_DECL_CONSTEXPR Q_OUTOFLINE_TEMPLATE QPair<T1, T2> qMakePair(const T1 &x, const T2 &y)
152 noexcept(noexcept(QPair<T1, T2>(x, y)))
153{
154 return QPair<T1, T2>(x, y);
155}
156
157QT_END_NAMESPACE
158
159#endif // QPAIR_H
160