-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.hpp
More file actions
58 lines (50 loc) · 1.63 KB
/
Message.hpp
File metadata and controls
58 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include <map>
#include <string>
/**
* @brief Représente un message échangé via le protocole UDS
*
* Un message est composé d'un type et de champs optionnels key=value
*/
struct Message {
private:
const std::string type; ///< Type du message
const std::map<std::string, std::string> fields; ///< Champs du message
public:
/**
* @brief Constructeur avec type uniquement
* @param messageType Type du message
*/
explicit Message(std::string messageType) : type(std::move(messageType)) {}
/**
* @brief Constructeur avec type et champs
* @param messageType Type du message
* @param messageFields Champs du message
*/
Message(std::string messageType,
std::map<std::string, std::string> messageFields)
: type(std::move(messageType)), fields(std::move(messageFields)) {}
/**
* @brief Récupère valeur d'un champ
* @param key Clé du champ
* @return Valeur du champ ou chaîne vide si inexistant
*/
std::string getField(const std::string& key) const {
auto it = this->fields.find(key);
return (it != this->fields.end()) ? it->second : "";
}
/**
* @brief Vérifie si un champ existe
* @param key Clé du champ
* @return true si le champ existe
*/
bool hasField(const std::string& key) const {
return this->fields.find(key) != this->fields.end();
}
std::string getType() const { return this->type; }
std::map<std::string, std::string> getFields() const {
return this->fields;
}
};
#endif // MESSAGE_HPP