Skip to content

Commit c2c4b20

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 a919d81 commit c2c4b20

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

api/ErrorCodes.h

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

0 commit comments

Comments
 (0)