Cleanup utils.hh, remove auto_raii in favor of on_scope_end

This commit is contained in:
Maxime Coste 2012-06-12 18:45:13 +00:00
parent e36b3565bf
commit 5710893dd5
2 changed files with 50 additions and 60 deletions

View File

@ -25,7 +25,8 @@ CandidateList complete_filename(const String& prefix,
fileprefix = String(dir_end + 1, real_prefix.end()); fileprefix = String(dir_end + 1, real_prefix.end());
} }
auto dir = auto_raii(opendir(dirname.c_str()), closedir); DIR* dir = opendir(dirname.c_str());
auto closeDir = on_scope_end([=](){ closedir(dir); });
CandidateList result; CandidateList result;
if (not dir) if (not dir)

View File

@ -9,58 +9,10 @@
namespace Kakoune namespace Kakoune
{ {
// *** Singleton ***
template<typename Container> //
struct ReversedContainer // Singleton helper class, every singleton type T should inherit
{ // from Singleton<T> to provide a consistent interface.
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);
}
template<typename T>
bool operator== (const std::unique_ptr<T>& lhs, T* rhs)
{
return lhs.get() == rhs;
}
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);
}
template<typename T> template<typename T>
class Singleton class Singleton
{ {
@ -76,8 +28,8 @@ public:
static void delete_instance() static void delete_instance()
{ {
if (ms_instance)
delete ms_instance; delete ms_instance;
ms_instance = nullptr;
} }
protected: protected:
@ -100,6 +52,25 @@ private:
template<typename T> template<typename T>
T* Singleton<T>::ms_instance = nullptr; T* Singleton<T>::ms_instance = nullptr;
// *** 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);
}
template<typename Container, typename T> template<typename Container, typename T>
auto find(Container& container, const T& value) -> decltype(container.begin()) auto find(Container& container, const T& value) -> decltype(container.begin())
{ {
@ -112,11 +83,16 @@ bool contains(const Container& container, const T& value)
return find(container, value) != container.end(); return find(container, value) != container.end();
} }
inline String str_to_str(const String& str) // *** On scope end ***
{ //
return str; // 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
template<typename T> template<typename T>
class OnScopeEnd class OnScopeEnd
{ {
@ -133,6 +109,19 @@ OnScopeEnd<T> on_scope_end(T t)
return OnScopeEnd<T>(t); return OnScopeEnd<T>(t);
} }
// *** Misc helper functions ***
template<typename T>
bool operator== (const std::unique_ptr<T>& lhs, T* rhs)
{
return lhs.get() == rhs;
}
inline String str_to_str(const String& str)
{
return str;
}
} }
#endif // utils_hh_INCLUDED #endif // utils_hh_INCLUDED