-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResult.h
47 lines (40 loc) · 1.26 KB
/
Result.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once
#ifndef __RESULT_H__
#define __RESULT_H__
/// @brief Result codes / error codes (sync with app)
enum ResultType
{
Ok = 0,
Timeout = 1,
Cancelled = 2,
Unexpected = 3,
Overflow = 4,
SyncError = 5,
ChecksumError = 6,
End = 0xFF
};
/// @brief Result class for read functions -- stores errors as negative values
class Result
{
public:
Result(int v) : value(v) {}
Result(ResultType rt) : value(-rt) {}
~Result() = default;
Result(const Result& other) = default;
Result& operator=(const Result& other) = default;
Result(Result&& other) noexcept = default;
Result& operator=(Result&& other) noexcept = default;
// Type conversion operator
operator int() const { return Value(); }
operator byte() const { return Value(); }
operator ResultType() const { return Type(); }
bool HasValue() const { return value >= 0; }
bool IsError() const { return value < 0; }
bool IsDone() const { return value == (-ResultType::End); }
ResultType Type() const { return (value >= 0 ? ResultType::Ok : (ResultType)-value); }
int Value() const { return value >= 0 ? value : 0; }
Result AsErrorCode() { return HasValue() ? (ResultType)Value() : Type(); }
private:
int value;
};
#endif