Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Observable.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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-2021 */
9/* */
10/* Observateur. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Observable.h"
15#include "arcane/utils/Observer.h"
16
17/*---------------------------------------------------------------------------*/
18/*---------------------------------------------------------------------------*/
19
20namespace Arcane
21{
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
28{
29 if (arcaneIsCheck()){
30 if (!m_observers.empty())
31 cout << "** WARNING: Observable p=" << this
32 << " is destroyed but has n="
33 << m_observers.size() << " observer(s) attached\n";
34 }
35}
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
42{
43 // Vérifie que l'observeur n'est pas dans la liste.
44 if (m_observers.contains(obs))
45 return;
46 obs->attachToObservable(this);
47 m_observers.add(obs);
48}
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
55{
56 //std::cout << "DETACH OB this=" << this << " o=" << obs
57 // << " destroyed=" << m_is_destroyed << '\n';
58 if (m_is_destroyed)
59 return;
60 for( Integer i=0, n=m_observers.size(); i<n; ++i ){
61 if (m_observers[i]==obs){
62 m_observers.remove(i);
63 return;
64 }
65 }
66}
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
73{
74 if (m_observers.empty())
75 return;
76 for( Integer i=0, n=m_observers.size(); i<n; ++i )
77 m_observers[i]->observerUpdate(this);
78}
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
84hasObservers() const
85{
86 return (!m_observers.empty());
87}
88
89/*---------------------------------------------------------------------------*/
90/*---------------------------------------------------------------------------*/
91
93isDestroyed() const
94{
95 return m_is_destroyed;
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
102destroy()
103{
104 if (m_is_destroyed)
105 return;
106 m_is_destroyed = true;
108 m_observers.clear();
109 delete this;
110}
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115void Observable::
116_detachAllObservers()
117{
118 //std::cout << "DESTROY this=" << this << '\n';
119 for( Integer i=0, n=m_observers.size(); i<n; ++i ){
120 IObserver* o = m_observers[i];
121 //std::cout << "DETACH o=" << o << '\n';
122 o->detach();
123 }
124 m_observers.clear();
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
132{
133 _detachAllObservers();
134}
135
136/*---------------------------------------------------------------------------*/
137/*---------------------------------------------------------------------------*/
138
139IObservable* IObservable::
140createDefault()
141{
142 return new Observable();
143}
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151AutoDetachObservable::
152~AutoDetachObservable()
153{
154 _detachAllObservers();
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160} // End namespace Arcane
161
162/*---------------------------------------------------------------------------*/
163/*---------------------------------------------------------------------------*/
Interface d'un observable.
Interface d'un observateur.
virtual void detach()=0
Se détache de l'observable.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Classe de base d'un observable.
void destroy() override
Détruit l'observable. Cet appel détruit l'instance (via delete this). Elle ne doit donc plus être uti...
void attachObserver(IObserver *obs) override
Attache l'observateur obs à cette observable.
Definition Observable.cc:41
void detachAllObservers() override
Détache tous les observeurs associés à cette instance.
bool isDestroyed() const override
Vrai si l'observable est détruit et ne doit plus être utilisé.
Definition Observable.cc:93
bool hasObservers() const override
Vrai si des observers sont attachées à cette observable.
Definition Observable.cc:84
void detachObserver(IObserver *obs) override
Détache l'observateur obs de cette observable.
Definition Observable.cc:54
void notifyAllObservers() override
Notifie tous les observateurs.
Definition Observable.cc:72
virtual ~Observable()
Libère les ressources.
Definition Observable.cc:27
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
bool arcaneIsCheck()
Vrai si on est en mode vérification.
Definition Misc.cc:151