iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_REPORTING_ERROR_REPORTING_TYPES_HPP
15#define IOX2_BB_REPORTING_ERROR_REPORTING_TYPES_HPP
16
17#include <cstdint>
18#include <utility>
19
20namespace iox2 {
21namespace legacy {
22namespace er {
23
24static constexpr const char* UNKNOWN_ERROR_NAME = "unknown error";
25static constexpr const char* UNKNOWN_MODULE_NAME = "unknown module";
26
27// These are lightweight regular read/write types that do not require encapsulation (no invariants
28// can be broken).
29
30struct ErrorCode {
31 using type = uint32_t;
32
34
35 constexpr explicit ErrorCode(uint32_t value)
36 : value(value) {
37 }
38
39 bool operator==(const ErrorCode& rhs) const {
40 return value == rhs.value;
41 }
42
43 bool operator!=(const ErrorCode& rhs) const {
44 return !(*this == rhs);
45 }
46};
47
48struct ModuleId {
49 using type = uint32_t;
50
52
53 static constexpr type ANY { 0 };
54 static constexpr type IOX2_BB { 1 };
55 static constexpr type IOX2 { 2 };
56
57 // User module ids should be larger or equal than this to avoid conflicts
58 // with internal modules.
59 // All lower values are reserved.
60 static constexpr type USER_MODULE_BASE { 0x100 };
61
62 constexpr explicit ModuleId(uint32_t value = ANY)
63 : value(value) {
64 }
65
66 bool operator==(const ModuleId& rhs) const {
67 return value == rhs.value;
68 }
69
70 bool operator!=(const ModuleId& rhs) const {
71 return !(*this == rhs);
72 }
73};
74
75// primary template is the identity
76// this can be overriden by modules to handle specific errors
77template <typename ErrorLike>
78auto toError(ErrorLike&& value) {
79 return std::forward<ErrorLike>(value);
80}
81
82template <class Error>
83inline ErrorCode toCode(const Error& error) {
84 return error.code();
85}
86
87template <>
89 return error;
90}
91
92template <class Error>
93inline ModuleId toModule(const Error& error) {
94 return error.module();
95}
96
97template <class Error>
98inline const char* toModuleName(const Error& error) {
99 return toError(error).moduleName();
100}
101
102template <class Error>
103inline const char* toErrorName(const Error& error) {
104 return toError(error).name();
105}
106
107} // namespace er
108} // namespace legacy
109} // namespace iox2
110
111// NOLINTJUSTIFICATION These macros are used to create enums and corresponding string arrays with the names of the enum tags
112// NOLINTBEGIN(cppcoreguidelines-macro-usage)
113#define IOX2_CREATE_ERROR_ENUM(name) name,
114#define IOX2_CREATE_ERROR_STRING(name) #name,
115// NOLINTEND(cppcoreguidelines-macro-usage)
116
117#endif // IOX2_BB_REPORTING_ERROR_REPORTING_TYPES_HPP
HoofsErrorType toError(HoofsError code)
static constexpr const char * UNKNOWN_MODULE_NAME
Definition types.hpp:25
ErrorCode toCode(const Error &error)
Definition types.hpp:83
const char * toErrorName(const Error &error)
Definition types.hpp:103
ErrorCode toCode< ErrorCode >(const ErrorCode &error)
Definition types.hpp:88
ModuleId toModule(HoofsError)
static constexpr const char * UNKNOWN_ERROR_NAME
Definition types.hpp:24
const char * toModuleName(const Error &error)
Definition types.hpp:98
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
detail::err< T > error
Definition expected.hpp:32
helper struct to create an expected which is signalling an error more easily
bool operator==(const ErrorCode &rhs) const
Definition types.hpp:39
bool operator!=(const ErrorCode &rhs) const
Definition types.hpp:43
constexpr ErrorCode(uint32_t value)
Definition types.hpp:35
static constexpr type IOX2
Definition types.hpp:55
constexpr ModuleId(uint32_t value=ANY)
Definition types.hpp:62
static constexpr type IOX2_BB
Definition types.hpp:54
bool operator!=(const ModuleId &rhs) const
Definition types.hpp:70
static constexpr type ANY
Definition types.hpp:53
static constexpr type USER_MODULE_BASE
Definition types.hpp:60
bool operator==(const ModuleId &rhs) const
Definition types.hpp:66