t/cookbook/recipe12.xsi

filter: Statementstmt 3.8% 1 / 26 Branchbran 0.0% 0 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 1.1% 1 / 84 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/typemap/object/threads.xsi ↑ Summary t/cookbook/recipe09.xsi »
1 MODE: INLINE
2  
3 #include <cmath>
4 #include <iostream>
5  
6 struct PointRecipe12 {
7 double x;
8 double y;
9 };
10  
11 double distance(const PointRecipe12& pt_a, const PointRecipe12& pt_b) {
12 auto dx = pt_a.x - pt_a.x;
13 auto dy = pt_a.y - pt_b.y;
14 return std::sqrt(dx * dx + dy * dy);
15 }
16  
17 struct StatisticsRecipe12 {
18 StatisticsRecipe12(const PointRecipe12& interest_, const std::vector<PointRecipe12>& points_): interest{interest_}, points{points_} {
19 if(points.empty()) throw std::runtime_error("points cannot be empty");
20 }
21  
22 const PointRecipe12& nearest() const {
23 return select([](const double& prev_dist, const double& new_dist) { return new_dist < prev_dist; } );
24 }
25  
26 const PointRecipe12& farest() const {
27 return select([](const double& prev_dist, const double& new_dist) { return new_dist > prev_dist; } );
28 }
29  
30 private:
31 template<typename F>
32 const PointRecipe12& select(F&& fn) const {
33 const PointRecipe12* pt = &points.front();
34 auto best_distance = distance(interest, *pt);
35 for(auto it = points.cbegin() + 1; it != points.cend(); ++it) {
36 auto new_distance = distance(interest, *it);
37 if(fn(best_distance, new_distance)) {
38 pt = &*it;
39 best_distance = new_distance;
40 }
41 }
42 return *pt;
43 }
44  
45 const PointRecipe12& interest;
46 const std::vector<PointRecipe12>& points;
47 };
48  
49  
50 namespace xs {
51 template <>
52 struct Typemap<PointRecipe12*> : TypemapObject<PointRecipe12*, PointRecipe12*, ObjectTypePtr, ObjectStorageMG> {
53 static std::string package () { return "MyTest::Cookbook::PointRecipe12"; }
54 };
55  
56  
57 template <>
58 struct Typemap<StatisticsRecipe12*> : TypemapObject<StatisticsRecipe12*, StatisticsRecipe12*, ObjectTypePtr, ObjectStorageMG> {
59 static std::string package () { return "MyTest::Cookbook::StatisticsRecipe12"; }
60 };
61 }
62  
63 static xs::Sv::payload_marker_t payload_marker_12;
64  
65 struct StatisticsRecipe12_payload {
66 PointRecipe12 interest;
67 std::vector<PointRecipe12> points;
68 ~StatisticsRecipe12_payload() { std::cout << "~StatisticsRecipe12_payload\n"; }
69 };
70  
71 static int payload_marker_IntersectionFinderAdder_free(pTHX_ SV*, MAGIC* mg) {
72 if (mg->mg_virtual == &payload_marker_12) {
73 auto* payload = static_cast<StatisticsRecipe12_payload*>((void*)mg->mg_ptr);
74 delete payload;
75 }
76 return 0;
77 }
78  
79 MODULE = MyTest::Cookbook PACKAGE = MyTest::Cookbook::PointRecipe12
80 PROTOTYPES: DISABLE
81  
82 double PointRecipe12::x(SV* new_val = nullptr) : ALIAS(y = 1) {
83 0 double* val = nullptr;
84 0 ▶ switch(ix) {
Branch: 1 branch
BranchTrueFalseTotal
switch(ix) { 0 0 0
85 0 case 1: val = &THIS->y; break;
86 0 default: val = &THIS->x; break;
87 }
88 0 ▶ if (new_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_val) { 0 0 0
89 0 *val = SvNV(new_val);
90 }
91 0 ▶ RETVAL = *val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = *val; 0 0 0
92 }
93  
94 PointRecipe12* PointRecipe12::new(double x = 0, double y = 0) {
95 0 RETVAL = new PointRecipe12{x, y};
96 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
97 0 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
0 0 0
0 0 0
98  
99 MODULE = MyTest::Cookbook PACKAGE = MyTest::Cookbook::StatisticsRecipe12
100 PROTOTYPES: DISABLE
101  
102 PointRecipe12* StatisticsRecipe12::nearest() {
103 0 RETVAL = new PointRecipe12(THIS->nearest());
104 0 ▶}
Branch: 4 branches
BranchTrueFalseTotal
} 0 0 0
} 0 0 0
} 0 0 0
} 0 0 0
105  
106 PointRecipe12* StatisticsRecipe12::farest() {
107 0 RETVAL = new PointRecipe12(THIS->farest());
108 0 ▶}
Branch: 4 branches
BranchTrueFalseTotal
} 0 0 0
} 0 0 0
} 0 0 0
} 0 0 0
109  
110 Sv StatisticsRecipe12::new(PointRecipe12* interest, Array pts) {
111 using payload_ptr_t = std::unique_ptr<StatisticsRecipe12_payload>;
112 using stats_ptr_t = std::unique_ptr<StatisticsRecipe12>;
113  
114 0 std::vector<PointRecipe12> points;
115 0 ▶ for(auto it: pts) {
Branch: 4 branches
BranchTrueFalseTotal
for(auto it: pts) { 0 0 0
for(auto it: pts) { 0 0 0
for(auto it: pts) { 0 0 0
for(auto it: pts) { 0 0 0
116 0 ▶ points.push_back(*(xs::in<PointRecipe12*>(it)));
Branch: 2 branches
BranchTrueFalseTotal
points.push_back(*(xs::in<PointRecipe12*>(it))); 0 0 0
points.push_back(*(xs::in<PointRecipe12*>(it))); 0 0 0
117 0 }
118  
119 // better to use std::make_unique from C++ 14, if available.
120 0 ▶ auto payload_holder = payload_ptr_t{new StatisticsRecipe12_payload{ *interest, std::move(points) }};
Branch: 1 branch
BranchTrueFalseTotal
auto payload_holder = payload_ptr_t{new StatisticsRecipe12_payload{ *interest, std::move(points) }}; 0 0 0
121 0 ▶ auto self_holder = stats_ptr_t{new StatisticsRecipe12{ payload_holder->interest, payload_holder->points }};
Branch: 3 branches
BranchTrueFalseTotal
auto self_holder = stats_ptr_t{new StatisticsRecipe12{ payload_holder->interest, payload_holder->points }}; 0 0 0
auto self_holder = stats_ptr_t{new StatisticsRecipe12{ payload_holder->interest, payload_holder->points }}; 0 0 0
auto self_holder = stats_ptr_t{new StatisticsRecipe12{ payload_holder->interest, payload_holder->points }}; 0 0 0
122  
123 0 ▶ Object self = xs::out(self_holder.get(), CLASS);
Branch: 2 branches
BranchTrueFalseTotal
Object self = xs::out(self_holder.get(), CLASS); 0 0 0
Object self = xs::out(self_holder.get(), CLASS); 0 0 0
124 0 ▶ self.payload_attach((void*)payload_holder.get(), &payload_marker_12);
Branch: 1 branch
BranchTrueFalseTotal
self.payload_attach((void*)payload_holder.get(), &payload_marker_12); 0 0 0
125  
126 0 payload_holder.release();
127 0 self_holder.release();
128  
129 0 ▶ RETVAL = self.ref();
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = self.ref(); 0 0 0
RETVAL = self.ref(); 0 0 0
130 }
131  
132 BOOT {
133 35 payload_marker_12.svt_free = payload_marker_IntersectionFinderAdder_free;
134 }
« t/typemap/object/threads.xsi ↑ Summary t/cookbook/recipe09.xsi »