Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
VariableCollection.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/* VariableCollection.h (C) 2000-2025 */
9/* */
10/* Variable collection. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_VARIABLECOLLECTION_H
13#define ARCANE_CORE_VARIABLECOLLECTION_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Array.h"
18
19#include "arcane/core/VariableRef.h"
20#include "arcane/core/SharedReference.h"
21#include "arcane/utils/AutoRef.h"
22
23#include <algorithm>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39class ARCANE_CORE_EXPORT VariableCollectionEnumerator
40{
41 private:
42
43 friend class VariableCollection;
44
45 public:
46
47 VariableCollectionEnumerator(const VariableCollection& col);
48
49 public:
50
51 bool operator++()
52 {
53 ++m_index;
54 return m_index < m_count;
55 }
56 IVariable* operator*()
57 {
58 return m_collection[m_index];
59 }
60 IVariable* current()
61 {
62 return this->operator*();
63 }
64 bool moveNext()
65 {
66 return this->operator++();
67 }
68 void reset()
69 {
70 m_index = -1;
71 }
72
73 private:
74
75 Integer m_index;
76 Integer m_count;
77 ConstArrayView<IVariable*> m_collection;
78};
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
83/*!
84 * \brief Variable collection.
85 *
86 * This type has a reference semantics (like the SharedArray class).
87 */
88class ARCANE_CORE_EXPORT VariableCollection
89{
90 private:
91
92 class Impl
93 : public SharedReference
94 {
95 public:
96
97 const Array<IVariable*>& variables() const { return m_variables; }
98 Array<IVariable*>& variables() { return m_variables; }
99
100 public:
101
102 void deleteMe() override { delete this; }
103
104 private:
105
106 UniqueArray<IVariable*> m_variables;
107 };
108
109 private:
110
111 typedef Array<IVariable*> BaseClass;
112
113 public:
114
115 friend class VariableCollectionEnumerator;
116 typedef VariableCollectionEnumerator Enumerator;
117
118 public:
119
120 VariableCollection();
121 VariableCollection(const Enumerator& rhs);
122
123 public:
124
125 void add(IVariable* var)
126 {
127 _values().add(var);
128 }
129
130 void add(VariableRef& var)
131 {
132 _values().add(var.variable());
133 }
134
135 IVariable* front()
136 {
137 return _values()[0];
138 }
139
140 Integer count() const
141 {
142 return _values().size();
143 }
144
145 //! Applies the functor \a f to all elements in the collection
146 template <class Function> Function
147 each(Function f)
148 {
149 std::for_each(_values().begin(), _values().end(), f);
150 return f;
151 }
152
153 void clear()
154 {
155 _values().clear();
156 }
157
158 bool empty() const
159 {
160 return _values().empty();
161 }
162
163 VariableCollection clone() const
164 {
165 VariableCollection new_collection;
166 new_collection._values().copy(_values());
167 return new_collection;
168 }
169
170 VariableCollectionEnumerator enumerator() const
171 {
172 return VariableCollectionEnumerator(*this);
173 }
174
175 bool contains(IVariable* v) const
176 {
177 return _values().contains(v);
178 }
179
180 bool contains(VariableRef& v) const
181 {
182 return _values().contains(v.variable());
183 }
184
185 //! Sorts the list by ascending or descending variable names
186 void sortByName(bool is_ascendent);
187
188 private:
189
190 const Array<IVariable*>& _values() const { return m_p->variables(); }
191 Array<IVariable*>& _values() { return m_p->variables(); }
192
193 private:
194
195 AutoRefT<Impl> m_p;
196};
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
200
201} // End namespace Arcane
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206#endif
Base class for 1D data vectors.
Constant view of an array of type T.
Interface of a variable.
Definition IVariable.h:40
Implementation of a reference counter using std::atomic.
1D data vector with value semantics (STL style).
Function each(Function f)
Applies the functor f to all elements in the collection.
Reference to a variable.
Definition VariableRef.h:56
IVariable * variable() const
Associated variable.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.