Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
andreagilardoni committed Jul 21, 2025
commit 863ca6645d536889ea98522b3a6c2f04eec32d0e
32 changes: 32 additions & 0 deletions api/ErrorCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,37 @@ class ErrorCode {
}
};

/* ReturnValueClass is meant to represent all the cases where with a single value
* we want to return an error, if the value is negative, or a meaningful result
* if greater than or equal to 0.
* In order to be retrocompatible with the previous definition boolean evaluation:
* - It must return true, if the value is greater than or equal 0
* - It must return false, if the value is negatie
* - It must be evaluable as the primitive type associated with
*/
template<typename T>
class ReturnValueClass {
public:
constexpr ReturnValueClass(T value)
: value(value >= 0? value : 0), error(value < 0? (error_t)value : ArduinoSuccess) {}

// it would be nice to have a default value on error to Success
constexpr ReturnValueClass(T value, error_t error)
: value(value), error(error) {}

const T value; // TODO should we leave the const modifier?
const error_t error;

constexpr operator bool() const {
return error == ArduinoSuccess;
}

constexpr operator T() const {
return error == ArduinoSuccess ? value : error;
}
};

using ReturnValue = ReturnValueClass<int>;
using ReturnLongValue = ReturnValueClass<int64_t>;

}