summaryrefslogtreecommitdiff
path: root/lib/callable.cpp
blob: 461eebdc9a6a3bd7ac27abebbaffac21fdee9aef (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

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

using namespace gras;

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

Callable::Callable(void)
{
    _call_registry.reset(new CallableRegistry());
}

Callable::~Callable(void)
{
    _call_registry.reset();
}

std::vector<std::string> Callable::get_registered_names(void) const
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry.get());
    std::vector<std::string> names;
    BOOST_FOREACH(const CallableRegistryPair &p, (*cr))
    {
        names.push_back(p.first);
    }
    return names;
}

void Callable::unregister_call(const std::string &name)
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry.get());
    if (cr->count(name) == 0) throw std::invalid_argument("Callable - no method registered for name: " + name);
    cr->erase(name);
}

void Callable::_register_call(const std::string &name, void *entry)
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry.get());
    if (cr->count(name) != 0) throw std::invalid_argument("Callable - method already registered for name: " + name);
    (*cr)[name].reset(reinterpret_cast<CallableRegistryEntry *>(entry));
}

PMCC Callable::_handle_call(const std::string &name, const PMCC &args)
{
    CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry.get());
    if (cr->count(name) == 0) throw std::invalid_argument("Callable - no method registered for name: " + name);
    return (*cr)[name]->call(args);
}

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

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

void CallableRegistryEntry::arg_check(const PMCList &args, const size_t nargs)
{
    if (args.size() != nargs) throw std::runtime_error(str(boost::format(
        "callable expected %u arguments but got %u") % nargs % args.size()));
}