Skip to content

Commit e175fff

Browse files
Introducing ReturnValue
The objective of ReturnValue is to maintain coherency with ErrorCode class in cases where a single value is used to represent both the error and the return value
1 parent f208b38 commit e175fff

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
@@ -26,5 +26,38 @@ class ErrorCode {
2626
}
2727
};
2828

29+
/* ReturnValueClass is meant to represent all the cases where with a single value
30+
* we want to return an error, if the value is negative, or a meaningful result
31+
* if greater than or equal to 0.
32+
* In order to be retrocompatible with the previous definition boolean evaluation:
33+
* - It must return true, if the value is greater than or equal 0
34+
* - It must return false, if the value is negatie
35+
* - It must be evaluable as the primitive type associated with
36+
*/
37+
template<typename T>
38+
class ReturnValueClass {
39+
public:
40+
constexpr ReturnValueClass(T value)
41+
: value(value >= 0? value : 0), error(value < 0? value : ArduinoSuccess) {}
42+
43+
// it would be nice to have a default value on error to Success
44+
constexpr ReturnValueClass(T value, error_t error)
45+
: value(value), error(error) {}
46+
47+
const T value;
48+
const error_t error;
49+
50+
constexpr operator bool() const {
51+
return error == ArduinoSuccess;
52+
}
53+
54+
constexpr operator T() const {
55+
return value;
56+
}
57+
};
58+
59+
using ReturnValue = ReturnValueClass<int>;
60+
using ReturnBoolValue = ReturnValueClass<bool>;
61+
using ReturnLongValue = ReturnValueClass<int64_t>;
2962

3063
}

0 commit comments

Comments
 (0)