GCC Code Coverage Report


Directory: include/sumty/
File: include/sumty/exceptions.hpp
Date: 2024-04-28 13:27:51
Exec Total Coverage
Lines: 0 10 0.0%
Functions: 0 9 0.0%
Branches: 0 0 -%
Decisions: 0 0 -%

Line Branch Decision Exec Source
1 /* Copyright 2023 Jack A Bernard Jr.
2 *
3 * Licensed under the Apache License, Version 2.0 (the License);
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an AS IS BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SUMTY_EXCEPTIONS_HPP
17 #define SUMTY_EXCEPTIONS_HPP
18
19 #include <exception>
20
21 namespace sumty {
22
23 /// @relates variant
24 class bad_variant_access : public std::exception {
25 public:
26 bad_variant_access() noexcept = default;
27 bad_variant_access(const bad_variant_access&) = default;
28 bad_variant_access(bad_variant_access&&) noexcept = default;
29 ~bad_variant_access() override = default;
30 bad_variant_access& operator=(const bad_variant_access&) = default;
31 bad_variant_access& operator=(bad_variant_access&&) noexcept = default;
32
33 [[nodiscard]] const char* what() const noexcept override {
34 return "bad variant access";
35 }
36 };
37
38 /// @relates option
39 class bad_option_access : public std::exception {
40 public:
41 bad_option_access() noexcept = default;
42 bad_option_access(const bad_option_access&) = default;
43 bad_option_access(bad_option_access&&) noexcept = default;
44 ~bad_option_access() override = default;
45 bad_option_access& operator=(const bad_option_access&) = default;
46 bad_option_access& operator=(bad_option_access&&) noexcept = default;
47
48 [[nodiscard]] const char* what() const noexcept override { return "bad option access"; }
49 };
50
51 /// @relates result
52 template <typename E>
53 class bad_result_access : public std::exception {
54 private:
55 E err_;
56
57 public:
58 explicit bad_result_access(E error) : err_(std::move(error)) {}
59
60 bad_result_access(const bad_result_access&) = default;
61
62 bad_result_access(bad_result_access&&) noexcept(
63 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
64 std::is_nothrow_move_constructible_v<E>) = default;
65
66 ~bad_result_access() noexcept override = default;
67
68 bad_result_access& operator=(const bad_result_access&) = default;
69
70 bad_result_access& operator=(bad_result_access&&) noexcept(
71 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
72 std::is_nothrow_move_assignable_v<E>) = default;
73
74 [[nodiscard]] E& error() & noexcept { return err_; }
75
76 [[nodiscard]] const E& error() const& noexcept { return err_; }
77
78 [[nodiscard]] E&& error() && { return std::move(err_); }
79
80 [[nodiscard]] const E&& error() const&& { return std::move(err_); }
81
82 [[nodiscard]] const char* what() const noexcept override { return "bad result access"; }
83 };
84
85 /// @relates result
86 template <>
87 class bad_result_access<void> : public std::exception {
88 public:
89 bad_result_access() noexcept = default;
90
91 bad_result_access(const bad_result_access&) noexcept = default;
92
93 bad_result_access(bad_result_access&&) noexcept = default;
94
95 ~bad_result_access() noexcept override = default;
96
97 bad_result_access& operator=(const bad_result_access&) = default;
98
99 bad_result_access& operator=(bad_result_access&&) = default;
100
101 void error() const noexcept {}
102
103 [[nodiscard]] const char* what() const noexcept override { return "bad result access"; }
104 };
105
106 } // namespace sumty
107
108 #endif
109