FastMM documentation
FastMM C++ API 0.6.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).
7
8#include <concepts>
9#include <cstddef>
10#include <optional>
11#include <string>
12#include <string_view>
13
14namespace fastmm {
15
16template <class S>
17concept StrategyLike = requires {
18 { S::name() } -> std::convertible_to<std::string_view>;
19 { S::schema() } -> std::convertible_to<const ParamSchema&>;
20} && std::is_default_constructible_v<S>;
21
22template <class Params>
23class StrategyBase {
24 public:
25 using params_type = Params;
26 static const ParamSchema& schema() { return Params::schema(); }
27 // Applies string parameters, then runs `validate() const` when the params struct declares one.
28 // Returns the first error message; the parameters are unchanged then (startup only).
29 std::optional<std::string> configure(const ParamMap& m) {
30 Params next = params_;
31 if (auto err = next.apply(m)) return err;
32 if (auto err = detail::validate_params(next)) return err;
33 params_ = next;
34 return std::nullopt;
35 }
36 // Read-only: parameters change through configure() and apply_param_update().
37 [[nodiscard]] const Params& params() const noexcept { return params_; }
38 // Engine thread: assigns the (field, raw value) pairs of a ParamUpdateMsg, which the publisher
39 // validated (strategies/param_publisher.hpp). A StrategyBase keeps one parameter set for every
40 // instrument, so the message's instrument is not used; a field outside the schema is skipped.
41 void apply_param_update(const ParamUpdateMsg& m) noexcept {
42 const ParamSchema& s = Params::schema();
43 const std::size_t n =
45 for (std::size_t i = 0; i < n; ++i) {
46 if (m.field[i] < s.size()) s.begin()[m.field[i]].set_raw(&params_, m.value[i]);
47 }
48 }
49 [[nodiscard]] std::string describe_params() const { return params_.describe(); }
50
51 private:
52 Params params_{};
53};
54
55} // 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
Params params_type
Definition strategy.hpp:27
std::optional< std::string > configure(const ParamMap &m)
Applies string parameters, then runs validate() const when the params struct declares one.
Definition strategy.hpp:31
static const ParamSchema & schema()
Definition strategy.hpp:28
std::string describe_params() const
Definition strategy.hpp:51
const Params & params() const noexcept
Read-only: parameters change through configure() and apply_param_update().
Definition strategy.hpp:39
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:43
Definition strategy.hpp:19
Strategy hooks (ADR-0012): the hook table, the compile-time checker and the Fill view.
Definition strategy.hpp:16
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:574
static constexpr std::size_t kMaxFields
Definition messages.hpp:575