Skip to content

Commit 5ab135b

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 5ab135b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

api/ErrorCodes.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
#include <stdint.h>
3+
4+
namespace arduino {
5+
6+
typedef int_fast32_t error_t;
7+
8+
/* Error Codes:
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+
14+
enum : error_t {
15+
ArduinoError = 0,
16+
ArduinoSuccess = 1,
17+
};
18+
19+
class ErrorCode {
20+
public:
21+
constexpr ErrorCode(error_t value): value(value) {}
22+
const error_t value;
23+
24+
inline constexpr operator bool() const {
25+
return value == ArduinoSuccess;
26+
}
27+
28+
inline constexpr operator int() const {
29+
return value;
30+
}
31+
};
32+
33+
}

0 commit comments

Comments
 (0)