ClientManager: support naming clients and accessing client's context by name

This commit is contained in:
Maxime Coste 2012-12-03 18:56:53 +01:00
parent e8df81a39d
commit 4a3f9d6187
3 changed files with 38 additions and 0 deletions

View File

@ -95,6 +95,33 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
m_windows.erase(end, m_windows.end()); m_windows.erase(end, m_windows.end());
} }
void ClientManager::set_client_name(Context& context, String name)
{
auto it = find_if(m_clients, [&name](std::unique_ptr<Client>& client)
{ return client->name == name; });
if (it != m_clients.end() and &(*it)->context != &context)
throw runtime_error("name not unique: " + name);
for (auto& client : m_clients)
{
if (&client->context == &context)
{
client->name = std::move(name);
return;
}
}
throw runtime_error("no client for current context");
}
Context& ClientManager::get_client_context(const String& name)
{
auto it = find_if(m_clients, [&name](std::unique_ptr<Client>& client)
{ return client->name == name; });
if (it != m_clients.end())
return (*it)->context;
throw runtime_error("no client named: " + name);
}
void ClientManager::redraw_clients() const void ClientManager::redraw_clients() const
{ {
for (auto& client : m_clients) for (auto& client : m_clients)

View File

@ -22,6 +22,9 @@ public:
void ensure_no_client_uses_buffer(Buffer& buffer); void ensure_no_client_uses_buffer(Buffer& buffer);
void redraw_clients() const; void redraw_clients() const;
void set_client_name(Context& context, String name);
Context& get_client_context(const String& name);
private: private:
void remove_client_by_context(Context& context); void remove_client_by_context(Context& context);
@ -36,6 +39,7 @@ private:
std::unique_ptr<UserInterface> user_interface; std::unique_ptr<UserInterface> user_interface;
InputHandler input_handler; InputHandler input_handler;
Context context; Context context;
String name;
}; };
std::vector<std::unique_ptr<Client>> m_clients; std::vector<std::unique_ptr<Client>> m_clients;

View File

@ -148,6 +148,13 @@ auto find(Container& container, const T& value) -> decltype(container.begin())
return std::find(container.begin(), container.end(), value); return std::find(container.begin(), container.end(), value);
} }
template<typename Container, typename T>
auto find_if(Container& container, T op) -> decltype(container.begin())
{
return std::find_if(container.begin(), container.end(), op);
}
template<typename Container, typename T> template<typename Container, typename T>
bool contains(const Container& container, const T& value) bool contains(const Container& container, const T& value)
{ {