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
|
#ifndef TERRACES_UTILS_HPP
#define TERRACES_UTILS_HPP
#include <cctype>
#include <iterator>
#include <utility>
namespace terraces {
namespace utils {
template <class T, class U = T>
T exchange(T& obj, U&& new_value) {
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
template <typename Iterator>
Iterator skip_ws(Iterator it, Iterator last) {
while (it != last and std::isspace(*it)) {
++it;
}
return it;
}
template <typename Iterator>
Iterator reverse_skip_ws(Iterator first, Iterator last) {
while (first != last) {
if (std::isspace(*std::prev(last))) {
--last;
} else {
break;
}
}
return last;
}
template <typename Exception, typename... Args>
void ensure(bool b, Args&&... args) {
if (not b) {
throw Exception{std::forward<Args>(args)...};
}
}
} // namespace utils
} // namespace terraces
#endif // TERRACES_UTILS_HPP
|