-
Notifications
You must be signed in to change notification settings - Fork 710
/
utils.hh
216 lines (179 loc) · 4.67 KB
/
utils.hh
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
210
211
212
213
214
215
216
#ifndef utils_hh_INCLUDED
#define utils_hh_INCLUDED
#include "assert.hh"
#include <memory>
namespace Kakoune
{
// *** Singleton ***
//
// Singleton helper class, every singleton type T should inherit
// from Singleton<T> to provide a consistent interface.
template<typename T>
class Singleton
{
public:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
static T& instance()
{
kak_assert(ms_instance);
return *static_cast<T*>(ms_instance);
}
static bool has_instance()
{
return ms_instance != nullptr;
}
protected:
Singleton()
{
kak_assert(ms_instance == nullptr);
ms_instance = this;
}
~Singleton()
{
kak_assert(ms_instance == this);
ms_instance = nullptr;
}
private:
static Singleton* ms_instance;
};
template<typename T>
Singleton<T>* Singleton<T>::ms_instance = nullptr;
// *** On scope end ***
//
// on_scope_end provides a way to register some code to be
// executed when current scope closes.
//
// usage:
// auto cleaner = on_scope_end([]() { ... });
//
// This permits to cleanup c-style resources without implementing
// a wrapping class
template<typename T>
class [[nodiscard]] OnScopeEnd
{
public:
[[gnu::always_inline]]
OnScopeEnd(T func) : m_valid{true}, m_func{std::move(func)} {}
[[gnu::always_inline]]
OnScopeEnd(OnScopeEnd&& other)
: m_valid{other.m_valid}, m_func{std::move(other.m_func)}
{ other.m_valid = false; }
[[gnu::always_inline]]
~OnScopeEnd() noexcept(noexcept(std::declval<T>()())) { if (m_valid) m_func(); }
private:
bool m_valid;
T m_func;
};
template<typename T>
OnScopeEnd<T> on_scope_end(T t)
{
return OnScopeEnd<T>{std::move(t)};
}
// bool that can be set (to true) multiple times, and will
// be false only when unset the same time;
struct NestedBool
{
void set() { m_count++; }
void unset() { kak_assert(m_count > 0); m_count--; }
operator bool() const { return m_count > 0; }
private:
int m_count = 0;
};
struct ScopedSetBool
{
ScopedSetBool(NestedBool& nested_bool, bool condition = true)
: m_nested_bool(nested_bool), m_condition(condition)
{
if (m_condition)
m_nested_bool.set();
}
ScopedSetBool(ScopedSetBool&& other)
: m_nested_bool(other.m_nested_bool), m_condition(other.m_condition)
{
other.m_condition = false;
}
~ScopedSetBool()
{
if (m_condition)
m_nested_bool.unset();
}
private:
NestedBool& m_nested_bool;
bool m_condition;
};
// *** Misc helper functions ***
template<typename T>
bool operator== (const std::unique_ptr<T>& lhs, T* rhs)
{
return lhs.get() == rhs;
}
template<typename T>
const T& clamp(const T& val, const T& min, const T& max)
{
return (val < min ? min : (val > max ? max : val));
}
template<typename Iterator, typename EndIterator, typename T>
bool skip_while(Iterator& it, const EndIterator& end, T condition)
{
while (it != end and condition(*it))
++it;
return it != end;
}
template<typename Iterator, typename BeginIterator, typename T>
bool skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition)
{
while (it != begin and condition(*it))
--it;
return condition(*it);
}
template<typename E>
auto to_underlying(E value)
{
return static_cast<std::underlying_type_t<E>>(value);
}
template<typename> class FunctionRef;
template<typename From, typename To>
concept ConvertibleTo = std::is_convertible_v<From, To>;
template<typename Res, typename... Args>
class FunctionRef<Res(Args...)>
{
public:
FunctionRef()
: m_target{nullptr},
m_invoker{[](void* target, Args... args) {
if constexpr (!std::is_same_v<Res, void>) return Res{};
}}
{}
template<typename Target>
requires requires (Target t, Args... a) {
requires not std::is_same_v<FunctionRef, std::remove_cvref_t<Target>>;
{ t(a...) } -> ConvertibleTo<Res>;
}
FunctionRef(Target&& target)
: m_target{&target},
m_invoker{[](void* target, Args... args) {
return (*reinterpret_cast<Target*>(target))(static_cast<Args>(args)...);
}}
{}
Res operator()(Args... args) const
{
return m_invoker(m_target, static_cast<Args>(args)...);
}
private:
using Invoker = Res (void*, Args...);
void* m_target;
Invoker* m_invoker;
};
template<typename... Funcs>
struct Overload : Funcs...
{
using Funcs::operator()...;
};
template<typename... Funcs>
auto overload(Funcs&&... funcs)
{
return Overload<std::remove_cvref_t<Funcs>...>{std::forward<Funcs>(funcs)...};
}
}
#endif // utils_hh_INCLUDED