home/src/buffer_manager.cc
Maxime Coste 26537d7028 Utils: Singleton template and migration of the BufferManager
Singletons are not autocreating, the application needs to create
exactly one instance (I want to avoid implicit initialization order
hell)
2011-09-23 14:26:53 +00:00

41 lines
824 B
C++

#include "buffer_manager.hh"
#include "assert.hh"
#include "buffer.hh"
#include "window.hh"
#include "exception.hh"
namespace Kakoune
{
struct name_not_unique : logic_error {};
void BufferManager::register_buffer(Buffer* buffer)
{
assert(buffer);
const std::string& name = buffer->name();
if (m_buffers.find(name) != m_buffers.end())
throw name_not_unique();
m_buffers[name] = std::unique_ptr<Buffer>(buffer);
}
void BufferManager::delete_buffer(Buffer* buffer)
{
assert(buffer);
auto buffer_it = m_buffers.find(buffer->name());
assert(buffer_it->second.get() == buffer);
m_buffers.erase(buffer_it);
}
Buffer* BufferManager::get_buffer(const std::string& name)
{
if (m_buffers.find(name) == m_buffers.end())
return nullptr;
return m_buffers[name].get();
}
}