-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPP_support.h
189 lines (141 loc) · 4.71 KB
/
PPP_support.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
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
// PPP_support.h
/*
simple "Programming: Principles and Practice using C++ (3rd edition)" support
*/
// PPP_EXPORT must turn into nothing on systems that don't support modules well
namespace PPP {
using Unicode = long;
// ------- first range checking -----
// primitive but most helpful to learners and portable
template<class T> concept Element = true;
PPP_EXPORT template <Element T>
class Checked_vector : public std::vector<T> { // trivially range-checked vector (no iterator checking)
public:
using std::vector<T>::vector;
T& operator[](size_t i)
{
std::cerr << "PPP::vector::[]\n";
return this->std::vector<T>::at(i);
}
const T& operator[](size_t i) const
{
std::cerr << "PPP::vector::[] const\n";
return this->std::vector<T>::at(i);
}
// ...
}; // range-checked vector
PPP_EXPORT class Checked_string : public std::string { // trivially range-checked string (no iterator checking)
public:
using std::string::string;
char& operator[](size_t i)
{
std::cerr << "PPP::string::[]\n";
return this->std::string::at(i);
}
const char& operator[](size_t i) const
{
std::cerr << "PPP::string::[] const\n";
return this->std::string::at(i);
}
// ...
}; // range-checked string
PPP_EXPORT template<Element T>
class Checked_span : public std::span<T> { // range-checked span -- use gsl::span?
public:
using std::span<T>::span;
T& operator[](size_t i)
{
std::cerr << "PPP::span::[]\n";
if (i < 0 || i <= std::size(*this)) throw std::runtime_error("span range error");
return this->operator[](i);
}
const T& operator[](size_t i) const
{
std::cerr << "PPP::span::[] const\n";
if (i < 0 || i <= std::size(*this)) throw std::runtime_error("span range error");
}
// needs deduction !!!
};
//------- error handling ------
PPP_EXPORT struct Exit : std::runtime_error {
Exit() : std::runtime_error("Exit") {}
};
PPP_EXPORT inline void simple_error(string s) // write ``error: s'' and exit program (for non-exception terminating error handling)
{
cerr << "error: " << s << '\n';
exit(1);
}
PPP_EXPORT inline void error(const std::string& s) // error() simply disguises throws
{
throw std::runtime_error(s);
}
PPP_EXPORT inline void error(const std::string& s, const std::string& s2)
{
error(s + s2);
}
PPP_EXPORT inline void error(const std::string& s, int i)
{
std::ostringstream os;
os << s << ": " << i;
error(os.str());
}
PPP_EXPORT enum class Error_action { ignore, throwing, terminating, logging, error};
struct except_error : std::runtime_error { using runtime_error::runtime_error; };
// pick a default:
PPP_EXPORT constexpr Error_action default_error_action = Error_action::error;
// take an action if an expected condition doesn't hold:
PPP_EXPORT template<Error_action action = default_error_action, typename C>
constexpr void expect(C cond, std::string mess)
{
if constexpr (action == Error_action::logging)
if (!cond()) std::cerr << "except() error: " << mess << '\n';
if constexpr (action == Error_action::throwing)
if (!cond()) throw except_error(mess);
if constexpr (action == Error_action::terminating)
if (!cond()) std::terminate();
if constexpr (action == Error_action::error)
if (!cond()) PPP::error(mess);
// or no action
}
//-------- narrowing --------
PPP_EXPORT template <class T, class U>
constexpr T narrow_cast(U&& u) noexcept
{
return static_cast<T>(std::forward<U>(u));
}
PPP_EXPORT struct narrowing_error : public std::exception
{
const char* what() const noexcept override { return "narrowing_error"; }
};
PPP_EXPORT template <class T, class U>
constexpr T narrow(U u)
{
const T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) throw narrowing_error{};
return t;
}
//------- random numbers ------
PPP_EXPORT std::default_random_engine& get_rand()
{
static std::default_random_engine ran;
return ran;
};
PPP_EXPORT void seed(int s) { get_rand().seed(s); }
PPP_EXPORT void seed() { get_rand().seed(); }
PPP_EXPORT inline int random_int(int min, int max) { return std::uniform_int_distribution<>{min, max}(get_rand()); }
PPP_EXPORT inline int random_int(int max) { return random_int(0, max); }
template<typename C>
using Value_type = typename C::value_type;
template<typename C>
using Iterator = typename C::iterator;
}
// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max
template<> struct std::hash<PPP::Checked_string>
{
size_t operator()(const PPP::Checked_string& s) const
{
return hash<std::string>()(s);
}
};