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
|
|
|
|
2015-05-26 20:42:09 +03:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
std::unique_ptr<T> make_unique(Args&&... args)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t... I>
|
|
|
|
struct IndexSequence
|
|
|
|
{
|
|
|
|
using Next = IndexSequence<I..., sizeof...(I)>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
struct MakeIndexSequence
|
|
|
|
{
|
|
|
|
using Type = typename MakeIndexSequence<N-1>::Type::Next;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct MakeIndexSequence<0>
|
|
|
|
{
|
|
|
|
using Type = IndexSequence<>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
constexpr typename MakeIndexSequence<N>::Type
|
|
|
|
make_index_sequence() { return typename MakeIndexSequence<N>::Type{}; }
|
|
|
|
|
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()
|
|
|
|
{
|
2013-06-06 20:54:33 +04:00
|
|
|
kak_assert (ms_instance);
|
2011-09-23 18:26:53 +04:00
|
|
|
return *ms_instance;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(not ms_instance);
|
2011-09-23 18:26:53 +04:00
|
|
|
ms_instance = static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~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:
|
|
|
|
static T* ms_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* Singleton<T>::ms_instance = nullptr;
|
|
|
|
|
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]]
|
2013-04-02 16:03:22 +04:00
|
|
|
OnScopeEnd(T func) : m_func(std::move(func)) {}
|
2015-03-11 23:52:39 +03:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2011-12-20 23:18:00 +04: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 18:20:11 +04:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // utils_hh_INCLUDED
|