Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ObserverPool.h
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/* ObserverPool.h (C) 2000-2025 */
9/* */
10/* List of observers. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_OBSERVERPOOL_H
13#define ARCANE_CORE_OBSERVERPOOL_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/List.h"
18
19#include "arcane/core/IObservable.h"
20#include "arcane/core/Observer.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief List of observers
33 *
34 * This class allows managing a list of observers and ensuring
35 * their destruction when the observer's target object is destroyed.
36 */
37class ARCANE_CORE_EXPORT ObserverPool
38{
39 public:
40
41 typedef Collection<IObserver*> ObserverCollection;
42
43 public:
44
45 //! Constructor
47 ~ObserverPool(); //!< Frees resources
48
49 public:
50
51 //! Adds an observer
52 template <class T> inline void
53 addObserver(T* obj, void (T::*func)(const IObservable&), IObservable* oba)
54 {
55 IObserver* obs = new ObserverT<T>(obj, func);
56 oba->attachObserver(obs);
57 m_observers.add(obs);
58 }
59
60 //! Adds an observer
61 template <class T> inline void
62 addObserver(T* obj, void (T::*func)(), IObservable* oba)
63 {
64 IObserver* obs = new ObserverT<T>(obj, func);
65 oba->attachObserver(obs);
66 m_observers.add(obs);
67 }
68
69 //! List of observers
70 ObserverCollection observers() { return m_observers; }
71
72 //! Detaches all observers (also detaches them in the process)
73 void detachAll();
74
75 private:
76
77 List<IObserver*> m_observers; //!< List of observers
78};
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
83} // namespace Arcane
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88#endif
Base class for a strongly typed collection.
virtual void attachObserver(IObserver *obs)=0
Attaches the observer obs to this observable.
Implementation of a collection of elements in vector form.
List of observers.
ObserverPool()
Constructor.
void addObserver(T *obj, void(T::*func)(), IObservable *oba)
Adds an observer.
void addObserver(T *obj, void(T::*func)(const IObservable &), IObservable *oba)
Adds an observer.
ObserverCollection observers()
List of observers.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --