From 5710893dd53bf07b406b25313618f247091c113b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 12 Jun 2012 18:45:13 +0000 Subject: [PATCH] Cleanup utils.hh, remove auto_raii in favor of on_scope_end --- src/completion.cc | 3 +- src/utils.hh | 107 +++++++++++++++++++++------------------------- 2 files changed, 50 insertions(+), 60 deletions(-) diff --git a/src/completion.cc b/src/completion.cc index 8b1e8757..06cf0856 100644 --- a/src/completion.cc +++ b/src/completion.cc @@ -25,7 +25,8 @@ CandidateList complete_filename(const String& prefix, 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; if (not dir) diff --git a/src/utils.hh b/src/utils.hh index 32e593b8..ebde984f 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -9,58 +9,10 @@ namespace Kakoune { - -template -struct ReversedContainer -{ - ReversedContainer(Container& container) : container(container) {} - Container& container; - - decltype(container.rbegin()) begin() { return container.rbegin(); } - decltype(container.rend()) end() { return container.rend(); } -}; - -template -ReversedContainer reversed(Container& container) -{ - return ReversedContainer(container); -} - -template -bool operator== (const std::unique_ptr& lhs, T* rhs) -{ - return lhs.get() == rhs; -} - -template -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 -AutoRaii auto_raii(T* resource, F cleanup) -{ - return AutoRaii(resource, cleanup); -} - +// *** Singleton *** +// +// Singleton helper class, every singleton type T should inherit +// from Singleton to provide a consistent interface. template class Singleton { @@ -76,8 +28,8 @@ public: static void delete_instance() { - if (ms_instance) - delete ms_instance; + delete ms_instance; + ms_instance = nullptr; } protected: @@ -100,6 +52,25 @@ private: template T* Singleton::ms_instance = nullptr; +// *** Containers helpers *** + +template +struct ReversedContainer +{ + ReversedContainer(Container& container) : container(container) {} + Container& container; + + decltype(container.rbegin()) begin() { return container.rbegin(); } + decltype(container.rend()) end() { return container.rend(); } +}; + +template +ReversedContainer reversed(Container& container) +{ + return ReversedContainer(container); +} + + template 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(); } -inline String str_to_str(const String& str) -{ - return str; -} - +// *** 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 template class OnScopeEnd { @@ -133,6 +109,19 @@ OnScopeEnd on_scope_end(T t) return OnScopeEnd(t); } +// *** Misc helper functions *** + +template +bool operator== (const std::unique_ptr& lhs, T* rhs) +{ + return lhs.get() == rhs; +} + +inline String str_to_str(const String& str) +{ + return str; +} + } #endif // utils_hh_INCLUDED