HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_exceptions.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <stdexcept>
5
6/// @defgroup Exceptions Exceptions
7/// @brief Exceptions and error handling
8
9namespace hart
10{
11
12/// @brief Thrown by test asserts like `HART_ASSERT_TRUE()` and `AudioTestBuilder::assertFalse()`
14 public std::runtime_error
15{
16 using std::runtime_error::runtime_error;
17};
18
19/// @brief Thrown when some I/O operation fails
20/// @ingroup Exceptions
21class IOError:
22 public std::runtime_error
23{
24 using std::runtime_error::runtime_error;
25};
26
27/// @brief Thrown when some unexpected state is encountered
28/// @ingroup Exceptions
30 public std::runtime_error
31{
32 using std::runtime_error::runtime_error;
33};
34
35/// @brief Thrown when an unexpected container size is encountered
36/// @ingroup Exceptions
38 public std::runtime_error
39{
40 using std::runtime_error::runtime_error;
41};
42
43/// @brief Thrown when an inappropriate value is encountered
44/// @ingroup Exceptions
46 public std::runtime_error
47{
48 using std::runtime_error::runtime_error;
49};
50
51/// @brief Thrown when sample rate is mismatched
52/// @ingroup Exceptions
54 public std::runtime_error
55{
56 using std::runtime_error::runtime_error;
57};
58
59/// @brief Thrown when a numbers of channels is mismatched
60/// @ingroup Exceptions
62 public std::runtime_error
63{
64 using std::runtime_error::runtime_error;
65};
66
67/// @brief Thrown when `hassert()` or `hassertfalse` are triggered
68/// @ingroup Exceptions
70 public std::runtime_error
71{
72 using std::runtime_error::runtime_error;
73};
74
75/// @brief Thrown when some parameter has an unsupported value
76/// @ingroup Exceptions
78 public std::runtime_error
79{
80 using std::runtime_error::runtime_error;
81};
82
83/// @brief Thrown when the test runner is misconfigured
84/// @details For example, if a CLI data directory argument is required, but missing
85/// @ingroup Exceptions
87 public std::runtime_error
88{
89 using std::runtime_error::runtime_error;
90};
91
92/// @brief Thrown when a container index is out of range
93/// @ingroup Exceptions
95 public std::runtime_error
96{
97 using std::runtime_error::runtime_error;
98};
99
100/// @brief Thrown when a nullptr could be handled gracefully
101/// @ingroup Exceptions
103 public std::runtime_error
104{
105 using std::runtime_error::runtime_error;
106};
107
108#ifndef HART_STRINGIFY
109#define HART_STRINGIFY(x) HART_STRINGIFY2(x)
110#define HART_STRINGIFY2(x) #x
111#endif // HART_STRINGIFY
112
113#define HART_LINE_STRING HART_STRINGIFY(__LINE__)
114
115#if HART_DO_NOT_THROW_EXCEPTIONS
116
117#define HART_THROW_IMPL(ExceptionType, message) std::cout << #ExceptionType << " triggered: \"" << message << "\", file: " << __FILE__ << ", line: " << __LINE__ << std::endl
118
119/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message otherwise
120/// @ingroup Exceptions
121#define HART_THROW(ExceptionType, message) do { HART_THROW_IMPL(ExceptionType, message); } while (0)
122
123/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message and returns a specified value otherwise
124/// @ingroup Exceptions
125#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue) { HART_THROW_IMPL (ExceptionType, message); return returnValue; }
126
127/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message and returns otherwise
128/// @ingroup Exceptions
129#define HART_THROW_OR_RETURN_VOID(ExceptionType, message) { HART_THROW_IMPL (ExceptionType, message); return; }
130
131/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message and jumps to the next iteration (via `continue`) otherwise
132/// @ingroup Exceptions
133#define HART_THROW_OR_CONTINUE(ExceptionType, message) { HART_THROW_IMPL (ExceptionType, message); continue; }
134
135#else
136
137#define HART_THROW_IMPL(ExceptionType, message) throw ExceptionType (std::string (message) + ", file: " __FILE__ ", line: " HART_LINE_STRING)
138
139/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message otherwise
140/// @ingroup Exceptions
141#define HART_THROW(ExceptionType, message) do { HART_THROW_IMPL(ExceptionType, message); } while (0)
142
143/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message and returns a specified value otherwise
144/// @ingroup Exceptions
145#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue) HART_THROW (ExceptionType, message)
146
147/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message and returns otherwise
148/// @ingroup Exceptions
149#define HART_THROW_OR_RETURN_VOID(ExceptionType, message) HART_THROW (ExceptionType, message)
150
151/// @brief Throws an exception if `HART_DO_NOT_THROW_EXCEPTIONS` is set, prints a message and jumps to the next iteration (via `continue`) otherwise
152/// @ingroup Exceptions
153#define HART_THROW_OR_CONTINUE(ExceptionType, message) HART_THROW (ExceptionType, message)
154#endif // HART_DO_NOT_THROW_EXCEPTIONS
155
156// TODO: Make hassert() and hassertfalse not throw if HART_DO_NOT_THROW_EXCEPTIONS is set
157
158/// @brief Triggers a `HartAssertException`
159/// @ingroup Exceptions
160#define hassertfalse HART_THROW (hart::HartAssertException, "hassertfalse failed")
161
162/// @brief Triggers a `HartAssertException` if the `condition` is `false`
163/// @ingroup Exceptions
164#define hassert(condition) if (! (condition)) { HART_THROW (hart::HartAssertException, std::string ("hassert failed:") + #condition); }
165
166/// @brief Prints a warning message
167/// @ingroup Exceptions
168#define HART_WARNING(message) std::cout << "Warning: " << message << ", file: " << __FILE__ << ", line: " << __LINE__ << std::endl
169
170} // namespace hart
Thrown when a numbers of channels is mismatched.
Thrown when the test runner is misconfigured.
Thrown when hassert() or hassertfalse are triggered.
Thrown when some I/O operation fails.
Thrown when a container index is out of range.
Thrown when a nullptr could be handled gracefully.
Thrown when sample rate is mismatched.
Thrown when an unexpected container size is encountered.
Thrown when some unexpected state is encountered.
Thrown by test asserts like HART_ASSERT_TRUE() and AudioTestBuilder::assertFalse()
Thrown when some parameter has an unsupported value.
Thrown when an inappropriate value is encountered.
#define HART_THROW(ExceptionType, message)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message otherwise.
#define HART_LINE_STRING
#define HART_STRINGIFY(x)
#define HART_STRINGIFY2(x)
#define HART_THROW_IMPL(ExceptionType, message)