RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
13#define RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
14
15#include <rtctk/config.hpp>
16
17#include <ciiBasicDataType.hpp>
18#include <ciiException.hpp>
19
20#include <concepts>
21#include <string_view>
22#include <type_traits>
23#include <typeinfo>
24
25// We temporarily redefine broken CII macros.
26// TODO: Remove the redefinitions once they are fixed.
27
28#ifdef CII_MAKE_EXCEPTION
29#undef CII_MAKE_EXCEPTION
30
31#define CII_MAKE_EXCEPTION(exceptionType_t, ...) \
32 ::error::elt::details::CreateExceptionObject<exceptionType_t>( \
33 0, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
34
35#endif // CII_MAKE_EXCEPTION
36
37#ifdef CII_MAKE_EXCEPTION_WITH_NESTED
38#undef CII_MAKE_EXCEPTION_WITH_NESTED
39
40#define CII_MAKE_EXCEPTION_WITH_NESTED(exceptionType_t, nested_exception, ...) \
41 ::error::elt::details::CreateExceptionObject<exceptionType_t>( \
42 nested_exception, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
43
44#endif // CII_MAKE_EXCEPTION_WITH_NESTED
45
47namespace detail {
48
56template <class T>
57struct UnspecifiedNested : virtual T, virtual std::nested_exception {
58 template <class E>
59 requires std::constructible_from<T, E>
60 explicit UnspecifiedNested(E&& e) noexcept(std::is_nothrow_constructible_v<T, E&&>)
61 : T(std::forward<E>(e)) {
62 }
63};
64
70std::ostream& JoinLines(std::ostream& os,
71 std::string_view lines,
72 std::string_view initial_indent,
73 std::string_view subsequent_indent);
74
75} // namespace detail
76
95template <class E>
96auto WrapWithNested(E&& exception) noexcept(
97 std::is_nothrow_constructible_v<detail::UnspecifiedNested<typename std::decay_t<E>>, E&&>) {
98 using Type = typename std::decay_t<E>;
99 static_assert(std::is_class_v<Type>, "exception must be a non-union class-type");
100
101 return detail::UnspecifiedNested<Type>(std::forward<E>(exception));
102}
103
123void PrintComposedExceptions(std::ostream& os, const std::vector<std::exception_ptr>& exceptions);
124
142void PrintNestedExceptions(std::ostream& os, const std::exception& exception);
143
154void PrintNestedExceptions(std::ostream& os, std::exception_ptr ptr);
155
169public:
173 explicit NestedExceptionPrinter(const std::exception& exception) noexcept
174 : m_ptr(), m_exception(&exception) {};
175
178 explicit NestedExceptionPrinter(std::exception_ptr ptr) noexcept
179 : m_ptr(std::move(ptr)), m_exception(nullptr) {};
180
186 std::string Str() const {
187 std::stringstream ss;
188 ss << *this;
189 return ss.str();
190 }
191
199 friend std::ostream& operator<<(std::ostream& os, const NestedExceptionPrinter& printer) {
200 if (printer.m_ptr) {
201 PrintNestedExceptions(os, printer.m_ptr);
202 } else if (printer.m_exception) {
203 PrintNestedExceptions(os, *printer.m_exception);
204 }
205 return os;
206 }
207
208private:
209 // A variant or union would occupy same space so this was preferred over
210 // including <variant>
211 std::exception_ptr m_ptr;
212 const std::exception* m_exception;
213};
214
220class RtctkException : public elt::error::CiiBaseException {
221public:
222 RtctkException() noexcept;
223
224 explicit RtctkException(const std::string& msg);
225
226 RtctkException(const RtctkException& other) noexcept;
227
228 ~RtctkException() override = default;
229};
230
238public:
239 explicit NotImplementedException(const std::string& feature);
240};
241
249public:
250 explicit UnsupportedTypeException(const std::string& type);
251 explicit UnsupportedTypeException(const std::type_info& type);
252 explicit UnsupportedTypeException(const elt::common::CiiBasicDataType type);
253};
254
262public:
263 explicit UnsupportedUriException(const elt::mal::Uri& uri);
264 explicit UnsupportedUriException(const std::string& context, const elt::mal::Uri& uri);
265};
266
274public:
280 explicit BufferTooSmall(const std::size_t actual, const std::size_t expected);
281
288 explicit BufferTooSmall(const std::size_t actual,
289 const std::size_t expected,
290 const std::string& reason);
291
292 std::size_t GetActualBufferSize() const {
293 return m_actual;
294 }
295 std::size_t GetExpectedBufferSize() const {
296 return m_expected;
297 }
298
299protected:
300 std::size_t m_actual;
301 std::size_t m_expected;
302};
303
310public:
311 explicit InvalidArgumentException(const std::string& message);
312};
313
323public:
324 explicit InitialisationException(const std::string& message);
325};
326
334public:
336 explicit InvalidStateChange(const std::string& message)
337 : RtctkException("Invalid Operation: " + message) {
338 }
339};
340
348public:
350 explicit InvalidSetting(const std::string& message)
351 : RtctkException("Invalid Setting: " + message) {
352 }
353};
354
362public:
364 explicit DdtSinkNotFound(const std::string& message)
365 : RtctkException("DDT Sink not found: " + message) {
366 }
367};
368
376class DdtError : public RtctkException {
377public:
379 explicit DdtError(const std::string& message)
380 : RtctkException("Error in DDT Publisher: " + message) {
381 }
382};
383
391class IpcqError : public RtctkException {
392public:
394 explicit IpcqError(const std::string& message) : RtctkException("Error in IPCQ: " + message) {
395 }
396};
397
405public:
406 explicit AdapterCreationException(const std::string& interface_name);
407};
408
409} // namespace rtctk::componentFramework
410
411#endif // RTCTK_COMPONENTFRAMEWORK_EXCEPTIONS_HPP
InvalidArgumentException(const std::string &message)
Definition exceptions.cpp:179
AdapterCreationException(const std::string &interface_name)
Definition exceptions.cpp:191
std::size_t m_actual
Definition exceptions.hpp:300
std::size_t GetActualBufferSize() const
Definition exceptions.hpp:292
std::size_t m_expected
Definition exceptions.hpp:301
BufferTooSmall(const std::size_t actual, const std::size_t expected)
Construct exception object.
Definition exceptions.cpp:158
std::size_t GetExpectedBufferSize() const
Definition exceptions.hpp:295
RtctkException() noexcept
Definition exceptions.cpp:112
DdtError(const std::string &message)
Definition exceptions.hpp:379
RtctkException() noexcept
Definition exceptions.cpp:112
DdtSinkNotFound(const std::string &message)
Definition exceptions.hpp:364
InitialisationException(const std::string &message)
Definition exceptions.cpp:185
InvalidArgumentException(const std::string &message)
Definition exceptions.cpp:179
InvalidSetting(const std::string &message)
Definition exceptions.hpp:350
RtctkException() noexcept
Definition exceptions.cpp:112
InvalidStateChange(const std::string &message)
Definition exceptions.hpp:336
RtctkException() noexcept
Definition exceptions.cpp:112
RtctkException() noexcept
Definition exceptions.cpp:112
IpcqError(const std::string &message)
Definition exceptions.hpp:394
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:186
NestedExceptionPrinter(const std::exception &exception) noexcept
Construct from exception derived from std::exception.
Definition exceptions.hpp:173
NestedExceptionPrinter(std::exception_ptr ptr) noexcept
Construct from exception_ptr.
Definition exceptions.hpp:178
friend std::ostream & operator<<(std::ostream &os, const NestedExceptionPrinter &printer)
Formats exception from printer using PrintNestedExceptions.
Definition exceptions.hpp:199
NotImplementedException(const std::string &feature)
Definition exceptions.cpp:123
RtctkException() noexcept
Definition exceptions.cpp:112
RtctkException(const RtctkException &other) noexcept
UnsupportedTypeException(const std::string &type)
Definition exceptions.cpp:130
UnsupportedUriException(const elt::mal::Uri &uri)
Definition exceptions.cpp:147
Project-wide configuration header.
#define RTCTK_API
Helper to indicate that a class or function must be exported in the public symbol table.
Definition config.hpp:32
void PrintNestedExceptions(std::ostream &os, const std::exception &exception)
Print nested exception(s) in exception to os.
Definition exceptions.cpp:212
void PrintComposedExceptions(std::ostream &os, const std::vector< std::exception_ptr > &exceptions)
Print composed exception messages in exceptions list to os.
Definition exceptions.cpp:197
auto WrapWithNested(E &&exception) noexcept(std::is_nothrow_constructible_v< detail::UnspecifiedNested< typename std::decay_t< E > >, E && >)
Constructs an unspecified exception that derives from both the provided object and std::nested_except...
Definition exceptions.hpp:96
Definition measurable.hpp:25
std::ostream & JoinLines(std::ostream &os, std::string_view lines, std::string_view initial_indent, std::string_view subsequent_indent)
Join each line in lines with.
Definition exceptions.cpp:25
Definition commandReplier.cpp:21
Definition ddsSub.hpp:155
Unspecified exception used by WrapWithNested.
Definition exceptions.hpp:57
UnspecifiedNested(E &&e) noexcept(std::is_nothrow_constructible_v< T, E && >)
Definition exceptions.hpp:60