Ersa Wearable Platform
Embedded smartwatch firmware and application framework
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <stddef.h>
5
6namespace ersa {
7
13enum class ErrorCode : int {
14 Ok = 0,
15 InvalidParam = -1,
16 OutOfMemory = -2,
17 NotFound = -3,
18 Busy = -4,
19 Timeout = -5,
20 NotSupported = -6,
21 HardwareFault = -7,
22 IoError = -8
23};
24
26struct Error {
30 const char* message{""};
31
33 constexpr Error() = default;
35 constexpr Error(ErrorCode c, const char* msg = "") : code(c), message(msg) {}
36
38 constexpr bool isOk() const { return code == ErrorCode::Ok; }
40 constexpr bool isError() const { return code != ErrorCode::Ok; }
41};
42
51template <typename T>
52class Result {
53public:
55 Result(const T& val) : value_(val), error_(ErrorCode::Ok) {}
57 Result(T&& val) : value_(static_cast<T&&>(val)), error_(ErrorCode::Ok) {}
59 Result(const Error& err) : error_(err) {}
61 Result(ErrorCode code, const char* msg = "") : error_(code, msg) {}
62
64 bool isOk() const { return error_.isOk(); }
66 bool isError() const { return error_.isError(); }
67
69 const T& value() const { return value_; }
71 T& value() { return value_; }
72
74 const Error& error() const { return error_; }
75
76private:
77 T value_{};
79};
80
82template <>
83class Result<void> {
84public:
88 Result(const Error& err) : error_(err) {}
90 Result(ErrorCode code, const char* msg = "") : error_(code, msg) {}
91
93 bool isOk() const { return error_.isOk(); }
95 bool isError() const { return error_.isError(); }
97 const Error& error() const { return error_; }
98
99private:
101};
102
104struct Point {
106 int16_t x{0};
108 int16_t y{0};
109
111 constexpr Point() = default;
113 constexpr Point(int16_t px, int16_t py) : x(px), y(py) {}
114};
115
121struct Rect {
123 int16_t x{0};
125 int16_t y{0};
127 int16_t w{0};
129 int16_t h{0};
130
132 constexpr Rect() = default;
134 constexpr Rect(int16_t rx, int16_t ry, int16_t rw, int16_t rh)
135 : x(rx), y(ry), w(rw), h(rh) {}
136
138 constexpr bool isEmpty() const { return w <= 0 || h <= 0; }
140 constexpr bool contains(int16_t px, int16_t py) const {
141 return px >= x && px < (x + w) && py >= y && py < (y + h);
142 }
143};
144
145} // namespace ersa
Result(ErrorCode code, const char *msg="")
Definition types.h:90
Result()
Definition types.h:86
bool isOk() const
Definition types.h:93
bool isError() const
Definition types.h:95
const Error & error() const
Definition types.h:97
Result(const Error &err)
Definition types.h:88
Definition types.h:52
T & value()
Definition types.h:71
bool isOk() const
Definition types.h:64
Result(const Error &err)
Definition types.h:59
const T & value() const
Definition types.h:69
Result(T &&val)
Definition types.h:57
Result(const T &val)
Definition types.h:55
Result(ErrorCode code, const char *msg="")
Definition types.h:61
bool isError() const
Definition types.h:66
const Error & error() const
Definition types.h:74
T value_
Definition types.h:77
Error error_
Definition types.h:78
Definition application.h:10
ErrorCode
Definition types.h:13
Definition types.h:26
constexpr bool isError() const
Definition types.h:40
constexpr bool isOk() const
Definition types.h:38
ErrorCode code
Definition types.h:28
constexpr Error(ErrorCode c, const char *msg="")
Definition types.h:35
const char * message
Definition types.h:30
constexpr Error()=default
Definition types.h:104
int16_t y
Definition types.h:108
int16_t x
Definition types.h:106
constexpr Point()=default
constexpr Point(int16_t px, int16_t py)
Definition types.h:113
Definition types.h:121
int16_t y
Definition types.h:125
constexpr bool contains(int16_t px, int16_t py) const
Definition types.h:140
int16_t h
Definition types.h:129
int16_t x
Definition types.h:123
int16_t w
Definition types.h:127
constexpr Rect(int16_t rx, int16_t ry, int16_t rw, int16_t rh)
Definition types.h:134
constexpr Rect()=default
constexpr bool isEmpty() const
Definition types.h:138