Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
event_serializer.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <slick/queue.hpp>
9#include <slick/stream_buffer_multiplexer.hpp>
10#include <cstring>
11#include <type_traits>
12
13namespace slick::trader {
14
15using producer_buffer = slick::stream_buffer_multiplexer::producer_buffer;
16
17// Event serialization utilities for SlickQueue<uint8_t>
19public:
20 // Publish event to queue
21 template<typename T, typename populate_event_fn>
22 static bool publish(slick::SlickQueue<uint8_t>& queue, uint32_t size, populate_event_fn &&populate_event_func) {
23 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
24 auto index = queue.reserve(size);
25 auto *e = new (queue[index]) T(); // Placement new to construct event in-place
26 populate_event_func(*e);
27 queue.publish(index, size);
28 return true;
29 }
30
31 template<typename T, typename populate_event_fn>
32 static bool publish(slick::SlickQueue<uint8_t>& queue, populate_event_fn &&populate_event_func) {
33 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
34 auto index = queue.reserve(sizeof(T));
35 auto *e = new (queue[index]) T(); // Placement new to construct event in-place
36 populate_event_func(*e);
37 queue.publish(index, sizeof(T));
38 return true;
39 }
40
41 // Publish event to queue
42 template<typename T>
43 static bool publish(slick::SlickQueue<uint8_t>& queue, const T& event) {
44 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
45 auto index = queue.reserve(sizeof(T));
46 auto *e = reinterpret_cast<T*>(queue[index]);
47 *e = event;
48 queue.publish(index, sizeof(T));
49 return true;
50 }
51
52 // Consume event from queue
53 template<typename T>
54 static T* consume(slick::SlickQueue<uint8_t>& queue, uint64_t& cursor) {
55 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
56 auto [data, size] = queue.read(cursor);
57 if (data && size >= sizeof(T)) {
58 return reinterpret_cast<T*>(data);
59 }
60 return nullptr;
61 }
62
63 // Publish event to producer_buffer
64 template<typename T, typename populate_event_fn>
65 static bool publish(producer_buffer& buffer, uint32_t size, populate_event_fn &&populate_event_func) {
66 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
67 auto [ptr, sz] = buffer.prepare(size);
68 auto *e = new (ptr) T(); // Placement new to construct event in-place
69 populate_event_func(*e);
70 buffer.commit(size);
71 buffer.consume(size);
72 return true;
73 }
74
75 template<typename T, typename populate_event_fn>
76 static bool publish(producer_buffer& buffer, populate_event_fn &&populate_event_func) {
77 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
78 auto [ptr, sz] = buffer.prepare(sizeof(T));
79 auto *e = new (ptr) T(); // Placement new to construct event in-place
80 populate_event_func(*e);
81 buffer.commit(sizeof(T));
82 buffer.consume(sizeof(T));
83 return true;
84 }
85
86 // Publish event to queue
87 template<typename T>
88 static bool publish(producer_buffer& buffer, const T& event) {
89 static_assert(std::is_trivially_copyable_v<T>, "Event must be trivially copyable");
90 auto [ptr, sz] = buffer.prepare(sizeof(T));
91 auto *e = reinterpret_cast<T*>(ptr);
92 *e = event;
93 buffer.commit(sizeof(T));
94 buffer.consume(sizeof(T));
95 return true;
96 }
97};
98
99} // namespace slick::trader
static bool publish(producer_buffer &buffer, uint32_t size, populate_event_fn &&populate_event_func)
static bool publish(slick::SlickQueue< uint8_t > &queue, uint32_t size, populate_event_fn &&populate_event_func)
static bool publish(slick::SlickQueue< uint8_t > &queue, const T &event)
static bool publish(slick::SlickQueue< uint8_t > &queue, populate_event_fn &&populate_event_func)
static T * consume(slick::SlickQueue< uint8_t > &queue, uint64_t &cursor)
static bool publish(producer_buffer &buffer, const T &event)
static bool publish(producer_buffer &buffer, populate_event_fn &&populate_event_func)
slick::stream_buffer_multiplexer::producer_buffer producer_buffer