From 914ede7a8258962990a7c24fac0e451bc7c1d7a6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 11 Jan 2013 14:28:13 +0100 Subject: [PATCH] Add and use a Set template class for recuring small sets --- src/buffer.cc | 15 -------------- src/buffer.hh | 6 ++---- src/dynamic_selection_list.cc | 16 +++++++-------- src/editor.cc | 4 ++-- src/event_manager.cc | 20 ++++--------------- src/event_manager.hh | 7 ++----- src/utils.hh | 37 +++++++++++++++++++++++++++++++++++ 7 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 7ffd417e..b0a48543 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -414,19 +414,4 @@ void Buffer::notify_saved() } } -void Buffer::add_change_listener(BufferChangeListener& listener) const -{ - assert(not contains(m_change_listeners, &listener)); - m_change_listeners.push_back(&listener); -} - -void Buffer::remove_change_listener(BufferChangeListener& listener) const -{ - auto it = std::find(m_change_listeners.begin(), - m_change_listeners.end(), - &listener); - assert(it != m_change_listeners.end()); - m_change_listeners.erase(it); -} - } diff --git a/src/buffer.hh b/src/buffer.hh index 2c4b0042..9a35bae2 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -167,9 +167,7 @@ public: HookManager& hooks() { return m_hooks; } const HookManager& hooks() const { return m_hooks; } - void add_change_listener(BufferChangeListener& listener) const; - void remove_change_listener(BufferChangeListener& listener) const; - + Set& change_listeners() const { return m_change_listeners; } private: friend class BufferIterator; @@ -213,7 +211,7 @@ private: // this is mutable as adding or removing listeners is not muting the // buffer observable state. - mutable std::vector m_change_listeners; + mutable Set m_change_listeners; OptionManager m_options; HookManager m_hooks; diff --git a/src/dynamic_selection_list.cc b/src/dynamic_selection_list.cc index 05b90501..d7eb5beb 100644 --- a/src/dynamic_selection_list.cc +++ b/src/dynamic_selection_list.cc @@ -7,19 +7,19 @@ DynamicSelectionList::DynamicSelectionList(const Buffer& buffer, SelectionList selections) : m_buffer(&buffer), SelectionList(std::move(selections)) { - m_buffer->add_change_listener(*this); + m_buffer->change_listeners().add(this); check_invariant(); } DynamicSelectionList::~DynamicSelectionList() { - m_buffer->remove_change_listener(*this); + m_buffer->change_listeners().remove(this); } DynamicSelectionList::DynamicSelectionList(const DynamicSelectionList& other) : SelectionList(other), m_buffer(other.m_buffer) { - m_buffer->add_change_listener(*this); + m_buffer->change_listeners().add(this); } DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList& other) @@ -27,9 +27,9 @@ DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList SelectionList::operator=((const SelectionList&)other); if (m_buffer != other.m_buffer) { - m_buffer->remove_change_listener(*this); + m_buffer->change_listeners().remove(this); m_buffer = other.m_buffer; - m_buffer->add_change_listener(*this); + m_buffer->change_listeners().add(this); } check_invariant(); return *this; @@ -38,7 +38,7 @@ DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList DynamicSelectionList::DynamicSelectionList(DynamicSelectionList&& other) : SelectionList(std::move(other)), m_buffer(other.m_buffer) { - m_buffer->add_change_listener(*this); + m_buffer->change_listeners().add(this); } DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& other) @@ -46,9 +46,9 @@ DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& oth SelectionList::operator=(std::move(other)); if (m_buffer != other.m_buffer) { - m_buffer->remove_change_listener(*this); + m_buffer->change_listeners().remove(this); m_buffer = other.m_buffer; - m_buffer->add_change_listener(*this); + m_buffer->change_listeners().add(this); } check_invariant(); return *this; diff --git a/src/editor.cc b/src/editor.cc index 48b3ac1e..e755f50f 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -327,10 +327,10 @@ class LastModifiedRangeListener : public BufferChangeListener public: LastModifiedRangeListener(Buffer& buffer) : m_buffer(buffer) - { m_buffer.add_change_listener(*this); } + { m_buffer.change_listeners().add(this); } ~LastModifiedRangeListener() - { m_buffer.remove_change_listener(*this); } + { m_buffer.change_listeners().remove(this); } void on_insert(const BufferIterator& begin, const BufferIterator& end) { diff --git a/src/event_manager.cc b/src/event_manager.cc index 92019a15..9ae3e78c 100644 --- a/src/event_manager.cc +++ b/src/event_manager.cc @@ -8,12 +8,12 @@ namespace Kakoune FDWatcher::FDWatcher(int fd, Callback callback) : m_fd{fd}, m_callback{std::move(callback)} { - EventManager::instance().register_fd_watcher(this); + EventManager::instance().m_fd_watchers.add(this); } FDWatcher::~FDWatcher() { - EventManager::instance().unregister_fd_watcher(this); + EventManager::instance().m_fd_watchers.remove(this); } EventManager::EventManager() @@ -42,8 +42,8 @@ void EventManager::handle_next_events() const int fd = event.fd; if (event.revents or contains(forced, fd)) { - auto it = std::find_if(m_fd_watchers.begin(), m_fd_watchers.end(), - [fd](FDWatcher* w) { return w->fd() == fd; }); + auto it = find_if(m_fd_watchers, + [fd](FDWatcher* w) { return w->fd() == fd; }); if (it != m_fd_watchers.end()) (*it)->run(); } @@ -55,17 +55,5 @@ void EventManager::force_signal(int fd) m_forced_fd.push_back(fd); } -void EventManager::register_fd_watcher(FDWatcher* event) -{ - assert(not contains(m_fd_watchers, event)); - m_fd_watchers.push_back(event); -} - -void EventManager::unregister_fd_watcher(FDWatcher* event) -{ - auto it = find(m_fd_watchers, event); - assert(it != m_fd_watchers.end()); - m_fd_watchers.erase(it); -} } diff --git a/src/event_manager.hh b/src/event_manager.hh index 08c9bfca..ae719f06 100644 --- a/src/event_manager.hh +++ b/src/event_manager.hh @@ -39,11 +39,8 @@ public: private: friend class FDWatcher; - void register_fd_watcher(FDWatcher* watcher); - void unregister_fd_watcher(FDWatcher* watcher); - - std::vector m_fd_watchers; - std::vector m_forced_fd; + Set m_fd_watchers; + std::vector m_forced_fd; }; } diff --git a/src/utils.hh b/src/utils.hh index 3c68f1cd..2bf0ce80 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -6,6 +6,7 @@ #include #include +#include namespace Kakoune { @@ -206,6 +207,42 @@ const T& clamp(const T& val, const T& min, const T& max) return (val < min ? min : (val > max ? max : val)); } +// *** set *** +// generic simple set based on vector + +template +class Set +{ +public: + using iterator = typename std::vector::iterator; + using const_iterator = typename std::vector::const_iterator; + + void add(T value) + { + assert(not contains(m_values, value)); + m_values.push_back(value); + } + + void remove(T value) + { + auto it = find(m_values, value); + assert(it != m_values.end()); + m_values.erase(it); + } + + size_t size() const { return m_values.size(); } + bool empty() const { return m_values.empty(); } + + iterator begin() { return m_values.begin(); } + iterator end() { return m_values.end(); } + + const_iterator begin() const { return m_values.begin(); } + const_iterator end() const { return m_values.end(); } + +private: + std::vector m_values; +}; + } #endif // utils_hh_INCLUDED