Skip to content

Commit a9cc59b

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

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

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

3164
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy