Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
Observable.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Observable.cc (C) 2000-2026 */
9/* */
10/* Observer. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/base/Observable.h"
15#include "arccore/base/Observer.h"
16
17#include <iostream>
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
30{
31 if (arccoreIsCheck()) {
32 if (!m_observers.empty())
33 std::cout << "** WARNING: Observable p=" << this
34 << " is destroyed but has n="
35 << m_observers.size() << " observer(s) attached\n";
36 }
37}
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
44{
45 // Checks that the observer is not in the list.
46 if (m_observers.contains(obs))
47 return;
48 obs->attachToObservable(this);
49 m_observers.add(obs);
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
57{
58 //std::cout << "DETACH OB this=" << this << " o=" << obs
59 // << " destroyed=" << m_is_destroyed << '\n';
60 if (m_is_destroyed)
61 return;
62 m_observers.removeValue(obs);
63}
64
65/*---------------------------------------------------------------------------*/
66/*---------------------------------------------------------------------------*/
67
70{
71 if (m_observers.empty())
72 return;
73 for (IObserver* o : m_observers)
74 o->observerUpdate(this);
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
81hasObservers() const
82{
83 return (!m_observers.empty());
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89void Observable::
90_detachAllObservers()
91{
92 //std::cout << "DESTROY this=" << this << '\n';
93 for (IObserver* o : m_observers) {
94 o->detach();
95 }
96 m_observers.clear();
97}
98
99/*---------------------------------------------------------------------------*/
100/*---------------------------------------------------------------------------*/
101
104{
105 _detachAllObservers();
106}
107
108/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110
111IObservable* IObservable::
112createDefault()
113{
114 return new Observable();
115}
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120/*---------------------------------------------------------------------------*/
121/*---------------------------------------------------------------------------*/
122
123AutoDetachObservable::
124~AutoDetachObservable()
125{
126 _detachAllObservers();
127}
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
131
132} // End namespace Arcane
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
virtual void attachToObservable(IObservable *obs)=0
Attaches to the observable obs.
void attachObserver(IObserver *obs) override
Attaches the observer obs to this observable.
Definition Observable.cc:43
void detachAllObservers() override
Detaches all observers associated with this instance.
bool hasObservers() const override
True if observers are attached to this observable.
Definition Observable.cc:81
void detachObserver(IObserver *obs) override
Detaches the observer obs from this observable.
Definition Observable.cc:56
~Observable() override
Releases resources.
Definition Observable.cc:29
void notifyAllObservers() override
Notifies all observers.
Definition Observable.cc:69
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
bool arccoreIsCheck()
True if in check mode.