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
|
/*
* Copyright © 2020 Christian Persch
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <algorithm>
#include <exception>
#include <type_traits>
#include <memory>
namespace vte {
// This is like std::clamp, except that it doesn't throw when
// max_v<min_v, but instead returns min_v in that case.
template<typename T>
constexpr inline T const&
clamp(T const& v,
T const& min_v,
T const& max_v)
{
return std::max(std::min(v, max_v), min_v);
}
// Converts from E to the underlying integral type, where E is an enum
// with integral underlying type.
template<typename E>
inline constexpr auto to_integral(E e) noexcept
-> std::enable_if_t<std::is_enum_v<E> &&
std::is_integral_v<std::underlying_type_t<E>>,
std::underlying_type_t<E>>
{
return static_cast<std::underlying_type_t<E>>(e);
}
#ifdef VTE_DEBUG
void log_exception(char const* func = __builtin_FUNCTION(),
char const* filename = __builtin_FILE(),
int const line = __builtin_LINE()) noexcept;
#else
inline void log_exception() noexcept;
#endif
template <typename T, typename D, D func>
class FreeablePtrDeleter {
public:
void operator()(T* obj) const
{
if (obj)
func(obj);
}
};
template <typename T, typename D, D func>
using FreeablePtr = std::unique_ptr<T, FreeablePtrDeleter<T, D, func>>;
} // namespace vte
|