iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
violation.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved.
3// Copyright (c) 2025 Contributors to the Eclipse Foundation
4//
5// See the NOTICE file(s) distributed with this work for additional
6// information regarding copyright ownership.
7//
8// This program and the accompanying materials are made available under the
9// terms of the Apache Software License 2.0 which is available at
10// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
11// which is available at https://opensource.org/licenses/MIT.
12//
13// SPDX-License-Identifier: Apache-2.0 OR MIT
14
15#ifndef IOX2_BB_ERROR_REPORTING_VIOLATION_HPP
16#define IOX2_BB_ERROR_REPORTING_VIOLATION_HPP
17
18#include <utility>
19
21
22namespace iox2 {
23namespace legacy {
24namespace er {
25
26// We expect an error to have the following interface
27// 1. ErrorCode code() const
28// 2. ModuleId module() const
29
30// By default, there are only error codes and violations.
31// Custom errors can be added but must satisfy the minimal interface.
32
33// NOLINTNEXTLINE(performance-enum-size) the type is required for error handling
38
39class Violation {
40 public:
42 : m_code(static_cast<ErrorCode::type>(code)) {
43 }
44
46 : m_code(code) {
47 }
48
50 : m_code(code)
51 , m_module(module) {
52 }
53
54 ErrorCode code() const {
55 return m_code;
56 }
57
58 ModuleId module() const {
59 return m_module;
60 }
61
62 const char* name() const {
63 switch (static_cast<ViolationErrorCode>(m_code.value)) {
65 return "ASSERT_VIOLATION";
67 return "ENFORCE_VIOLATION";
68 }
69 return "unknown error";
70 }
71
72 static const char* moduleName() {
73 return "ANY";
74 }
75
76 bool operator==(const Violation& rhs) const {
77 return m_code == rhs.m_code && m_module == rhs.m_module;
78 }
79
80 bool operator!=(const Violation& rhs) const {
81 return !(*this == rhs);
82 }
83
87
91
92 private:
93 ErrorCode m_code;
94 ModuleId m_module { ModuleId::ANY };
95};
96
97} // namespace er
98
99const char* asStringLiteral(const er::ViolationErrorCode error) noexcept;
100
101} // namespace legacy
102} // namespace iox2
103
104#endif // IOX2_BB_ERROR_REPORTING_VIOLATION_HPP
ErrorCode code() const
Definition violation.hpp:54
Violation(ViolationErrorCode code)
Definition violation.hpp:41
Violation(ErrorCode code)
Definition violation.hpp:45
bool operator!=(const Violation &rhs) const
Definition violation.hpp:80
Violation(ErrorCode code, ModuleId module)
Definition violation.hpp:49
static const char * moduleName()
Definition violation.hpp:72
static Violation createAssertViolation()
Definition violation.hpp:84
static Violation createEnforceViolation()
Definition violation.hpp:88
ModuleId module() const
Definition violation.hpp:58
bool operator==(const Violation &rhs) const
Definition violation.hpp:76
const char * name() const
Definition violation.hpp:62
constexpr AssertViolationKind ASSERT_VIOLATION
constexpr EnforceViolationKind ENFORCE_VIOLATION
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
static const char * asStringLiteral(const HoofsError error)
helper struct to create an expected which is signalling an error more easily
static constexpr type ANY
Definition types.hpp:53