2013-03-27 13:41:41 +01:00
|
|
|
#ifndef function_group_hh_INCLUDED
|
|
|
|
#define function_group_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "exception.hh"
|
2013-11-18 23:24:31 +01:00
|
|
|
#include "id_map.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2013-03-27 13:41:41 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
class FunctionGroup
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Function = std::function<void (Args...)>;
|
|
|
|
using FunctionAndId = std::pair<String, std::function<void (Args...)>>;
|
|
|
|
|
2014-01-17 14:13:08 +01:00
|
|
|
void operator()(Args... args)
|
2013-03-27 13:41:41 +01:00
|
|
|
{
|
|
|
|
for (auto& func : m_functions)
|
|
|
|
func.second(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(FunctionAndId&& function)
|
|
|
|
{
|
2013-11-18 23:17:50 +01:00
|
|
|
if (m_functions.contains(function.first))
|
2013-03-27 13:41:41 +01:00
|
|
|
throw runtime_error("duplicate id: " + function.first);
|
|
|
|
|
2013-11-18 23:17:50 +01:00
|
|
|
m_functions.append(std::forward<FunctionAndId>(function));
|
2013-03-27 13:41:41 +01:00
|
|
|
}
|
|
|
|
void remove(const String& id)
|
|
|
|
{
|
2013-11-18 23:17:50 +01:00
|
|
|
m_functions.remove(id);
|
2013-03-27 13:41:41 +01:00
|
|
|
}
|
|
|
|
|
2013-12-03 23:03:10 +01:00
|
|
|
FunctionGroup& get_group(const String& path, Codepoint path_separator = 0)
|
2013-03-27 13:41:41 +01:00
|
|
|
{
|
2013-12-03 23:03:10 +01:00
|
|
|
auto sep_it = std::find(path.begin(), path.end(), path_separator);
|
|
|
|
String id(path.begin(), sep_it);
|
2013-03-27 13:41:41 +01:00
|
|
|
auto it = m_functions.find(id);
|
|
|
|
if (it == m_functions.end())
|
|
|
|
throw runtime_error("no such id: " + id);
|
|
|
|
FunctionGroup* group = it->second.template target<FunctionGroup>();
|
|
|
|
if (not group)
|
|
|
|
throw runtime_error("not a group: " + id);
|
2013-12-03 23:03:10 +01:00
|
|
|
if (sep_it != path.end())
|
|
|
|
return group->get_group(String(sep_it+1, path.end()), path_separator);
|
|
|
|
else
|
|
|
|
return *group;
|
2013-03-27 13:41:41 +01:00
|
|
|
}
|
|
|
|
|
2013-03-27 14:09:09 +01:00
|
|
|
CandidateList complete_id(const String& prefix, ByteCount cursor_pos) const
|
2013-03-27 13:41:41 +01:00
|
|
|
{
|
2013-11-18 23:17:50 +01:00
|
|
|
return m_functions.complete_id(prefix, cursor_pos);
|
2013-03-27 13:41:41 +01:00
|
|
|
}
|
|
|
|
|
2013-03-27 14:09:09 +01:00
|
|
|
CandidateList complete_group_id(const String& prefix, ByteCount cursor_pos) const
|
2013-03-27 13:41:41 +01:00
|
|
|
{
|
2013-11-18 23:17:50 +01:00
|
|
|
return m_functions.complete_id_if(
|
|
|
|
prefix, cursor_pos, [](const FunctionAndId& func) {
|
2013-03-27 13:41:41 +01:00
|
|
|
return func.second.template target<FunctionGroup>() != nullptr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-18 23:24:31 +01:00
|
|
|
id_map<Function> m_functions;
|
2013-03-27 13:41:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // function_group_hh_INCLUDED
|