src/xs/function.h

filter: Statementstmt 8.1% 3 / 37 Branchbran 9.3% 3 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 8.7% 6 / 69 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/cookbook/recipe08.xsi ↑ Summary t/cookbook/recipe07.xsi »
1 #pragma once
2 #include "typemap.h"
3 #include <tuple>
4 #include <utility>
5 #include <panda/cast.h>
6 #include <panda/function.h>
7 #ifdef USE_ITHREADS
8 #include <mutex>
9 #endif
10  
11 namespace xs { namespace func {
12  
13 using namespace panda;
14  
15 extern Sv::payload_marker_t out_marker;
16 extern Sv::payload_marker_t in_marker;
17  
18 template <class T, class F>
19 struct ConverterIn {
20 using type = T;
21 F f;
22 ConverterIn (const F& f) : f(f) {}
23 ConverterIn (F&& f) : f(std::move(f)) {}
24 decltype(std::declval<F>()(Sv())) in (const Sv& sv) { return f(sv); }
25 };
26  
27 template <class T>
28 struct ConverterIn<T, std::nullptr_t> {
29 using type = T;
30 ConverterIn (std::nullptr_t) {}
31 decltype(xs::in<T>(Sv())) in (const Sv& sv) { return xs::in<T>(sv); }
32 };
33  
34 template <>
35 struct ConverterIn<void, std::nullptr_t> {
36 using type = void;
37 ConverterIn (std::nullptr_t) {}
38 void in (const Sv&) {}
39 };
40  
41 template <class T, class F>
42 struct ConverterOut {
43 using type = T;
44 F f;
45 ConverterOut (const F& f) : f(f) {}
46 ConverterOut (F&& f) : f(std::move(f)) {}
47 Sv out (const T& val) { return f(val); }
48 };
49  
50 template <class T>
51 struct ConverterOut<T, std::nullptr_t> {
52 using type = T;
53 ConverterOut (std::nullptr_t) {}
54 Sv out (const T& val) { return xs::out<T>(val); }
55 };
56  
57 template <>
58 struct ConverterOut<void, std::nullptr_t> {
59 using type = void;
60 0 ConverterOut (std::nullptr_t) {}
61 Sv out () { return {}; }
62 };
63  
64 #ifdef USE_ITHREADS
65 struct SubPad : AtomicRefcnt {
66 SubPad (const Sub& sub);
67  
68 const Sub& get_sub () const;
69 void add_sub (const Sub& sub);
70 void remove_sub ();
71  
72 static SubPad* get (Sub);
73  
74 private:
75 mutable std::mutex mutex;
76 std::vector<std::pair<PerlInterpreter*, Sub>> pad;
77 };
78  
79 template <typename Ret, typename...Args>
80 struct SubHolder : panda::Ifunction<Ret, Args...> {
81 SubHolder (const Sub& sub) : pad(SubPad::get(sub)) {}
82  
83 const Sub& sub () const { return pad->get_sub(); }
84  
85 bool equals (const panda::function_details::AnyFunction* oth) const override {
86 auto oth_caller = dyn_cast<const SubHolder*>(oth);
87 return oth_caller && this->sub() == oth_caller->sub();
88 }
89  
90 private:
91 iptr<SubPad> pad;
92 };
93 #else
94 template <typename Ret, typename...Args>
95 struct SubHolder : panda::Ifunction<Ret, Args...> {
96 SubHolder (const Sub& sub) : _sub(sub) {}
97  
98 0 const Sub& sub () const {
99 0 ▶ if (SvREFCNT(_sub) == 0) throw "calling typemap'ed perl function after perl global destroy";
Branch: 1 branch
BranchTrueFalseTotal
if (SvREFCNT(_sub) == 0) throw "calling typemap'ed perl function after perl global destroy"; 0 0 0
100 0 return _sub;
101 }
102  
103 bool equals (const panda::function_details::AnyFunction* oth) const override {
104 auto oth_caller = dyn_cast<const SubHolder*>(oth);
105 return oth_caller && this->_sub == oth_caller->_sub;
106 }
107  
108 private:
109 Sub _sub;
110 };
111 #endif
112  
113 template <typename Ret, typename...Args>
114 struct SubCallerComparator : SubHolder<Ret, Args...> {
115 using SubHolder<Ret, Args...>::SubHolder;
116 Ret operator() (Args...) override { throw "should not be called"; }
117 };
118  
119 template <typename RetConv, typename...Convs>
120 struct SubCaller : SubHolder<typename RetConv::type, typename Convs::type...> {
121 using Ret = typename RetConv::type;
122 using Super = SubHolder<Ret, typename Convs::type...>;
123 using Tuple = std::tuple<Convs...>;
124  
125 template <class RetConvArg, class...ConvArgs>
126 SubCaller (const Sub& sub, RetConvArg&& rcarg, ConvArgs&&...cargs)
127 : Super(sub), ret_conv(RetConv{std::forward<RetConvArg>(rcarg)}), arg_convs(Convs{std::forward<ConvArgs>(cargs)}...) {}
128  
129 SubCaller (const Sub& sub, const RetConv& rc, const Convs&...acs) : Super(sub), ret_conv(rc), arg_convs(acs...) {}
130  
131 Ret operator() (typename Convs::type... args) override {
132 constexpr size_t ARGS_COUNT = sizeof...(Convs);
133 SV* sv_args[ARGS_COUNT];
134 push(sv_args, args...);
135 return call_impl(sv_args, ARGS_COUNT, typename std::is_void<Ret>::type());
136 }
137  
138 private:
139 RetConv ret_conv;
140 Tuple arg_convs;
141  
142 template <size_t pos = 0>
143 void push (SV**) {}
144  
145 template <size_t pos = 0, typename First, typename...Others>
146 void push (SV** dest, First&& f, Others&&...oths) {
147 dest[pos] = std::get<pos>(arg_convs).out(std::forward<First>(f)).detach();
148 push<pos+1>(dest, std::forward<Others>(oths)...);
149 }
150  
151 Ret call_impl (SV** args, size_t items, std::false_type) {
152 Scalar ret = this->sub().call(args, items);
153 for (size_t i = 0; i < items; ++i) SvREFCNT_dec(args[i]);
154 return ret_conv.in(ret ? ret : Scalar::undef);
155 }
156  
157 Ret call_impl (SV** args, size_t items, std::true_type) {
158 this->sub().template call<void>(args, items);
159 for (size_t i = 0; i < items; ++i) SvREFCNT_dec(args[i]);
160 }
161 };
162  
163 struct IFunctionCaller {
164 virtual Sv call (SV**, size_t) = 0;
165 virtual IFunctionCaller* clone () const = 0;
166 0 virtual ~IFunctionCaller () {}
167 };
168  
169 template <typename Ret, typename...Args>
170 struct FunctionHolder : IFunctionCaller {
171 using Func = panda::function<Ret(Args...)>;
172 Func func;
173 0 ▶ FunctionHolder (const Func& f) : func(f) {}
Branch: 1 branch
BranchTrueFalseTotal
FunctionHolder (const Func& f) : func(f) {} 0 0 0
174 };
175  
176 template <typename RetConv, typename...Convs>
177 struct FunctionCaller : FunctionHolder<typename RetConv::type, typename Convs::type...> {
178 using Ret = typename RetConv::type;
179 using Super = FunctionHolder<Ret, typename Convs::type...>;
180 using Func = typename Super::Func;
181 using Tuple = std::tuple<Convs...>;
182  
183 template <class RetConvArg, class...ConvArgs>
184 0 FunctionCaller (const Func& f, RetConvArg&& rcarg, ConvArgs&&...cargs)
185 0 : Super(f), ret_conv(RetConv{std::forward<RetConvArg>(rcarg)}), arg_convs(Convs{std::forward<ConvArgs>(cargs)}...) {}
186  
187 FunctionCaller (const Func& f, const RetConv& rc, const Convs&...acs) : Super(f), ret_conv(rc), arg_convs(acs...) {}
188  
189 0 FunctionCaller (const FunctionCaller& oth) = default;
190  
191 0 Sv call (SV** svs, size_t items) override {
192 0 constexpr size_t ARGS_COUNT = sizeof...(Convs);
193 0 ▶ if (items != ARGS_COUNT) throw Simple::format("wrong number of arguments for subroutine call: expected %d, passed %d", ARGS_COUNT, items);
Branch: 2 branches
BranchTrueFalseTotal
if (items != ARGS_COUNT) throw Simple::format("wrong number of arguments for subroutine call: expected %d, passed %d", ARGS_COUNT, items); 0 0 0
if (items != ARGS_COUNT) throw Simple::format("wrong number of arguments for subroutine call: expected %d, passed %d", ARGS_COUNT, items); 0 0 0
194 0 return call_impl(svs, std::make_index_sequence<ARGS_COUNT>(), typename std::is_void<Ret>::type());
195 }
196  
197 0 IFunctionCaller* clone () const override {
198 0 ▶ return new FunctionCaller(*this);
Branch: 2 branches
BranchTrueFalseTotal
return new FunctionCaller(*this); 0 0 0
return new FunctionCaller(*this); 0 0 0
199 }
200  
201 private:
202 RetConv ret_conv;
203 Tuple arg_convs;
204  
205 template <size_t...Inds>
206 Sv call_impl (SV** svs, std::index_sequence<Inds...>, std::false_type) {
207 auto args = std::make_tuple(std::get<Inds>(arg_convs).in(svs[Inds])...); // proxy on stack to be able to pass non-const references
208 (void)args; // supress warning when no input args
209 return ret_conv.out(this->func(std::get<Inds>(args)...));
210 }
211  
212 template <size_t... Inds>
213 0 Sv call_impl (SV** svs, std::index_sequence<Inds...>, std::true_type) {
214 auto args = std::make_tuple(std::get<Inds>(arg_convs).in(svs[Inds])...);
215 (void)args;
216 0 ▶ this->func(std::get<Inds>(args)...);
Branch: 1 branch
BranchTrueFalseTotal
this->func(std::get<Inds>(args)...); 0 0 0
217 0 return {};
218 }
219 };
220  
221 template <int N, class R, class F, class...Args>
222 std::enable_if_t<(sizeof...(Args) == N), R>
223 0 fill_default_args (const F& f, Args&&... args) {
224 0 return f(std::forward<Args>(args)...);
225 }
226  
227 template <int N, class R, class F, class...Args>
228 std::enable_if_t<(sizeof...(Args) < N), R>
229 fill_default_args (const F& f, Args&&... args) {
230 return fill_default_args<N, R>(f, std::forward<Args>(args)..., nullptr);
231 }
232  
233 template <typename Ret, typename...Args, typename...ConvArgs>
234 panda::iptr<panda::Ifunction<Ret, Args...>> sub2function_impl (const Sub& sub, ConvArgs&&...cargs) {
235 return fill_default_args<sizeof...(Args) + 1, panda::Ifunction<Ret, Args...>*>([&](auto&& rcarg, auto&&...cargs) {
236 return new SubCaller<ConverterIn<Ret, std::decay_t<decltype(rcarg)>>, ConverterOut<Args, std::decay_t<decltype(cargs)>>...>(
237 sub, std::forward<decltype(rcarg)>(rcarg), std::forward<decltype(cargs)>(cargs)...
238 );
239 }, std::forward<ConvArgs>(cargs)...);
240 }
241  
242 template <typename Ret, typename...Args, typename...ConvArgs>
243 0 IFunctionCaller* function2sub_impl (const panda::function<Ret(Args...)>& f, ConvArgs&&...cargs) {
244 0 ▶ return fill_default_args<sizeof...(Args) + 1, IFunctionCaller*>([&](auto&& rcarg, auto&&...cargs) {
Branch: 1 branch
BranchTrueFalseTotal
return fill_default_args<sizeof...(Args) + 1, IFunctionCaller*>([&](auto&& rcarg, auto&&...cargs) { 0 0 0
245 0 ▶ return new FunctionCaller<ConverterOut<Ret, std::decay_t<decltype(rcarg)>>, ConverterIn<Args, std::decay_t<decltype(cargs)>>...>(
Branch: 1 branch
BranchTrueFalseTotal
return new FunctionCaller<ConverterOut<Ret, std::decay_t<decltype(rcarg)>>, ConverterIn<Args, std::decay_t<decltype(cargs)>>...>( 0 0 0
246 f, std::forward<decltype(rcarg)>(rcarg), std::forward<decltype(cargs)>(cargs)...
247 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
); 0 0 0
248 0 }, std::forward<ConvArgs>(cargs)...);
249 }
250  
251 template <typename Ret, typename...Args, typename...ConvArgs>
252 std::enable_if_t<std::is_void<Ret>::value, panda::function<Ret(Args...)>>
253 sub2function (panda::function<Ret(Args...)>*, const Sub& sub, ConvArgs&&...cargs) {
254 auto fc = reinterpret_cast<IFunctionCaller*>(sub.payload(&out_marker).ptr);
255 if (auto holder = dyn_cast<FunctionHolder<Ret, Args...>*>(fc)) return holder->func;
256 return sub2function_impl<Ret, Args...>(sub, nullptr, std::forward<ConvArgs>(cargs)...);
257 }
258  
259 template <typename Ret, typename...Args, typename...ConvArgs>
260 std::enable_if_t<!std::is_void<Ret>::value, panda::function<Ret(Args...)>>
261 sub2function (panda::function<Ret(Args...)>*, const Sub& sub, ConvArgs&&...cargs) {
262 auto fc = reinterpret_cast<IFunctionCaller*>(sub.payload(&out_marker).ptr);
263 if (auto holder = dyn_cast<FunctionHolder<Ret, Args...>*>(fc)) return holder->func;
264 return sub2function_impl<Ret, Args...>(sub, std::forward<ConvArgs>(cargs)...);
265 }
266  
267 template <typename Ret, typename...Args, typename...ConvArgs>
268 std::enable_if_t<std::is_void<Ret>::value, IFunctionCaller*>
269 0 function2sub (const panda::function<Ret(Args...)>& f, ConvArgs&&...cargs) {
270 0 ▶ return function2sub_impl(f, nullptr, std::forward<ConvArgs>(cargs)...);
Branch: 1 branch
BranchTrueFalseTotal
return function2sub_impl(f, nullptr, std::forward<ConvArgs>(cargs)...); 0 0 0
271 }
272  
273 template <typename Ret, typename...Args, typename...ConvArgs>
274 std::enable_if_t<!std::is_void<Ret>::value, IFunctionCaller*>
275 function2sub (const panda::function<Ret(Args...)>& f, ConvArgs&&...cargs) {
276 return function2sub_impl(f, std::forward<ConvArgs>(cargs)...);
277 }
278  
279 Sub create_sub (IFunctionCaller*);
280  
281  
282 // sub2function low-level interface (used by CallbackDispatcher)
283 template <typename RetConv, typename...Convs>
284 panda::iptr<panda::Ifunction<typename std::decay_t<RetConv>::type, typename std::decay_t<Convs>::type...>>
285 sub2function_with_convs (const Sub& sub, RetConv&& rc, Convs&&...acs) {
286 return new SubCaller<std::decay_t<RetConv>, std::decay_t<Convs>...>(sub, std::forward<RetConv>(rc), std::forward<Convs>(acs)...);
287 }
288  
289 // function2sub low-level interface (used by CallbackDispatcher)
290 template <typename RetConv, typename...Convs>
291 Sub function2sub_with_convs (const panda::function<typename std::decay_t<RetConv>::type(typename std::decay_t<Convs>::type...)>& f, RetConv&& rc, Convs&&...acs) {
292 auto fc = new FunctionCaller<std::decay_t<RetConv>, std::decay_t<Convs>...>(f, std::forward<RetConv>(rc), std::forward<Convs>(acs)...);
293 return create_sub(fc);
294 }
295  
296 }}
297  
298 namespace xs {
299 template <typename F, typename...ConvArgs>
300 std::enable_if_t<std::is_function<F>::value, panda::function<F>>
301 sub2function (const Sub& sub, ConvArgs&&...cargs) {
302 if (!sub) return {};
303 return xs::func::sub2function((panda::function<F>*)(nullptr), sub, std::forward<ConvArgs>(cargs)...);
304 }
305  
306 template <typename F, typename...ConvArgs>
307 std::enable_if_t<!std::is_function<F>::value, F>
308 sub2function (const Sub& sub, ConvArgs&&...cargs) {
309 if (!sub) return {};
310 return xs::func::sub2function((F*)(nullptr), sub, std::forward<ConvArgs>(cargs)...);
311 }
312  
313 template <class Ret, class...Args, class...ConvArgs>
314 0 Sub function2sub (const panda::function<Ret(Args...)>& f, ConvArgs&&...cargs) {
315 0 ▶ if (!f) return {};
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return {}; 0 0 0
316 0 auto caller = panda::dyn_cast<xs::func::SubHolder<Ret, Args...>*>(f.func.get());
317 0 ▶ if (caller) return caller->sub();
Branch: 1 branch
BranchTrueFalseTotal
if (caller) return caller->sub(); 0 0 0
318 0 return xs::func::create_sub(xs::func::function2sub(f, std::forward<ConvArgs>(cargs)...));
319 }
320  
321 template <typename Ret, typename... Args>
322 struct Typemap<panda::function<Ret(Args...)>> : TypemapBase<panda::function<Ret(Args...)>> {
323 using Func = panda::function<Ret(Args...)>;
324  
325 static inline Func in (const Sub& sub) {
326 return sub2function<Ret(Args...)>(sub);
327 }
328  
329 18 static inline Sv out (const Func& f, const Sv& = {}) {
330 18 ▶ if (!f) return Sv::undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return Sv::undef; 0 18 18
331 18 ▶ return Ref::create(function2sub(f));
Branch: 2 branches
BranchTrueFalseTotal
return Ref::create(function2sub(f)); 18 0 18
return Ref::create(function2sub(f)); 18 0 18
332 }
333 };
334 }
« t/cookbook/recipe08.xsi ↑ Summary t/cookbook/recipe07.xsi »