KiCad PCB EDA Suite
UTIL::OBSERVABLE< ObserverInterface > Class Template Reference

#include <observable.h>

Inheritance diagram for UTIL::OBSERVABLE< ObserverInterface >:
UTIL::DETAIL::OBSERVABLE_BASE

Public Member Functions

 OBSERVABLE ()
 Construct an observable with empty non-shared subscription list. More...
 
 OBSERVABLE (OBSERVABLE &aInherit)
 Construct an observable with a shared subscription list. More...
 
void SubscribeUnmanaged (ObserverInterface *aObserver)
 Add a subscription without RAII link. More...
 
LINK Subscribe (ObserverInterface *aObserver)
 Add a subscription returning an RAII link. More...
 
void Unsubscribe (ObserverInterface *aObserver)
 Cancel the subscription of a subscriber. More...
 
template<typename... Args1, typename... Args2>
void Notify (void(ObserverInterface::*Ptr)(Args1...), Args2 &&... aArgs)
 Notify event to all subscribed observers. More...
 
template<typename... Args1, typename... Args2>
void NotifyIgnore (void(ObserverInterface::*Ptr)(Args1...), ObserverInterface *aIgnore, Args2 &&... aArgs)
 Notify event to all subscribed observers but one to be ignore. More...
 
size_t size () const
 

Protected Member Functions

void on_observers_empty ()
 
void enter_iteration ()
 
void leave_iteration ()
 
void add_observer (void *observer)
 
void remove_observer (void *observer)
 

Protected Attributes

std::shared_ptr< IMPLimpl_
 

Detailed Description

template<typename ObserverInterface>
class UTIL::OBSERVABLE< ObserverInterface >

Definition at line 126 of file observable.h.

Constructor & Destructor Documentation

◆ OBSERVABLE() [1/2]

template<typename ObserverInterface>
UTIL::OBSERVABLE< ObserverInterface >::OBSERVABLE ( )
inline

Construct an observable with empty non-shared subscription list.

Definition at line 132 of file observable.h.

132 {}

◆ OBSERVABLE() [2/2]

template<typename ObserverInterface>
UTIL::OBSERVABLE< ObserverInterface >::OBSERVABLE ( OBSERVABLE< ObserverInterface > &  aInherit)
inline

Construct an observable with a shared subscription list.

Parameters
aInheritObservable to share the subscription list with.

Definition at line 139 of file observable.h.

139 : OBSERVABLE_BASE( aInherit ) {}

Member Function Documentation

◆ add_observer()

void UTIL::DETAIL::OBSERVABLE_BASE::add_observer ( void *  observer)
protectedinherited

Definition at line 216 of file observable.cpp.

