Repository navigation
Expand file tree
/
Copy path_dispatcherimpl.cpp
More file actions
85 lines (71 loc) · 2.05 KB
/
Copy path_dispatcherimpl.cpp
File metadata and controls
85 lines (71 loc) · 2.05 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "typeconv/typeconv.hpp"
#include <cassert>
#include <vector>
typedef std::vector<Type> TypeTable;
typedef std::vector<void*> Functions;
class Dispatcher {
public:
Dispatcher(TypeManager *tm, int argct): argct(argct), tm(tm) { }
void addDefinition(Type args[], void *callable) {
overloads.reserve(argct + overloads.size());
for (int i=0; i<argct; ++i) {
overloads.push_back(args[i]);
}
functions.push_back(callable);
}
void* resolve(Type sig[], int &matches) {
const int ovct = functions.size();
int selected;
matches = 0;
if (0 == ovct) {
return NULL;
}
if (overloads.size() > 0) {
matches = tm->selectOverload(sig, &overloads[0], selected, argct,
ovct);
} else if (argct == 0){
matches = 1;
selected = 0;
}
if (matches == 1){
return functions[selected];
}
return NULL;
}
int count() const { return functions.size(); }
const int argct;
private:
TypeManager *tm;
TypeTable overloads;
Functions functions;
};
#include "_dispatcher.h"
void*
dispatcher_new(void *tm, int argct){
return new Dispatcher(static_cast<TypeManager*>(tm), argct);
}
void
dispatcher_del(void *obj) {
Dispatcher *disp = static_cast<Dispatcher*>(obj);
delete disp;
}
void
dispatcher_add_defn(void *obj, int tys[], void* callable) {
assert(sizeof(int) == sizeof(Type) &&
"Type should be representable by an int");
Dispatcher *disp = static_cast<Dispatcher*>(obj);
Type *args = reinterpret_cast<Type*>(tys);
disp->addDefinition(args, callable);
}
void*
dispatcher_resolve(void* obj, int sig[], int *count) {
Dispatcher *disp = static_cast<Dispatcher*>(obj);
Type *args = reinterpret_cast<Type*>(sig);
void *callable = disp->resolve(args, *count);
return callable;
}
int
dispatcher_count(void *obj) {
Dispatcher *disp = static_cast<Dispatcher*>(obj);
return disp->count();
}