Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ObservablePool.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 observables. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_OBSERVABLEPOOL_H
13#define ARCANE_CORE_OBSERVABLEPOOL_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/FatalErrorException.h"
18
19#include "arcane/core/Observable.h"
20#include "arcane/core/Observer.h"
21
22#include <map>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33class IObservable;
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38/*!
39 * \brief List of observables.
40 *
41 * This class allows managing a list of observables. Each observable
42 * is associated with a key of type \a KeyType.
43 */
44template <typename KeyType>
46{
47 public:
48
49 typedef std::map<KeyType, IObservable*> ObservableListType;
50
51 public:
52
53 //! Constructor
55 //! Frees resources
57 {
58 for (const auto& x : m_observables) {
59 IObservable* o = x.second;
61 delete o;
62 }
63 }
64
65 public:
66
67 void add(const KeyType& key)
68 {
69 IObservable* x = _getIfExists(key);
70 if (x)
71 ARCANE_FATAL("Observable with current key already exists");
72 m_observables.insert(std::make_pair(key, new Observable()));
73 }
74
75 IObservable* operator[](const KeyType& key)
76 {
77 IObservable* x = _getIfExists(key);
78 if (!x)
79 ARCANE_FATAL("No observable with current key exists");
80 return x;
81 }
82
83 protected:
84 private:
85
86 ObservableListType m_observables; //!< List of observables
87
88 IObservable* _getIfExists(const KeyType& key) const
89 {
90 auto x = m_observables.find(key);
91 if (x != m_observables.end())
92 return x->second;
93 return nullptr;
94 }
95};
96
97/*---------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
99
100} // End namespace Arcane
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
105#endif
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
virtual void detachAllObservers()=0
Detaches all observers associated with this instance.
~ObservablePool()
Frees resources.
ObservablePool()
Constructor.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --