Skip to content

Commit f208b38

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 f208b38

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

api/ErrorCodes.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 an error is represented with the integer value 1, thus every value that is not 1 is considered error.
15+
* Errors are generally represented with an int type that may vary in size depending on the platform.
16+
* For this reason in this representation error_t type is defined with an integer type with a defined size.
17+
*/
18+
class ErrorCode {
19+
public:
20+
constexpr ErrorCode(int value): error(value == 1? ArduinoSuccess : ArduinoError) {}
21+
constexpr ErrorCode(error_t value): error(value) {}
22+
const error_t error;
23+
24+
constexpr operator bool() const {
25+
return error == ArduinoSuccess;
26+
}
27+
};
28+
29+
30+
}

0 commit comments

Comments
 (0)