Skip to content
Welcome to the new CppUtils documentation!

Thread-safe RAII observer for monitoring emitted events and collecting payloads.

Subscribes to the designated event upon construction and unsubscribes automatically upon destruction. Provides blocking synchronization utilities (waitForEvent, waitForOccurrences) and thread-safe inspection of occurrence counts and captured payloads.

Template parameterDescription
eventNameCompile-time hashed string identifying the event.
ArgsTypes of the event payload arguments.
using CppUtils::UnitTest::EventWatcher<eventName, Args>::Payload = std::conditional_t<sizeof...(Args) == 0, std::monostate, std::conditional_t<sizeof...(Args) == 1, std::tuple_element_t<0, std::tuple<Args..., void>>, std::tuple<Args...>>>;
explicit EventWatcher::EventWatcher(auto& emitter);

Subscribes the watcher to the specified event emitter.

ParameterDescription
emitterReference to the event emitter to monitor.
EventWatcher::~EventWatcher();
EventWatcher::EventWatcher(const EventWatcher&) = delete;
EventWatcher& EventWatcher::operator=(const EventWatcher&) = delete;
EventWatcher::EventWatcher(EventWatcher&& other) noexcept;
EventWatcher& EventWatcher::operator=(EventWatcher&& other) noexcept;
[[nodiscard]] auto EventWatcher::count() const noexcept -> std::size_t;

Returns the total number of times the monitored event has been fired.

Returns: Event occurrence count.

[[nodiscard]] auto EventWatcher::hasOccurred() const noexcept -> bool;

Checks if the monitored event has occurred at least once.

Returns: True if count() > 0.

template <Chrono::Duration TimeoutDuration = std::chrono::milliseconds>
auto EventWatcher::waitForOccurrences(std::size_t targetCount, TimeoutDuration timeout = std::chrono::milliseconds{1000}) -> bool;

Blocks until the event has been fired at least targetCount times or timeout expires.

Template parameterDescription
TimeoutDurationDuration type (defaults to std::chrono::milliseconds).
ParameterDescription
targetCountNumber of occurrences to wait for.
timeoutMaximum duration to wait before giving up.

Returns: True if the target occurrence count was reached before the timeout expired.

template <Chrono::Duration TimeoutDuration = std::chrono::milliseconds>
auto EventWatcher::waitForEvent(TimeoutDuration timeout = std::chrono::milliseconds{1000}) -> bool;

Blocks until the event occurs at least once or timeout expires.

Template parameterDescription
TimeoutDurationDuration type (defaults to std::chrono::milliseconds).
ParameterDescription
timeoutMaximum duration to wait before giving up.

Returns: True if the event occurred before the timeout expired.

[[nodiscard]] auto EventWatcher::occurrences() const -> auto;

Acquires exclusive locked access to the recorded state and payloads.

Returns: Unique accessor locking the internal state.

auto EventWatcher::clear() -> void;

Resets the event occurrence count to zero and clears recorded payloads.