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 <unordered_map>
5#include <variant>
6#include <vector>
7#include <cstdint>
8#include <iostream>
9
14namespace CCSDS {
35
43 class Error {
44 public:
50 Error(const ErrorCode code, std::string message)
51 : m_code(code), m_message(std::move(message)) {
52 }
53
58 [[nodiscard]] ErrorCode code() const { return m_code; }
59
64 [[nodiscard]] const std::string &message() const { return m_message; }
65
66 private:
68 std::string m_message;
69 };
70
80 template<typename T>
81 class Result {
82 std::variant<T, Error> data;
83
84 public:
89 Result(T value) : data(std::move(value)) {
90 }
91
98
103 [[nodiscard]] bool has_value() const {
104 return std::holds_alternative<T>(data);
105 }
106
112 T &value() { return std::get<T>(data); }
113 const T &value() const { return std::get<T>(data); }
114
119 [[nodiscard]] Error error() const {
120 return std::get<Error>(data);
121 }
122
127 explicit operator bool() const { return has_value(); }
128 };
129
130 // Convenient type aliases for common result types
133}
134
135/* ERROR MANAGEMENT MACROS */
136
141#define RETURN_IF_ERROR(condition, errorCode) \
142do { if (condition) return errorCode; } while (0)
143
148#define RET_IF_ERR_MSG(condition, errorCode, message) \
149do { \
150 if (condition) { \
151 return CCSDS::Error{errorCode,message}; \
152 } \
153} while (0)
154
159#define ASSIGN_MV(var, result) \
160do { \
161 auto&& _res = (result); \
162 if (!_res) return _res.error(); \
163 var = std::move(_res.value()); \
164} while (0)
165
170#define ASSIGN_CP(var, result) \
171do { \
172auto&& _res = (result); \
173if (!_res) return _res.error(); \
174var = _res.value(); \
175} while (0)
176
177
182#define ASSIGN_OR_PRINT(var, result) \
183do { \
184 auto&& _res = (result); \
185 if (!_res) { \
186 std::cerr << "[ Error ]: Code [" << _res.error().code() << "]: "<< _res.error().message() << '\n'; \
187 } else { \
188 var = std::move(_res.value()); \
189 } \
190} while (0)
191
196#define ASSERT_SUCCESS(result) \
197do { \
198 auto&& _res = (result); \
199 if (!_res.has_value()) return; \
200} while (0)
201
206#define FORWARD_RESULT(result) \
207do { \
208 auto&& _res = (result); \
209 if (!_res.has_value()) return _res; \
210} while (0)
211
212#endif // CCSDS_RESULT_H
Represents an error with both an error code and a message.
Definition CCSDSResult.h:43
ErrorCode code() const
Retrieves the error code.
Definition CCSDSResult.h:58
ErrorCode m_code
The error type.
Definition CCSDSResult.h:67
Error(const ErrorCode code, std::string message)
Constructs an error with a given error code and message.
Definition CCSDSResult.h:50
const std::string & message() const
Retrieves the error message.
Definition CCSDSResult.h:64
std::string m_message
A detailed message describing the error.
Definition CCSDSResult.h:68
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:81
bool has_value() const
Checks if the result contains a valid value.
Result(Error error)
Constructor for failure case.
Definition CCSDSResult.h:96
Result(T value)
Constructor for success case.
Definition CCSDSResult.h:89
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:82
Contains definitions and classes for handling CCSDS headers.
ErrorCode
Defines various error codes used in CCSDS packet handling.
Definition CCSDSResult.h:19
@ NO_DATA
No data available.
Definition CCSDSResult.h:22
@ NONE
No error.
Definition CCSDSResult.h:20
@ FILE_READ_ERROR
Reading from file failure.
Definition CCSDSResult.h:31
@ UNKNOWN_ERROR
Unknown error.
Definition CCSDSResult.h:21
@ INVALID_CHECKSUM
Checksum validation failed.
Definition CCSDSResult.h:28
@ INVALID_APPLICATION_DATA
Application data is invalid.
Definition CCSDSResult.h:26
@ INVALID_DATA
Data is invalid.
Definition CCSDSResult.h:23
@ INVALID_HEADER_DATA
Header data is invalid.
Definition CCSDSResult.h:24
@ CONFIG_FILE_ERROR
Definition CCSDSResult.h:33
@ SOMETHING_WENT_WRONG
General failure.
Definition CCSDSResult.h:30
@ VALIDATION_FAILURE
Validation Failure.
Definition CCSDSResult.h:29
@ FILE_WRITE_ERROR
Writing to file failure.
Definition CCSDSResult.h:32
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.
Definition CCSDSResult.h:25
@ NULL_POINTER
Null pointer encountered.
Definition CCSDSResult.h:27