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()
|
|
|
|
{
|
2013-06-06 18:54:33 +02:00
|
|
|
kak_assert (ms_instance);
|
2011-09-23 16:26:53 +02:00
|
|
|
return *ms_instance;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(not ms_instance);
|
2011-09-23 16:26:53 +02:00
|
|
|
ms_instance = static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~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:
|
|
|
|
static T* ms_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* Singleton<T>::ms_instance = nullptr;
|
|
|
|
|
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]]
|
2013-04-02 14:03:22 +02:00
|
|
|
OnScopeEnd(T func) : m_func(std::move(func)) {}
|
2015-03-11 21:52:39 +01:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2011-12-20 20:18:00 +01:00
|
|
|
~OnScopeEnd() { m_func(); }
|
|
|
|
private:
|
|
|
|
T m_func;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
OnScopeEnd<T> on_scope_end(T t)
|
|
|
|
{
|
|
|
|
return OnScopeEnd<T>(t);
|
|
|
|
}
|
2011-12-02 15:20:11 +01:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // utils_hh_INCLUDED
|