Skip to content

Commit e73c610

Browse files
Introducing ErrorCode
The objective of error code class to wrap an integer that may be returned from a function that may fail for different reasons. Since we are not able to return values different from 0 in case of error in the arduino language we need to wrap them into this class and redefine boolean operator to mantain the same behaviour
1 parent 4a02bfc commit e73c610

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

api/ErrorCodes.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
#include <stdint.h>
3+
4+
namespace arduino {
5+
6+
typedef int_fast32_t error_t;
7+
8+
/* Error Values encoding:
9+
* In Arduino an error is represented with the integer value 1, thus every value that is not 1 is considered error.
10+
* Errors are generally represented with an int type that may vary in size depending on the platform.
11+
* For this reason in this representation error_t type is defined with an integer type with a defined size.
12+
*/
13+
enum : error_t {
14+
ArduinoError = 0,
15+
ArduinoSuccess = 1,
16+
};
17+
18+
class ErrorCode {
19+
public:
20+
constexpr ErrorCode(error_t value): value(value) {}
21+
const error_t value;
22+
23+
inline constexpr operator bool() const {
24+
return value == ArduinoSuccess;
25+
}
26+
27+
inline constexpr operator int() const {
28+
return value;
29+
}
30+
};
31+
32+
}

0 commit comments

Comments
 (0)