2 * Copyright (c) 2024 EfficiOS Inc.
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_ERROR_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_ERROR_HPP
13 #include <babeltrace2/babeltrace.h>
15 #include "common/assert.h"
16 #include "cpp-common/bt2c/c-string-view.hpp"
17 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
19 #include "borrowed-object.hpp"
20 #include "component-class.hpp"
24 class ConstComponentClassErrorCause;
25 class ConstComponentErrorCause;
26 class ConstMessageIteratorErrorCause;
28 class ConstErrorCause : public BorrowedObject<const bt_error_cause>
33 Unknown = BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN,
34 Component = BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT,
35 ComponentClass = BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS,
36 MessageIterator = BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR,
39 explicit ConstErrorCause(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
43 ActorType actorType() const noexcept
45 return static_cast<ActorType>(bt_error_cause_get_actor_type(this->libObjPtr()));
48 bool actorTypeIsComponentClass() const noexcept
50 return this->actorType() == ActorType::ComponentClass;
53 bool actorTypeIsComponent() const noexcept
55 return this->actorType() == ActorType::Component;
58 bool actorTypeIsMessageIterator() const noexcept
60 return this->actorType() == ActorType::MessageIterator;
63 ConstComponentClassErrorCause asComponentClass() const noexcept;
64 ConstComponentErrorCause asComponent() const noexcept;
65 ConstMessageIteratorErrorCause asMessageIterator() const noexcept;
67 bt2c::CStringView message() const noexcept
69 return bt_error_cause_get_message(this->libObjPtr());
72 bt2c::CStringView moduleName() const noexcept
74 return bt_error_cause_get_module_name(this->libObjPtr());
77 bt2c::CStringView fileName() const noexcept
79 return bt_error_cause_get_file_name(this->libObjPtr());
82 std::uint64_t lineNumber() const noexcept
84 return bt_error_cause_get_line_number(this->libObjPtr());
88 class ConstComponentClassErrorCause final : public ConstErrorCause
91 explicit ConstComponentClassErrorCause(const LibObjPtr libObjPtr) : ConstErrorCause {libObjPtr}
93 BT_ASSERT(this->actorTypeIsComponentClass());
96 bt2::ComponentClass::Type componentClassType() const noexcept
98 return static_cast<bt2::ComponentClass::Type>(
99 bt_error_cause_component_class_actor_get_component_class_type(this->libObjPtr()));
102 bt2c::CStringView componentClassName() const noexcept
104 return bt_error_cause_component_class_actor_get_component_class_name(this->libObjPtr());
107 bt2c::CStringView pluginName() const noexcept
109 return bt_error_cause_component_class_actor_get_plugin_name(this->libObjPtr());
113 inline ConstComponentClassErrorCause ConstErrorCause::asComponentClass() const noexcept
115 return ConstComponentClassErrorCause {this->libObjPtr()};
118 class ConstComponentErrorCause final : public ConstErrorCause
121 explicit ConstComponentErrorCause(const LibObjPtr libObjPtr) : ConstErrorCause {libObjPtr}
123 BT_ASSERT(this->actorTypeIsComponent());
126 bt2c::CStringView componentName() const noexcept
128 return bt_error_cause_component_actor_get_component_name(this->libObjPtr());
131 bt2::ComponentClass::Type componentClassType() const noexcept
133 return static_cast<bt2::ComponentClass::Type>(
134 bt_error_cause_component_actor_get_component_class_type(this->libObjPtr()));
137 bt2c::CStringView componentClassName() const noexcept
139 return bt_error_cause_component_actor_get_component_class_name(this->libObjPtr());
142 bt2c::CStringView pluginName() const noexcept
144 return bt_error_cause_component_actor_get_plugin_name(this->libObjPtr());
148 inline ConstComponentErrorCause ConstErrorCause::asComponent() const noexcept
150 return ConstComponentErrorCause {this->libObjPtr()};
153 class ConstMessageIteratorErrorCause final : public ConstErrorCause
156 explicit ConstMessageIteratorErrorCause(const LibObjPtr libObjPtr) : ConstErrorCause {libObjPtr}
158 BT_ASSERT(this->actorTypeIsMessageIterator());
161 bt2c::CStringView componentOutputPortName() const noexcept
163 return bt_error_cause_message_iterator_actor_get_component_name(this->libObjPtr());
166 bt2c::CStringView componentName() const noexcept
168 return bt_error_cause_message_iterator_actor_get_component_name(this->libObjPtr());
171 bt2::ComponentClass::Type componentClassType() const noexcept
173 return static_cast<bt2::ComponentClass::Type>(
174 bt_error_cause_message_iterator_actor_get_component_class_type(this->libObjPtr()));
177 bt2c::CStringView componentClassName() const noexcept
179 return bt_error_cause_message_iterator_actor_get_component_class_name(this->libObjPtr());
182 bt2c::CStringView pluginName() const noexcept
184 return bt_error_cause_message_iterator_actor_get_plugin_name(this->libObjPtr());
188 inline ConstMessageIteratorErrorCause ConstErrorCause::asMessageIterator() const noexcept
190 return ConstMessageIteratorErrorCause {this->libObjPtr()};
193 class ConstErrorIterator;
195 class ConstErrorCauseProxy final
197 friend ConstErrorIterator;
200 explicit ConstErrorCauseProxy(const ConstErrorCause cause) noexcept : _mCause {cause}
205 const ConstErrorCause *operator->() const noexcept
211 ConstErrorCause _mCause;
214 class UniqueConstError;
216 class ConstErrorIterator final
218 friend UniqueConstError;
221 explicit ConstErrorIterator(const UniqueConstError& error, const std::uint64_t index) noexcept :
222 _mError {error}, _mIndex {index}
227 bool operator==(const ConstErrorIterator& other) const noexcept
229 BT_ASSERT(&other._mError == &_mError);
230 return other._mIndex == _mIndex;
233 bool operator!=(const ConstErrorIterator& other) const noexcept
235 return !(*this == other);
238 ConstErrorIterator& operator++() noexcept
244 ConstErrorIterator operator++(int) noexcept
246 const auto ret = *this;
252 ConstErrorCause operator*() const noexcept;
254 ConstErrorCauseProxy operator->() const noexcept
256 return ConstErrorCauseProxy {**this};
260 const UniqueConstError& _mError;
261 std::uint64_t _mIndex;
264 class UniqueConstError final
267 using LibObjPtr = const bt_error *;
269 explicit UniqueConstError(const LibObjPtr libError) noexcept : _mLibError {libError}
273 explicit operator bool() const noexcept
275 return this->libObjPtr();
278 LibObjPtr libObjPtr() const noexcept
280 return _mLibError.get();
283 LibObjPtr release() noexcept
285 return _mLibError.release();
288 std::uint64_t length() const noexcept
290 return bt_error_get_cause_count(this->libObjPtr());
293 ConstErrorCause operator[](const std::uint64_t index) const noexcept
295 return ConstErrorCause {bt_error_borrow_cause_by_index(this->libObjPtr(), index)};
298 ConstErrorIterator begin() const noexcept
300 BT_ASSERT(_mLibError);
301 return ConstErrorIterator {*this, 0};
304 ConstErrorIterator end() const noexcept
306 BT_ASSERT(_mLibError);
307 return ConstErrorIterator {*this, this->length()};
311 struct _LibErrorDeleter final
313 void operator()(const LibObjPtr libError) const noexcept
315 bt_error_release(libError);
319 std::unique_ptr<std::remove_pointer<LibObjPtr>::type, _LibErrorDeleter> _mLibError;
322 inline ConstErrorCause ConstErrorIterator::operator*() const noexcept
324 return _mError[_mIndex];
327 inline UniqueConstError takeCurrentThreadError() noexcept
329 return UniqueConstError {bt_current_thread_take_error()};
332 inline void moveErrorToCurrentThread(UniqueConstError error) noexcept
334 bt_current_thread_move_error(error.release());
337 } /* namespace bt2 */
339 #endif /* BABELTRACE_CPP_COMMON_BT2_ERROR_HPP */