CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
CCSDSSecondaryHeaderFactory.h
Go to the documentation of this file.
1#ifndef CCSDS_SECONDARY_HEADER_FACTORY_H
2#define CCSDS_SECONDARY_HEADER_FACTORY_H
3
4#include <memory>
5#include <unordered_map>
6#include <functional>
7#include <string>
8#include "CCSDSSecondaryHeaderAbstract.h" // Base class header
9
10namespace CCSDS {
11
19public:
24
29 using CreatorFunc = std::function<std::shared_ptr<SecondaryHeaderAbstract>()>;
30
39 ResultBool registerType(std::shared_ptr<SecondaryHeaderAbstract> header) {
40 RET_IF_ERR_MSG(!header, INVALID_HEADER_DATA, "Cannot register, invalid Header provided.");
41 m_creators[header->getType()] = std::move(header);
42 return true;
43 }
44
53 std::shared_ptr<SecondaryHeaderAbstract> create(const std::string& type) {
54 if (const auto it = m_creators.find(type); it != m_creators.end()) {
55 return it->second; // Call the stored creation function
56 }
57 return nullptr; // Return nullptr if type not found
58 }
59
68 bool typeIsRegistered(const std::string& type) {
69 if (const auto it = m_creators.find(type); it != m_creators.end()) {
70 return true; // return true if found
71 }
72 return false; // Return false if type not found
73 }
74
75private:
81 std::unordered_map<std::string, std::shared_ptr<SecondaryHeaderAbstract> > m_creators;
82};
83
84} // namespace CCSDS
85
86#endif // CCSDS_SECONDARY_HEADER_FACTORY_H
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:82
A singleton factory class responsible for registering and creating instances of SecondaryHeaderAbstra...
std::function< std::shared_ptr< SecondaryHeaderAbstract >()> CreatorFunc
Type alias for a function that creates an instance of SecondaryHeaderAbstract.
ResultBool registerType(std::shared_ptr< SecondaryHeaderAbstract > header)
Registers a new header type with its creation function.
std::shared_ptr< SecondaryHeaderAbstract > create(const std::string &type)
Creates an instance of a registered header type.
std::unordered_map< std::string, std::shared_ptr< SecondaryHeaderAbstract > > m_creators
A map of header types to their corresponding SecondaryHeaderAbstract objects.
bool typeIsRegistered(const std::string &type)
Checks if a header type is registered.
SecondaryHeaderFactory()=default
Default constructor for the factory.
Contains definitions and classes for handling CCSDS headers.
@ INVALID_HEADER_DATA
Header data is invalid.
Definition CCSDSResult.h:25