Sen API
Sen Libraries
Loading...
Searching...
No Matches
assert_impl.h
Go to the documentation of this file.
1// === assert_impl.h ===================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_DETAIL_ASSERT_IMPL_H
9#define SEN_CORE_BASE_DETAIL_ASSERT_IMPL_H
10
12
13// std
14#include <cstdint>
15#include <functional>
16#include <optional>
17#include <ostream>
18#include <string>
19#include <string_view>
20
21namespace sen::impl
22{
23
25enum class CheckType : std::uint8_t
26{
27 assert = 0,
28 expect = 1,
29 ensure = 2,
30};
31
33struct CheckInfo
34{
42 CheckInfo(CheckType checkType, std::string_view expression, SourceLocation sourceLocation)
43 : checkType_(checkType), expression_(expression), sourceLocation_(sourceLocation)
44 {
45 }
46
47 [[nodiscard]] CheckType getCheckType() const noexcept { return checkType_; }
48 [[nodiscard]] std::string_view getExpression() const noexcept { return expression_; }
49 [[nodiscard]] const SourceLocation& getSourceLocation() const noexcept { return sourceLocation_; }
50
51 [[nodiscard]] std::string str() const noexcept;
52
53 friend std::ostream& operator<<(std::ostream& s, const CheckInfo& checkInfo) { return s << checkInfo.str(); }
54
55private:
56 CheckType checkType_;
57 std::string_view expression_;
58 SourceLocation sourceLocation_;
59};
60
61inline std::optional<CheckInfo> lastReportedAssertionError {std::nullopt};
62
65using FailedCheckHandler = std::function<void(const CheckInfo&)>;
66
71[[nodiscard]] FailedCheckHandler setFailedCheckHandler(FailedCheckHandler handler) noexcept;
72
77void senCheckImpl(bool checkResult,
78 CheckType checkType,
79 std::string_view expression,
80 const SourceLocation& sourceLocation) noexcept;
81
82} // namespace sen::impl
83
84#endif // SEN_CORE_BASE_DETAIL_ASSERT_IMPL_H