FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
hooks.hpp File Reference

Strategy hooks (ADR-0012): the hook table, the compile-time checker and the Fill view. More...

#include "fastmm/core/enums.hpp"
#include "fastmm/core/fixed_point.hpp"
#include "fastmm/core/messages.hpp"
#include "fastmm/core/oms.hpp"
#include "fastmm/core/strong_id.hpp"
#include "fastmm/core/timer_wheel.hpp"
#include <array>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <type_traits>

Go to the source code of this file.

Classes

struct  fastmm::Fill
 One execution as a strategy sees it (on_fill). More...
 
struct  fastmm::RiskReject
 A new order or replace the engine's own risk check refused (RiskEngine, [risk]): nothing was sent and no order exists for it. More...
 
struct  fastmm::HookInfo
 

Namespaces

namespace  fastmm
 

Concepts

concept  fastmm::KeepsState
 Optional state the engine keeps across sessions ([strategy] state_file): std::string_view state() returns the bytes to save (valid until the strategy's next hook; the engine copies them at once) and bool restore(std::string_view) takes the bytes saved before, false when they are not this strategy's (it then starts fresh).
 

Macros

#define FASTMM_STRATEGY_HOOKS(X)
 The hook table.
 
#define FASTMM_STRATEGY_HOOK_NEAR_MISSES(Y)
 Y(misspelling, hook): names that produce a "did you mean" warning.
 
#define FASTMM_HOOK_ENUM(E, name, word, params, args, sig)   E,
 
#define FASTMM_HOOK_INFO(E, name, word, params, args, sig)   {Hook::E, #name, word, sig},
 
#define FASTMM_HOOK_UNPAREN(...)   __VA_ARGS__
 
#define FASTMM_HOOK_PROBE(name)
 
#define FASTMM_HOOK_CALL(E, name, word, params, args, sig)
 
#define FASTMM_HOOK_NEAR_MISS(name, hook)
 
#define FASTMM_HOOK_STATUS(E, name, word, params, args, sig)
 
#define FASTMM_HOOK_ASSERT(E, name, word, params, args, sig)
 
#define FASTMM_HOOK_WARN(name, hook)
 

Enumerations

enum class  fastmm::Hook : std::uint8_t { fastmm::FASTMM_HOOK_ENUM , fastmm::FASTMM_STRATEGY_HOOKS }
 
enum class  fastmm::HookStatus : std::uint8_t { fastmm::Absent , fastmm::Ok , fastmm::Mismatch }
 Absent: no member with the hook's name. More...
 

Functions

template<class S , class Ctx , class Book >
constexpr HookStatus fastmm::hook_status (Hook h) noexcept
 
template<class S , class Ctx , class Book >
constexpr bool fastmm::implements_hook (Hook h) noexcept
 True when the engine calls S's hook h (the engine dispatches on this at compile time).
 
template<class S , class Ctx = detail::hooks::StandInContext, class Book = detail::hooks::StandInBook>
constexpr bool fastmm::verify_strategy () noexcept
 Compile-time check of every hook of S; always returns true (failures are static_asserts), so it can be written as static_assert(fastmm::verify_strategy<MyMM>());.
 
template<class S , class Ctx , class Book >
constexpr std::string_view fastmm::implemented_hooks () noexcept
 The implemented hooks as words in table order ("start book fill timer connection quoting").
 

Variables

constexpr std::array< HookInfo, static_cast< std::size_t >(Hook::Count)> fastmm::kHooks
 

Detailed Description

Strategy hooks (ADR-0012): the hook table, the compile-time checker and the Fill view.

A strategy is a plain class. Its hooks are ordinary public member functions, all optional:

on_start(ctx) on_stop(ctx) on_book(ctx, id, book) on_book_ticker(ctx, id, m) on_trade(ctx, id, m) on_option_ticker(ctx, id, m) on_fill(ctx, fill) on_order_update(ctx, u) on_timer(ctx, timer_id, tag) on_connection(ctx, m) on_quoting(ctx, enabled) on_params(ctx) on_balance(ctx, m) on_perp_state(ctx, id, m) on_risk_reject(ctx, r) on_batch_end(ctx)

The engine checks every hook name when it is instantiated. If the strategy has a member with a hook's name (function, template, data member, static, inherited or private) and the engine's call does not compile, the build stops with one error per hook:

fastmm: on_trade has the wrong signature or is not public; expected void on_trade(auto& ctx, InstrumentId id, const TradeMsg& m)

A few likely misspellings (on_fills, on_tick, onBook, ...) produce a deprecation warning instead, because a private helper may use such a name; static constexpr bool fastmm_allow_near_miss_names = true; in the strategy silences it. Strategy headers can run the same check early with stand-in context and book types:

static_assert(fastmm::verify_strategy<MyMM>());

Limits:

  • a final class loses the name probe; only the call check remains, so a wrong signature is silently not called;
  • a hook that is ambiguous across two bases is reported as a wrong signature;
  • a hook with a deduced (auto) return type must not be checked with verify_strategy: the stand-in types instantiate its body;
  • private hooks are rejected, also when the class befriends the engine;
  • implicit argument conversions are accepted (on_timer(auto&, TimerId, int) compiles);
  • near-miss warnings are emitted inside this header, so a build that includes FastMM as a system header does not show them.

Macro Definition Documentation

◆ FASTMM_STRATEGY_HOOKS

#define FASTMM_STRATEGY_HOOKS (   X)

The hook table.

X(Enum, name, word, (parameters), (arguments), "expected signature"). The parameters name the engine's call; Ctx and Book are the engine's context and book types.

◆ FASTMM_STRATEGY_HOOK_NEAR_MISSES

#define FASTMM_STRATEGY_HOOK_NEAR_MISSES (   Y)

Y(misspelling, hook): names that produce a "did you mean" warning.

◆ FASTMM_HOOK_ENUM

#define FASTMM_HOOK_ENUM (   E,
  name,
  word,
  params,
  args,
  sig 
)    E,

