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#ifndef CCSDS_CONFIG_H
2#define CCSDS_CONFIG_H
3
4#include <string>
5#include <cstdint>
6#include <vector>
7#include "CCSDSResult.h"
8#include <unordered_map>
9
11class Config {
12public:
13 using ConfigValue = std::variant<std::string, int, float, bool, std::vector<uint8_t>>;
14
16 CCSDS::ResultBool load(const std::string &filename);
17
19 template<typename T>
20 CCSDS::Result<T> get(const std::string& key) const {
21 auto it = values.find(key);
22 RET_IF_ERR_MSG(it == values.end(), CCSDS::ErrorCode::NO_DATA,"Config: No data found for key: " + key);
23
24 if (const auto* p = std::get_if<T>(&it->second)) {
25 return *p;
26 }
27 return CCSDS::Error{ CCSDS::ErrorCode::INVALID_DATA, "Config: Wrong type for key: " + key};
28 }
29
30 bool isKey(const std::string& key) const;
31
32private:
33 std::unordered_map<std::string, ConfigValue> values;
34
36 static std::tuple<std::string, std::string, std::string> parseLine(const std::string& line);
37
39 static CCSDS::ResultBuffer parseBytes(const std::string &valueStr);
40};
41
42
43
44
45#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:44
Encapsulates a result that can hold either a value or an Error.
Definition CCSDSResult.h:82
Parses and stores config values from custom file format.
Definition CCSDSConfig.h:11
std::variant< std::string, int, float, bool, std::vector< uint8_t > > ConfigValue
Definition CCSDSConfig.h:13
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:33
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:20
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:23
@ INVALID_DATA
Data is invalid.
Definition CCSDSResult.h:24