CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
CCSDSConfig.h
Go to the documentation of this file.
1// Copyright 2025-2026 ExoSpaceLabs
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef CCSDS_CONFIG_H
5#define CCSDS_CONFIG_H
6
7#include <string>
8#include <cstdint>
9#include <vector>
10#include "CCSDSResult.h"
11#include <unordered_map>
12
14class Config {
15public:
16 using ConfigValue = std::variant<std::string, int, float, bool, std::vector<uint8_t>>;
17
19 CCSDS::ResultBool load(const std::string &filename);
20
22 template<typename T>
23 CCSDS::Result<T> get(const std::string& key) const {
24 auto it = values.find(key);
25 RET_IF_ERR_MSG(it == values.end(), CCSDS::ErrorCode::NO_DATA,"Config: No data found for key: " + key);
26
27 if (const auto* p = std::get_if<T>(&it->second)) {
28 return *p;
29 }
30 return CCSDS::Error{ CCSDS::ErrorCode::INVALID_DATA, "Config: Wrong type for key: " + key};
31 }
32
33 bool isKey(const std::string& key) const;
34
35private:
36 std::unordered_map<std::string, ConfigValue> values;
37
39 static std::tuple<std::string, std::string, std::string> parseLine(const std::string& line);
40
42 static CCSDS::ResultBuffer parseBytes(const std::string &valueStr);
43};
44
45
46
47
48#endif // CCSDS_CONFIG_H
#define RET_IF_ERR_MSG(condition, errorCode, message)
Macro to return an error with an error message if a condition is met.
Represents an error with both an error code and a message.
Definition CCSDSResult.h:47
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:85
Parses and stores config values from custom file format.
Definition CCSDSConfig.h:14
std::variant< std::string, int, float, bool, std::vector< uint8_t > > ConfigValue
Definition CCSDSConfig.h:16
bool isKey(const std::string &key) const
static CCSDS::ResultBuffer parseBytes(const std::string &valueStr)
Parse string "[1,2,3]" into vector<uint8_t>
std::unordered_map< std::string, ConfigValue > values
Definition CCSDSConfig.h:36
CCSDS::ResultBool load(const std::string &filename)
Load config file.
CCSDS::Result< T > get(const std::string &key) const
Get value by key and type.
Definition CCSDSConfig.h:23
static std::tuple< std::string, std::string, std::string > parseLine(const std::string &line)
Parse a single line from config.
@ NO_DATA
No data available.
Definition CCSDSResult.h:26
@ INVALID_DATA
Data is invalid.
Definition CCSDSResult.h:27