From a18241a03b9b7024a8f307301bc4ebcce10560a1 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 2 Apr 2013 13:56:30 +0200 Subject: [PATCH] Extract AutoRegister logic to a helper class --- src/buffer.hh | 65 ++++++++++++++------------------------------------- src/utils.hh | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 47 deletions(-) diff --git a/src/buffer.hh b/src/buffer.hh index 260c00a09..9879bec72 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -244,57 +244,28 @@ constexpr Buffer::Flags operator~(Buffer::Flags lhs) return (Buffer::Flags)(~(int)lhs); } -class BufferChangeListener_AutoRegister : public BufferChangeListener +struct BufferListenerRegisterFuncs +{ + static void insert(const Buffer& buffer, BufferChangeListener& listener) + { + buffer.change_listeners().insert(&listener); + } + static void remove(const Buffer& buffer, BufferChangeListener& listener) + { + buffer.change_listeners().erase(&listener); + } +}; + +class BufferChangeListener_AutoRegister + : public BufferChangeListener, + public AutoRegister { public: BufferChangeListener_AutoRegister(const Buffer& buffer) - : m_buffer(&buffer) - { - m_buffer->change_listeners().insert(this); - } + : AutoRegister(buffer) {} - BufferChangeListener_AutoRegister(const BufferChangeListener_AutoRegister& other) - : m_buffer(other.m_buffer) - { - m_buffer->change_listeners().insert(this); - } - - BufferChangeListener_AutoRegister(BufferChangeListener_AutoRegister&& other) - : m_buffer(other.m_buffer) - { - m_buffer->change_listeners().insert(this); - } - - ~BufferChangeListener_AutoRegister() - { - m_buffer->change_listeners().erase(this); - } - - BufferChangeListener_AutoRegister& operator=(const BufferChangeListener_AutoRegister& other) - { - if (m_buffer != other.m_buffer) - { - m_buffer->change_listeners().erase(this); - m_buffer = other.m_buffer; - m_buffer->change_listeners().insert(this); - } - return *this; - } - - BufferChangeListener_AutoRegister& operator=(BufferChangeListener_AutoRegister&& other) - { - if (m_buffer != other.m_buffer) - { - m_buffer->change_listeners().erase(this); - m_buffer = other.m_buffer; - m_buffer->change_listeners().insert(this); - } - return *this; - } - - const Buffer& buffer() const { return *m_buffer; } -private: - const Buffer* m_buffer; + const Buffer& buffer() const { return registry(); } }; } diff --git a/src/utils.hh b/src/utils.hh index 0db71f40a..0e155a371 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -227,6 +227,63 @@ const T& clamp(const T& val, const T& min, const T& max) return (val < min ? min : (val > max ? max : val)); } +// *** AutoRegister: RAII handling of value semantics registering classes *** + +template +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(*this); } + Registry* m_registry; +}; + } #endif // utils_hh_INCLUDED