2011-09-05 21:06:31 +02:00
|
|
|
#ifndef utils_hh_INCLUDED
|
|
|
|
#define utils_hh_INCLUDED
|
|
|
|
|
2011-09-09 21:24:18 +02:00
|
|
|
#include "exception.hh"
|
2011-09-23 16:26:53 +02:00
|
|
|
#include "assert.hh"
|
2011-09-09 21:24:18 +02:00
|
|
|
|
2011-09-08 02:08:55 +02:00
|
|
|
#include <memory>
|
2011-10-17 23:05:22 +02:00
|
|
|
#include <algorithm>
|
2011-09-08 02:08:55 +02:00
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2011-09-06 20:33:35 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-08 02:08:55 +02:00
|
|
|
template<typename T>
|
|
|
|
bool operator== (const std::unique_ptr<T>& lhs, T* rhs)
|
|
|
|
{
|
|
|
|
return lhs.get() == rhs;
|
|
|
|
}
|
2011-09-06 20:33:35 +02:00
|
|
|
|
2011-09-14 17:36:35 +02:00
|
|
|
template<typename T, typename F>
|
|
|
|
class AutoRaii
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoRaii(T* resource, F cleanup)
|
|
|
|
: m_resource(resource), m_cleanup(cleanup) {}
|
|
|
|
|
|
|
|
AutoRaii(AutoRaii&& other) : m_resource(other.m_resource),
|
|
|
|
m_cleanup(other.m_cleanup)
|
|
|
|
{ other.m_resource = nullptr; }
|
|
|
|
|
|
|
|
AutoRaii(const AutoRaii&) = delete;
|
|
|
|
AutoRaii& operator=(const AutoRaii&) = delete;
|
|
|
|
|
|
|
|
~AutoRaii() { if (m_resource) m_cleanup(m_resource); }
|
|
|
|
|
|
|
|
operator T*() { return m_resource; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_resource;
|
|
|
|
F m_cleanup;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename F>
|
|
|
|
AutoRaii<T, F> auto_raii(T* resource, F cleanup)
|
|
|
|
{
|
|
|
|
return AutoRaii<T, F>(resource, cleanup);
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
assert (ms_instance);
|
|
|
|
return *ms_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_instance()
|
|
|
|
{
|
|
|
|
if (ms_instance)
|
|
|
|
delete ms_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-10-17 23:05:22 +02:00
|
|
|
template<typename Container, typename T>
|
|
|
|
bool contains(const Container& container, const T& value)
|
|
|
|
{
|
|
|
|
return std::find(container.begin(), container.end(), value)
|
|
|
|
!= container.end();
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // utils_hh_INCLUDED
|