BufferManager no longer owns buffers, only register them

This commit is contained in:
Maxime Coste 2012-03-26 14:21:49 +00:00
parent 3c798a4ee8
commit 792f0f7f2e
4 changed files with 15 additions and 12 deletions

View File

@ -41,6 +41,7 @@ Buffer::Buffer(const std::string& name, Type type,
Buffer::~Buffer() Buffer::~Buffer()
{ {
m_windows.clear(); m_windows.clear();
BufferManager::instance().unregister_buffer(this);
assert(m_modification_listeners.empty()); assert(m_modification_listeners.empty());
} }

View File

@ -2,7 +2,6 @@
#include "assert.hh" #include "assert.hh"
#include "buffer.hh" #include "buffer.hh"
#include "window.hh"
#include "exception.hh" #include "exception.hh"
namespace Kakoune namespace Kakoune
@ -17,15 +16,18 @@ void BufferManager::register_buffer(Buffer* buffer)
if (m_buffers.find(name) != m_buffers.end()) if (m_buffers.find(name) != m_buffers.end())
throw name_not_unique(); throw name_not_unique();
m_buffers[name] = std::unique_ptr<Buffer>(buffer); m_buffers[name] = buffer;
} }
void BufferManager::delete_buffer(Buffer* buffer) void BufferManager::unregister_buffer(Buffer* buffer)
{ {
assert(buffer); assert(buffer);
auto buffer_it = m_buffers.find(buffer->name()); auto buffer_it = m_buffers.find(buffer->name());
assert(buffer_it->second.get() == buffer); if (buffer_it != m_buffers.end())
{
assert(buffer_it->second == buffer);
m_buffers.erase(buffer_it); m_buffers.erase(buffer_it);
}
} }
Buffer* BufferManager::get_buffer(const std::string& name) Buffer* BufferManager::get_buffer(const std::string& name)
@ -33,7 +35,7 @@ Buffer* BufferManager::get_buffer(const std::string& name)
if (m_buffers.find(name) == m_buffers.end()) if (m_buffers.find(name) == m_buffers.end())
return nullptr; return nullptr;
return m_buffers[name].get(); return m_buffers[name];
} }
CandidateList BufferManager::complete_buffername(const std::string& prefix, CandidateList BufferManager::complete_buffername(const std::string& prefix,

View File

@ -1,20 +1,20 @@
#ifndef buffer_manager_hh_INCLUDED #ifndef buffer_manager_hh_INCLUDED
#define buffer_manager_hh_INCLUDED #define buffer_manager_hh_INCLUDED
#include "buffer.hh"
#include "completion.hh" #include "completion.hh"
#include "utils.hh" #include "utils.hh"
#include <unordered_map> #include <unordered_map>
#include <memory>
namespace Kakoune namespace Kakoune
{ {
class Buffer;
class BufferManager : public Singleton<BufferManager> class BufferManager : public Singleton<BufferManager>
{ {
public: public:
typedef std::unordered_map<std::string, std::unique_ptr<Buffer>> BufferMap; typedef std::unordered_map<std::string, Buffer*> BufferMap;
struct iterator : public BufferMap::const_iterator struct iterator : public BufferMap::const_iterator
{ {
@ -23,11 +23,11 @@ public:
iterator() {} iterator() {}
iterator(const parent_type& other) : parent_type(other) {} iterator(const parent_type& other) : parent_type(other) {}
Buffer& operator*() const { return *(parent_type::operator*().second); } Buffer& operator*() const { return *(parent_type::operator*().second); }
Buffer* operator->() const { return parent_type::operator*().second.get(); } Buffer* operator->() const { return parent_type::operator*().second; }
}; };
void register_buffer(Buffer* buffer); void register_buffer(Buffer* buffer);
void delete_buffer(Buffer* buffer); void unregister_buffer(Buffer* buffer);
iterator begin() const { return iterator(m_buffers.begin()); } iterator begin() const { return iterator(m_buffers.begin()); }
iterator end() const { return iterator(m_buffers.end()); } iterator end() const { return iterator(m_buffers.end()); }

View File

@ -77,7 +77,7 @@ Buffer* create_buffer_from_file(const std::string& filename)
std::string content = read_file(filename); std::string content = read_file(filename);
if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
BufferManager::instance().delete_buffer(buffer); delete buffer;
return new Buffer(filename, Buffer::Type::File, content); return new Buffer(filename, Buffer::Type::File, content);
} }