217  {
218  allocate_impl();
219  impl_->add_observer( observer );
220  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

References UTIL::DETAIL::OBSERVABLE_BASE::allocate_impl(), and UTIL::DETAIL::OBSERVABLE_BASE::impl_.

◆ enter_iteration()

void UTIL::DETAIL::OBSERVABLE_BASE::enter_iteration ( )
protectedinherited

Definition at line 230 of file observable.cpp.

231  {
232  if( impl_ )
233  impl_->enter_iteration();
234  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

References UTIL::DETAIL::OBSERVABLE_BASE::impl_.

Referenced by UTIL::OBSERVABLE< GAL_DISPLAY_OPTIONS_OBSERVER >::Notify(), and UTIL::OBSERVABLE< GAL_DISPLAY_OPTIONS_OBSERVER >::NotifyIgnore().

◆ leave_iteration()

void UTIL::DETAIL::OBSERVABLE_BASE::leave_iteration ( )
protectedinherited

Definition at line 237 of file observable.cpp.

238  {
239  if( impl_)
240  {
241  impl_->leave_iteration();
242 
243  if( !impl_->is_iterating() && !impl_->is_shared() && impl_.use_count() == 1 )
244  impl_.reset();
245  }
246  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

References UTIL::DETAIL::OBSERVABLE_BASE::impl_.

Referenced by UTIL::OBSERVABLE< GAL_DISPLAY_OPTIONS_OBSERVER >::Notify(), and UTIL::OBSERVABLE< GAL_DISPLAY_OPTIONS_OBSERVER >::NotifyIgnore().

◆ Notify()

template<typename ObserverInterface>
template<typename... Args1, typename... Args2>
void UTIL::OBSERVABLE< ObserverInterface >::Notify ( void(ObserverInterface::*)(Args1...)  Ptr,
Args2 &&...  aArgs 
)
inline

Notify event to all subscribed observers.

Parameters
Ptris a pointer to method of the observer interface.
aArgsis a list of arguments to each notification call, will be perfectly forwarded.

Definition at line 182 of file observable.h.

183  {
184  static_assert( sizeof...( Args1 ) == sizeof...( Args2 ), "argument counts don't match" );
185 
186  if( impl_ )
187  {
188  enter_iteration();
189  try
190  {
191  for( auto* void_ptr : impl_->observers_ )
192  {
193  if( void_ptr )
194  {
195  auto* typed_ptr = static_cast<ObserverInterface*>( void_ptr );
196  ( typed_ptr->*Ptr )( std::forward<Args2>( aArgs )... );
197  }
198  }
199  }
200  catch( ... )
201  {
202  leave_iteration();
203  throw;
204  }
205 
206  leave_iteration();
207  }
208  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

◆ NotifyIgnore()

template<typename ObserverInterface>
template<typename... Args1, typename... Args2>
void UTIL::OBSERVABLE< ObserverInterface >::NotifyIgnore ( void(ObserverInterface::*)(Args1...)  Ptr,
ObserverInterface *  aIgnore,
Args2 &&...  aArgs 
)
inline

Notify event to all subscribed observers but one to be ignore.

Parameters
Ptris a pointer to method of the observer interface.
aIgnoreis an observer to ignore during this notification.
aArgsis a list of arguments to each notification call, will be perfectly forwarded.

Definition at line 218 of file observable.h.

220  {
221  static_assert( sizeof...( Args1 ) == sizeof...( Args2 ), "argument counts don't match" );
222 
223  if( impl_ )
224  {
225  enter_iteration();
226 
227  try
228  {
229  for( auto* void_ptr : impl_->observers_ )
230  {
231  if( void_ptr && void_ptr != aIgnore )
232  {
233  auto* typed_ptr = static_cast<ObserverInterface*>( void_ptr );
234  ( typed_ptr->*Ptr )( std::forward<Args2>( aArgs )... );
235  }
236  }
237  }
238  catch( ... )
239  {
240  leave_iteration();
241  throw;
242  }
243 
244  leave_iteration();
245  }
246  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

◆ on_observers_empty()

void UTIL::DETAIL::OBSERVABLE_BASE::on_observers_empty ( )
protectedinherited

Definition at line 258 of file observable.cpp.

259  {
260  // called by an impl that is owned by this, ie. it is a non-shared impl
261  // also it is not iterating
262  deallocate_impl();
263  }

References UTIL::DETAIL::OBSERVABLE_BASE::deallocate_impl().

◆ remove_observer()

void UTIL::DETAIL::OBSERVABLE_BASE::remove_observer ( void *  observer)
protectedinherited

Definition at line 223 of file observable.cpp.

224  {
225  assert( impl_ );
226  impl_->remove_observer( observer );
227  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

References UTIL::DETAIL::OBSERVABLE_BASE::impl_.

◆ size()

size_t UTIL::DETAIL::OBSERVABLE_BASE::size ( ) const
inherited

Definition at line 249 of file observable.cpp.

250  {
251  if( impl_ )
252  return impl_->observers_.size();
253  else
254  return 0;
255  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

References UTIL::DETAIL::OBSERVABLE_BASE::impl_.

◆ Subscribe()

template<typename ObserverInterface>
LINK UTIL::OBSERVABLE< ObserverInterface >::Subscribe ( ObserverInterface *  aObserver)
inline

Add a subscription returning an RAII link.

Parameters
aObserverobserver to subscribe
Returns
RAII link controlling the lifetime of the subscription

Definition at line 157 of file observable.h.

158  {
159  OBSERVABLE_BASE::add_observer( static_cast<void*>( aObserver ) );
160  return LINK( impl_, static_cast<void*>( aObserver ) );
161  }
std::shared_ptr< IMPL > impl_
Definition: observable.h:93

Referenced by KIGFX::GAL::GAL().

◆ SubscribeUnmanaged()

template<typename ObserverInterface>
void UTIL::OBSERVABLE< ObserverInterface >::SubscribeUnmanaged ( ObserverInterface *  aObserver)
inline

Add a subscription without RAII link.

Parameters
aObserverObserver to subscribe.

Definition at line 146 of file observable.h.

147  {
148  OBSERVABLE_BASE::add_observer( static_cast<void*>( aObserver ) );
149  }

◆ Unsubscribe()

template<typename ObserverInterface>
void UTIL::OBSERVABLE< ObserverInterface >::Unsubscribe ( ObserverInterface *  aObserver)
inline

Cancel the subscription of a subscriber.

This can be called during notification calls.

Parameters
aObserverobserver to remove from the subscription list.

Definition at line 170 of file observable.h.

171  {
172  OBSERVABLE_BASE::remove_observer( static_cast<void*>( aObserver ) );
173  }

Member Data Documentation

◆ impl_


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