GCC Code Coverage Report


./
File: mrubyfunction.hpp
Date: 2024-05-21 01:02:25
Lines:
15/15
100.0%
Functions:
4/4
100.0%
Branches:
4/8
50.0%

Line Branch Exec Source
1 #ifndef __MRUBYFUNCTION_HPP__
2 #define __MRUBYFUNCTION_HPP__
3
4 class BaseFunction
5 {
6 protected:
7 mrb_state* mrb;
8 RProc* proc;
9 1 BaseFunction(mrb_state* mrb, RProc* proc) : mrb(mrb), proc(proc)
10 1 { }
11
12 template<typename TArgHead, typename TArgHead2, typename ... TArgTail>
13 static void push_args(mrb_state* mrb, std::vector<mrb_value>* vector, TArgHead head, TArgHead2 head2, TArgTail ... tail)
14 {
15 vector->push_back(TypeBinder<TArgHead>::to_mrb_value(mrb, head));
16 push_args<TArgHead2, TArgTail...>(mrb, vector, head2, tail...);
17 }
18
19 template<typename TArgHead>
20 1 static void push_args(mrb_state* mrb, std::vector<mrb_value>* vector, TArgHead head)
21 {
22
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 vector->push_back(TypeBinder<TArgHead>::to_mrb_value(mrb, head));
23 1 }
24 };
25
26 template<typename TFunc>
27 class Function : public BaseFunction
28 {
29 Function()
30 { /* private constructor */
31 }
32 };
33
34 template<typename TRet, typename ... TArgs>
35 class Function<TRet(TArgs...)> : public BaseFunction
36 {
37 public:
38 1 Function(mrb_state* mrb, RProc* proc) : BaseFunction(mrb, proc)
39 1 { }
40
41 1 TRet invoke(TArgs... args)
42 {
43 1 size_t argc = sizeof...(TArgs);
44 1 std::vector<mrb_value> argvector;
45
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 push_args(mrb, &argvector, args...);
46
47
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 mrb_sym call = mrb_intern_cstr(mrb, "call");
48
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 return TypeBinder<TRet>::from_mrb_value(
49 mrb,
50 mrb_funcall_argv(
51 mrb,
52 TypeBinder<RProc*>::to_mrb_value(mrb, proc),
53 call,
54 argc,
55 3 &argvector[0]));
56 1 }
57 };
58
59 template<typename ... TArgs>
60 class Function<void(TArgs...)> : public BaseFunction
61 {
62 public:
63 Function(mrb_state* mrb, RProc* proc) : BaseFunction(mrb, proc)
64 { }
65
66 void invoke(TArgs... args)
67 {
68 size_t argc = sizeof...(TArgs);
69 std::vector<mrb_value> argvector;
70 push_args(mrb, &argvector, args...);
71
72 mrb_sym call = mrb_intern_cstr(mrb, "call");
73 mrb_funcall_argv(
74 mrb,
75 TypeBinder<RProc*>::to_mrb_value(mrb, proc),
76 call,
77 argc,
78 &argvector[0]);
79 }
80 };
81
82 template<typename TRet>
83 class Function<TRet()> : public BaseFunction
84 {
85 public:
86 Function(mrb_state* mrb, RProc* proc) : BaseFunction(mrb, proc)
87 { }
88
89 TRet invoke()
90 {
91 mrb_value no = mrb_nil_value();
92
93 mrb_sym call = mrb_intern_cstr(mrb, "call");
94 return TypeBinder<TRet>::from_mrb_value(
95 mrb,
96 mrb_funcall_argv(mrb, TypeBinder<RProc*>::to_mrb_value(mrb, proc), call, 0, &no));
97 }
98 };
99
100 template<>
101 class Function< void() > : public BaseFunction
102 {
103 public:
104 Function(mrb_state* mrb, RProc* proc) : BaseFunction(mrb, proc)
105 { }
106
107 void invoke()
108 {
109 mrb_value no = mrb_nil_value();
110
111 mrb_sym call = mrb_intern_cstr(mrb, "call");
112 mrb_funcall_argv(mrb, TypeBinder<RProc*>::to_mrb_value(mrb, proc), call, 0, &no);
113 }
114 };
115 #endif // __MRUBYFUNCTION_HPP__
116