-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmrubybind_types.h
109 lines (89 loc) · 3.24 KB
/
mrubybind_types.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef __MRUBYBIND_TYPES_H__
#define __MRUBYBIND_TYPES_H__
#include <mruby.h>
#include <mruby/string.h>
#include <string>
#include <assert.h>
#define ASSERT(expr) assert(expr)
namespace mrubybind {
//===========================================================================
// C <-> mruby type converter.
template <class T>
struct Type {
//static int check(mrb_value v) = 0;
//static int get(mrb_value v) = 0;
//static mrb_value ret(mrb_state*, int i) = 0;
};
// Fixnum
template<>
struct Type<int> {
static int check(mrb_value v) { return mrb_fixnum_p(v); }
static int get(mrb_value v) { return mrb_fixnum(v); }
static mrb_value ret(mrb_state*, int i) { return mrb_fixnum_value(i); }
};
// float
template<>
struct Type<float> {
static int check(mrb_value v) { return mrb_float_p(v); }
static float get(mrb_value v) { return mrb_float(v); }
static mrb_value ret(mrb_state*, float f) { return mrb_float_value(f); }
};
// double
template<>
struct Type<mrb_float> {
static int check(mrb_value v) { return mrb_float_p(v); }
static double get(mrb_value v) { return mrb_float(v); }
static mrb_value ret(mrb_state*, double f) { return mrb_float_value(f); }
};
// String
template<>
struct Type<const char*> {
static int check(mrb_value v) { return mrb_string_p(v); }
static const char* get(mrb_value v) { return RSTRING_PTR(v); }
static mrb_value ret(mrb_state* mrb, const char* s) { return mrb_str_new_cstr(mrb, s); }
};
template<>
struct Type<std::string> {
static int check(mrb_value v) { return mrb_string_p(v); }
static const std::string get(mrb_value v) { return std::string(RSTRING_PTR(v), RSTRING_LEN(v)); }
static mrb_value ret(mrb_state* mrb, const std::string& s) { return mrb_str_new(mrb, s.c_str(), s.size()); }
};
template<>
struct Type<const std::string> {
static int check(mrb_value v) { return mrb_string_p(v); }
static const std::string get(mrb_value v) { return std::string(RSTRING_PTR(v), RSTRING_LEN(v)); }
static mrb_value ret(mrb_state* mrb, const std::string& s) { return mrb_str_new(mrb, s.c_str(), s.size()); }
};
template<>
struct Type<const std::string&> {
static int check(mrb_value v) { return mrb_string_p(v); }
static const std::string get(mrb_value v) { return std::string(RSTRING_PTR(v), RSTRING_LEN(v)); }
static mrb_value ret(mrb_state* mrb, const std::string& s) { return mrb_str_new(mrb, s.c_str(), s.size()); }
};
// Boolean
template<>
struct Type<bool> {
static int check(mrb_value v) { return 1; }
static bool get(mrb_value v) { return mrb_test(v); }
static mrb_value ret(mrb_state* mrb, bool b) { return b ? mrb_true_value() : mrb_false_value(); }
};
// Raw pointer
template<>
struct Type<void*> {
static int check(mrb_value v) { return mrb_voidp_p(v); }
static void* get(mrb_value v) { return mrb_voidp(v); }
static mrb_value ret(mrb_state* mrb, void* p) { return mrb_voidp_value(p); }
};
//===========================================================================
// Binder
// Template class for Binder.
// Binder template class is specialized with type.
template <class T>
struct Binder {
//static mrb_value call(mrb_state* mrb, void* p, mrb_value* args, int narg) = 0;
};
// Includes generated template specialization.
#include "mrubybind.inc"
} // namespace mrubybind
#undef ASSERT
#endif