summaryrefslogtreecommitdiff
path: root/lib/callable.cpp
blob: a6417bb696e03c18a4be3b7605de957ebaf02ae4 (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
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include <gras/callable.hpp>
#include <boost/shared_ptr.hpp>
#include <stdexcept>
#include <map>

using namespace gras;

typedef std::map<std::string, boost::shared_ptr<CallableRegistryEntry> > CallableRegistry;

Callable::Callable(void)
{
    _call_registry = new CallableRegistry();
}

Callable::~Callable(void)
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry);
    delete cr;
}

void Callable::_register_call(const std::string &key, void *entry)
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry);
    (*cr)[key].reset(reinterpret_cast<CallableRegistryEntry *>(entry));
}

PMCC Callable::_handle_call(const std::string &key, const std::vector<PMCC> &args)
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry);
    boost::shared_ptr<CallableRegistryEntry> entry = (*cr)[key];
    if (not entry) throw std::invalid_argument("Callable: no method registered for key: " + key);
    return entry->call(args);
}

CallableRegistryEntry::CallableRegistryEntry(void)
{
    //NOP
}

CallableRegistryEntry::~CallableRegistryEntry(void)
{
    //NOP
}