Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
bertini::TypedObserver< Derived, ObservedT, ConcreteEvents > Class Template Reference

CRTP base that turns an observer into a set of typed Observe() overloads. More...

#include <observer.hpp>

Inheritance diagram for bertini::TypedObserver< Derived, ObservedT, ConcreteEvents >:
[legend]
Collaboration diagram for bertini::TypedObserver< Derived, ObservedT, ConcreteEvents >:
[legend]

Public Member Functions

std::vector< std::type_index > SubscribedEventTypes () const override
 Declares which event types this observer wants to receive.
ObserveResult Observe (AnyEvent const &e) override
 Observe the observable object being observed.
virtual ~TypedObserver ()=default
std::type_index ObservedKind () const override
 The type this observer expects to observe.

Detailed Description

template<class Derived, class ObservedT, class... ConcreteEvents>
class bertini::TypedObserver< Derived, ObservedT, ConcreteEvents >

CRTP base that turns an observer into a set of typed Observe() overloads.

Instead of writing one Observe(AnyEvent const&) body full of dynamic_casts (and a matching hand-written SubscribedEventTypes()), derive from this and provide one ObserveResult OnEvent(ConcreteEvent const&) overload per event you care about. This helper:

  • implements SubscribedEventTypes() from ConcreteEvents..., so the observable routes only those exact event types here; and
  • implements the single Observe(AnyEvent const&) virtual once, doing the one downcast centrally and dispatching to the matching OnEvent overload on Derived.

The per-event handler is named OnEvent (not an Observe overload) so it does not hide the Observe(AnyEvent const&) virtual – keeping Observe the single dispatch entry point and avoiding -Woverloaded-virtual noise.

The downcast stays a dynamic_cast (so the dispatch semantics are identical to the old hand-written observers), but it now lives in exactly one place rather than being copy-pasted into every observer body.

Template Parameters
DerivedThe concrete observer (CRTP).
ObservedTThe observed type (so this is still an Observer<ObservedT>).
ConcreteEventsThe exact event types to subscribe to, e.g. TrackingStarted<EmitterT>. Derived must provide an ObserveResult OnEvent(E const&) overload for each one.

Constructor & Destructor Documentation

◆ ~TypedObserver()

template<class Derived, class ObservedT, class... ConcreteEvents>
virtual bertini::TypedObserver< Derived, ObservedT, ConcreteEvents >::~TypedObserver ( )
virtualdefault

Member Function Documentation

◆ Observe()

template<class Derived, class ObservedT, class... ConcreteEvents>
ObserveResult bertini::TypedObserver< Derived, ObservedT, ConcreteEvents >::Observe ( AnyEvent const & e)
inlineoverridevirtual

Observe the observable object being observed.

This is probably in response to NotifyObservers.

This virtual function must be overridden by actual observers, defining how they observe the observable they are observing, probably filtering events and doing something specific for different ones.

Parameters
eThe event which was emitted by the observed object.
Returns
ObserveResult::KeepObserving to keep receiving events, or ObserveResult::Unsubscribe to ask the observable to drop this observer once the current notification finishes.

Implements bertini::AnyObserver.

◆ ObservedKind()

template<class ObservedT>
std::type_index bertini::Observer< ObservedT >::ObservedKind ( ) const
inlineoverridevirtualinherited

The type this observer expects to observe.

Used by Observable::AddObserver to reject attaching an observer to an incompatible observable (which would otherwise silently never fire). The default is typeid(void), a wildcard meaning "attach me anywhere"; Observer<ObservedT> reports typeid(ObservedT).

Reimplemented from bertini::AnyObserver.

◆ SubscribedEventTypes()

template<class Derived, class ObservedT, class... ConcreteEvents>
std::vector< std::type_index > bertini::TypedObserver< Derived, ObservedT, ConcreteEvents >::SubscribedEventTypes ( ) const
inlineoverridevirtual

Declares which event types this observer wants to receive.

Return a non-empty vector to opt into type-indexed dispatch: the observable will only call Observe() for events whose dynamic type exactly matches one of the returned type_index values. Return an empty vector (the default) to receive every event — this is the correct choice for observers that handle many or all event types, and is the automatic behavior for Python-defined observers.

Reimplemented from bertini::AnyObserver.