-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre.h
140 lines (115 loc) · 4.07 KB
/
pre.h
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
/*
Copyright 2023 Dennis Bautembach
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace pre {
namespace detail {
template <class T, class... Ts>
struct first {
using type = T;
};
template <class... Ts>
using first_t = typename first<Ts...>::type;
} // namespace detail
template <class T, auto Check>
struct cond {
T value;
template <class... Args>
constexpr cond(Args&&... args) noexcept(
std::is_nothrow_constructible_v<T, Args...>&& noexcept(Check(value)))
requires((sizeof...(Args) > 1) ||
sizeof...(Args) == 1 &&
!std::is_same_v<std::decay_t<T>,
std::decay_t<detail::first_t<Args...>>>)
: value(std::forward<Args>(args)...) {
Check(value);
}
constexpr cond(T value_) noexcept(
std::is_nothrow_constructible_v<T, T>&& noexcept(Check(value_)))
: value{std::forward<T>(value_)} {
Check(value);
}
cond(const cond&) = delete;
cond(cond&&) = delete;
cond& operator=(const cond&) = delete;
cond& operator=(cond&&) = delete;
constexpr operator T&() & noexcept { return value; }
constexpr operator T&&() && noexcept { return std::move(value); }
constexpr auto* operator->() noexcept { return &value; }
template <class U>
constexpr cond& operator=(U&& u)
requires(std::is_assignable_v<T, U>)
{
value = std::forward<U>(u);
return *this;
}
constexpr auto& operator[](std::size_t i)
requires(requires { value[std::size_t(0u)]; })
{
return value[i];
}
template <class... Args>
constexpr decltype(auto) operator()(Args&&... args)
requires(requires { value(std::forward<Args>(args)...); })
{
return value(std::forward<Args>(args)...);
}
};
namespace assert_on_fail {
template <class T>
using not_zero = cond<T, [](auto i) { assert(i != T{0}); }>;
template <class T>
using positive = cond<T, [](auto i) { assert(i > T{0}); }>;
template <class T>
using not_null = cond<T, [](auto* p) { assert(p != nullptr); }>;
template <class T>
using not_empty = cond<T, [](auto&& cont) { assert(!cont.empty()); }>;
template <class T>
using sorted = cond<T, [](auto&& cont) {
assert(std::is_sorted(cont.begin(), cont.end()));
}>;
} // namespace assert_on_fail
namespace throw_on_fail {
template <class T>
using not_zero = cond<T, [](auto i) {
if (i == T{0}) throw std::logic_error("Precondition 'not_zero' failed");
}>;
template <class T>
using positive = cond<T, [](auto i) {
if (!(i > T{0})) throw std::logic_error("Precondition 'positive' failed");
}>;
template <class T>
using not_null = cond<T, [](auto* p) {
if (p == nullptr) throw std::logic_error("Precondition 'not_null' failed");
}>;
template <class T>
using not_empty = cond<T, [](auto&& cont) {
if (cont.empty()) throw std::logic_error("Precondition 'not_empty' failed");
}>;
template <class T>
using sorted = cond<T, [](auto&& cont) {
if (!std::is_sorted(cont.begin(), cont.end()))
throw std::logic_error("Precondition 'sorted' failed");
}>;
} // namespace throw_on_fail
} // namespace pre