ACF $AcfVersion:0$
Public Member Functions | Protected Member Functions | List of all members
imod::CMultiModelObserverBase Class Reference

Basic implementation of a multiple model observer. More...

#include <CMultiModelObserverBase.h>

Inheritance diagram for imod::CMultiModelObserverBase:
imod::IObserver istd::IPolymorphic imod::TMultiModelObserverBase< ModelInterface >

Public Member Functions

 CMultiModelObserverBase ()
 
virtual ~CMultiModelObserverBase ()
 
int GetModelCount () const
 Gets the number of connected models.
 
IModelGetObservedModel (int modelIndex) const
 Get access to connected model with the index index.
 
IModelGetModelPtr (int modelIndex) const
 Get access to connected model with the index index.
 
void EnsureModelsDetached ()
 Ensure all attached models are detached.
 
void SetObservedIds (const istd::IChangeable::ChangeSet &changeMask)
 Set list of ID's beeing observed.
 
virtual bool IsModelAttached (const IModel *modelPtr) const override
 Checks if the specified model is currently attached to this observer.
 
virtual bool OnModelAttached (IModel *modelPtr, istd::IChangeable::ChangeSet &changeMask) override
 Callback invoked when an observable model is about to be attached to this observer.
 
virtual bool OnModelDetached (IModel *modelPtr) override
 Callback invoked when an observable model is about to be detached from this observer.
 
virtual void BeforeUpdate (IModel *modelPtr) override
 Callback invoked before an update of the observer's content occurs.
 
virtual void AfterUpdate (IModel *modelPtr, const istd::IChangeable::ChangeSet &changeSet) override
 Callback invoked after an update of the observer's content occurs.
 
- Public Member Functions inherited from istd::IPolymorphic
virtual ~IPolymorphic ()
 

Protected Member Functions

virtual void OnUpdate (IModel *modelPtr, const istd::IChangeable::ChangeSet &changeSet)
 Called on update of observed model.
 

Detailed Description

Basic implementation of a multiple model observer.

Definition at line 23 of file CMultiModelObserverBase.h.

Constructor & Destructor Documentation

◆ CMultiModelObserverBase()

imod::CMultiModelObserverBase::CMultiModelObserverBase ( )

◆ ~CMultiModelObserverBase()

virtual imod::CMultiModelObserverBase::~CMultiModelObserverBase ( )
virtual

Member Function Documentation

◆ AfterUpdate()

virtual void imod::CMultiModelObserverBase::AfterUpdate ( IModel modelPtr,
const istd::IChangeable::ChangeSet changeSet 
)
overridevirtual

Callback invoked after an update of the observer's content occurs.

This is the primary notification method where observers react to model changes. It's called after the model has finished updating its state. The implementation should examine the changeSet to determine what changed and update accordingly.

Parameters
modelPtrPointer to the model that has changed
changeSetContains information about what changed in the model. Use changeSet.Contains(changeId) to check for specific changes. The changeSet respects the mask specified in OnModelAttached().
Note
This is typically where you update your GUI, recalculate derived values, or trigger other dependent operations.
Keep this method fast - it's called frequently and may block the UI thread.
Use the changeSet to perform incremental updates rather than refreshing everything.
virtual void AfterUpdate(IModel* modelPtr,
const istd::IChangeable::ChangeSet& changeSet)
{
CMyModel* model = dynamic_cast<CMyModel*>(modelPtr);
if (!model) return;
// Selective update based on what changed
if (changeSet.Contains(CMyModel::CF_NAME_CHANGED)) {
m_nameLabel->setText(model->GetName());
}
if (changeSet.Contains(CMyModel::CF_VALUE_CHANGED)) {
m_valueSpinBox->setValue(model->GetValue());
RecalculateDependentValues();
}
// Re-enable signals if blocked in BeforeUpdate
if (m_widget) {
m_widget->blockSignals(false);
}
}
virtual void AfterUpdate(IModel *modelPtr, const istd::IChangeable::ChangeSet &changeSet) override
Callback invoked after an update of the observer's content occurs.
Common interface for model objects, that supports Model/Observer design pattern.
Definition IModel.h:25
Set of change flags (its IDs).
Definition IChangeable.h:36
bool Contains(int changeId) const
Check if there is specific change flag in the set.
See also
BeforeUpdate(), OnModelAttached()

Implements imod::IObserver.

◆ BeforeUpdate()

virtual void imod::CMultiModelObserverBase::BeforeUpdate ( IModel modelPtr)
overridevirtual

Callback invoked before an update of the observer's content occurs.

This method is called at the start of a model update cycle, before any data actually changes. It allows the observer to prepare for the update, such as saving current state for comparison or disabling UI updates temporarily.

Parameters
modelPtrPointer to the model that is about to change
Warning
In some error cases, this method may be called without a subsequent AfterUpdate() call. Don't rely on AfterUpdate() always being called.
Note
This method is optional to implement - if you don't need preparation logic, just provide an empty implementation.
virtual void BeforeUpdate(IModel* modelPtr)
{
// Example: Block signals to prevent multiple GUI updates
if (m_widget) {
m_widget->blockSignals(true);
}
// Example: Save state for delta comparison
m_previousValue = m_currentValue;
}
virtual void BeforeUpdate(IModel *modelPtr) override
Callback invoked before an update of the observer's content occurs.
See also
AfterUpdate()

