utils.hh (4644B)
1 #ifndef utils_hh_INCLUDED 2 #define utils_hh_INCLUDED 3 4 #include "assert.hh" 5 6 #include <utility> 7 #include <type_traits> 8 9 namespace Kakoune 10 { 11 12 // *** Singleton *** 13 // 14 // Singleton helper class, every singleton type T should inherit 15 // from Singleton<T> to provide a consistent interface. 16 template<typename T> 17 class Singleton 18 { 19 public: 20 Singleton(const Singleton&) = delete; 21 Singleton& operator=(const Singleton&) = delete; 22 23 static T& instance() 24 { 25 kak_assert(ms_instance); 26 return *static_cast<T*>(ms_instance); 27 } 28 29 static bool has_instance() 30 { 31 return ms_instance != nullptr; 32 } 33 34 protected: 35 Singleton() 36 { 37 kak_assert(ms_instance == nullptr); 38 ms_instance = this; 39 } 40 41 ~Singleton() 42 { 43 kak_assert(ms_instance == this); 44 ms_instance = nullptr; 45 } 46 47 private: 48 static Singleton* ms_instance; 49 }; 50 51 template<typename T> 52 Singleton<T>* Singleton<T>::ms_instance = nullptr; 53 54 // *** On scope end *** 55 // 56 // OnScopeEnd provides a way to register some code to be 57 // executed when current scope closes. 58 // 59 // usage: 60 // auto cleaner = OnScopeEnd([]() { ... }); 61 // 62 // This permits to cleanup c-style resources without implementing 63 // a wrapping class 64 template<typename T> 65 class [[nodiscard]] OnScopeEnd 66 { 67 public: 68 [[gnu::always_inline]] 69 OnScopeEnd(T func) : m_valid{true}, m_func{std::move(func)} {} 70 71 [[gnu::always_inline]] 72 OnScopeEnd(OnScopeEnd&& other) 73 : m_valid{other.m_valid}, m_func{std::move(other.m_func)} 74 { other.m_valid = false; } 75 76 [[gnu::always_inline]] 77 void trigger() { if (m_valid) m_func(); m_valid = false; } 78 79 [[gnu::always_inline]] 80 ~OnScopeEnd() noexcept(noexcept(m_func())) { if (m_valid) m_func(); } 81 82 private: 83 bool m_valid; 84 T m_func; 85 }; 86 87 // bool that can be set (to true) multiple times, and will 88 // be false only when unset the same time; 89 struct NestedBool 90 { 91 void set() { m_count++; } 92 void unset() { kak_assert(m_count > 0); m_count--; } 93 94 operator bool() const { return m_count > 0; } 95 private: 96 int m_count = 0; 97 }; 98 99 struct ScopedSetBool 100 { 101 ScopedSetBool(NestedBool& nested_bool, bool condition = true) 102 : m_nested_bool(nested_bool), m_condition(condition) 103 { 104 if (m_condition) 105 m_nested_bool.set(); 106 } 107 108 ScopedSetBool(ScopedSetBool&& other) 109 : m_nested_bool(other.m_nested_bool), m_condition(other.m_condition) 110 { 111 other.m_condition = false; 112 } 113 114 ~ScopedSetBool() 115 { 116 if (m_condition) 117 m_nested_bool.unset(); 118 } 119 120 private: 121 NestedBool& m_nested_bool; 122 bool m_condition; 123 }; 124 125 template<typename T> 126 const T& clamp(const T& val, const T& min, const T& max) 127 { 128 return (val < min ? min : (val > max ? max : val)); 129 } 130 131 template<typename Iterator, typename EndIterator, typename T> 132 bool skip_while(Iterator& it, const EndIterator& end, T condition) 133 { 134 while (it != end and condition(*it)) 135 ++it; 136 return it != end; 137 } 138 139 template<typename Iterator, typename BeginIterator, typename T> 140 bool skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition) 141 { 142 while (it != begin and condition(*it)) 143 --it; 144 return condition(*it); 145 } 146 147 template<typename E> 148 auto to_underlying(E value) 149 { 150 return static_cast<std::underlying_type_t<E>>(value); 151 } 152 153 template<typename> class FunctionRef; 154 155 template<typename From, typename To> 156 concept ConvertibleTo = std::is_convertible_v<From, To>; 157 158 template<typename Res, typename... Args> 159 class FunctionRef<Res(Args...)> 160 { 161 public: 162 FunctionRef() 163 : m_target{nullptr}, 164 m_invoker{[](void* target, Args... args) { 165 if constexpr (not std::is_same_v<Res, void>) return Res{}; 166 }} 167 {} 168 169 template<typename Target> 170 requires requires (Target t, Args... a) { 171 requires not std::is_same_v<FunctionRef, std::remove_cvref_t<Target>>; 172 { t(a...) } -> ConvertibleTo<Res>; 173 } 174 FunctionRef(Target&& target) 175 : m_target{&target}, 176 m_invoker{[](void* target, Args... args) { 177 return (*reinterpret_cast<Target*>(target))(static_cast<Args>(args)...); 178 }} 179 {} 180 181 Res operator()(Args... args) const 182 { 183 return m_invoker(m_target, static_cast<Args>(args)...); 184 } 185 186 private: 187 using Invoker = Res (void*, Args...); 188 void* m_target; 189 Invoker* m_invoker; 190 }; 191 192 template<typename... Funcs> 193 struct Overload : Funcs... 194 { 195 using Funcs::operator()...; 196 }; 197 198 template<typename... Funcs> 199 auto overload(Funcs&&... funcs) 200 { 201 return Overload<std::remove_cvref_t<Funcs>...>{std::forward<Funcs>(funcs)...}; 202 } 203 204 } 205 206 #endif // utils_hh_INCLUDED