From ce99856bfa15ca8212ba19b01568d59eeb4e62ca Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 8 Aug 2012 19:36:40 +0200 Subject: [PATCH] Refactor BufferManager --- src/buffer.cc | 4 ++-- src/buffer_manager.cc | 53 ++++++++++++++++++++++++------------------- src/buffer_manager.hh | 23 ++++++------------- src/commands.cc | 10 ++++---- 4 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index f619edb1..620cce3f 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -29,7 +29,7 @@ Buffer::Buffer(String name, Type type, m_hook_manager(GlobalHookManager::instance()), m_option_manager(GlobalOptionManager::instance()) { - BufferManager::instance().register_buffer(this); + BufferManager::instance().register_buffer(*this); if (not initial_content.empty()) apply_modification(Modification::make_insert(begin(), std::move(initial_content))); @@ -48,7 +48,7 @@ Buffer::~Buffer() m_hook_manager.run_hook("BufClose", m_name, Context(Editor(*this))); m_windows.clear(); - BufferManager::instance().unregister_buffer(this); + BufferManager::instance().unregister_buffer(*this); assert(m_change_listeners.empty()); } diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index 3aa64aaa..9aba924d 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -13,36 +13,42 @@ BufferManager::~BufferManager() { // delete remaining buffers while (not m_buffers.empty()) - delete m_buffers.begin()->second.get(); + delete m_buffers.front().get(); } -void BufferManager::register_buffer(Buffer* buffer) +void BufferManager::register_buffer(Buffer& buffer) { - assert(buffer); - const String& name = buffer->name(); - if (m_buffers.find(name) != m_buffers.end()) - throw name_not_unique(); - - m_buffers[name] = safe_ptr(buffer); -} - -void BufferManager::unregister_buffer(Buffer* buffer) -{ - assert(buffer); - auto buffer_it = m_buffers.find(buffer->name()); - if (buffer_it != m_buffers.end()) + const String& name = buffer.name(); + for (auto& buf : m_buffers) { - assert(buffer_it->second == buffer); - m_buffers.erase(buffer_it); + if (buf->name() == name) + throw name_not_unique(); } + + m_buffers.push_back(safe_ptr(&buffer)); +} + +void BufferManager::unregister_buffer(Buffer& buffer) +{ + for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it) + { + if (*it == &buffer) + { + m_buffers.erase(it); + return; + } + } + assert(false); } Buffer* BufferManager::get_buffer(const String& name) { - if (m_buffers.find(name) == m_buffers.end()) - return nullptr; - - return m_buffers[name].get(); + for (auto& buf : m_buffers) + { + if (buf->name() == name) + return buf.get(); + } + return nullptr; } CandidateList BufferManager::complete_buffername(const String& prefix, @@ -52,8 +58,9 @@ CandidateList BufferManager::complete_buffername(const String& prefix, CandidateList result; for (auto& buffer : m_buffers) { - if (buffer.first.substr(0, real_prefix.length()) == real_prefix) - result.push_back(buffer.first); + const String& name = buffer->name(); + if (name.substr(0, real_prefix.length()) == real_prefix) + result.push_back(name); } return result; } diff --git a/src/buffer_manager.hh b/src/buffer_manager.hh index 80198942..36373a69 100644 --- a/src/buffer_manager.hh +++ b/src/buffer_manager.hh @@ -14,25 +14,16 @@ class Buffer; class BufferManager : public Singleton { public: - typedef std::unordered_map> BufferMap; - - struct iterator : public BufferMap::const_iterator - { - typedef BufferMap::const_iterator parent_type; - - iterator() {} - iterator(const parent_type& other) : parent_type(other) {} - Buffer& operator*() const { return *(parent_type::operator*().second); } - Buffer* operator->() const { return parent_type::operator*().second.get(); } - }; + using BufferList = std::vector>; + using iterator = BufferList::const_iterator; ~BufferManager(); - void register_buffer(Buffer* buffer); - void unregister_buffer(Buffer* buffer); + void register_buffer(Buffer& buffer); + void unregister_buffer(Buffer& buffer); - iterator begin() const { return iterator(m_buffers.begin()); } - iterator end() const { return iterator(m_buffers.end()); } + iterator begin() const { return m_buffers.cbegin(); } + iterator end() const { return m_buffers.cend(); } size_t count() const { return m_buffers.size(); } Buffer* get_buffer(const String& name); @@ -41,7 +32,7 @@ public: size_t cursor_pos = String::npos); private: - BufferMap m_buffers; + BufferList m_buffers; }; } diff --git a/src/commands.cc b/src/commands.cc index 5b0f4b4c..38dc1225 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -293,8 +293,8 @@ void quit(const CommandParameters& params, Context& context) std::vector names; for (auto& buffer : BufferManager::instance()) { - if (buffer.type() != Buffer::Type::Scratch and buffer.is_modified()) - names.push_back(buffer.name()); + if (buffer->type() != Buffer::Type::Scratch and buffer->is_modified()) + names.push_back(buffer->name()); } if (not names.empty()) { @@ -355,11 +355,11 @@ void delete_buffer(const CommandParameters& params, Context& context) { if (manager.count() == 1) throw runtime_error("buffer " + buffer->name() + " is the last one"); - for (Buffer& buf : manager) + for (auto& buf : manager) { - if (&buf != buffer) + if (buf != buffer) { - context = Context(*buf.get_or_create_window()); + context = Context(*buf->get_or_create_window()); break; } }