Implements imod::IObserver.

◆ EnsureModelsDetached()

void imod::CMultiModelObserverBase::EnsureModelsDetached ( )

Ensure all attached models are detached.

If there are some attached models they will be detached and removed from observed list.

◆ GetModelCount()

int imod::CMultiModelObserverBase::GetModelCount ( ) const

Gets the number of connected models.


◆ GetModelPtr()

IModel * imod::CMultiModelObserverBase::GetModelPtr ( int  modelIndex) const

Get access to connected model with the index index.

Deprecated:
use GetObservedModel instead.

◆ GetObservedModel()

IModel * imod::CMultiModelObserverBase::GetObservedModel ( int  modelIndex) const

Get access to connected model with the index index.

Parameters
modelIndexindex of model.

◆ IsModelAttached()

virtual bool imod::CMultiModelObserverBase::IsModelAttached ( const IModel modelPtr) const
overridevirtual

Checks if the specified model is currently attached to this observer.

This method allows querying whether a specific model is being observed, or whether any model at all is attached.

Parameters
modelPtrPointer to the model object to check. If nullptr, checks whether any model is attached to this observer.
Returns
true if the specified model (or any model when modelPtr is nullptr) is attached to this observer, false otherwise.
// Check if a specific model is attached
if (observer->IsModelAttached(myModel)) {
// This specific model is being observed
}
// Check if any model is attached
if (observer->IsModelAttached(nullptr)) {
// At least one model is attached
}
See also
OnModelAttached(), OnModelDetached()

Implements imod::IObserver.

◆ OnModelAttached()

virtual bool imod::CMultiModelObserverBase::OnModelAttached ( IModel modelPtr,
istd::IChangeable::ChangeSet changeMask 
)
overridevirtual

Callback invoked when an observable model is about to be attached to this observer.

This method is called by the model when attempting to establish an observer relationship. The implementation should:

  1. Validate that the model is of an acceptable type
  2. Set the changeMask to specify which changes to monitor
  3. Return true to accept the attachment, or false to reject it
Parameters
modelPtrPointer to the model object being attached. Never nullptr.
changeMaskOutput parameter where the observer specifies which change types it wants to be notified about. Set the appropriate change flags using changeMask.Set(changeId). An empty mask means the observer wants all changes.
Returns
true if the model is accepted and attachment succeeds, false to reject the attachment (e.g., if the model is of incompatible type).
Note
After successful attachment, the observer should initialize its state based on the current model data.
The changeMask allows filtering notifications for better performance. Only changes matching the mask will trigger AfterUpdate() calls.
virtual bool OnModelAttached(IModel* modelPtr,
{
// Type check
CMyModel* model = dynamic_cast<CMyModel*>(modelPtr);
if (!model) return false;
// Set up change filtering
changeMask.Set(CMyModel::CF_DATA_CHANGED);
changeMask.Set(CMyModel::CF_STATUS_CHANGED);
// Don't set CF_INTERNAL_CHANGED - we don't care about those
// Initialize view from current model state
UpdateViewFromModel(model);
return true;
}
virtual bool OnModelAttached(IModel *modelPtr, istd::IChangeable::ChangeSet &changeMask) override
Callback invoked when an observable model is about to be attached to this observer.
See also
OnModelDetached(), IsModelAttached(), AfterUpdate()

Implements imod::IObserver.

Reimplemented in imod::TMultiModelObserverBase< ModelInterface >.

◆ OnModelDetached()

virtual bool imod::CMultiModelObserverBase::OnModelDetached ( IModel modelPtr)
overridevirtual

Callback invoked when an observable model is about to be detached from this observer.

This method is called when the observer-model relationship is being terminated, either explicitly or because the model is being destroyed. The implementation should clean up any state or resources related to this model.

Parameters
modelPtrPointer to the model object being detached
Returns
true if the detachment was handled successfully, false otherwise
Note
After this call, the observer should not access the model pointer anymore.
This method may be called even if OnModelAttached() previously returned false.
Clean up any cached data or references to this model.
virtual bool OnModelDetached(IModel* modelPtr)
{
if (m_currentModel == modelPtr) {
// Clear any cached data
m_cachedData.clear();
m_currentModel = nullptr;
// Update UI to show no model is attached
ClearDisplay();
return true;
}
return false;
}
virtual bool OnModelDetached(IModel *modelPtr) override
Callback invoked when an observable model is about to be detached from this observer.
See also
OnModelAttached(), IsModelAttached()

Implements imod::IObserver.

◆ OnUpdate()

virtual void imod::CMultiModelObserverBase::OnUpdate ( IModel modelPtr,
const istd::IChangeable::ChangeSet changeSet 
)
protectedvirtual

Called on update of observed model.

This method is designed to be overload by derrived classes.

◆ SetObservedIds()

void imod::CMultiModelObserverBase::SetObservedIds ( const istd::IChangeable::ChangeSet changeMask)

Set list of ID's beeing observed.


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