FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
module.hpp
Go to the documentation of this file.
1#pragma once
2// Strategy modules (ADR-0012, section 5): one registration function per strategy library.
3//
4// // mm/strategies.hpp
5// namespace mm { void register_strategies(fastmm::StrategyRegistry& r); }
6//
7// // strategies.cpp: the only registration file a strategy library needs
8// #include "fastmm/strategies/factories.hpp" // factory definitions: instantiates the engines
9// #include "mm/microprice_mm.hpp"
10// #include "mm/strategies.hpp"
11// void mm::register_strategies(fastmm::StrategyRegistry& r) {
12// fastmm::register_strategy<mm::MicropriceMM>(r); // Sim + Replay + Live
13// }
14//
15// // apps: int main(int argc, char** argv) { return fastmm::cli::live(argc, argv,
16// {mm::register_strategies}); }
17//
18// This header only declares the factories; it never instantiates Engine. Their definitions are in
19// factory_sim.hpp, factory_replay.hpp and factory_live.hpp (factories.hpp includes all three). A
20// file that calls register_strategy<S> without them only references the factories, so a missing
21// instantiation is a link error naming the factory, not an "unknown strategy" at runtime. The app
22// references the registration function, so the linker pulls it and the instantiations out of static
23// archives: no static initialisers and no whole-archive linking.
24//
25// Only a strategy's owner instantiates it (two libraries instantiating the same factory break the
26// one-definition rule); to use someone else's strategy, call their registration function.
29
30#include <cstddef>
31#include <cstdint>
32#include <memory>
33#include <stdexcept>
34#include <string>
35
36namespace fastmm {
37
38// A strategy library's registration function.
39using StrategyModule = void (*)(StrategyRegistry&);
40
41// Thrown by register_strategy when a name is already registered with a different schema or factory.
42class StrategyConflict : public std::logic_error {
43 public:
44 using std::logic_error::logic_error;
45};
46
47// Declared `template <class S>`, not `template <StrategyLike S>`: GCC and Clang mangle constrained
48// templates differently. The definitions check StrategyLike<S> with a static_assert.
49template <class S>
50std::unique_ptr<IEngineRunner> sim_factory(TransportKind kind, RunnerDeps& deps);
51template <class S>
52std::unique_ptr<IEngineRunner> replay_factory(TransportKind kind, RunnerDeps& deps);
53template <class S>
54std::unique_ptr<IEngineRunner> live_factory(TransportKind kind, RunnerDeps& deps);
55
56// Internal: the factories register_strategy adds. FastMM's tests use it; a strategy that should not
57// run live is simply not registered by that project's live app. The set is a template argument so
58// that the factories it leaves out are never referenced, at any optimisation level.
59enum class Transports : std::uint8_t { None = 0, Sim = 1, Replay = 2, Live = 4, All = 7 };
60[[nodiscard]] constexpr Transports operator|(Transports a, Transports b) noexcept {
61 return static_cast<Transports>(static_cast<std::uint8_t>(a) | static_cast<std::uint8_t>(b));
63[[nodiscard]] constexpr bool has(Transports set, Transports t) noexcept {
64 return (static_cast<std::uint8_t>(set) & static_cast<std::uint8_t>(t)) != 0;
66
67namespace detail {
68// The entry's publisher factory for a strategy whose parameters are a FASTMM_PARAMS struct; null
69// for one whose are not (it cannot be copied and validated generically).
70template <class S>
71constexpr StrategyEntry::PublisherFactory publisher_factory() {
72 if constexpr (requires { typename S::params_type; }) {
73 return [](ParamSink sink, const ParamMap& params, std::size_t instruments) {
74 typename S::params_type p{};
75 if (auto err = p.apply(params)) throw std::invalid_argument(*err);
76 if (auto err = validate_params(p)) throw std::invalid_argument(*err);
77 bool per_instrument = false;
78 if constexpr (requires { S::param_scope; })
79 per_instrument = S::param_scope == ParamScope::PerInstrument;
80 return std::make_unique<ParamPublisher>(sink, p, per_instrument ? instruments : 0);
81 };
82 } else {
83 return nullptr;
84 }
85}
86} // namespace detail
87
88// Adds the Sim, Replay and Live factories of S (or those in `Enabled`). Registering the same
89// strategy again does nothing; throws StrategyConflict when another strategy already uses the name.
90template <class S, Transports Enabled = Transports::All>
91void register_strategy(StrategyRegistry& r) {
92 static_assert(StrategyLike<S>,
93 "fastmm: register_strategy<S> needs a strategy: static name(), static schema() "
94 "and a default constructor");
95 const auto add = [&](TransportKind kind, StrategyEntry::Factory factory) {
96 switch (r.try_add(S::name(), &S::schema(), kind, factory, detail::publisher_factory<S>())) {
99 return;
101 throw StrategyConflict("strategy '" + std::string(S::name()) + "' (" +
102 std::string(to_string(kind)) +
103 ") is already registered by different code; strategy names must be "
104 "unique across every registered library");
106 break;
107 }
108 throw std::invalid_argument("strategy '" + std::string(S::name()) +
109 "' cannot be registered: empty name or no parameter schema");
110 };
111 // if constexpr: a discarded branch is not instantiated, so it does not reference its factory.
112 if constexpr (has(Enabled, Transports::Sim)) add(TransportKind::Sim, &sim_factory<S>);
113 if constexpr (has(Enabled, Transports::Replay)) add(TransportKind::Replay, &replay_factory<S>);
114 if constexpr (has(Enabled, Transports::Live)) add(TransportKind::Live, &live_factory<S>);
115}
116
117} // namespace fastmm
118
119// Explicit instantiation of one factory, for splitting a strategy's engines over several files:
120//
121// // microprice_mm_live.cpp
122// #include "fastmm/strategies/factory_live.hpp"
123// #include "mm/microprice_mm.hpp"
124// FASTMM_INSTANTIATE_STRATEGY(mm::MicropriceMM, live);
125//
126// `kind` is sim, replay or live; the matching factory_<kind>.hpp must be included.
127// (The trailing return type keeps `unique_ptr<...> ::fastmm::...` from parsing as a nested name.)
128#define FASTMM_INSTANTIATE_STRATEGY(S, kind) \
129 template auto ::fastmm::kind##_factory<S>(::fastmm::TransportKind, ::fastmm::RunnerDeps&) \
130 ->std::unique_ptr<::fastmm::IEngineRunner>
Thrown by register_strategy when a name is already registered with a different schema or factory.
Definition module.hpp:44
Definition strategy.hpp:23
void(*)(StrategyRegistry &) StrategyModule
A strategy library's registration function.
Definition module.hpp:41
std::unique_ptr< IEngineRunner > replay_factory(TransportKind kind, RunnerDeps &deps)
Definition factory_replay.hpp:15
constexpr bool has(Transports set, Transports t) noexcept
Definition module.hpp:65
constexpr Transports operator|(Transports a, Transports b) noexcept
Definition module.hpp:62
Transports
Internal: the factories register_strategy adds.
Definition module.hpp:61
TransportKind
Definition registry.hpp:33
std::unique_ptr< IEngineRunner > sim_factory(TransportKind kind, RunnerDeps &deps)
Declared template <class S>, not template <StrategyLike S>: GCC and Clang mangle constrained template...
Definition factory_sim.hpp:15
constexpr std::string_view to_string(ParamType t) noexcept
Definition params.hpp:63
@ 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)
void register_strategy(StrategyRegistry &r)
Adds the Sim, Replay and Live factories of S (or those in Enabled).
Definition module.hpp:93
std::unique_ptr< IEngineRunner > live_factory(TransportKind kind, RunnerDeps &deps)
Definition factory_live.hpp:15
std::map< std::string, std::string > ParamMap
Definition params.hpp:119
Strategy registry (8.6, ADR-0012): name -> parameter schema + one factory per transport kind,...
Strategy concept and the StrategyBase helper (8.6).
Everything a factory needs that is backend-independent.
Definition registry.hpp:51
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