From 9657ef88b7056b6f84007ac886c8d7d45cf94905 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 14 Mar 2015 11:27:01 +0000 Subject: [PATCH] Small refactoring in client_manager.cc --- src/client_manager.cc | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index 974be41f..26a278ae 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -79,21 +79,18 @@ void ClientManager::remove_client(Client& client) WindowAndSelections ClientManager::get_free_window(Buffer& buffer) { - for (auto it = m_free_windows.rbegin(), end = m_free_windows.rend(); - it != end; ++it) - { - auto& w = it->window; - if (&w->buffer() == &buffer) - { - w->forget_timestamp(); - WindowAndSelections res = std::move(*it); - m_free_windows.erase(it.base()-1); - res.selections.update(); - return res; - } - } - return WindowAndSelections{ std::unique_ptr{new Window{buffer}}, - SelectionList{ buffer, Selection{} } }; + auto it = find_if(reversed(m_free_windows), + [&](const WindowAndSelections& ws) + { return &ws.window->buffer() == &buffer; }); + + if (it == m_free_windows.rend()) + return { make_unique(buffer), { buffer, Selection{} } }; + + it->window->forget_timestamp(); + WindowAndSelections res = std::move(*it); + m_free_windows.erase(it.base()-1); + res.selections.update(); + return res; } void ClientManager::add_free_window(std::unique_ptr&& window, SelectionList selections) @@ -136,9 +133,7 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) bool ClientManager::validate_client_name(StringView name) const { - auto it = find_if(m_clients, [&](const std::unique_ptr& client) - { return client->context().name() == name; }); - return it == m_clients.end(); + return const_cast(this)->get_client_ifp(name) == nullptr; } Client* ClientManager::get_client_ifp(StringView name)