summaryrefslogtreecommitdiff
path: root/src/function_group.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2013-03-27 13:41:41 +0100
committerMaxime Coste <frrrwww@gmail.com>2013-03-27 13:45:22 +0100
commit251f09ff89bdba6cf52b3f9b7764620e0a2307e5 (patch)
tree18c39231e0a603491121eaf5156c922169dd77a1 /src/function_group.hh
parentee0e640384d001f7b3f9c48eff157ee8ad37d4b6 (diff)
Merge {Filter,Highlighter}Group in a FunctionGroup template
Diffstat (limited to 'src/function_group.hh')
-rw-r--r--src/function_group.hh66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/function_group.hh b/src/function_group.hh
new file mode 100644
index 00000000..bafdab27
--- /dev/null
+++ b/src/function_group.hh
@@ -0,0 +1,66 @@
+#ifndef function_group_hh_INCLUDED
+#define function_group_hh_INCLUDED
+
+#include "string.hh"
+#include "idvaluemap.hh"
+#include "exception.hh"
+
+namespace Kakoune
+{
+
+template<typename... Args>
+class FunctionGroup
+{
+public:
+ using Function = std::function<void (Args...)>;
+ using FunctionAndId = std::pair<String, std::function<void (Args...)>>;
+
+ void operator()(Args&&... args)
+ {
+ for (auto& func : m_functions)
+ func.second(std::forward<Args>(args)...);
+ }
+
+ void append(FunctionAndId&& function)
+ {
+ if (m_functions.contains(function.first))
+ throw runtime_error("duplicate id: " + function.first);
+
+ m_functions.append(std::forward<FunctionAndId>(function));
+ }
+ void remove(const String& id)
+ {
+ m_functions.remove(id);
+ }
+
+ FunctionGroup& get_group(const String& id)
+ {
+ 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);
+ return *group;
+ }
+
+ CandidateList complete_id(const String& prefix, ByteCount cursor_pos)
+ {
+ return m_functions.complete_id(prefix, cursor_pos);
+ }
+
+ CandidateList complete_group_id(const String& prefix, ByteCount cursor_pos)
+ {
+ return m_functions.complete_id_if(
+ prefix, cursor_pos, [](FunctionAndId& func) {
+ return func.second.template target<FunctionGroup>() != nullptr;
+ });
+ }
+
+private:
+ idvaluemap<String, Function> m_functions;
+};
+
+}
+
+#endif // function_group_hh_INCLUDED