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>
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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>
|
|
|
|
ReversedContainer<Container> reversed(Container& container)
|
|
|
|
{
|
|
|
|
return ReversedContainer<Container>(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-26 02:31:47 +04:00
|
|
|
template<typename Container, typename T>
|
|
|
|
auto find(Container& container, const T& value) -> decltype(container.begin())
|
|
|
|
{
|
|
|
|
return std::find(container.begin(), container.end(), value);
|
|
|
|
}
|
|
|
|
|
2011-10-18 01:05:22 +04:00
|
|
|
template<typename Container, typename T>
|
|
|
|
bool contains(const Container& container, const T& value)
|
|
|
|
{
|
2012-01-26 02:31:47 +04:00
|
|
|
return 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:
|
|
|
|
OnScopeEnd(T func) : m_func(func) {}
|
|
|
|
~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;
|
|
|
|
}
|
|
|
|
|
2011-09-05 23:06:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // utils_hh_INCLUDED
|