GCC Code Coverage Report


./
File: mrubyfunctional.hpp
Date: 2024-05-21 01:02:25
Lines:
28/36
77.8%
Functions:
29/44
65.9%
Branches:
21/68
30.9%

Line Branch Exec Source
1 #ifndef __MRUBYFUNCTIONAL_HPP__
2 #define __MRUBYFUNCTIONAL_HPP__
3
4 template<typename TFunc>
5 struct currier;
6
7 template<typename TRet, typename TArg>
8 struct currier< std::function<TRet(TArg)> >
9 {
10 using type = std::function<TRet(TArg)>;
11 const type result;
12
13 12 currier(const type fun) : result(fun) {}
14 };
15
16 template<typename TRet, typename TArgHead, typename ...TArgs>
17 struct currier< std::function<TRet(TArgHead, TArgs...)> >
18 {
19 using remaining_type = typename currier< std::function<TRet(TArgs...)> >::type;
20 using type = std::function<remaining_type(TArgHead)>;
21
22 const type result;
23
24 currier(const std::function<TRet(TArgHead, TArgs...)> fun) : result(
25 [=](const TArgHead& t)
26 {
27 return currier< std::function<TRet(TArgs...)> >(
28 [=](const TArgs&... ts)
29 {
30 return fun(t, ts...);
31 }
32 ).result;
33
34 }
35 ) {} // : result(
36 };
37
38 template <typename TRet, typename ...TArgs>
39 12 static auto curry(const std::function<TRet(TArgs...)> fun)
40 -> typename currier< std::function< TRet(TArgs...) > >::type
41 {
42
3/6
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
12 return currier< std::function< TRet(TArgs...) > >(fun).result;
43 }
44
45 template <typename TRet, typename ...TArgs>
46 static auto curry(TRet(*const fun)(TArgs...))
47 -> typename currier< std::function< TRet(TArgs...) > >::type
48 {
49 return currier< std::function< TRet(TArgs...) > >(fun).result;
50 }
51
52 template <int idx, typename TRet>
53 10 static TRet func_caller(mrb_state* mrb, TRet t, mrb_value* args)
54 {
55 10 return t;
56 }
57
58 template <int idx, typename TRet, typename TArgHead, typename ...TArgs>
59 11 static TRet func_caller(
60 mrb_state* mrb,
61 typename currier< std::function< TRet(TArgHead, TArgs...) > >::type fn,
62 mrb_value* args)
63 {
64
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
4 return func_caller<idx + 1, TRet, TArgs...>(
65 mrb,
66 fn(TypeBinder<TArgHead>::from_mrb_value(mrb, args[idx])),
67
5/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
11 args);
68 }
69
70 template <int idx, typename TArgHead>
71 1 static void void_func_caller(
72 mrb_state* mrb,
73 typename currier< std::function< void(TArgHead) > >::type fn,
74 mrb_value* args)
75 {
76 1 fn(TypeBinder<TArgHead>::from_mrb_value(mrb, args[idx]));
77 1 }
78
79 template <int idx, typename TArgHead, typename TArgHead2, typename ...TArgs>
80 static void void_func_caller(
81 mrb_state* mrb,
82 typename currier< std::function< void(TArgHead, TArgHead2, TArgs...) > >::type fn,
83 mrb_value* args)
84 {
85 void_func_caller<idx + 1, TArgHead2, TArgs...>(
86 mrb,
87 fn(TypeBinder<TArgHead>::from_mrb_value(mrb, args[idx])),
88 args);
89 }
90
91 template<typename TRet, typename ... TArgs>
92 struct mruby_func_called_returner
93 {
94 6 static mrb_value call(mrb_state* mrb, std::function<TRet(TArgs...)> func, mrb_value* args)
95 {
96
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 auto curried = curry(func);
97
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 TRet result = func_caller<0, TRet, TArgs...>(mrb, curried, args);
98
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
12 return TypeBinder<TRet>::to_mrb_value(mrb, result);
99 6 }
100 };
101
102 template<typename ... TArgs>
103 struct mruby_func_called_returner<void, TArgs...>
104 {
105 1 static mrb_value call(mrb_state* mrb, std::function<void(TArgs...)> func, mrb_value* args)
106 {
107
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 auto curried = curry(func);
108
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 void_func_caller<0, TArgs...>(mrb, curried, args);
109 2 return mrb_nil_value();
110 1 }
111 };
112
113 template<typename TRet>
114 struct mruby_func_called_returner<TRet>
115 {
116 15 static mrb_value call(mrb_state* mrb, std::function<TRet()> func, mrb_value* args)
117 {
118
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
15 TRet result = func();
119
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
11 return TypeBinder<TRet>::to_mrb_value(mrb, result);
120 2 }
121 };
122
123 template<>
124 struct mruby_func_called_returner<void>
125 {
126 1 static mrb_value call(mrb_state* mrb, std::function<void()> func, mrb_value* args)
127 {
128 1 func();
129 1 return mrb_nil_value();
130 }
131 };
132
133
134 #endif // __MRUBYFUNCTIONAL_HPP__
135