Refactor BufferManager
This commit is contained in:
parent
b29b75aa81
commit
ce99856bfa
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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>(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>(&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())
|
||||
for (auto& buf : m_buffers)
|
||||
{
|
||||
if (buf->name() == name)
|
||||
return buf.get();
|
||||
}
|
||||
return nullptr;
|
||||
|
||||
return m_buffers[name].get();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -14,25 +14,16 @@ class Buffer;
|
|||
class BufferManager : public Singleton<BufferManager>
|
||||
{
|
||||
public:
|
||||
typedef std::unordered_map<String, safe_ptr<Buffer>> 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<safe_ptr<Buffer>>;
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -293,8 +293,8 @@ void quit(const CommandParameters& params, Context& context)
|
|||
std::vector<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user