mirror of
https://github.com/mawww/kakoune.git
synced 2024-11-23 15:23:29 +03:00
Add a BufferManager singleton class, handling buffer lifetime
it also provide access to buffers by name
This commit is contained in:
parent
511df5b660
commit
535e200587
@ -1,4 +1,7 @@
|
|||||||
#include "buffer.hh"
|
#include "buffer.hh"
|
||||||
|
|
||||||
|
#include "buffer_manager.hh"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
@ -122,6 +125,8 @@ Buffer::Buffer(const std::string& name, const BufferString& initial_content)
|
|||||||
: m_name(name), m_history(1), m_history_cursor(m_history.begin()),
|
: m_name(name), m_history(1), m_history_cursor(m_history.begin()),
|
||||||
m_content(initial_content)
|
m_content(initial_content)
|
||||||
{
|
{
|
||||||
|
BufferManager::instance().register_buffer(this);
|
||||||
|
|
||||||
compute_lines();
|
compute_lines();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
60
src/buffer_manager.cc
Normal file
60
src/buffer_manager.cc
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include "buffer_manager.hh"
|
||||||
|
|
||||||
|
#include "buffer.hh"
|
||||||
|
#include "window.hh"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
namespace Kakoune
|
||||||
|
{
|
||||||
|
|
||||||
|
struct name_not_unique {};
|
||||||
|
|
||||||
|
|
||||||
|
BufferManager* BufferManager::ms_instance = nullptr;
|
||||||
|
|
||||||
|
BufferManager& BufferManager::instance()
|
||||||
|
{
|
||||||
|
if (not ms_instance)
|
||||||
|
ms_instance = new BufferManager();
|
||||||
|
|
||||||
|
return *ms_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BufferManager::delete_instance()
|
||||||
|
{
|
||||||
|
delete ms_instance;
|
||||||
|
ms_instance = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferManager::BufferManager()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
32
src/buffer_manager.hh
Normal file
32
src/buffer_manager.hh
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef buffer_manager_hh_INCLUDED
|
||||||
|
#define buffer_manager_hh_INCLUDED
|
||||||
|
|
||||||
|
#include "buffer.hh"
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace Kakoune
|
||||||
|
{
|
||||||
|
|
||||||
|
class BufferManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void register_buffer(Buffer* buffer);
|
||||||
|
void delete_buffer(Buffer* buffer);
|
||||||
|
|
||||||
|
Buffer* get_buffer(const std::string& name);
|
||||||
|
|
||||||
|
static BufferManager& instance();
|
||||||
|
static void delete_instance();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BufferManager();
|
||||||
|
static BufferManager* ms_instance;
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::unique_ptr<Buffer>> m_buffers;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // buffer_manager_hh_INCLUDED
|
Loading…
Reference in New Issue
Block a user