-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-0-5.cpp
More file actions
177 lines (112 loc) · 2.87 KB
/
A15-0-5.cpp
File metadata and controls
177 lines (112 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Rule: A15-0-5
// Source line: 22747
// Original file: A15-0-5.cpp
//% $Id: A15-0-5.cpp 309502 2018-02-28 09:17:39Z michal.szczepankiewicz $
#include <cstdint>
#include <stdexcept>
#include <system_error>
// @checkedException
class CommunicationError
: public std::exception // Compliant - communication error is "checked"
{
public:
explicit CommunicationError(const char* message) : msg(message) {}
CommunicationError(CommunicationError const&) noexcept = default;
CommunicationError& operator=(CommunicationError const&) noexcept = default;
~CommunicationError() override = default;
const char* what() const noexcept override {
return msg;
}
private:
const char* msg;
};
// @checkedException
class BusError
: public CommunicationError // Compliant - bus error is "checked"
{
public:
using CommunicationError::CommunicationError;
};
// @checkedException
class Timeout : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
// Compliant - communication timeout
// is "checked"
};
// @checkedException
class PreconditionsError : public std::exception
{
// Implementation
// Non-compliant - error on
// preconditions check should
// be "unchecked" but is
// defined to be "checked"
};
void Fn1(std::uint8_t* buffer, std::uint8_t bufferLength) noexcept(false)
{
bool sentSuccessfully = true;
// ...
if (!sentSuccessfully)
{
throw CommunicationError(
"Could not send data");
}
// Checked exception thrown correctly
}
void Fn2(std::uint8_t* buffer, std::uint8_t bufferLength) noexcept(false)
{
bool initSuccessfully = true;
if (!initSuccessfully)
{
throw PreconditionsError();
// An exception thrown on preconditions
// check failure should be "Unchecked", but
// PreconditionsError is "Checked"
}
// ...
bool sentSuccessfully = true;
bool isTimeout = false;
// ...
if (!sentSuccessfully)
{
throw BusError(
"Could not send data");
}
// Checked exception thrown correctly
// ...
if (isTimeout)
{
throw Timeout("Timeout reached");
}
// Checked exception thrown correctly
}
void Fn3(std::uint8_t* buffer) noexcept(false)
{
bool isResourceBusy = false;
// ...
if (isResourceBusy)
{
throw std::runtime_error(
"Resource is busy now");
}
// Checked exception thrown correctly
}
class Thread // Class which mimics the std::thread
{
public:
// Implementation
Thread() noexcept(false)
{
bool resourcesAvailable = false;
// ...
if (!resourcesAvailable)
{
throw std::system_error(
static_cast<int>(std::errc::resource_unavailable_try_again),
std::generic_category()); // Compliant - correct usage of
// checked exception system_error
}
}
};