FastMM documentation
FastMM C++ API 0.6.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// Storage backend registry: name -> factories. [storage] backend names one entry.
3//
4// fastmm::store::StoreRegistry::instance().add("clickhouse", &make_ch_backend, &make_ch_reader);
5//
6// Nothing registers itself, for the same reason the strategy registry does not (a static library's
7// self-registering object is dropped by the linker): fastmm::store::register_builtin_backends()
8// adds the ones FastMM ships, and an out-of-tree backend calls add() before starting a session.
9// The name "none" is reserved and never reaches the registry: it means no backend at all.
12
13#include <memory>
14#include <string>
15#include <string_view>
16#include <vector>
17
18namespace fastmm::store {
19
20inline constexpr std::string_view kNoBackend = "none";
21
22struct StoreEntry {
23 using BackendFactory = std::unique_ptr<Backend> (*)();
24 using ReaderFactory = std::unique_ptr<Reader> (*)();
25 std::string name;
27 ReaderFactory reader = nullptr; // null: the backend records but cannot be queried in process
28};
30enum class AddResult : std::uint8_t { Added, AlreadyPresent, Conflict, Invalid };
31[[nodiscard]] constexpr std::string_view to_string(AddResult r) noexcept {
32 switch (r) {
34 return "added";
36 return "already present";
38 return "conflict";
40 return "invalid";
41 }
42 return "?";
43}
44
45class StoreRegistry {
46 public:
47 // The registry fastmm-live and fastmm-pnl use. Not thread-safe for writers: register before
48 // starting threads. Tests use a local registry.
49 static StoreRegistry& instance();
50
51 AddResult add(std::string_view name,
53 StoreEntry::ReaderFactory reader = nullptr);
54 [[nodiscard]] const StoreEntry* find(std::string_view name) const noexcept;
55 [[nodiscard]] const std::vector<StoreEntry>& entries() const noexcept { return entries_; }
56 // Comma-separated names, for an error message; always starts with "none".
57 [[nodiscard]] std::string names() const;
58 // Null when `name` is unknown or, for make_reader, when the backend cannot be queried.
59 [[nodiscard]] std::unique_ptr<Backend> make_backend(std::string_view name) const;
60 [[nodiscard]] std::unique_ptr<Reader> make_reader(std::string_view name) const;
62 private:
63 std::vector<StoreEntry> entries_;
64};
65
66// Adds the backends FastMM ships ("sqlite"). Idempotent.
68
69// [storage] backend, defaulting to "sqlite"; "none" disables the store.
70[[nodiscard]] std::string configured_backend(const GenericSection& storage);
71
72} // namespace fastmm::store
Storage backend interface: what a session's fills, orders, positions, PnL and kill events are handed ...
Definition registry.hpp:47
const StoreEntry * find(std::string_view name) const noexcept
const std::vector< StoreEntry > & entries() const noexcept
Definition registry.hpp:57
std::unique_ptr< Backend > make_backend(std::string_view name) const
Null when name is unknown or, for make_reader, when the backend cannot be queried.
static StoreRegistry & instance()
The registry fastmm-live and fastmm-pnl use.
AddResult add(std::string_view name, StoreEntry::BackendFactory backend, StoreEntry::ReaderFactory reader=nullptr)
std::string names() const
Comma-separated names, for an error message; always starts with "none".
std::unique_ptr< Reader > make_reader(std::string_view name) const
Definition backend.hpp:39
std::string configured_backend(const GenericSection &storage)
[storage] backend, defaulting to "sqlite"; "none" disables the store.
AddResult
Definition registry.hpp:32
constexpr std::string_view to_string(AddResult r) noexcept
Definition registry.hpp:33
constexpr std::string_view kNoBackend
Definition registry.hpp:22
void register_builtin_backends(StoreRegistry &r=StoreRegistry::instance())
Adds the backends FastMM ships ("sqlite"). Idempotent.
Query side of a storage backend: what fastmm-pnl and a restarting session read back.
Free-form section used by the sim / backtest libraries: dotted keys -> stringified value.
Definition config.hpp:287
Definition registry.hpp:24
std::unique_ptr< Backend >(*)() BackendFactory
Definition registry.hpp:25
std::string name
Definition registry.hpp:27
BackendFactory backend
Definition registry.hpp:28
std::unique_ptr< Reader >(*)() ReaderFactory
Definition registry.hpp:26
ReaderFactory reader
null: the backend records but cannot be queried in process
Definition registry.hpp:29