Refactor BufferManager

This commit is contained in:
Maxime Coste 2012-08-08 19:36:40 +02:00
parent b29b75aa81
commit ce99856bfa
4 changed files with 44 additions and 46 deletions

View File

@ -29,7 +29,7 @@ Buffer::Buffer(String name, Type type,
m_hook_manager(GlobalHookManager::instance()), m_hook_manager(GlobalHookManager::instance()),
m_option_manager(GlobalOptionManager::instance()) m_option_manager(GlobalOptionManager::instance())
{ {
BufferManager::instance().register_buffer(this); BufferManager::instance().register_buffer(*this);
if (not initial_content.empty()) if (not initial_content.empty())
apply_modification(Modification::make_insert(begin(), std::move(initial_content))); 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_hook_manager.run_hook("BufClose", m_name, Context(Editor(*this)));
m_windows.clear(); m_windows.clear();
BufferManager::instance().unregister_buffer(this); BufferManager::instance().unregister_buffer(*this);
assert(m_change_listeners.empty()); assert(m_change_listeners.empty());
} }

View File

@ -13,36 +13,42 @@ BufferManager::~BufferManager()
{ {
// delete remaining buffers // delete remaining buffers
while (not m_buffers.empty()) 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();
const String& name = buffer->name(); for (auto& buf : m_buffers)
if (m_buffers.find(name) != m_buffers.end()) {
if (buf->name() == name)
throw name_not_unique(); throw name_not_unique();
m_buffers[name] = safe_ptr<Buffer>(buffer);
} }
void BufferManager::unregister_buffer(Buffer* buffer) m_buffers.push_back(safe_ptr<Buffer>(&buffer));
{
assert(buffer);
auto buffer_it = m_buffers.find(buffer->name());
if (buffer_it != m_buffers.end())
{
assert(buffer_it->second == buffer);
m_buffers.erase(buffer_it);
} }
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) Buffer* BufferManager::get_buffer(const String& name)
{ {
if (m_buffers.find(name) == m_buffers.end()) for (auto& buf : m_buffers)
{
if (buf->name() == name)
return buf.get();
}
return nullptr; return nullptr;
return m_buffers[name].get();
} }
CandidateList BufferManager::complete_buffername(const String& prefix, CandidateList BufferManager::complete_buffername(const String& prefix,
@ -52,8 +58,9 @@ CandidateList BufferManager::complete_buffername(const String& prefix,
CandidateList result; CandidateList result;
for (auto& buffer : m_buffers) for (auto& buffer : m_buffers)
{ {
if (buffer.first.substr(0, real_prefix.length()) == real_prefix) const String& name = buffer->name();
result.push_back(buffer.first); if (name.substr(0, real_prefix.length()) == real_prefix)
result.push_back(name);
} }
return result; return result;
} }

View File

@ -14,25 +14,16 @@ class Buffer;
class BufferManager : public Singleton<BufferManager> class BufferManager : public Singleton<BufferManager>
{ {
public: public:
typedef std::unordered_map<String, safe_ptr<Buffer>> BufferMap; using BufferList = std::vector<safe_ptr<Buffer>>;
using iterator = BufferList::const_iterator;
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(); }
};
~BufferManager(); ~BufferManager();
void register_buffer(Buffer* buffer); void register_buffer(Buffer& buffer);
void unregister_buffer(Buffer* buffer); void unregister_buffer(Buffer& buffer);
iterator begin() const { return iterator(m_buffers.begin()); } iterator begin() const { return m_buffers.cbegin(); }
iterator end() const { return iterator(m_buffers.end()); } iterator end() const { return m_buffers.cend(); }
size_t count() const { return m_buffers.size(); } size_t count() const { return m_buffers.size(); }
Buffer* get_buffer(const String& name); Buffer* get_buffer(const String& name);
@ -41,7 +32,7 @@ public:
size_t cursor_pos = String::npos); size_t cursor_pos = String::npos);
private: private:
BufferMap m_buffers; BufferList m_buffers;
}; };
} }

View File

@ -293,8 +293,8 @@ void quit(const CommandParameters& params, Context& context)
std::vector<String> names; std::vector<String> names;
for (auto& buffer : BufferManager::instance()) for (auto& buffer : BufferManager::instance())
{ {
if (buffer.type() != Buffer::Type::Scratch and buffer.is_modified()) if (buffer->type() != Buffer::Type::Scratch and buffer->is_modified())
names.push_back(buffer.name()); names.push_back(buffer->name());
} }
if (not names.empty()) if (not names.empty())
{ {
@ -355,11 +355,11 @@ void delete_buffer(const CommandParameters& params, Context& context)
{ {
if (manager.count() == 1) if (manager.count() == 1)
throw runtime_error("buffer " + buffer->name() + " is the last one"); 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; break;
} }
} }