From 77272db0fd28af7ee9ade28434c52e628e4d5a32 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 12 Nov 2012 20:11:27 +0100 Subject: [PATCH] make Buffer::{add,remove}_change_listener const and the listener list mutable --- src/buffer.cc | 4 ++-- src/buffer.hh | 8 +++++--- src/selection.cc | 6 ++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index e40383cd..4b53d210 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -415,13 +415,13 @@ void Buffer::notify_saved() m_last_save_undo_index = history_cursor_index; } -void Buffer::add_change_listener(BufferChangeListener& listener) +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) +void Buffer::remove_change_listener(BufferChangeListener& listener) const { auto it = std::find(m_change_listeners.begin(), m_change_listeners.end(), diff --git a/src/buffer.hh b/src/buffer.hh index de5e0cbf..807f8acd 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -156,8 +156,8 @@ public: // notify the buffer that it was saved in the current state void notify_saved(); - void add_change_listener(BufferChangeListener& listener); - void remove_change_listener(BufferChangeListener& listener); + void add_change_listener(BufferChangeListener& listener) const; + void remove_change_listener(BufferChangeListener& listener) const; // returns an iterator pointing to the first character of the line // iterator is on @@ -224,7 +224,9 @@ private: size_t m_last_save_undo_index; size_t m_timestamp; - std::vector m_change_listeners; + // this mutable as adding or removing listeners is not muting the buffer + // observable state. + mutable std::vector m_change_listeners; OptionManager m_option_manager; HookManager m_hook_manager; diff --git a/src/selection.cc b/src/selection.cc index baab004d..08456860 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -86,14 +86,12 @@ void Selection::on_erase(const BufferIterator& begin, const BufferIterator& end) void Selection::register_with_buffer() { - Buffer& buffer = const_cast(m_first.buffer()); - buffer.add_change_listener(*this); + m_first.buffer().add_change_listener(*this); } void Selection::unregister_with_buffer() { - Buffer& buffer = const_cast(m_first.buffer()); - buffer.remove_change_listener(*this); + m_first.buffer().remove_change_listener(*this); } void Selection::check_invariant() const