2011-09-05 21:06:31 +02:00
|
|
|
#ifndef utils_hh_INCLUDED
|
|
|
|
#define utils_hh_INCLUDED
|
|
|
|
|
2011-09-23 16:26:53 +02:00
|
|
|
#include "assert.hh"
|
2011-09-09 21:24:18 +02:00
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include <memory>
|
2011-09-08 02:08:55 +02:00
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
2014-10-22 01:20:09 +02:00
|
|
|
|
2012-06-12 20:45:13 +02:00
|
|
|
// *** Singleton ***
|
|
|
|
//
|
|
|
|
// Singleton helper class, every singleton type T should inherit
|
|
|
|
// from Singleton<T> to provide a consistent interface.
|
2011-09-23 16:26:53 +02:00
|
|
|
template<typename T>
|
|
|
|
class Singleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Singleton(const Singleton&) = delete;
|
|
|
|
Singleton& operator=(const Singleton&) = delete;
|
|
|
|
|
|
|
|
static T& instance()
|
|
|
|
{
|
2017-06-26 12:27:18 +02:00
|
|
|
kak_assert(ms_instance);
|
|
|
|
return *static_cast<T*>(ms_instance);
|
2011-09-23 16:26:53 +02:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:26:44 +01:00
|
|
|
static bool has_instance()
|
|
|
|
{
|
|
|
|
return ms_instance != nullptr;
|
|
|
|
}
|
|
|
|
|
2011-09-23 16:26:53 +02:00
|
|
|
protected:
|
|
|
|
Singleton()
|
|
|
|
{
|
2017-06-26 12:27:18 +02:00
|
|
|
kak_assert(ms_instance == nullptr);
|
|
|
|
ms_instance = this;
|
2011-09-23 16:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~Singleton()
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(ms_instance == this);
|
2011-09-23 16:26:53 +02:00
|
|
|
ms_instance = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-06-26 12:27:18 +02:00
|
|
|
static Singleton* ms_instance;
|
2011-09-23 16:26:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-06-26 12:27:18 +02:00
|
|
|
Singleton<T>* Singleton<T>::ms_instance = nullptr;
|
2011-09-23 16:26:53 +02:00
|
|
|
|
2012-06-12 20:45:13 +02:00
|
|
|
// *** 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
|
2011-12-20 20:18:00 +01:00
|
|
|
template<typename T>
|
|
|
|
class OnScopeEnd
|
|
|
|
{
|
|
|
|
public:
|
2015-03-11 21:52:39 +01:00
|
|
|
[[gnu::always_inline]]
|
2019-02-09 05:41:09 +01:00
|
|
|
OnScopeEnd(T func) : m_valid{true}, m_func{std::move(func)} {}
|
2015-03-11 21:52:39 +01:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2018-05-01 14:38:51 +02:00
|
|
|
OnScopeEnd(OnScopeEnd&& other)
|
2019-02-09 05:41:09 +01:00
|
|
|
: m_valid{other.m_valid}, m_func{std::move(other.m_func)}
|
2018-05-01 14:38:51 +02:00
|
|
|
{ other.m_valid = false; }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2018-05-04 23:57:37 +02:00
|
|
|
~OnScopeEnd() noexcept(noexcept(std::declval<T>()())) { if (m_valid) m_func(); }
|
2018-05-01 14:38:51 +02:00
|
|
|
|
2011-12-20 20:18:00 +01:00
|
|
|
private:
|
2018-05-01 14:38:51 +02:00
|
|
|
bool m_valid;
|
2011-12-20 20:18:00 +01:00
|
|
|
T m_func;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
OnScopeEnd<T> on_scope_end(T t)
|
|
|
|
{
|
2017-05-17 21:12:36 +02:00
|
|
|
return OnScopeEnd<T>{std::move(t)};
|
2011-12-20 20:18:00 +01:00
|
|
|
}
|
2011-12-02 15:20:11 +01:00
|
|
|
|
2017-06-07 12:55:42 +02:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2020-07-21 12:27:42 +02:00
|
|
|
ScopedSetBool(ScopedSetBool&& other)
|
|
|
|
: m_nested_bool(other.m_nested_bool), m_condition(other.m_condition)
|
|
|
|
{
|
|
|
|
other.m_condition = false;
|
|
|
|
}
|
|
|
|
|
2017-06-07 12:55:42 +02:00
|
|
|
~ScopedSetBool()
|
|
|
|
{
|
|
|
|
if (m_condition)
|
|
|
|
m_nested_bool.unset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NestedBool& m_nested_bool;
|
|
|
|
bool m_condition;
|
|
|
|
};
|
|
|
|
|
2012-06-12 20:45:13 +02:00
|
|
|
// *** Misc helper functions ***
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator== (const std::unique_ptr<T>& lhs, T* rhs)
|
|
|
|
{
|
|
|
|
return lhs.get() == rhs;
|
|
|
|
}
|
|
|
|
|
2012-10-02 14:08:09 +02:00
|
|
|
template<typename T>
|
|
|
|
const T& clamp(const T& val, const T& min, const T& max)
|
|
|
|
{
|
|
|
|
return (val < min ? min : (val > max ? max : val));
|
|
|
|
}
|
|
|
|
|
2016-03-06 01:07:06 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:35:38 +02:00
|
|
|
template<typename E>
|
|
|
|
auto to_underlying(E value)
|
|
|
|
{
|
|
|
|
return static_cast<std::underlying_type_t<E>>(value);
|
|
|
|
}
|
|
|
|
|
2019-09-01 05:03:01 +02:00
|
|
|
template<typename> class FunctionRef;
|
|
|
|
|
|
|
|
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>
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-11-22 11:48:26 +01:00
|
|
|
template<typename... Funcs>
|
|
|
|
struct Overload : Funcs...
|
|
|
|
{
|
|
|
|
using Funcs::operator()...;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Funcs>
|
|
|
|
auto overload(Funcs&&... funcs)
|
|
|
|
{
|
|
|
|
return Overload<std::decay_t<Funcs>...>{std::forward<Funcs>(funcs)...};
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // utils_hh_INCLUDED
|