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"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "exception.hh"
|
2011-09-09 21:24:18 +02:00
|
|
|
|
2011-10-17 23:05:22 +02:00
|
|
|
#include <algorithm>
|
2013-04-09 20:05:40 +02:00
|
|
|
#include <memory>
|
2013-01-11 14:28:13 +01:00
|
|
|
#include <vector>
|
2013-05-06 13:51:23 +02:00
|
|
|
#include <unordered_set>
|
2011-09-08 02:08:55 +02:00
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
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
|
|
|
// *** 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(); }
|
|
|
|
};
|
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
template<typename Container>
|
|
|
|
auto begin(ReversedContainer<Container>& c) -> decltype(c.begin())
|
|
|
|
{
|
|
|
|
return c.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
auto end(ReversedContainer<Container>& c) -> decltype(c.end())
|
|
|
|
{
|
|
|
|
return c.end();
|
|
|
|
}
|
|
|
|
|
2012-06-12 20:45:13 +02:00
|
|
|
template<typename Container>
|
2013-02-27 19:58:07 +01:00
|
|
|
ReversedContainer<Container> reversed(Container&& container)
|
2012-06-12 20:45:13 +02:00
|
|
|
{
|
|
|
|
return ReversedContainer<Container>(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-25 23:31:47 +01:00
|
|
|
template<typename Container, typename T>
|
2014-03-20 20:51:25 +01:00
|
|
|
auto find(Container&& container, const T& value) -> decltype(begin(container))
|
2012-01-25 23:31:47 +01:00
|
|
|
{
|
2014-03-20 20:51:25 +01:00
|
|
|
return std::find(begin(container), end(container), value);
|
2012-01-25 23:31:47 +01:00
|
|
|
}
|
|
|
|
|
2012-12-03 18:56:53 +01:00
|
|
|
template<typename Container, typename T>
|
2014-03-20 20:51:25 +01:00
|
|
|
auto find_if(Container&& container, T op) -> decltype(begin(container))
|
2012-12-03 18:56:53 +01:00
|
|
|
{
|
2014-03-20 20:51:25 +01:00
|
|
|
return std::find_if(begin(container), end(container), op);
|
2012-12-03 18:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-17 23:05:22 +02:00
|
|
|
template<typename Container, typename T>
|
2013-02-27 19:58:07 +01:00
|
|
|
bool contains(Container&& container, const T& value)
|
2011-10-17 23:05:22 +02:00
|
|
|
{
|
2014-03-20 20:51:25 +01:00
|
|
|
return find(container, value) != end(container);
|
2011-10-17 23:05:22 +02:00
|
|
|
}
|
|
|
|
|
2013-05-06 13:51:23 +02:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
bool contains(const std::unordered_set<T1>& container, const T2& value)
|
|
|
|
{
|
|
|
|
return container.find(value) != container.end();
|
|
|
|
}
|
|
|
|
|
2013-12-14 15:49:10 +01:00
|
|
|
template<typename Iterator, typename EndIterator, typename T>
|
|
|
|
void skip_while(Iterator& it, const EndIterator& end, T condition)
|
|
|
|
{
|
|
|
|
while (it != end and condition(*it))
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Iterator, typename BeginIterator, typename T>
|
|
|
|
void skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition)
|
|
|
|
{
|
|
|
|
while (it != begin and condition(*it))
|
|
|
|
--it;
|
|
|
|
}
|
|
|
|
|
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:
|
2013-04-02 14:03:22 +02:00
|
|
|
OnScopeEnd(T func) : m_func(std::move(func)) {}
|
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));
|
|
|
|
}
|
|
|
|
|
2013-04-23 19:08:44 +02:00
|
|
|
template<typename T>
|
|
|
|
bool is_in_range(const T& val, const T& min, const T& max)
|
|
|
|
{
|
|
|
|
return min <= val and val <= max;
|
|
|
|
}
|
|
|
|
|
2013-04-02 13:56:30 +02: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 21:06:31 +02:00
|
|
|
}
|
|
|
|
|
2013-10-24 23:21:41 +02:00
|
|
|
// std::pair hashing
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
struct hash<std::pair<T1,T2>>
|
|
|
|
{
|
|
|
|
size_t operator()(const std::pair<T1,T2>& val) const
|
|
|
|
{
|
|
|
|
size_t seed = std::hash<T2>()(val.second);
|
|
|
|
return seed ^ (std::hash<T1>()(val.first) + 0x9e3779b9 +
|
|
|
|
(seed << 6) + (seed >> 2));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
#endif // utils_hh_INCLUDED
|