File Coverage

/usr/local/lib/perl5/site_perl/5.26.1/x86_64-linux/XS/libpanda.x/i/panda/optional.h
Criterion Covered Total %
statement 9 10 90.0
branch 2 4 50.0
condition n/a
subroutine n/a
pod n/a
total 11 14 78.5


line stmt bran cond sub pod time code
1             #pragma once
2              
3             namespace panda {
4              
5             template struct optional {
6 106           ~optional() { reset(); }
7              
8 0           optional() : nullable_val(nullptr) {}
9            
10 106 50         optional(const T& val) : nullable_val(new (storage) T(val)) {}
11            
12             optional(const optional& oth) : nullable_val(oth ? new (storage) T(*oth) : nullptr) {}
13            
14             optional& operator=(optional const& oth) {
15             if (&oth != this) {
16             reset();
17             if (oth)
18             nullable_val = new (storage) T(*oth);
19             }
20             return *this;
21             }
22            
23             optional& operator=(const T& val) {
24             reset();
25             nullable_val = new (storage) T(val);
26             return *this;
27             }
28              
29 53           void reset() {
30 53 50         if (nullable_val)
31 53           nullable_val->~T();
32 53           nullable_val = nullptr;
33 53           }
34              
35             T& operator*() { return *nullable_val; }
36             const T& operator*() const { return *nullable_val; }
37             T* operator->() { return nullable_val; }
38             const T* operator->() const { return nullable_val; }
39              
40             T value_or(const T& default_val) const { return nullable_val ? *nullable_val : default_val; }
41              
42 106           T value() const { return *nullable_val; }
43              
44 106           explicit operator bool() const { return nullable_val != nullptr; }
45              
46             private:
47             T* nullable_val;
48             alignas(alignof(T)) char storage[sizeof(T)];
49             };
50              
51             template struct optional_tools {
52             using type = optional;
53             static type default_value () { return type{}; }
54             };
55              
56             template <> struct optional_tools {
57             using type = void;
58             static void default_value () {}
59             };
60              
61             template inline constexpr bool operator== (const optional& lhs, const optional& rhs) {
62             return (lhs && rhs) ? (*lhs == *rhs) : (lhs || rhs ? false : true);
63             }
64             template inline constexpr bool operator!= (const optional& lhs, const optional& rhs) { return !operator==(lhs, rhs); }
65              
66             template
67             inline constexpr bool operator< (const optional& lhs, const optional& rhs) {
68             return (lhs && rhs) ? (*lhs < *rhs) : (rhs ? true : false);
69             }
70              
71             template
72             constexpr bool operator<= (const optional& lhs, const optional& rhs) {
73             return (lhs && rhs) ? (*lhs < *rhs) : (lhs ? false : true);
74             }
75              
76             template
77             constexpr bool operator> (const optional& lhs, const optional& rhs) {
78             return (lhs && rhs) ? (*lhs < *rhs) : (lhs ? true : false);
79             }
80              
81             template
82             constexpr bool operator>= (const optional& lhs, const optional& rhs) {
83             return (lhs && rhs) ? (*lhs < *rhs) : (rhs ? false : true);
84             }
85              
86             template constexpr bool operator== (const optional& opt, const U& value) { return opt && *opt == value; }
87             template constexpr bool operator== (const T& value, const optional& opt) { return opt && value == *opt; }
88             template constexpr bool operator!= (const optional& opt, const U& value) { return !operator==(opt, value); }
89             template constexpr bool operator!= (const T& value, const optional& opt) { return !operator==(value, opt); }
90             template constexpr bool operator< (const optional& opt, const U& value) { return opt ? *opt < value : true; }
91             template constexpr bool operator< (const T& value, const optional& opt) { return opt && value < *opt; }
92             template constexpr bool operator<= (const optional& opt, const U& value) { return opt ? *opt <= value : true; }
93             template constexpr bool operator<= (const T& value, const optional& opt) { return opt && value <= *opt; }
94             template constexpr bool operator> (const optional& opt, const U& value) { return opt && *opt > value; }
95             template constexpr bool operator> (const T& value, const optional& opt) { return opt ? value > *opt : true; }
96             template constexpr bool operator>= (const optional& opt, const U& value) { return opt && *opt >= value; }
97             template constexpr bool operator>= (const T& value, const optional& opt) { return opt ? value >= *opt : true; }
98              
99             }