lintel 0.2.0
Loading...
Searching...
No Matches
Message.h
Go to the documentation of this file.
1#ifndef LINTEL_MESSAGE_H
2#define LINTEL_MESSAGE_H
3
4#include <lintel/config.h>
5
6#include <chrono>
7#include <string>
8
9namespace lintel {
14{
15private:
16 char m_receiver[MSG_QUEUE_NAME_SIZE]{};
17 char m_sender[MSG_QUEUE_NAME_SIZE]{};
18 char m_content[MSG_QUEUE_CONTENT_SIZE]{};
19 time_t m_sendTime{};
20
21public:
27 Message(const std::string &receiver, const std::string &sender);
28
32 Message() = default;
33
37 [[nodiscard]] const char *receiverOf() const;
38
42 [[nodiscard]] const char *senderOf() const;
43
47 [[nodiscard]] std::chrono::time_point<std::chrono::system_clock> sendTimeOf() const;
48
58 template<typename T> void assign(T content)
59 {
60 static_assert(sizeof(T) <= sizeof(m_content), "content is bigger than reserved size for content");
61 char *temp = static_cast<char *>(static_cast<void *>(&content));
62 /*
63 * We'll cast an object to a char array. We have to copy it manually
64 * and not with std::strcpy or std::strncpy bc. those function will stop
65 * copying by the first 0 terminator.
66 */
67 for (int i = 0; i < sizeof(T); i++) { m_content[i] = temp[i]; }
68 }
69
76 template<typename T> T as()
77 {
78 static_assert(sizeof(T) <= sizeof(m_content), "content is bigger than reserved size for content");
79 return *static_cast<T *>(static_cast<void *>(m_content));
80 }
81};
82
83}// namespace lintel
84#endif// LINTEL_MESSAGE_H
Definition Message.h:14
T as()
Definition Message.h:76
Message()=default
void assign(T content)
Definition Message.h:58
const char * senderOf() const
std::chrono::time_point< std::chrono::system_clock > sendTimeOf() const
const char * receiverOf() const
Message(const std::string &receiver, const std::string &sender)
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9