Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
replay_gate.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5#include <functional>
6#include <memory>
7#include <utility>
8
9namespace slick::sim::md_feed {
10
22public:
26 explicit ReplayGate(std::function<uint64_t()> position = {}) : position_(std::move(position)) {}
27
30 void open() noexcept {
31 if (open_.load(std::memory_order_relaxed)) {
32 return;
33 }
34 open_index_ = position_ ? position_() : 0;
35 open_.store(true, std::memory_order_release);
36 }
37
39 [[nodiscard]] bool is_open_at(uint64_t index) const noexcept {
40 return open_.load(std::memory_order_acquire) && index >= open_index_;
41 }
42
43 [[nodiscard]] bool is_open() const noexcept { return open_.load(std::memory_order_acquire); }
44
45private:
46 std::function<uint64_t()> position_;
47 uint64_t open_index_ = 0; // Written before open_ is released.
48 std::atomic<bool> open_{false};
49};
50
54public:
56 void set(std::shared_ptr<ReplayGate> gate) {
57 gate_ = std::move(gate);
58 opened_ = false;
59 }
60 [[nodiscard]] bool gated() const noexcept { return gate_ != nullptr; }
62 [[nodiscard]] bool publishing() const noexcept { return !gate_ || opened_; }
63
65 void set_position(uint64_t index) noexcept { position_ = index; }
66
69 [[nodiscard]] bool opened_now() noexcept {
70 if (!gate_ || opened_ || !gate_->is_open_at(position_)) {
71 return false;
72 }
73 opened_ = true;
74 return true;
75 }
76
77private:
78 std::shared_ptr<ReplayGate> gate_;
79 bool opened_ = false;
80 uint64_t position_ = 0;
81};
82
83} // namespace slick::sim::md_feed
A publisher channel's view of the gate: whether it may publish yet, for the md_queue frame it is work...
bool publishing() const noexcept
False while a gate is closed (or not yet open for the frame being read).
bool opened_now() noexcept
True once, when the gate has opened for the frames now being read: the moment the channel starts publ...
void set(std::shared_ptr< ReplayGate > gate)
Call before the channel is used. A channel with a gate publishes nothing until it opens.
void set_position(uint64_t index) noexcept
The md_queue index of the next frame the publisher thread will read.
void open() noexcept
Exchange thread, after its last write to md_queue for the held book.
bool is_open() const noexcept
bool is_open_at(uint64_t index) const noexcept
True once open, for a publisher that is about to read the md_queue frame at index.
ReplayGate(std::function< uint64_t()> position={})
position returns the md_queue's write index (see slick::queue::initial_reading_index),...