FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
strategy.hpp
Go to the documentation of this file.
1#pragma once
2// Strategy concept and the StrategyBase helper (8.6). A strategy is a plain class with
3// static name()/schema(), a params struct declared with FASTMM_PARAMS, and any subset of
4// the engine hooks (all optional; see strategies/hooks.hpp and docs/reference/strategy-api.md).
8#include "fastmm/strategies/param_overrides.hpp"
10
11#include <array>
12#include <concepts>
13#include <cstddef>
14#include <cstdint>
15#include <optional>
16#include <span>
17#include <string>
18#include <string_view>
19#include <type_traits>
20
21namespace fastmm {
22
23template <class S>
24concept StrategyLike = requires {
25 { S::name() } -> std::convertible_to<std::string_view>;
26 { S::schema() } -> std::convertible_to<const ParamSchema&>;
27} && std::is_default_constructible_v<S>;
28
29// How a StrategyBase keeps its parameters. Shared: one set for every instrument, and an update
30// naming an instrument is refused by its publisher. PerInstrument: a set per instrument as well,
31// which params(id) reads. An update naming an instrument ([strategy.overrides], fastmm-ctl param
32// --instrument, an [ab] test that splits the instruments) sets that instrument's own values; one
33// without an instrument changes the shared set and every instrument's values that are not its own
34// (strategies/param_overrides.hpp).
35enum class ParamScope : std::uint8_t { Shared, PerInstrument };
36
37template <class Params, ParamScope Scope = ParamScope::Shared>
38class StrategyBase {
39 public:
40 using params_type = Params;
41 static constexpr ParamScope param_scope = Scope;
42 static const ParamSchema& schema() { return Params::schema(); }
43 // Applies string parameters, then runs `validate() const` when the params struct declares one.
44 // Returns the first error message; the parameters are unchanged then (startup only).
45 std::optional<std::string> configure(const ParamMap& m) {
46 Params next = params_;
47 if (auto err = next.apply(m)) return err;
48 if (auto err = detail::validate_params(next)) return err;
49 params_ = next;
50 if constexpr (Scope == ParamScope::PerInstrument) {
51 per_instrument_.fill(params_);
52 for (ParamMask& own : own_) own.reset();
53 }
54 return std::nullopt;
55 }
56 // Read-only: parameters change through configure() and apply_param_update(). The set every
57 // instrument starts from: the configured values and the updates that named no instrument.
58 [[nodiscard]] const Params& params() const noexcept { return params_; }
59 // Instrument `id`'s parameters: params() with the instrument's own values on top. A Shared
60 // strategy has one set, so this is params(). O(1).
61 [[nodiscard]] const Params& params(InstrumentId id) const noexcept {
62 if constexpr (Scope == ParamScope::PerInstrument) {
63 return id.value < kMaxInstruments ? per_instrument_[id.value] : params_;
64 } else {
65 static_cast<void>(id);
66 return params_;
67 }
68 }
69 // The parameters instrument `id` holds a value of its own for, by schema index; none with
70 // Shared.
71 [[nodiscard]] ParamMask own_params(InstrumentId id) const noexcept {
72 if constexpr (Scope == ParamScope::PerInstrument) {
73 return id.value < kMaxInstruments ? own_[id.value] : ParamMask{};
74 } else {
75 static_cast<void>(id);
76 return {};
77 }
78 }
79 // Engine thread: assigns the (field, raw value) pairs of a ParamUpdateMsg, which the publisher
80 // validated (strategies/param_publisher.hpp): to the shared set, or with PerInstrument to the
81 // sets apply_param_pairs() names. A field outside the schema is skipped.
82 void apply_param_update(const ParamUpdateMsg& m) noexcept {
83 const ParamSchema& s = Params::schema();
84 if constexpr (Scope == ParamScope::PerInstrument) {
85 apply_param_pairs(m, std::span<ParamMask>(own_), s.size(), [&](std::size_t k, std::size_t i) {
86 s.begin()[m.field[i]].set_raw(&per_instrument_[k], m.value[i]);
87 });
88 if (!m.all_instruments()) return;
89 }
90 assign(m, params_);
91 }
92 [[nodiscard]] std::string describe_params() const { return params_.describe(); }
93
94 private:
95 static void assign(const ParamUpdateMsg& m, Params& p) noexcept {
96 const ParamSchema& s = Params::schema();
97 const std::size_t n =
99 for (std::size_t i = 0; i < n; ++i) {
100 if (m.field[i] < s.size()) s.begin()[m.field[i]].set_raw(&p, m.value[i]);
101 }
102 }
103
104 struct NoSets {};
105 Params params_{};
106 // PerInstrument: one set per instrument id.
107 [[no_unique_address]] std::conditional_t<Scope == ParamScope::PerInstrument,
108 std::array<Params, kMaxInstruments>,
109 NoSets> per_instrument_{};
110 [[no_unique_address]] std::conditional_t<Scope == ParamScope::PerInstrument,
111 std::array<ParamMask, kMaxInstruments>,
112 NoSets> own_{};
113};
114
115} // namespace fastmm
Definition params.hpp:102
const ParamDesc * begin() const noexcept
Definition params.hpp:112
std::size_t size() const noexcept
Definition params.hpp:111
Definition strategy.hpp:40
static const ParamSchema & schema()
Definition strategy.hpp:44
Params params_type
Definition strategy.hpp:42
ParamMask own_params(InstrumentId id) const noexcept
The parameters instrument id holds a value of its own for, by schema index; none with Shared.
Definition strategy.hpp:73
std::string describe_params() const
Definition strategy.hpp:94
std::optional< std::string > configure(const ParamMap &m)
Applies string parameters, then runs validate() const when the params struct declares one.
Definition strategy.hpp:47
const Params & params() const noexcept
Read-only: parameters change through configure() and apply_param_update().
Definition strategy.hpp:60
void apply_param_update(const ParamUpdateMsg &m) noexcept
Engine thread: assigns the (field, raw value) pairs of a ParamUpdateMsg, which the publisher validate...
Definition strategy.hpp:84
static constexpr ParamScope param_scope
Definition strategy.hpp:43
Definition strategy.hpp:26
Strategy hooks (ADR-0012): the hook table, the compile-time checker and the Fill view.
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Event bus message structs (5.5).
Definition strategy.hpp:23
StrongId< struct InstrumentTag, std::uint32_t > InstrumentId
Dense index into the engine's instrument table.
Definition strong_id.hpp:38
constexpr std::size_t kMaxInstruments
Definition instrument.hpp:26
ParamScope
How a StrategyBase keeps its parameters.
Definition strategy.hpp:37
std::map< std::string, std::string > ParamMap
Definition params.hpp:119
Strategy parameter schema (8.6, ADR-0012).
void(* set_raw)(void *obj, std::int64_t raw) noexcept
Definition params.hpp:97
New strategy parameter values (ADR-0013): up to kMaxFields (field index, raw value) pairs,...
Definition messages.hpp:795
static constexpr std::size_t kMaxFields
Definition messages.hpp:796