-
Notifications
You must be signed in to change notification settings - Fork 12
/
mrubybind.h
47 lines (38 loc) · 1.35 KB
/
mrubybind.h
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
#ifndef __MRUBYBIND_H__
#define __MRUBYBIND_H__
#include "mrubybind_types.h"
namespace mrubybind {
class MrubyBind {
public:
MrubyBind(mrb_state* mrb);
// Bind function.
template <class Func>
void bind(const char* name, Func f) {
mrb_value binder = mrb_voidp_value((void*)Binder<Func>::call);
mrb_value fn = mrb_str_new_cstr(mrb, name);
mrb_value fp = mrb_voidp_value((void*)f);
mrb_funcall(mrb, mod_mrubybind, "define_function", 3, binder, fn, fp);
}
// Bind class.
template <class Func>
void define_class(const char* class_name, Func f) {
mrb_value binder = mrb_voidp_value((void*)Binder<Func>::call);
mrb_value cn = mrb_str_new_cstr(mrb, class_name);
mrb_value fp = mrb_voidp_value((void*)f);
mrb_funcall(mrb, mod_mrubybind, "create_class", 3, binder, cn, fp);
}
// Bind class method.
template <class Method>
void define_class_method(const char* class_name, const char* method_name, Method m) {
mrb_value binder = mrb_voidp_value((void*)Binder<Method>::call);
mrb_value cn = mrb_str_new_cstr(mrb, class_name);
mrb_value mn = mrb_str_new_cstr(mrb, method_name);
mrb_value mp = mrb_str_new(mrb, (char*)&m, sizeof(m));
mrb_funcall(mrb, mod_mrubybind, "define_class_method", 4, binder, cn, mn, mp);
}
private:
mrb_state* mrb;
mrb_value mod_mrubybind;
};
} // namespace mrubybind
#endif