Audacity 3.2.0
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Private Attributes | List of all members
Observer::Publisher< Message, NotifyAll > Class Template Reference

An object that sends messages to an open-ended list of subscribed callbacks. More...

#include <Observer.h>

Inherited by CompressorInstance, CompressorInstance, and DeviceManager.

Collaboration diagram for Observer::Publisher< Message, NotifyAll >:
[legend]

Classes

struct  Record
 

Public Types

using message_type = Message
 
using CallbackReturn = std::conditional_t< NotifyAll, void, bool >
 
using Callback = std::function< CallbackReturn(const Message &) >
 Type of functions that can be connected to the Publisher. More...
 

Public Member Functions

template<typename Alloc = std::allocator<Record>>
 Publisher (ExceptionPolicy *pPolicy=nullptr, Alloc a={})
 Constructor supporting type-erased custom allocation/deletion. More...
 
 Publisher (Publisher &&)=default
 
Publisheroperator= (Publisher &&)=default
 
Subscription Subscribe (Callback callback)
 Connect a callback to the Publisher; later-connected are called earlier. More...
 
template<typename Object , typename Return , typename... Args>
Subscription Subscribe (Object &obj, Return(Object::*callback)(Args...))
 Overload of Subscribe takes an object and pointer-to-member-function. More...
 

Static Public Attributes

static constexpr bool notifies_all = NotifyAll
 

Protected Member Functions

CallbackReturn Publish (const Message &message)
 Send a message to connected callbacks. More...
 

Private Attributes

std::shared_ptr< detail::RecordListm_list
 
std::function< std::shared_ptr< detail::RecordBase >(Callback)> m_factory
 

Detailed Description

template<typename Message = Message, bool NotifyAll = true>
class Observer::Publisher< Message, NotifyAll >

An object that sends messages to an open-ended list of subscribed callbacks.

Intended for single-threaded use only. Move-only; allows type-erased custom allocation.

Template Parameters
Messagethe type of message
NotifyAllif true, callback return values are ignored; else, a callback returning true causes earlier subscribed callbacks to be skipped

Definition at line 108 of file Observer.h.

Member Typedef Documentation

◆ Callback

template<typename Message = Message, bool NotifyAll = true>
using Observer::Publisher< Message, NotifyAll >::Callback = std::function< CallbackReturn(const Message&) >

Type of functions that can be connected to the Publisher.

Definition at line 130 of file Observer.h.

◆ CallbackReturn

template<typename Message = Message, bool NotifyAll = true>
using Observer::Publisher< Message, NotifyAll >::CallbackReturn = std::conditional_t<NotifyAll, void, bool>

Definition at line 127 of file Observer.h.

◆ message_type

template<typename Message = Message, bool NotifyAll = true>
using Observer::Publisher< Message, NotifyAll >::message_type = Message

Definition at line 114 of file Observer.h.

Constructor & Destructor Documentation

◆ Publisher() [1/2]

template<typename Message , bool NotifyAll>
template<typename Alloc >
Observer::Publisher< Message, NotifyAll >::Publisher ( ExceptionPolicy pPolicy = nullptr,
Alloc  a = {} 
)
inlineexplicit

Constructor supporting type-erased custom allocation/deletion.

Parameters
pPolicyif null, exceptions from callbacks are caught and ignored; else, *pPolicy must have a lifetime encompassing the Publisher's

Definition at line 176 of file Observer.h.

177: m_list{ std::allocate_shared<detail::RecordList>( a, pPolicy,
178 []( // The visitor. Lambda with no capture converts to pointer-to-function
179 const detail::RecordBase &recordBase, const void *arg){
180 auto &record = static_cast<const Record&>(recordBase);
181 assert(arg);
182 auto &message = *static_cast<const Message*>(arg);
183 assert(record.callback);
184 // Calling foreign code! Which is why we have an exception policy.
185 if constexpr (NotifyAll)
186 return (record.callback(message), false);
187 else
188 return record.callback(message);
189 }
190) }
191, m_factory( [a = move(a)](Callback callback) {
192 return std::allocate_shared<Record>(a, move(callback));
193} )
194{}
std::shared_ptr< detail::RecordList > m_list
Definition: Observer.h:170
std::function< std::shared_ptr< detail::RecordBase >(Callback)> m_factory
Definition: Observer.h:171
std::function< CallbackReturn(const Message &) > Callback
Type of functions that can be connected to the Publisher.
Definition: Observer.h:130
TranslatableString Message(unsigned trackCount)

◆ Publisher() [2/2]

template<typename Message = Message, bool NotifyAll = true>
Observer::Publisher< Message, NotifyAll >::Publisher ( Publisher< Message, NotifyAll > &&  )
default

Member Function Documentation

◆ operator=()

template<typename Message = Message, bool NotifyAll = true>
Publisher & Observer::Publisher< Message, NotifyAll >::operator= ( Publisher< Message, NotifyAll > &&  )
default

◆ Publish()

template<typename Message , bool NotifyAll>
auto Observer::Publisher< Message, NotifyAll >::Publish ( const Message message)
inlineprotected

Send a message to connected callbacks.

