CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
CCSDSResult.h
Go to the documentation of this file.
1#ifndef CCSDS_RESULT_H
2#define CCSDS_RESULT_H
3
4#include <variant>
5#include <vector>
6#include <cstdint>
7
8//exclude includes when building for MCU
9#ifndef CCSDS_MCU
10 #include <iostream>
11#else
12 #include <string>
13#endif //CCSDS_MCU
14
15namespace CCSDS {
36
44 class Error {
45 public:
51 Error(const ErrorCode code, std::string message)
52 : m_code(code), m_message(std::move(message)) {
53 }
54
59 [[nodiscard]] ErrorCode code() const { return m_code; }
60
65 [[nodiscard]] const std::string &message() const { return m_message; }
66
67 private:
69 std::string m_message;
70 };
71
81 template<typename T>
82 class Result {
83 std::variant<T, Error> data;
84
85 public:
90 Result(T value) : data(std::move(value)) {
91 }
92
99
104 [[nodiscard]] bool has_value() const {
105 return std::holds_alternative<T>(data);
106 }
107
113 T &value() { return std::get<T>(data); }
114 const T &value() const { return std::get<T>(data); }
115
120 [[nodiscard]] Error error() const {
121 return std::get<Error>(data);
122 }
123
128 explicit operator bool() const { return has_value(); }
129 };
130
131 // Convenient type aliases for common result types
134}
135
136/* ERROR MANAGEMENT MACROS */
137
142#define RETURN_IF_ERROR(condition, errorCode) \
143do { if (condition) return errorCode; } while (0)
144
149#define RET_IF_ERR_MSG(condition, errorCode, message) \
150do { \
151 if (condition) { \
152 return CCSDS::Error{errorCode,message}; \
153 } \
154} while (0)
155
160#define ASSIGN_MV(var, result) \
161do { \
162 auto&& _res = (result); \
163 if (!_res) return _res.error(); \
164 var = std::move(_res.value()); \
165} while (0)
166
171#define ASSIGN_CP(var, result) \
172do { \
173auto&& _res = (result); \
174if (!_res) return _res.error(); \
175var = _res.value(); \
176} while (0)
177
178
183#define ASSIGN_OR_PRINT(var, result) \
184do { \
185 auto&& _res = (result); \
186 if (!_res) { \
187 printf("[ Error ]: Code [%u]: %s\n", static_cast<unsigned>(_res.error().code()), _res.error().message().c_str());; \
188 } else { \
189 var = std::move(_res.value()); \
190 } \
191} while (0)
192
197#define ASSERT_SUCCESS(result) \
198do { \
199 auto&& _res = (result); \
200 if (!_res.has_value()) return; \
201} while (0)
202
207#define FORWARD_RESULT(result) \
208do { \
209 auto&& _res = (result); \
210 if (!_res.has_value()) return _res; \
211} while (0)
212
213#endif // CCSDS_RESULT_H
Represents an error with both an error code and a message.
Definition CCSDSResult.h:44
ErrorCode code() const
Retrieves the error code.
Definition CCSDSResult.h:59
ErrorCode m_code
The error type.
Definition CCSDSResult.h:68
Error(const ErrorCode code, std::string message)
Constructs an error with a given error code and message.
Definition CCSDSResult.h:51
const std::string & message() const
Retrieves the error message.
Definition CCSDSResult.h:65
std::string m_message
A detailed message describing the error.
Definition CCSDSResult.h:69
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:82
bool has_value() const
Checks if the result contains a valid value.
Result(Error error)
Constructor for failure case.
Definition CCSDSResult.h:97
Result(T value)
Constructor for success case.
Definition CCSDSResult.h:90
const T & value() const
Error error() const
Retrieves the stored error.
T & value()
Retrieves the stored value.
std::variant< T, Error > data
Holds either a valid value or an Error.
Definition CCSDSResult.h:83
Contains definitions and classes for handling CCSDS headers.
ErrorCode
Defines various error codes used in CCSDS packet handling.
Definition CCSDSResult.h:20
@ NO_DATA
No data available.
Definition CCSDSResult.h:23
@ NONE
No error.
Definition CCSDSResult.h:21
@ FILE_READ_ERROR
Reading from file failure.
Definition CCSDSResult.h:32
@ UNKNOWN_ERROR
Unknown error.
Definition CCSDSResult.h:22
@ INVALID_CHECKSUM
Checksum validation failed.
Definition CCSDSResult.h:29
@ INVALID_APPLICATION_DATA
Application data is invalid.
Definition CCSDSResult.h:27
@ INVALID_DATA
Data is invalid.
Definition CCSDSResult.h:24
@ INVALID_HEADER_DATA
Header data is invalid.
Definition CCSDSResult.h:25
@ CONFIG_FILE_ERROR
Configuration file error.
Definition CCSDSResult.h:34
@ TEMPLATE_SET_FAILURE
Failed to set Template Packet.
Definition CCSDSResult.h:31
@ VALIDATION_FAILURE
Validation Failure.
Definition CCSDSResult.h:30
@ FILE_WRITE_ERROR
Writing to file failure.
Definition CCSDSResult.h:33
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.
Definition CCSDSResult.h:26
@ NULL_POINTER
Null pointer encountered.
Definition CCSDSResult.h:28