FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1#pragma once
2// Strategy registry (8.6, ADR-0012): name -> parameter schema + one factory per transport kind,
3// each producing an IEngineRunner. Strategies are added by a registration function
4// (strategies/module.hpp):
5//
6// void mm::register_strategies(fastmm::StrategyRegistry& r) {
7// fastmm::register_strategy<mm::MicropriceMM>(r); // Sim + Replay + Live factories
8// }
9//
10// Nothing registers itself: apps pass their registration functions to fastmm::cli::live /
11// backtest / replay, and the built-in strategies come from fastmm::register_builtin_strategies().
12// This header does not parse the Engine template; make_engine_runner<S>() lives in
13// strategies/engine_factory.hpp.
15#include "fastmm/core/engine_runner.hpp"
17#include "fastmm/core/msg_ring.hpp"
21
22#include <array>
23#include <cstddef>
24#include <cstdint>
25#include <memory>
26#include <string_view>
27#include <vector>
28
29namespace fastmm {
30
31enum class TransportKind : std::uint8_t { Sim = 0, Live = 1, Replay = 2, Count = 3 };
32[[nodiscard]] constexpr std::string_view to_string(TransportKind k) noexcept {
33 switch (k) {
35 return "sim";
37 return "live";
39 return "replay";
41 return "count";
42 }
43 return "?";
44}
45
46// Everything a factory needs that is backend-independent. `backend` points at the bundle the
47// runtime provides for the requested kind (sim::SimBackend, sim::ReplayBackend, live::LiveBackend)
48// holding clock, transport and feed.
49struct RunnerDeps {
50 EngineConfig engine;
51 const InstrumentTable* instruments = nullptr;
53 MsgRing* journal_ring = nullptr;
54 // Where the engine hands fills, orders, positions and kill events for a storage backend
55 // (core/record_stream.hpp); null disables them at no cost.
56 MsgRing* record_ring = nullptr;
57 void* backend = nullptr;
58};
60inline constexpr std::size_t kTransportKinds = static_cast<std::size_t>(TransportKind::Count);
61
63 using Factory = std::unique_ptr<IEngineRunner> (*)(TransportKind, RunnerDeps&);
64 // Builds a publisher for this strategy's parameters over `sink`, its copy seeded with the
65 // values the session configured the strategy with, so fastmm-live can accept parameter changes
66 // while it runs (src/live/control_socket.cpp). With ParamScope::PerInstrument it keeps one copy
67 // for each of `instruments` and takes updates for one instrument. Null when the parameters are
68 // not a FASTMM_PARAMS struct (a hot strategy builds its schema at run time and brings its own
69 // publisher). Throws std::invalid_argument on a value the schema refuses (startup only).
70 using PublisherFactory = std::unique_ptr<ParamPublisher> (*)(ParamSink,
71 const ParamMap&,
72 std::size_t instruments);
73 std::string_view name; // must outlive the registry (S::name() returns a literal)
74 const ParamSchema* schema = nullptr;
75 std::array<Factory, kTransportKinds> factories{}; // indexed by TransportKind
78 [[nodiscard]] bool supports(TransportKind k) const noexcept {
79 const auto i = static_cast<std::size_t>(k);
80 return i < kTransportKinds && factories[i] != nullptr;
81 }
82};
83
84enum class AddResult : std::uint8_t {
85 Added, // the factory is now registered for (name, kind)
86 AlreadyPresent, // the same schema and factory were registered before: nothing changed
87 Conflict, // `name` is registered with a different schema, or (name, kind) with another factory
88 Invalid, // empty name, null schema or factory, or an invalid kind
89};
90[[nodiscard]] constexpr std::string_view to_string(AddResult r) noexcept {
91 switch (r) {
93 return "added";
95 return "already present";
97 return "conflict";
99 return "invalid";
100 }
101 return "?";
102}
103
104class StrategyRegistry {
105 public:
106 // The registry the CLIs, run_backtest(cfg, name), replay_journal and Python use. Not
107 // thread-safe for writers: register before starting threads. Tests use a local registry.
108 static StrategyRegistry& instance() {
109 static StrategyRegistry r;
110 return r;
111 }
112
113 // Registers `factory` for one transport kind of `name`. Registering the same schema and factory
114 // again does nothing (AlreadyPresent); anything that would replace or disagree with an existing
115 // registration changes nothing and returns Conflict.
116 AddResult try_add(std::string_view name,
117 const ParamSchema* schema,
120 StrategyEntry::PublisherFactory publisher = nullptr) {
121 const auto k = static_cast<std::size_t>(kind);
122 if (name.empty() || factory == nullptr || k >= kTransportKinds || schema == nullptr)
123 return AddResult::Invalid;
124 for (StrategyEntry& e : entries_) {
125 if (e.name != name) continue;
126 if (e.schema != schema) return AddResult::Conflict;
127 if (e.publisher == nullptr) e.publisher = publisher;
128 if (e.factories[k] == factory) return AddResult::AlreadyPresent;
129 if (e.factories[k] != nullptr) return AddResult::Conflict;
130 e.factories[k] = factory;
131 return AddResult::Added;
132 }
133 StrategyEntry e{name, schema, {}, publisher};
134 e.factories[k] = factory;
135 entries_.push_back(e);
136 return AddResult::Added;
137 }
138 [[nodiscard]] const StrategyEntry* find(std::string_view name) const noexcept {
139 for (const StrategyEntry& e : entries_) {
140 if (e.name == name) return &e;
141 }
142 return nullptr;
143 }
144 [[nodiscard]] const std::vector<StrategyEntry>& entries() const noexcept { return entries_; }
145 // Builds a runner; null when `name` or its factory for `kind` is missing, or the factory refuses
146 // (wrong backend). Throws std::invalid_argument on bad parameters (startup only).
147 [[nodiscard]] std::unique_ptr<IEngineRunner> make(std::string_view name,
148 TransportKind kind,
149 RunnerDeps& deps) const {
150 const StrategyEntry* e = find(name);
151 if (e == nullptr || !e->supports(kind)) return nullptr;
152 return e->factories[static_cast<std::size_t>(kind)](kind, deps);
153 }
154
155 private:
156 std::vector<StrategyEntry> entries_;
157};
158
159[[nodiscard]] inline const std::vector<StrategyEntry>& list_strategies() {
162
163} // namespace fastmm
Dense-by-id table built at startup; symbol lookup is a sorted FlatMap keyed by (venue,...
Definition instrument.hpp:147
Definition params.hpp:102
Definition registry.hpp:106
const std::vector< StrategyEntry > & entries() const noexcept
Definition registry.hpp:146
AddResult try_add(std::string_view name, const ParamSchema *schema, TransportKind kind, StrategyEntry::Factory factory, StrategyEntry::PublisherFactory publisher=nullptr)
Registers factory for one transport kind of name.
Definition registry.hpp:118
const StrategyEntry * find(std::string_view name) const noexcept
Definition registry.hpp:140
std::unique_ptr< IEngineRunner > make(std::string_view name, TransportKind kind, RunnerDeps &deps) const
Builds a runner; null when name or its factory for kind is missing, or the factory refuses (wrong bac...
Definition registry.hpp:149
static StrategyRegistry & instance()
The registry the CLIs, run_backtest(cfg, name), replay_journal and Python use.
Definition registry.hpp:110
EngineConfig: the per-session settings an Engine is constructed with.
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Definition strategy.hpp:23
constexpr std::size_t kTransportKinds
Definition registry.hpp:62
const std::vector< StrategyEntry > & list_strategies()
Definition registry.hpp:161
TransportKind
Definition registry.hpp:33
constexpr std::string_view to_string(ParamType t) noexcept
Definition params.hpp:63
AddResult
Definition registry.hpp:86
@ Invalid
empty name, null schema or factory, or an invalid kind
@ AlreadyPresent
the same schema and factory were registered before: nothing changed
@ Conflict
name is registered with a different schema, or (name, kind) with another factory
@ Added
the factory is now registered for (name, kind)
std::map< std::string, std::string > ParamMap
Definition params.hpp:119
ParamPublisher: builds ParamUpdate messages for a running engine, off the engine thread (ADR-0013).
Strategy parameter schema (8.6, ADR-0012).
Strategy concept and the StrategyBase helper (8.6).
Where a built update goes; push returns false when it was not accepted (full).
Definition param_publisher.hpp:49
Everything a factory needs that is backend-independent.
Definition registry.hpp:51
MsgRing * journal_ring
Definition registry.hpp:55
void * backend
Definition registry.hpp:59
const InstrumentTable * instruments
Definition registry.hpp:53
ParamMap params
Definition registry.hpp:54
MsgRing * record_ring
Where the engine hands fills, orders, positions and kill events for a storage backend (core/record_st...
Definition registry.hpp:58
EngineConfig engine
Definition registry.hpp:52
Definition registry.hpp:64
std::unique_ptr< ParamPublisher >(*)(ParamSink, const ParamMap &, std::size_t instruments) PublisherFactory
Builds a publisher for this strategy's parameters over sink, its copy seeded with the values the sess...
Definition registry.hpp:74
std::unique_ptr< IEngineRunner >(*)(TransportKind, RunnerDeps &) Factory
Definition registry.hpp:65
const ParamSchema * schema
Definition registry.hpp:76
bool supports(TransportKind k) const noexcept
Definition registry.hpp:80
std::array< Factory, kTransportKinds > factories
indexed by TransportKind
Definition registry.hpp:77
PublisherFactory publisher
Definition registry.hpp:78
std::string_view name
must outlive the registry (S::name() returns a literal)
Definition registry.hpp:75