Later-connected are called earlier; if !NotifyAll, any callback may return true, to stop the notification; see also class ExceptionPolicy

Returns
if NotifyAll, then void; else, whether the visit was stopped (either because a callback returned true, or the exception policy consumed an exception, and ordered a stop)

Definition at line 207 of file Observer.h.

209{
210 bool result = m_list->Visit(&message);
211 if constexpr (!NotifyAll)
212 return result;
213}

Referenced by AColor::ApplyUpdatedImages(), PrefsListener::Broadcast(), AppEvents::ProviderBase::HandleAppIdle(), WaveClip::OnProjectTempoChange(), CompressorInstance::RealtimeFinalize(), CompressorInstance::RealtimeInitialize(), CompressorInstance::RealtimeResume(), DeviceManager::Rescan(), WaveClip::SetCentShift(), WaveClip::SetPitchAndSpeedPreset(), WaveClip::StretchBy(), WaveClip::StretchLeftTo(), and WaveClip::~WaveClip().

Here is the caller graph for this function:

◆ Subscribe() [1/2]

template<typename Message , bool NotifyAll>
auto Observer::Publisher< Message, NotifyAll >::Subscribe ( Callback  callback)

Connect a callback to the Publisher; later-connected are called earlier.

During Publish(), the callback may have the side-effect of adding or removing other Subscriptions. Added subscriptions will not be called, and removed ones, if not called already, will not be called after.

Precondition
callback != nullptr

Definition at line 199 of file Observer.h.

201{
202 assert(callback); // precondition
203 return m_list->Subscribe(m_factory(move(callback)));
204}

Referenced by AdornedRulerPanel::AdornedRulerPanel(), AudioSetupToolBar::AudioSetupToolBar(), LabelTrackView::BindTo(), ToolManager::CreateWindows(), DeviceToolBar::DeviceToolBar(), anonymous_namespace{DropoutDetector.cpp}::DropoutSubscription::DropoutSubscription(), anonymous_namespace{RealtimeEffectPanel.cpp}::EffectsMenuHelper::EffectsMenuHelper(), HistoryDialog::HistoryDialog(), PrefsListener::Impl::Impl(), DefaultPlaybackPolicy::Initialize(), EffectUIHost::InitializeInstance(), LoginDialog::LoginDialog(), MixerBoard::MixerBoard(), AppEvents::OnAppIdle(), AudacityApp::OnInit(), WaveTrackControls::PanSlider(), PlayIndicatorOverlay::PlayIndicatorOverlay(), TimeToolBar::Populate(), ProjectAudioManager::ProjectAudioManager(), ProjectManager::ProjectManager(), ProjectWindow::ProjectWindow(), RealtimeEffectPanel::RealtimeEffectPanel(), ScrubbingOverlay::ScrubbingOverlay(), SpectralSelectionBar::SpectralSelectionBar(), SqliteSampleBlockFactory::SqliteSampleBlockFactory(), TimeDisplayModeSetting::Subscribe(), anonymous_namespace{ProjectStatus.cpp}::Dispatcher::Subscribe(), FrameStatistics::Subscribe(), ThemedAButtonWrapper< AButtonBase >::ThemedAButtonWrapper(), ThemedButtonWrapper< ButtonBase >::ThemedButtonWrapper(), ThemedWindowWrapper< WindowBase >::ThemedWindowWrapper(), SelectHandle::TimerHandler::TimerHandler(), TimeToolBar::TimeToolBar(), ToolsToolBar::ToolsToolBar(), TrackPanel::TrackPanel(), anonymous_namespace{ChannelView.cpp}::TrackPositioner::TrackPositioner(), NoteTrackControls::VelocitySlider(), WaveTrackControls::VolumeSlider(), and WaveTrackAffordanceControls::WaveTrackAffordanceControls().

Here is the caller graph for this function:

◆ Subscribe() [2/2]

template<typename Message = Message, bool NotifyAll = true>
template<typename Object , typename Return , typename... Args>
Subscription Observer::Publisher< Message, NotifyAll >::Subscribe ( Object &  obj,
Return(Object::*)(Args...)  callback 
)
inline

Overload of Subscribe takes an object and pointer-to-member-function.

Definition at line 144 of file Observer.h.

146 {
147 return Subscribe( [&obj, callback](const Message &message){
148 return (obj.*callback)(message);
149 } );
150 }
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199

Member Data Documentation

◆ m_factory

template<typename Message = Message, bool NotifyAll = true>
std::function<std::shared_ptr<detail::RecordBase>(Callback)> Observer::Publisher< Message, NotifyAll >::m_factory
private

Definition at line 171 of file Observer.h.

◆ m_list

template<typename Message = Message, bool NotifyAll = true>
std::shared_ptr<detail::RecordList> Observer::Publisher< Message, NotifyAll >::m_list
private

Definition at line 170 of file Observer.h.

◆ notifies_all

template<typename Message = Message, bool NotifyAll = true>
constexpr bool Observer::Publisher< Message, NotifyAll >::notifies_all = NotifyAll
staticconstexpr

Definition at line 113 of file Observer.h.


The documentation for this class was generated from the following file: