lintel 0.2.0
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
1#ifndef LINTEL_EVENT_H
2#define LINTEL_EVENT_H
3
4#include <chrono>
5#include <cstddef>
6#include <cstdint>
7#include <cstring>
8#include <string>
9#include <type_traits>
10
11namespace lintel {
21class Event
22{
23public:
25 static constexpr std::size_t NAME_SIZE = 32;
27 static constexpr std::size_t CONTENT_SIZE = 256;
28
30 Event() = default;
31
37 Event(const std::string &topic, const std::string &type)
38 {
39 copyInto(m_topic, topic);
40 copyInto(m_type, type);
41 }
42
45 void setTopic(const std::string &topic) { copyInto(m_topic, topic); }
46
49 void setType(const std::string &type) { copyInto(m_type, type); }
50
52 [[nodiscard]] const char *topic() const { return m_topic; }
53
55 [[nodiscard]] const char *type() const { return m_type; }
56
59 void setSequence(std::int64_t sequence) { m_sequence = sequence; }
60
62 [[nodiscard]] std::int64_t sequence() const { return m_sequence; }
63
66 void setTimestamp(std::int64_t timestamp) { m_timestamp = timestamp; }
67
69 [[nodiscard]] std::int64_t timestamp() const { return m_timestamp; }
70
76 void setContent(const std::byte *data, std::size_t size)
77 {
78 const std::size_t length = size > CONTENT_SIZE ? CONTENT_SIZE : size;
79 if (length > 0 && data != nullptr) { std::memcpy(m_content, data, length); }
80 m_contentLength = length;
81 }
82
89 template<typename T> void assign(const T &content)
90 {
91 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable to be stored in an Event");
92 static_assert(sizeof(T) <= CONTENT_SIZE, "content is bigger than reserved size for content");
93 std::memcpy(m_content, &content, sizeof(T));
94 m_contentLength = sizeof(T);
95 }
96
102 template<typename T> [[nodiscard]] T as() const
103 {
104 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable to be read from an Event");
105 static_assert(sizeof(T) <= CONTENT_SIZE, "content is bigger than reserved size for content");
106 T result{};
107 std::memcpy(&result, m_content, sizeof(T));
108 return result;
109 }
110
112 [[nodiscard]] std::size_t contentLength() const { return m_contentLength; }
113
115 [[nodiscard]] const std::byte *content() const { return reinterpret_cast<const std::byte *>(m_content); }
116
118 [[nodiscard]] bool isContentEmpty() const { return m_contentLength == 0; }
119
120private:
121 char m_topic[NAME_SIZE]{};
122 char m_type[NAME_SIZE]{};
123 char m_content[CONTENT_SIZE]{};
124 std::size_t m_contentLength = 0;
125 std::int64_t m_sequence = 0;
126 std::int64_t m_timestamp = 0;
127
128 static constexpr void copyInto(char (&destination)[NAME_SIZE], std::string_view value)
129 {
130 const std::size_t length = value.size() >= NAME_SIZE ? NAME_SIZE - 1 : value.size();
131 std::memcpy(destination, value.data(), length);
132 destination[length] = '\0';
133 }
134};
135
136static_assert(std::is_trivially_copyable_v<Event>, "Event must stay trivially copyable for the lock-free queues");
137
138}// namespace lintel
139#endif// LINTEL_EVENT_H
Definition Event.h:22
std::int64_t sequence() const
Definition Event.h:62
Event(const std::string &topic, const std::string &type)
Definition Event.h:37
Event()=default
const std::byte * content() const
Definition Event.h:115
void setType(const std::string &type)
Definition Event.h:49
void assign(const T &content)
Definition Event.h:89
std::int64_t timestamp() const
Definition Event.h:69
std::size_t contentLength() const
Definition Event.h:112
void setTimestamp(std::int64_t timestamp)
Definition Event.h:66
static constexpr std::size_t CONTENT_SIZE
Definition Event.h:27
const char * topic() const
Definition Event.h:52
void setSequence(std::int64_t sequence)
Definition Event.h:59
static constexpr std::size_t NAME_SIZE
Definition Event.h:25
void setContent(const std::byte *data, std::size_t size)
Definition Event.h:76
const char * type() const
Definition Event.h:55
bool isContentEmpty() const
Definition Event.h:118
void setTopic(const std::string &topic)
Definition Event.h:45
T as() const
Definition Event.h:102
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9