GCC Code Coverage Report


Directory: ./
File: src/utilities/optional.h
Date: 2023-04-27 00:55:30
Exec Total Coverage
Lines: 29 31 93.5%
Functions: 71 75 94.7%
Branches: 15 24 62.5%

Line Branch Exec Source
1 #pragma once
2 #include <utility>
3
4 namespace lython {
5
6 template <typename T>
7 class Optional {
8 public:
9 // Pointer only
10 // nullptr are set to None
11 template <typename U = T, std::enable_if_t<std::is_pointer_v<U>, bool> = true>
12 Optional(const T& data): _has_data(false) {
13
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 49 times.
249 if (data != nullptr) {
14 200 new (&holder.data.value) T(data);
15 200 _has_data = true;
16 }
17 249 }
18
19 // Regular
20 template <typename U = T, std::enable_if_t<!std::is_pointer_v<U>, bool> = true>
21 Optional(const T& data): _has_data(true) {
22 15 new (&holder.data.value) T(data);
23 15 }
24
25 Optional(): _has_data(false) {}
26
27 Optional(const Optional& opt) {
28
2/2
✓ Branch 0 taken 889 times.
✓ Branch 1 taken 3381 times.
7206 if (opt._has_data) {
29
1/2
✓ Branch 1 taken 205 times.
✗ Branch 2 not taken.
1778 set_data(opt.holder.data.value);
30 }
31 7206 }
32
33 // void print(std::ostream &out) {
34 // if (has_value()) {
35 // out << str(value());
36 // return;
37 // }
38 // out << "none";
39 // return;
40 // }
41
42 Optional& operator=(const T& data) {
43 15846 set_data(data);
44 15846 return *this;
45 }
46
47 Optional& operator=(Optional const& opt) {
48
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 348 times.
756 if (opt._has_data) {
49 60 set_data(opt.holder.data.value);
50 }
51 756 return *this;
52 }
53
54 bool operator==(Optional const& opt) const {
55
1/2
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if (opt.has_value() == has_value()) {
56
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (opt.has_value()) {
57 10 return opt.value() == value();
58 }
59 }
60 return false;
61 }
62
63 ~Optional() {
64
2/2
✓ Branch 0 taken 8586 times.
✓ Branch 1 taken 18355 times.
33491 if (_has_data) {
65 11003 holder.data.value.~T();
66 }
67 33491 }
68
69 bool has_value() const { return _has_data; }
70
71 T const& value() const { return holder.data.value; }
72
73 T& value() { return holder.data.value; }
74
75 T const& fold(T const& default_value) const {
76
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 if (has_value())
77 43 return holder.data.value;
78
79 return default_value;
80 }
81
82 private:
83 void set_data(const T& data) {
84
2/2
✓ Branch 0 taken 8654 times.
✓ Branch 1 taken 188 times.
11265 if (!_has_data) {
85
1/4
✓ Branch 2 taken 563 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
11077 new (&holder.data.value) T(data);
86 } else {
87
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
188 holder.data.value = data;
88 }
89 11265 _has_data = true;
90 11265 }
91
92 struct data_t {
93 T value;
94 };
95
96 struct no_data_t {};
97
98 union holder_t {
99 data_t data;
100 no_data_t nothing;
101
102 holder_t() {}
103 ~holder_t() {}
104 } holder;
105
106 bool _has_data = false;
107 };
108
109 template <typename T>
110 Optional<T> none() {
111 26 return Optional<T>();
112 }
113
114 template <typename T>
115 Optional<T> some(const T& value) {
116 161 return Optional<T>(value);
117 }
118
119 template <typename T>
120 String str(Optional<T> const& obj) {
121 if (obj.has_value()) {
122 return "Some(" + str(obj.value()) + ")";
123 }
124
125 return "None";
126 }
127
128 } // namespace lython
129