-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathErrorStatus.h
More file actions
142 lines (114 loc) · 3.65 KB
/
ErrorStatus.h
File metadata and controls
142 lines (114 loc) · 3.65 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
//
// Copyright 2013 (C). Alex Robenko. All rights reserved.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include "ErrorCode.h"
namespace embxx
{
namespace error
{
/// @addtogroup error
/// @{
/// @brief Error code wrapper class.
/// @details This class wraps error code enum value to provide simple boolean
/// checks for operation success/error
/// @tparam TErrorCode Error code enum type. 0 value must correspond to success.
/// @headerfile embxx/error/ErrorStatus.h
template <typename TErrorCode = ErrorCode>
class ErrorStatusT
{
public:
/// @brief Error code enum type
typedef TErrorCode ErrorCodeType;
/// @brief Default constructor.
/// @details The code value is 0, which is "success".
ErrorStatusT()
: code_(static_cast<ErrorCodeType>(0))
{
}
/// @brief Constructor
/// @details This constructor may be used for implicit construction of
/// error status object out of error code value.
/// @param codeVal Numeric error code value.
ErrorStatusT(ErrorCodeType codeVal)
: code_(codeVal)
{
}
/// @brief Copy constructor is default
ErrorStatusT(const ErrorStatusT&) = default;
/// @brief Destructor is default
~ErrorStatusT() = default;
/// @brief Copy assignment is default
ErrorStatusT& operator=(const ErrorStatusT&) = default;
/// @brief Retrieve error code value.
const ErrorCodeType code() const
{
return code_;
}
/// @brief Equality comparison operator
bool operator==(const ErrorStatusT& other) const
{
return code() == other.code();
}
/// @brief Equality comparison operator
bool operator==(ErrorCodeType otherCode) const
{
return code() == otherCode;
}
/// @brief Inequality comparison operator
template <typename TOther>
bool operator!=(TOther&& other) const
{
return !(operator==(std::forward<TOther>(other)));
}
/// @brief boolean conversion operator.
/// @details Returns true if error code is not equal 0, i.e. any error
/// will return true, success value will return false.
operator bool() const
{
return (code_ != static_cast<ErrorCodeType>(0));
}
/// @brief Same as !(static_cast<bool>(*this)).
bool operator!() const
{
return !(static_cast<bool>(*this));
}
private:
ErrorCodeType code_;
};
/// @brief ErrorStatus that uses embxx::error::ErrorCode for its error codes.
/// @related ErrorStatusT
/// @headerfile embxx/error/ErrorStatus.h
typedef ErrorStatusT<ErrorCode> ErrorStatus;
// Implementation
/// @brief Error status equality comparison operator
/// @related ErrorStatusT
template <typename TOther, typename TErrorCode>
bool operator==(
TOther&& value,
const ErrorStatusT<TErrorCode>& status)
{
return status == std::forward<TOther>(value);
}
template <typename TOther, typename TErrorCode>
bool operator!=(
TOther&& value,
const ErrorStatusT<TErrorCode>& status)
{
return status != std::forward<TOther>(value);
}
/// @}
} // namespace error
} // namespace embxx