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