summaryrefslogtreecommitdiff
path: root/src/function_registry.hh
blob: 1565c36a5fc9e0606b8e59e929f273a1a90f4d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef function_registry_h_INCLUDED
#define function_registry_h_INCLUDED

#include "completion.hh"
#include "id_map.hh"
#include "string.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(StringView name, const FunctionType& function)
    {
        kak_assert(not m_functions.contains(name));
        m_functions.append(std::make_pair(name, function));
    }

    const FunctionType& operator[](StringView name) const
    {
        auto it = m_functions.find(name);
        if (it == m_functions.end())
            throw function_not_found(name);
        return it->second;
    }

    CandidateList complete_name(StringView prefix, ByteCount cursor_pos)
    {
        return m_functions.complete_id(prefix, cursor_pos);
    }

private:
    id_map<FunctionType> m_functions;
};

}

#endif // function_registry_h_INCLUDED