1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
10#define TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
11
12#include <vector>
13
14#include "test_macros.h"
15
16template <class T>
17struct CopyOnlyVector : std::vector<T> {
18 using std::vector<T>::vector;
19
20 CopyOnlyVector(const CopyOnlyVector&) = default;
21 CopyOnlyVector(CopyOnlyVector&& other) : CopyOnlyVector(other) {}
22 CopyOnlyVector(CopyOnlyVector&& other, std::vector<T>::allocator_type alloc) : CopyOnlyVector(other, alloc) {}
23
24 CopyOnlyVector& operator=(const CopyOnlyVector&) = default;
25 CopyOnlyVector& operator=(CopyOnlyVector& other) { return this->operator=(other); }
26};
27
28template <class T>
29struct SillyReserveVector : std::vector<T> {
30 using std::vector<T>::vector;
31
32 void reserve(size_t) { this->clear(); }
33};
34
35template <class T, bool ConvertibleToT = false>
36struct Transparent {
37 T t;
38
39 explicit operator T() const
40 requires ConvertibleToT
41 {
42 return t;
43 }
44};
45
46template <class T>
47using ConvertibleTransparent = Transparent<T, true>;
48
49template <class T>
50using ExplicitlyConvertibleTransparent = Transparent<T, true>;
51
52template <class T>
53using NonConvertibleTransparent = Transparent<T, false>;
54
55struct TransparentComparator {
56 using is_transparent = void;
57
58 bool* transparent_used = nullptr;
59 TransparentComparator() = default;
60 TransparentComparator(bool& used) : transparent_used(&used) {}
61
62 template <class T, bool Convertible>
63 bool operator()(const T& t, const Transparent<T, Convertible>& transparent) const {
64 if (transparent_used != nullptr) {
65 *transparent_used = true;
66 }
67 return t < transparent.t;
68 }
69
70 template <class T, bool Convertible>
71 bool operator()(const Transparent<T, Convertible>& transparent, const T& t) const {
72 if (transparent_used != nullptr) {
73 *transparent_used = true;
74 }
75 return transparent.t < t;
76 }
77
78 template <class T>
79 bool operator()(const T& t1, const T& t2) const {
80 return t1 < t2;
81 }
82};
83
84struct NonTransparentComparator {
85 template <class T, bool Convertible>
86 bool operator()(const T&, const Transparent<T, Convertible>&) const;
87
88 template <class T, bool Convertible>
89 bool operator()(const Transparent<T, Convertible>&, const T&) const;
90
91 template <class T>
92 bool operator()(const T&, const T&) const;
93};
94
95struct NoDefaultCtr {
96 NoDefaultCtr() = delete;
97};
98
99class Moveable {
100 int int_;
101 double double_;
102
103public:
104 Moveable() : int_(0), double_(0) {}
105 Moveable(int i, double d) : int_(i), double_(d) {}
106 Moveable(Moveable&& x) : int_(x.int_), double_(x.double_) {
107 x.int_ = -1;
108 x.double_ = -1;
109 }
110 Moveable& operator=(Moveable&& x) {
111 int_ = x.int_;
112 x.int_ = -1;
113 double_ = x.double_;
114 x.double_ = -1;
115 return *this;
116 }
117
118 Moveable(const Moveable&) = delete;
119 Moveable& operator=(const Moveable&) = delete;
120 bool operator==(const Moveable& x) const { return int_ == x.int_ && double_ == x.double_; }
121 bool operator<(const Moveable& x) const { return int_ < x.int_ || (int_ == x.int_ && double_ < x.double_); }
122
123 int get() const { return int_; }
124 bool moved() const { return int_ == -1; }
125};
126
127#ifndef TEST_HAS_NO_EXCEPTIONS
128template <class T>
129struct EmplaceUnsafeContainer : std::vector<T> {
130 using std::vector<T>::vector;
131
132 template <class... Args>
133 auto emplace(Args&&... args) -> decltype(std::declval<std::vector<T>>().emplace(std::forward<Args>(args)...)) {
134 if (this->size() > 1) {
135 auto it1 = this->begin();
136 auto it2 = it1 + 1;
137 // messing up the container
138 std::iter_swap(it1, it2);
139 }
140
141 throw 42;
142 }
143
144 template <class... Args>
145 auto insert(Args&&... args) -> decltype(std::declval<std::vector<T>>().insert(std::forward<Args>(args)...)) {
146 if (this->size() > 1) {
147 auto it1 = this->begin();
148 auto it2 = it1 + 1;
149 // messing up the container
150 std::iter_swap(it1, it2);
151 }
152
153 throw 42;
154 }
155
156 template <class... Args>
157 auto insert_range(Args&&... args)
158 -> decltype(std::declval<std::vector<T>>().insert_range(std::forward<Args>(args)...)) {
159 if (this->size() > 1) {
160 auto it1 = this->begin();
161 auto it2 = it1 + 1;
162 // messing up the container
163 std::iter_swap(it1, it2);
164 }
165
166 throw 42;
167 }
168};
169
170template <class T>
171struct ThrowOnEraseContainer : std::vector<T> {
172 using std::vector<T>::vector;
173
174 template <class... Args>
175 auto erase(Args&&... args) -> decltype(std::declval<std::vector<T>>().erase(std::forward<Args>(args)...)) {
176 throw 42;
177 }
178};
179
180template <class T>
181struct ThrowOnMoveContainer : std::vector<T> {
182 using std::vector<T>::vector;
183
184 ThrowOnMoveContainer(ThrowOnMoveContainer&&) { throw 42; }
185
186 ThrowOnMoveContainer& operator=(ThrowOnMoveContainer&&) { throw 42; }
187};
188
189#endif // TEST_HAS_NO_EXCEPTIONS
190
191#endif // TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of libcxx/test/std/containers/container.adaptors/flat_helpers.h