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// Copyright 2025-2026 ExoSpaceLabs
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef CCSDS_RESULT_H
5#define CCSDS_RESULT_H
6
7#include <variant>
8#include <vector>
9#include <cstdint>
10
11//exclude includes when building for MCU
12#ifndef CCSDS_MCU
13 #include <iostream>
14#else
15 #include <string>
16#endif //CCSDS_MCU
17
18namespace CCSDS {
39
47 class Error {
48 public:
54 Error(const ErrorCode code, std::string message)
55 : m_code(code), m_message(std::move(message)) {
56 }
57
62 [[nodiscard]] ErrorCode code() const { return m_code; }
63
68 [[nodiscard]] const std::string &message() const { return m_message; }
69
70 private:
72 std::string m_message;
73 };
74
84 template<typename T>
85 class Result {
86 std::variant<T, Error> data;
87
88 public:
93 Result(T value) : data(std::move(value)) {
94 }
95
102
107 [[nodiscard]] bool has_value() const {
108 return std::holds_alternative<T>(data);
109 }
110
116 T &value() { return std::get<T>(data); }
117 const T &value() const { return std::get<T>(data); }
118
123 [[nodiscard]] Error error() const {
124 return std::get<Error>(data);
125 }
126
131 explicit operator bool() const { return has_value(); }
132 };
133
134 // Convenient type aliases for common result types
137}
138
139/* ERROR MANAGEMENT MACROS */
140
145#define RETURN_IF_ERROR(condition, errorCode) \
146do { if (condition) return errorCode; } while (0)
147
152#define RET_IF_ERR_MSG(condition, errorCode, message) \
153do { \
154 if (condition) { \
155 return CCSDS::Error{errorCode,message}; \
156 } \
157} while (0)
158
163#define ASSIGN_MV(var, result) \
164do { \
165 auto&& _res = (result); \
166 if (!_res) return _res.error(); \
167 var = std::move(_res.value()); \
168} while (0)
169
174#define ASSIGN_CP(var, result) \
175do { \
176auto&& _res = (result); \
177if (!_res) return _res.error(); \
178var = _res.value(); \
179} while (0)
180
181
186#define ASSIGN_OR_PRINT(var, result) \
187do { \
188 auto&& _res = (result); \
189 if (!_res) { \
190 printf("[ Error ]: Code [%u]: %s\n", static_cast<unsigned>(_res.error().code()), _res.error().message().c_str());; \
191 } else { \
192 var = std::move(_res.value()); \
193 } \
194} while (0)
195
200#define ASSERT_SUCCESS(result) \
201do { \
202 auto&& _res = (result); \
203 if (!_res.has_value()) return; \
204} while (0)
205
210#define FORWARD_RESULT(result) \
211do { \
212 auto&& _res = (result); \
213 if (!_res.has_value()) return _res; \
214} while (0)
215
216#endif // CCSDS_RESULT_H
Represents an error with both an error code and a message.
Definition CCSDSResult.h:47
ErrorCode code() const
Retrieves the error code.
Definition CCSDSResult.h:62
ErrorCode m_code
The error type.
Definition CCSDSResult.h:71
Error(const ErrorCode code, std::string message)
Constructs an error with a given error code and message.
Definition CCSDSResult.h:54
const std::string & message() const
Retrieves the error message.
Definition CCSDSResult.h:68
std::string m_message
A detailed message describing the error.
Definition CCSDSResult.h:72
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:85
bool has_value() const
Checks if the result contains a valid value.
Result(Error error)
Constructor for failure case.
Result(T value)
Constructor for success case.
Definition CCSDSResult.h:93
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:86
Contains definitions and classes for handling CCSDS headers.
ErrorCode
Defines various error codes used in CCSDS packet handling.
Definition CCSDSResult.h:23
@ NO_DATA
No data available.
Definition CCSDSResult.h:26
@ NONE
No error.
Definition CCSDSResult.h:24
@ FILE_READ_ERROR
Reading from file failure.
Definition CCSDSResult.h:35
@ UNKNOWN_ERROR
Unknown error.
Definition CCSDSResult.h:25
@ INVALID_CHECKSUM
Checksum validation failed.
Definition CCSDSResult.h:32
@ INVALID_APPLICATION_DATA
Application data is invalid.
Definition CCSDSResult.h:30
@ INVALID_DATA
Data is invalid.
Definition CCSDSResult.h:27
@ INVALID_HEADER_DATA
Header data is invalid.
Definition CCSDSResult.h:28
@ CONFIG_FILE_ERROR
Configuration file error.
Definition CCSDSResult.h:37
@ TEMPLATE_SET_FAILURE
Failed to set Template Packet.
Definition CCSDSResult.h:34
@ VALIDATION_FAILURE
Validation Failure.
Definition CCSDSResult.h:33
@ FILE_WRITE_ERROR
Writing to file failure.
Definition CCSDSResult.h:36
@ INVALID_SECONDARY_HEADER_DATA
Secondary header data is invalid.
Definition CCSDSResult.h:29
@ NULL_POINTER
Null pointer encountered.
Definition CCSDSResult.h:31