ClientManager: store client with a unique_ptr

This commit is contained in:
Maxime Coste 2012-11-22 14:17:46 +01:00
parent 08ad8e8a40
commit 7bcd0353cb
2 changed files with 18 additions and 30 deletions

View File

@ -9,10 +9,10 @@ namespace Kakoune
void ClientManager::create_client(std::unique_ptr<UserInterface>&& ui, void ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
Buffer& buffer, int event_fd) Buffer& buffer, int event_fd)
{ {
m_clients.emplace_back(std::move(ui), get_unused_window_for_buffer(buffer)); m_clients.emplace_back(new Client{std::move(ui), get_unused_window_for_buffer(buffer)});
InputHandler* input_handler = m_clients.back().input_handler.get(); InputHandler* input_handler = &m_clients.back()->input_handler;
Context* context = m_clients.back().context.get(); Context* context = &m_clients.back()->context;
EventManager::instance().watch(event_fd, [input_handler, context, this](int fd) { EventManager::instance().watch(event_fd, [input_handler, context, this](int fd) {
try try
{ {
@ -40,7 +40,7 @@ void ClientManager::remove_client_by_context(Context& context)
{ {
for (auto it = m_clients.begin(); it != m_clients.end(); ++it) for (auto it = m_clients.begin(); it != m_clients.end(); ++it)
{ {
if (it->context.get() == &context) if (&(*it)->context == &context)
{ {
m_clients.erase(it); m_clients.erase(it);
return; return;
@ -57,9 +57,8 @@ Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer)
continue; continue;
auto it = std::find_if(m_clients.begin(), m_clients.end(), auto it = std::find_if(m_clients.begin(), m_clients.end(),
[&](const Client& client) { [&](const std::unique_ptr<Client>& client)
return &client.context->window() == w.get(); { return &client->context.window() == w.get(); });
});
if (it == m_clients.end()) if (it == m_clients.end())
{ {
w->forget_timestamp(); w->forget_timestamp();
@ -74,9 +73,9 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
{ {
for (auto& client : m_clients) for (auto& client : m_clients)
{ {
client.context->forget_jumps_to_buffer(buffer); client->context.forget_jumps_to_buffer(buffer);
if (&client.context->buffer() != &buffer) if (&client->context.buffer() != &buffer)
continue; continue;
// change client context to edit the first buffer which is not the // change client context to edit the first buffer which is not the
@ -87,13 +86,13 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
if (buf != &buffer) if (buf != &buffer)
{ {
Window& w = get_unused_window_for_buffer(*buf); Window& w = get_unused_window_for_buffer(*buf);
client.context->change_editor(w); client->context.change_editor(w);
break; break;
} }
} }
} }
auto end = std::remove_if(m_windows.begin(), m_windows.end(), auto end = std::remove_if(m_windows.begin(), m_windows.end(),
[&buffer](std::unique_ptr<Window>& w) [&buffer](const std::unique_ptr<Window>& w)
{ return &w->buffer() == &buffer; }); { return &w->buffer() == &buffer; });
m_windows.erase(end, m_windows.end()); m_windows.erase(end, m_windows.end());
} }
@ -102,7 +101,7 @@ void ClientManager::redraw_clients() const
{ {
for (auto& client : m_clients) for (auto& client : m_clients)
{ {
Context& context = *client.context; Context& context = client->context;
if (context.window().timestamp() != context.buffer().timestamp()) if (context.window().timestamp() != context.buffer().timestamp())
{ {
DisplayCoord dimensions = context.ui().dimensions(); DisplayCoord dimensions = context.ui().dimensions();

View File

@ -28,28 +28,17 @@ private:
struct Client struct Client
{ {
Client(std::unique_ptr<UserInterface>&& ui, Window& window) Client(std::unique_ptr<UserInterface>&& ui, Window& window)
: user_interface(std::move(ui)), input_handler(new InputHandler{}), : user_interface(std::move(ui)),
context(new Context(*input_handler, window, *user_interface)) context(input_handler, window, *user_interface) {}
{} Client(Client&&) = delete;
Client& operator=(Client&& other) = delete;
Client(Client&&) = default;
Client& operator=(Client&& other)
{
// drop safe pointers first
context.reset();
user_interface = std::move(other.user_interface);
input_handler = std::move(other.input_handler);
context = std::move(other.context);
return *this;
}
std::unique_ptr<UserInterface> user_interface; std::unique_ptr<UserInterface> user_interface;
std::unique_ptr<InputHandler> input_handler; InputHandler input_handler;
std::unique_ptr<Context> context; Context context;
}; };
std::vector<Client> m_clients; std::vector<std::unique_ptr<Client>> m_clients;
std::vector<std::unique_ptr<Window>> m_windows; std::vector<std::unique_ptr<Window>> m_windows;
}; };