src/xs/typemap/optional.h

filter: Statementstmt 100% 19 / 19 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 23 / 23 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/xs/catch.h ↑ Summary
1 #pragma once
2 #include "base.h"
3 #include <panda/optional.h>
4  
5 namespace xs {
6  
7 // typemap for optional<T>
8 template <class TYPE> struct Typemap<panda::optional<TYPE>> : Typemap<TYPE> {
9 static panda::optional<TYPE> in (SV* arg) {
10 if (!sv_defined(arg)) return {};
11 return Typemap<TYPE>::in(arg);
12 }
13 static Sv out (const panda::optional<TYPE>& var, const Sv& = {}) {
14 if (!var) return Sv::undef;
15 return Typemap<TYPE>::out(*var);
16 }
17 };
18  
19 // not-null wrapper for use in input typemaps
20 template <class T>
21 struct nn {
22 nn () {}
23 nn (const nn&) = default;
24 nn (nn&&) = default;
25  
26 template <class U>
27 nn (const nn<U>& oth) : val(oth.val) {}
28  
29 template <class U>
30 nn (nn<U>&& oth) : val(std::move(oth.val)) {}
31  
32 template <class U>
33 2 nn (U&& val) : val(std::forward<U>(val)) {}
34  
35 nn& operator= (const nn&) = default;
36 nn& operator= (nn&&) = default;
37  
38 template <class U>
39 nn& operator= (U&& v) {
40 val = std::forward<U>(v);
41 return *this;
42 }
43  
44 1 decltype(*T()) operator* () { return *val; }
45 1 T& operator-> () { return val; }
46  
47 operator T () { return val; }
48  
49 protected:
50 T val;
51 };
52  
53 template <class TYPE> struct Typemap<nn<TYPE>> : Typemap<TYPE> {
54 4 static TYPE in (SV* arg) {
55 4 auto ret = Typemap<TYPE>::in(arg);
56 4 ▶ if (!ret) throw "invalid value: undef not allowed";
Branch: 2 branches
BranchTrueFalseTotal
if (!ret) throw "invalid value: undef not allowed"; 1 1 2
if (!ret) throw "invalid value: undef not allowed"; 1 1 2
57 2 return ret;
58 1 }
59 static Sv out (TYPE& var, const Sv& = {}) = delete;
60 };
61  
62 }
« src/xs/catch.h ↑ Summary