forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.hpp
More file actions
209 lines (173 loc) · 5.21 KB
/
Copy pathhelpers.hpp
File metadata and controls
209 lines (173 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef TEST_HELPER_H_
#define TEST_HELPER_H_
#include <internal/iterbase.hpp>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace itertest {
// non-copyable. non-movable. non-default-constructible
class SolidInt {
private:
const int i;
public:
constexpr SolidInt(int n) : i{n} {}
constexpr int getint() const {
return this->i;
}
SolidInt() = delete;
SolidInt(const SolidInt&) = delete;
SolidInt& operator=(const SolidInt&) = delete;
SolidInt& operator=(SolidInt&&) = delete;
SolidInt(SolidInt&&) = delete;
};
namespace {
struct DoubleDereferenceError : std::exception {
const char* what() const noexcept override {
return "Iterator dereferenced twice without increment";
}
};
// this class's iterator will throw if it's dereference twice without
// an increment in between
class InputIterable {
public:
class Iterator {
private:
int i;
bool was_incremented = true;
public:
Iterator(int n) : i{n} {}
Iterator& operator++() {
++this->i;
this->was_incremented = true;
return *this;
}
int operator*() {
if (!this->was_incremented) {
throw DoubleDereferenceError{};
}
this->was_incremented = false;
return this->i;
}
bool operator!=(const Iterator& other) const {
return this->i != other.i;
}
};
Iterator begin() {
return {0};
}
Iterator end() {
return {5};
}
};
}
// BasicIterable provides a minimal forward iterator
// operator++(), operator!=(const BasicIterable&), operator*()
// move constructible only
// not copy constructible, move assignable, or copy assignable
template <typename T>
class BasicIterable {
private:
T* data;
std::size_t size;
bool was_moved_from_ = false;
bool was_copied_from_ = false;
public:
BasicIterable(std::initializer_list<T> il)
: data{new T[il.size()]}, size{il.size()} {
// would like to use enumerate, can't because it's for unit
// testing enumerate
std::size_t i = 0;
for (auto&& e : il) {
data[i] = e;
++i;
}
}
BasicIterable& operator=(BasicIterable&&) = delete;
BasicIterable& operator=(const BasicIterable&) = delete;
BasicIterable(const BasicIterable&) = delete;
#if 0
BasicIterable(const BasicIterable& other)
: data{new T[other.size()]},
size{other.size}
{
for (auto it = this->begin(), o_it = other.begin();
o_it != other.end();
++it, ++o_it) {
*it = *o_it;
}
}
#endif
BasicIterable(BasicIterable&& other) : data{other.data}, size{other.size} {
other.data = nullptr;
other.was_moved_from_ = true;
}
bool was_moved_from() const {
return this->was_moved_from_;
}
bool was_copied_from() const {
return this->was_copied_from_;
}
~BasicIterable() {
delete[] this->data;
}
class Iterator {
private:
T* p;
public:
#ifdef DEFINE_DEFAULT_ITERATOR_CTOR
Iterator() = default;
#endif
Iterator(T* b) : p{b} {}
bool operator!=(const Iterator& other) const {
return this->p != other.p;
}
Iterator& operator++() {
++this->p;
return *this;
}
T& operator*() {
return *this->p;
}
};
Iterator begin() {
return {this->data};
}
Iterator end() {
return {this->data + this->size};
}
#ifdef DECLARE_REVERSE_ITERATOR
Iterator rbegin();
Iterator rend();
#endif // ifdef DECLARE_REVERSE_ITERATOR
};
using iter::impl::void_t;
template <typename, typename = void>
struct IsIterator : std::false_type {};
template <typename T>
struct IsIterator<T,
void_t<decltype(T(std::declval<const T&>())), // copyctor
decltype(std::declval<T&>() =
std::declval<const T&>()), // copy =
decltype(*std::declval<T&>()), // operator*
decltype(
std::declval<T&>().operator->()), // operator->
decltype(++std::declval<T&>()), // prefix ++
decltype(std::declval<T&>()++), // postfix ++
decltype(std::declval<const T&>()
!= std::declval<const T&>()), // !=
decltype(std::declval<const T&>()
== std::declval<const T&>()) // ==
>> : std::true_type {};
template <typename T>
struct IsForwardIterator
: std::integral_constant<bool,
IsIterator<T>::value && std::is_default_constructible<T>::value> {};
template <typename T>
struct IsMoveConstructibleOnly
: std::integral_constant<bool,
!std::is_copy_constructible<T>::value
&& !std::is_copy_assignable<T>::value
&& !std::is_move_assignable<T>::value
&& std::is_move_constructible<T>::value> {};
}
#endif