diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-11-23 13:40:20 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-11-23 13:40:20 +0100 |
| commit | d2f811a8d5ef527eb98da7acdee93fa8f00131f2 (patch) | |
| tree | 151b9e0b9c2dbc5c952f329b919d2aa772ce0a25 /src/function_registry.hh | |
| parent | 11e885e5a5e31f75cfb18dd02907b86e3c40b8da (diff) | |
Refactor filter and highlighter registry into a common template
Diffstat (limited to 'src/function_registry.hh')
| -rw-r--r-- | src/function_registry.hh | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/function_registry.hh b/src/function_registry.hh new file mode 100644 index 00000000..058c4aa3 --- /dev/null +++ b/src/function_registry.hh @@ -0,0 +1,46 @@ +#ifndef function_registry_h_INCLUDED +#define function_registry_h_INCLUDED + +#include "string.hh" +#include "completion.hh" +#include "idvaluemap.hh" + +namespace Kakoune +{ + +struct function_not_found : runtime_error +{ + function_not_found(const String& name) + : runtime_error("'" + name + "' not found") {} +}; + +template<typename FunctionType> +class FunctionRegistry +{ +public: + void register_func(const String& name, const FunctionType& function) + { + assert(not m_functions.contains(name)); + m_functions.append(std::make_pair(name, function)); + } + + const FunctionType& operator[](const String& name) const + { + auto it = m_functions.find(name); + if (it == m_functions.end()) + throw function_not_found(name); + return it->second; + } + + CandidateList complete_name(const String& prefix, ByteCount cursor_pos) + { + return m_functions.complete_id(prefix, cursor_pos); + } + +private: + idvaluemap<String, FunctionType> m_functions; +}; + +} + +#endif // function_registry_h_INCLUDED |
