ipcq 0.12.0
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup ipcq
4 * @brief Contains declarations for ipcq::Error and related.
5 * @copyright
6 * SPDX-FileCopyrightText: 2018-2021 European Southern Observatory (ESO)
7 *
8 * SPDX-License-Identifier: LGPL-3.0-only
9 */
10#ifndef IPCQ_ERROR_HPP_
11#define IPCQ_ERROR_HPP_
12
13#include <system_error>
14
15namespace ipcq {
16
17/**
18 * ipcq err condition enum.
19 *
20 * Although the errors are "portable", so considered an error_condition, rather than an error_code,
21 * `std::error_code` can still be created explicitly with `ipcq::make_error_ode(Error)`.
22 *
23 * @ingroup ipcq
24 */
25enum class Error {
26 /**
27 * Non-blocking operation would block.
28 */
30 /**
31 * Queue is closed or otherwise not available for reading.
32 */
34 /**
35 * Operation timed out.
36 */
38 /**
39 * Reader is in an inconsistent state w.r.t. shared memory state.
40 *
41 * The typical scenario for this error is if the writer overtakes the reader, overwriting an
42 * element being expecter by the reader.
43 */
45 /**
46 * Queue type mismatch.
47 */
49 /**
50 * Value exeeds storage capacity.
51 */
53 /**
54 * File type is incorrect (not an ipcq shared object).
55 */
57 /**
58 * File version mismatch (shared object created by ipcq with different file version).
59 */
61};
62
63/**
64 * ipcq error category.
65 *
66 * @ingroup ipcq
67 */
68class ErrorCategory : public std::error_category {
69 /**
70 * @return a human readable string for the error category.
71 */
72 const char* name() const noexcept override {
73 return "ipcq";
74 }
75
76 /**
77 * @return a human readable message for an ipcq::Error.
78 */
79 std::string message(int ev) const noexcept override {
80 switch (static_cast<Error>(ev)) {
82 return "non-blocking operation would block";
83 case Error::Closed:
84 return "queue is closed";
85 case Error::Timeout:
86 return "operation timed out";
88 return "queue is in a inconsistent state";
90 return "type in shared memory does not match expected type";
91 case Error::Overflow:
92 return "value exeeds storage capacity";
93 case Error::BadFile:
94 return "file is not an ipcq shared object";
96 return "shared object file use a different protocol version";
97 default:
98 return "unknown";
99 }
100 }
101};
102
103/**
104 * ipcq error category.
105 *
106 * @note There can only be a single instance of the error-category, as mandated by the standard.
107 *
108 * @ingroup ipcq
109 */
110inline const std::error_category& GetErrorCategory() {
111 static const ErrorCategory inst{};
112 return inst;
113}
114
115/**
116 * Function overload to create `std::error_code` from `ipcq::Error`.
117 *
118 * @ingroup ipcq
119 */
120inline std::error_code make_error_code(Error e) { // NOLINT
121 return {static_cast<int>(e), ipcq::GetErrorCategory()};
122}
123
124/*
125 * Function overload to create `std::error_code` from `ipcq::Error`.
126 *
127 * @ingroup ipcq
128 */
129inline std::error_condition make_error_condition(Error e) { // NOLINT
130 return {static_cast<int>(e), ipcq::GetErrorCategory()};
131}
132
133} // namespace ipcq
134
135namespace std {
136
137template <>
138struct is_error_condition_enum<::ipcq::Error> : true_type {};
139} // namespace std
140
141#endif
ipcq error category.
Definition error.hpp:68
const std::error_category & GetErrorCategory()
ipcq error category.
Definition error.hpp:110
std::error_code make_error_code(Error e)
Function overload to create std::error_code from ipcq::Error.
Definition error.hpp:120
Error
ipcq err condition enum.
Definition error.hpp:25
@ Closed
Queue is closed or otherwise not available for reading.
Definition error.hpp:33
@ Overflow
Value exeeds storage capacity.
Definition error.hpp:52
@ InconsistentState
Reader is in an inconsistent state w.r.t.
Definition error.hpp:44
@ TypeMismatch
Queue type mismatch.
Definition error.hpp:48
@ BadVersion
File version mismatch (shared object created by ipcq with different file version).
Definition error.hpp:60
@ Timeout
Operation timed out.
Definition error.hpp:37
@ WouldBlock
Non-blocking operation would block.
Definition error.hpp:29
@ BadFile
File type is incorrect (not an ipcq shared object).
Definition error.hpp:56
std::error_condition make_error_condition(Error e)
Definition error.hpp:129
Definition error.hpp:135