Use a IdMap for storing hooks

The number of hook names is small (<20), using an UnorderedMap
is overkill, and prevents using StringView for key searching.
This commit is contained in:
Maxime Coste 2015-03-05 14:59:27 +00:00
parent d1f3c1832c
commit 99996902de
7 changed files with 24 additions and 9 deletions

View File

@ -516,7 +516,7 @@ void Buffer::on_option_changed(const Option& option)
option.name() + "=" + option.get_as_string()); option.name() + "=" + option.get_as_string());
} }
void Buffer::run_hook_in_own_context(const String& hook_name, StringView param) void Buffer::run_hook_in_own_context(StringView hook_name, StringView param)
{ {
InputHandler hook_handler({ *this, Selection{} }, Context::Flags::Transient); InputHandler hook_handler({ *this, Selection{} }, Context::Flags::Transient);
hooks().run_hook(hook_name, param, hook_handler.context()); hooks().run_hook(hook_name, param, hook_handler.context());

View File

@ -150,7 +150,7 @@ public:
ValueMap& values() const { return m_values; } ValueMap& values() const { return m_values; }
void run_hook_in_own_context(const String& hook_name, StringView param); void run_hook_in_own_context(StringView hook_name, StringView param);
void reload(BufferLines lines, time_t fs_timestamp = InvalidTime); void reload(BufferLines lines, time_t fs_timestamp = InvalidTime);

View File

@ -8,7 +8,7 @@
namespace Kakoune namespace Kakoune
{ {
void HookManager::add_hook(const String& hook_name, String group, HookFunc hook) void HookManager::add_hook(StringView hook_name, String group, HookFunc hook)
{ {
auto& hooks = m_hook[hook_name]; auto& hooks = m_hook[hook_name];
hooks.append({std::move(group), std::move(hook)}); hooks.append({std::move(group), std::move(hook)});
@ -37,7 +37,7 @@ CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_
return res; return res;
} }
void HookManager::run_hook(const String& hook_name, void HookManager::run_hook(StringView hook_name,
StringView param, Context& context) const StringView param, Context& context) const
{ {
if (m_parent) if (m_parent)

View File

@ -16,10 +16,10 @@ class HookManager
public: public:
HookManager(HookManager& parent) : m_parent(&parent) {} HookManager(HookManager& parent) : m_parent(&parent) {}
void add_hook(const String& hook_name, String group, HookFunc hook); void add_hook(StringView hook_name, String group, HookFunc hook);
void remove_hooks(StringView group); void remove_hooks(StringView group);
CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token); CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token);
void run_hook(const String& hook_name, StringView param, void run_hook(StringView hook_name, StringView param,
Context& context) const; Context& context) const;
private: private:
@ -29,7 +29,7 @@ private:
friend class Scope; friend class Scope;
HookManager* m_parent; HookManager* m_parent;
UnorderedMap<String, IdMap<HookFunc, MemoryDomain::Hooks>, MemoryDomain::Hooks> m_hook; IdMap<IdMap<HookFunc, MemoryDomain::Hooks>, MemoryDomain::Hooks> m_hook;
}; };
} }

View File

@ -59,6 +59,21 @@ public:
m_content.erase(it, end()); m_content.erase(it, end());
} }
Value& operator[](StringView id)
{
auto it = find(id);
if (it != m_content.end())
return it->second;
append({ id, Value{} });
return (m_content.end()-1)->second;
}
const Value& operator[](StringView id) const
{
return (*const_cast<IdMap*>(this))[id];
}
static const String& get_id(const value_type& v) { return v.first; } static const String& get_id(const value_type& v) { return v.first; }
bool empty() const { return m_content.empty(); } bool empty() const { return m_content.empty(); }

View File

@ -282,7 +282,7 @@ void Window::on_option_changed(const Option& option)
} }
void Window::run_hook_in_own_context(const String& hook_name, StringView param) void Window::run_hook_in_own_context(StringView hook_name, StringView param)
{ {
InputHandler hook_handler({ *m_buffer, Selection{} }, Context::Flags::Transient); InputHandler hook_handler({ *m_buffer, Selection{} }, Context::Flags::Transient);
hook_handler.context().set_window(*this); hook_handler.context().set_window(*this);

View File

@ -50,7 +50,7 @@ private:
void on_option_changed(const Option& option) override; void on_option_changed(const Option& option) override;
void scroll_to_keep_selection_visible_ifn(const Context& context); void scroll_to_keep_selection_visible_ifn(const Context& context);
void run_hook_in_own_context(const String& hook_name, StringView param); void run_hook_in_own_context(StringView hook_name, StringView param);
SafePtr<Buffer> m_buffer; SafePtr<Buffer> m_buffer;