Files
2026-11-03 15:22:24 -03:00

66 lines
2.5 KiB
C++

#pragma once
#include "hot_reload/SymbolEventInfo.hpp"
#include <functional>
#include <string_view>
/**
* @file Options.hpp
* @brief hot_reload::Options -- callbacks a Context is constructed with.
*/
namespace hot_reload
{
/**
* @brief Callbacks and configuration a Context is constructed with.
*
* Replaces hot_reload.h's `HotReloadOptions`: `std::function` in place of a raw function
* pointer plus a manually-threaded `void* user_data` -- a capturing lambda plays the role
* `user_data` used to, so there is no separate field for it here at all.
*
* A pure aggregate with no behavior of its own, so -- unlike every other new public type in
* this library -- there is no matching Options.cpp; see Context.hpp for the type whose
* constructor actually reads these.
*
* @code
* hot_reload::Options options;
* options.onSymbolEvent = [](const hot_reload::SymbolEventInfo& event)
* {
* std::clog << event.summary() << '\n';
* };
* hot_reload::Context ctx(std::move(options));
* @endcode
*/
struct Options
{
/**
* @brief Called once per symbol on every Context::reload() call, synchronously, on the
* calling thread, while the reload is in progress.
*
* @warning Do not call load(), reload(), or findSymbol() on the same Context from
* inside this callback (see Context.hpp's thread-safety note: that Context's
* own lock is already held, in a non-reentrant way, by the reload() call this
* callback runs inside of -- doing so deadlocks rather than corrupts anything).
*/
std::function<void(const SymbolEventInfo& event)> onSymbolEvent;
/**
* @brief Called instead of patching, once per SymbolEvent::Removed event, so the caller
* can redirect old callers of a deleted function somewhere deliberate instead of
* leaving its previous build running untouched (the default with no callback, or
* with one that returns null).
*
* Called synchronously, on the calling thread, before the corresponding onSymbolEvent
* call for the same symbol; the same warning about not calling load()/reload()/
* findSymbol() applies.
*
* @param deletedSymbolName The removed function's real (possibly mangled) name.
* @return The address of a replacement to redirect old callers to, or null to leave the
* previous build's implementation running (the default).
*/
std::function<void*(std::string_view deletedSymbolName)> onDeletedSymbol;
};
}