GCC Code Coverage Report


./
File: mrubytypebinder.hpp
Date: 2024-05-21 01:02:25
Lines:
35/36
97.2%
Functions:
38/41
92.7%
Branches:
13/28
46.4%

Line Branch Exec Source
1 #ifndef __MRUBYTYPEBINDER_HPP__
2 #define __MRUBYTYPEBINDER_HPP__
3
4 template<typename T>
5 struct TypeBinder
6 {
7 };
8
9 /* internally used datatypes */
10
11 template<>
12 struct TypeBinder<RClass*>
13 {
14 29 static mrb_value to_mrb_value(mrb_state* mrb, RClass* cls)
15 {
16 29 return mrb_class_path(mrb, cls);
17 }
18 47 static RClass* from_mrb_value(mrb_state* mrb, mrb_value val)
19 {
20 47 return mrb_class(mrb, val);
21 }
22 };
23
24 template<>
25 struct TypeBinder<RData*>
26 {
27 37 static mrb_value to_mrb_value(mrb_state* mrb, RData* data)
28 {
29 37 mrb_value val = {{ 0 }};
30 37 SET_OBJ_VALUE(val, data);
31 37 return val;
32 }
33 static RData* from_mrb_value(mrb_state* mrb, mrb_value val) { return RDATA(val); }
34 };
35
36 template<>
37 struct TypeBinder<RProc*>
38 {
39 1 static mrb_value to_mrb_value(mrb_state* mrb, RProc* data)
40 {
41 1 mrb_value val = {{ 0 }};
42 1 SET_OBJ_VALUE(val, data);
43 1 return val;
44 }
45 1 static RProc* from_mrb_value(mrb_state* mrb, mrb_value val)
46 {
47 1 return mrb_proc_ptr(val);
48 }
49 };
50
51 template<>
52 struct TypeBinder<mrb_sym>
53 {
54 static mrb_value to_mrb_value(mrb_state* mrb, mrb_sym sym) { return mrb_sym2str(mrb, sym); }
55 static mrb_sym from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_intern_str(mrb, val); }
56 };
57
58 /* public data types */
59
60 template<>
61 struct TypeBinder<bool>
62 {
63 static mrb_value to_mrb_value(mrb_state* mrb, bool b) { return mrb_bool_value(b); }
64 static bool from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_bool(val); }
65 };
66
67 template<>
68 struct TypeBinder<int>
69 {
70 17 static mrb_value to_mrb_value(mrb_state* mrb, int i) { return mrb_fixnum_value(i); }
71 46 static int from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_fixnum(val); }
72 };
73
74 template<>
75 struct TypeBinder<int64_t>
76 {
77 static mrb_value to_mrb_value(mrb_state* mrb, int64_t i) { return mrb_fixnum_value(i); }
78 static int64_t from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_fixnum(val); }
79 };
80
81 template<>
82 struct TypeBinder<float>
83 {
84 static mrb_value to_mrb_value(mrb_state* mrb, float f) { return mrb_float_value(mrb, f); }
85 static float from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_float(val); }
86 };
87
88 template<>
89 struct TypeBinder<double>
90 {
91 static mrb_value to_mrb_value(mrb_state* mrb, double f) { return mrb_float_value(mrb, f); }
92 static double from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_float(val); }
93 };
94
95 template<>
96 struct TypeBinder<size_t>
97 {
98 15 static mrb_value to_mrb_value(mrb_state* mrb, size_t i) { return mrb_fixnum_value(i); }
99 10 static size_t from_mrb_value(mrb_state* mrb, mrb_value val) { return mrb_fixnum(val); }
100 };
101
102 template<>
103 struct TypeBinder<std::string>
104 {
105 static mrb_value to_mrb_value(mrb_state* mrb, std::string str)
106 {
107 Arena arena(mrb);
108 {
109 return mrb_str_new(mrb, str.c_str(), str.size());
110 }
111 }
112 50 static std::string from_mrb_value(mrb_state* mrb, mrb_value val)
113 {
114
2/2
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 3 times.
50 if (mrb_type(val) == MRB_TT_SYMBOL)
115 {
116 47 val = mrb_sym2str(mrb, mrb_symbol(val));
117 }
118
5/6
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 1 times.
✓ Branch 6 taken 49 times.
✓ Branch 7 taken 1 times.
✓ Branch 11 taken 50 times.
✗ Branch 12 not taken.
100 return std::string(RSTRING_PTR(val), RSTRING_LEN(val));
119 }
120 };
121
122 template<>
123 struct TypeBinder<const std::string>
124 {
125 static mrb_value to_mrb_value(mrb_state* mrb, const std::string str)
126 {
127 Arena arena(mrb);
128 {
129 return mrb_str_new(mrb, str.c_str(), str.size());
130 }
131 }
132 static std::string from_mrb_value(mrb_state* mrb, mrb_value val)
133 {
134 if (mrb_type(val) == MRB_TT_SYMBOL)
135 {
136 val = mrb_sym2str(mrb, mrb_symbol(val));
137 }
138 return std::string(RSTRING_PTR(val), RSTRING_LEN(val));
139 }
140 };
141
142 template<>
143 struct TypeBinder<const std::string&>
144 {
145 static mrb_value to_mrb_value(mrb_state* mrb, const std::string& str)
146 {
147 Arena arena(mrb);
148 {
149 return mrb_str_new(mrb, str.c_str(), str.size());
150 }
151 }
152 static std::string from_mrb_value(mrb_state* mrb, mrb_value val)
153 {
154 if (mrb_type(val) == MRB_TT_SYMBOL)
155 {
156 val = mrb_sym2str(mrb, mrb_symbol(val));
157 }
158 return std::string(RSTRING_PTR(val), RSTRING_LEN(val));
159 }
160 };
161
162 // TODO
163 // add specializations for
164 // Array type
165 // Hash type
166 // We might need more than just std::map or std::vector, since objects can be any type.
167
168 template<class TClass>
169 struct TypeBinder< NativeObject<TClass> >
170 {
171 55 static mrb_value to_mrb_value(mrb_state* mrb, NativeObject<TClass> obj)
172 {
173 55 Arena arena(mrb);
174 {
175
2/4
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 37 times.
✗ Branch 6 not taken.
55 RClass* cls = mrb_class_get(mrb, obj.get_classname().c_str());
176
2/6
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 37 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
55 NativeObject<TClass>* objptr = new NativeObject<TClass>(obj);
177
1/2
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
55 RData* data = mrb_data_object_alloc(mrb, cls, objptr, objptr->get_type_ptr());
178
179 110 return TypeBinder<RData*>::to_mrb_value(mrb, data);
180 }
181 55 }
182
183 19 static NativeObject<TClass> from_mrb_value(mrb_state* mrb, mrb_value val)
184 {
185
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
19 if (mrb_type(val) == MRB_TT_DATA)
186 {
187 19 NativeObject<TClass>* thisptr = (NativeObject<TClass>*)DATA_PTR(val);
188 19 return *thisptr;
189 }
190
191 throw TypeError("Not a data type", "");
192 }
193 };
194
195 template<typename TFunc>
196 class Function;
197 template<typename TRet, typename ... TArgs>
198 class Function<TRet(TArgs...)>;
199
200 template<typename TRet, typename ... TArgs>
201 struct TypeBinder< Function<TRet(TArgs...)> >
202 {
203 static mrb_value to_mrb_value(mrb_state* mrb, Function<TRet(TArgs...)> func)
204 {
205 throw NotImplementedError("Not implemented", "");
206 }
207
208 1 static Function<TRet(TArgs...)> from_mrb_value(mrb_state* mrb, mrb_value val)
209 {
210 1 return Function<TRet(TArgs...)>(mrb, TypeBinder<RProc*>::from_mrb_value(mrb, val));
211 }
212 };
213
214 #endif // __MRUBYTYPEBINDER_HPP__
215