◆ FASTMM_HOOK_INFO

#define FASTMM_HOOK_INFO (   E,
  name,
  word,
  params,
  args,
  sig 
)    {Hook::E, #name, word, sig},

◆ FASTMM_HOOK_UNPAREN

#define FASTMM_HOOK_UNPAREN (   ...)    __VA_ARGS__

◆ FASTMM_HOOK_PROBE

#define FASTMM_HOOK_PROBE (   name)
Value:
struct probe_##name { \
void name(); \
}; \
template <class S> \
struct name_probe_##name : S, probe_##name {}; \
template <class S> \
concept declares_##name = probeable<S> && (!requires { &name_probe_##name<S>::name; });

◆ FASTMM_HOOK_CALL

#define FASTMM_HOOK_CALL (   E,
  name,
  word,
  params,
  args,
  sig 
)
Value:
template <class S, class Ctx, class Book> \
concept calls_##name = requires(S& s, FASTMM_HOOK_UNPAREN params) { \
{ s.name args } -> std::same_as<void>; \
};
#define FASTMM_HOOK_PROBE(name)
Definition hooks.hpp:252
#define FASTMM_HOOK_UNPAREN(...)
Definition hooks.hpp:243

◆ FASTMM_HOOK_NEAR_MISS

#define FASTMM_HOOK_NEAR_MISS (   name,
  hook 
)
Value:
template <bool Found> \
struct near_miss_##name { \
static constexpr bool value = false; \
}; \
template <> \
struct near_miss_##name<true> { \
[[deprecated("fastmm: " #name " is not a hook; did you mean " #hook "?")]] \
static constexpr bool value = true; \
};

◆ FASTMM_HOOK_STATUS

#define FASTMM_HOOK_STATUS (   E,
  name,
  word,
  params,
  args,
  sig 
)
Value:
case Hook::E: \
if (detail::hooks::calls_##name<S, Ctx, Book>) return HookStatus::Ok; \
return detail::hooks::declares_##name<S> ? HookStatus::Mismatch : HookStatus::Absent;

◆ FASTMM_HOOK_ASSERT

#define FASTMM_HOOK_ASSERT (   E,
  name,
  word,
  params,
  args,
  sig 
)
Value:
const bool name##_declared = detail::hooks::declares_##name<S>; \
const bool name##_signature_matches = detail::hooks::calls_##name<S, Ctx, Book>; \
static_assert(!name##_declared || name##_signature_matches, \
"fastmm: " #name " has the wrong signature or is not public; expected " sig);

◆ FASTMM_HOOK_WARN

#define FASTMM_HOOK_WARN (   name,
  hook 
)
Value:
static_cast<void>(detail::hooks::near_miss_##name < !near_miss_names_allowed && \
detail::hooks::declares_##name < S >> ::value);