2011-09-05 23:06:31 +04:00
|
|
|
#ifndef utils_hh_INCLUDED
|
|
|
|
#define utils_hh_INCLUDED
|
|
|
|
|
2011-09-09 23:24:18 +04:00
|
|
|
#include "exception.hh"
|
2011-09-23 18:26:53 +04:00
|
|
|
#include "assert.hh"
|
2011-09-09 23:24:18 +04:00
|
|
|
|
2011-09-08 04:08:55 +04:00
|
|
|
#include <memory>
|
2011-10-18 01:05:22 +04:00
|
|
|
#include <algorithm>
|
2013-01-11 17:28:13 +04:00
|
|
|
#include <vector>
|
2011-09-08 04:08:55 +04:00
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
assert (ms_instance);
|
|
|
|
return *ms_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_instance()
|
|
|
|
{
|
2012-06-12 22:45:13 +04:00
|
|
|
delete ms_instance;
|
|
|
|
ms_instance = nullptr;
|
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()
|
|
|
|
{
|
|
|
|
assert(not ms_instance);
|
|
|
|
ms_instance = static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Singleton()
|
|
|
|
{
|
|
|
|
assert(ms_instance == this);
|
|
|
|
ms_instance = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static T* ms_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* Singleton<T>::ms_instance = nullptr;
|
|
|
|
|
2012-06-28 15:42:55 +04:00
|
|
|
// *** safe_ptr: objects that assert nobody references them when they die ***
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class safe_ptr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
safe_ptr() : m_ptr(nullptr) {}
|
2013-02-28 21:54:15 +04:00
|
|
|
explicit safe_ptr(T* ptr) : m_ptr(ptr)
|
|
|
|
{
|
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->inc_safe_count();
|
|
|
|
#endif
|
|
|
|
}
|
2012-06-28 15:42:55 +04:00
|
|
|
safe_ptr(const safe_ptr& other) : safe_ptr(other.m_ptr) {}
|
|
|
|
safe_ptr(safe_ptr&& other) : m_ptr(other.m_ptr) { other.m_ptr = nullptr; }
|
2013-02-28 21:54:15 +04:00
|
|
|
~safe_ptr()
|
|
|
|
{
|
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->dec_safe_count();
|
|
|
|
#endif
|
|
|
|
}
|
2012-06-28 15:42:55 +04:00
|
|
|
|
|
|
|
safe_ptr& operator=(const safe_ptr& other)
|
|
|
|
{
|
2013-02-28 21:54:15 +04:00
|
|
|
#ifdef KAK_DEBUG
|
2012-06-28 15:42:55 +04:00
|
|
|
if (m_ptr != other.m_ptr)
|
|
|
|
{
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->dec_safe_count();
|
2013-02-28 21:54:15 +04:00
|
|
|
if (other.m_ptr)
|
|
|
|
other.m_ptr->inc_safe_count();
|
2012-06-28 15:42:55 +04:00
|
|
|
}
|
2013-02-28 21:54:15 +04:00
|
|
|
#endif
|
|
|
|
m_ptr = other.m_ptr;
|
2012-06-28 15:42:55 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-06-30 02:33:36 +04:00
|
|
|
safe_ptr& operator=(safe_ptr&& other)
|
|
|
|
{
|
2013-02-28 21:54:15 +04:00
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
if (m_ptr)
|
|
|
|
m_ptr->dec_safe_count();
|
|
|
|
#endif
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
other.m_ptr = nullptr;
|
2012-06-30 02:33:36 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(T* ptr)
|
|
|
|
{
|
|
|
|
*this = safe_ptr(ptr);
|
|
|
|
}
|
|
|
|
|
2012-06-28 15:42:55 +04:00
|
|
|
bool operator== (const safe_ptr& other) const { return m_ptr == other.m_ptr; }
|
|
|
|
bool operator!= (const safe_ptr& other) const { return m_ptr != other.m_ptr; }
|
|
|
|
bool operator== (T* ptr) const { return m_ptr == ptr; }
|
|
|
|
bool operator!= (T* ptr) const { return m_ptr != ptr; }
|
|
|
|
|
|
|
|
T& operator* () const { return *m_ptr; }
|
|
|
|
T* operator-> () const { return m_ptr; }
|
|
|
|
|
|
|
|
T* get() const { return m_ptr; }
|
|
|
|
|
2012-10-02 16:18:34 +04:00
|
|
|
explicit operator bool() const { return m_ptr; }
|
2012-06-28 15:42:55 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SafeCountable
|
|
|
|
{
|
|
|
|
public:
|
2013-02-28 21:54:15 +04:00
|
|
|
#ifdef KAK_DEBUG
|
2012-06-28 15:42:55 +04:00
|
|
|
SafeCountable() : m_count(0) {}
|
|
|
|
~SafeCountable() { assert(m_count == 0); }
|
|
|
|
|
2012-11-12 23:07:05 +04:00
|
|
|
void inc_safe_count() const { ++m_count; }
|
|
|
|
void dec_safe_count() const { --m_count; assert(m_count >= 0); }
|
2012-06-28 15:42:55 +04:00
|
|
|
|
|
|
|
private:
|
2012-11-12 23:07:05 +04:00
|
|
|
mutable int m_count;
|
2013-02-28 21:54:15 +04:00
|
|
|
#endif
|
2012-06-28 15:42:55 +04:00
|
|
|
};
|
|
|
|
|
2012-06-12 22:45:13 +04:00
|
|
|
// *** Containers helpers ***
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
struct ReversedContainer
|
|
|
|
{
|
|
|
|
ReversedContainer(Container& container) : container(container) {}
|
|
|
|
Container& container;
|
|
|
|
|
|
|
|
decltype(container.rbegin()) begin() { return container.rbegin(); }
|
|
|
|
decltype(container.rend()) end() { return container.rend(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Container>
|
2013-02-27 22:58:07 +04:00
|
|
|
ReversedContainer<Container> reversed(Container&& container)
|
2012-06-12 22:45:13 +04:00
|
|
|
{
|
|
|
|
return ReversedContainer<Container>(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-26 02:31:47 +04:00
|
|
|
template<typename Container, typename T>
|
2013-02-27 22:58:07 +04:00
|
|
|
auto find(Container&& container, const T& value) -> decltype(container.begin())
|
2012-01-26 02:31:47 +04:00
|
|
|
{
|
|
|
|
return std::find(container.begin(), container.end(), value);
|
|
|
|
}
|
|
|
|
|
2012-12-03 21:56:53 +04:00
|
|
|
template<typename Container, typename T>
|
2013-02-27 22:58:07 +04:00
|
|
|
auto find_if(Container&& container, T op) -> decltype(container.begin())
|
2012-12-03 21:56:53 +04:00
|
|
|
{
|
|
|
|
return std::find_if(container.begin(), container.end(), op);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-18 01:05:22 +04:00
|
|
|
template<typename Container, typename T>
|
2013-02-27 22:58:07 +04:00
|
|
|
bool contains(Container&& container, const T& value)
|
2011-10-18 01:05:22 +04:00
|
|
|
{
|
2013-01-29 21:55:32 +04:00
|
|
|
return (not container.empty()) and find(container, value) != container.end();
|
2011-10-18 01:05:22 +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:
|
2013-04-02 16:03:22 +04:00
|
|
|
OnScopeEnd(T func) : m_func(std::move(func)) {}
|
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-08-29 23:52:17 +04:00
|
|
|
inline String escape(const String& name)
|
|
|
|
{
|
|
|
|
return name.replace("([ \\t;])", R"(\\\1)");
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-04-02 15:56:30 +04:00
|
|
|
// *** AutoRegister: RAII handling of value semantics registering classes ***
|
|
|
|
|
|
|
|
template<typename EffectiveType, typename RegisterFuncs, typename Registry>
|
|
|
|
class AutoRegister
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoRegister(Registry& registry)
|
|
|
|
: m_registry(®istry)
|
|
|
|
{
|
|
|
|
RegisterFuncs::insert(*m_registry, effective_this());
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRegister(const AutoRegister& other)
|
|
|
|
: m_registry(other.m_registry)
|
|
|
|
{
|
|
|
|
RegisterFuncs::insert(*m_registry, effective_this());
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRegister(AutoRegister&& other)
|
|
|
|
: m_registry(other.m_registry)
|
|
|
|
{
|
|
|
|
RegisterFuncs::insert(*m_registry, effective_this());
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoRegister()
|
|
|
|
{
|
|
|
|
RegisterFuncs::remove(*m_registry, effective_this());
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRegister& operator=(const AutoRegister& other)
|
|
|
|
{
|
|
|
|
if (m_registry != other.m_registry)
|
|
|
|
{
|
|
|
|
RegisterFuncs::remove(*m_registry, effective_this());
|
|
|
|
m_registry = other.m_registry;
|
|
|
|
RegisterFuncs::insert(*m_registry, effective_this());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoRegister& operator=(AutoRegister&& other)
|
|
|
|
{
|
|
|
|
if (m_registry != other.m_registry)
|
|
|
|
{
|
|
|
|
RegisterFuncs::remove(*m_registry, effective_this());
|
|
|
|
m_registry = other.m_registry;
|
|
|
|
RegisterFuncs::insert(*m_registry, effective_this());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Registry& registry() const { return *m_registry; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
EffectiveType& effective_this() { return static_cast<EffectiveType&>(*this); }
|
|
|
|
Registry* m_registry;
|
|
|
|
};
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // utils_hh_INCLUDED
|