Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AnyItemVariableArray.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/* AnyItemVariableArray.h (C) 2000-2025 */
9/* */
10/* Aggregated variable array of arbitrary types. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ANYITEM_ANYITEMVARIABLEARRAY_H
13#define ARCANE_CORE_ANYITEM_ANYITEMVARIABLEARRAY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Exception.h"
18
19#include "arcane/core/anyitem/AnyItemGlobal.h"
20#include "arcane/core/anyitem/AnyItemGroup.h"
21#include "arcane/core/anyitem/AnyItemLinkFamily.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane::AnyItem
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
55template <typename DataType>
56class VariableArray : public IFamilyObserver
57{
58 public:
59
63 class VariableAdder
64 {
65 public:
66
67 VariableAdder(VariableArray<DataType>& variable, const ItemGroup& group)
68 : m_variable(variable)
69 , m_group(group)
70 , m_used(false)
71 {}
72
73 ~VariableAdder()
74 {
75 ARCANE_ASSERT((m_used == true), ("VariableAdder never used"));
76 }
77
79 template <typename K, typename T>
81 {
82 ARCANE_ASSERT((m_used == false), ("VariableAdder already used"));
83 m_variable._insertVariable(m_group, v.asArray());
84 m_variable._insertInternalVariable(m_group, v.variable());
85 m_used = true;
86 }
87
89 template <typename K, typename T>
91 {
92 ARCANE_ASSERT((m_used == false), ("VariableAdder already used"));
93 m_variable._insertPartialVariable(m_group, v.asArray());
94 m_variable._insertInternalVariable(m_group, v.variable());
95 m_used = true;
96 }
97
98 private:
99
101 VariableArray<DataType>& m_variable;
102
105
107 bool m_used;
108 };
109
110 public:
111
112 VariableArray(const Family& family)
114 , m_values(m_family.groupSize())
115 , m_variables(m_family.groupSize())
116 {
117 // The family registers the carried variables
118 m_family.registerObserver(*this);
119 for (Integer i = 0; i < m_family.groupSize(); ++i)
120 m_variables[i] = NULL;
121 }
122
124 : m_family(v.m_family)
125 , m_values(v.m_values)
127 {
128 // The family registers the carried variables
130 }
131
132 ~VariableArray()
133 {
134 // The family deregisters the variable
136 }
137
140 {
141 return m_values[item.groupIndex()][item.varIndex()];
142 }
143
146 {
147 return m_values[item.groupIndex()][item.varIndex()];
148 }
149
151 inline ArrayView<DataType> operator[](const LinkFamily::LinkData& item)
152 {
153 return m_values[item.groupIndex()][item.varIndex()];
154 }
155
157 inline ConstArrayView<DataType> operator[](const LinkFamily::LinkData& item) const
158 {
159 return m_values[item.groupIndex()][item.varIndex()];
160 }
161
163 inline VariableAdder operator[](const ItemGroup& group)
164 {
165 return VariableAdder(*this, group);
166 }
167
168 template <typename T>
169 inline VariableAdder operator[](const ItemGroupT<T>& group)
170 {
171 return VariableAdder(*this, group);
172 }
173
175 inline const Family& family() const { return m_family; }
176
179 {
180 return m_variables;
181 }
182
185 {
186 return m_values[igrp];
187 }
188
191 {
192 return m_values[igrp];
193 }
194
197 {
198 // If the family changes, we invalidate the variables and resize
199 m_values.resize(m_family.groupSize());
200 m_variables.resize(m_family.groupSize());
201 for (Integer i = 0; i < m_family.groupSize(); ++i)
202 m_variables[i] = NULL;
203 }
204
207 {
208 // If the family is enlarged, we simply resize
209 const Integer old_size = m_values.size();
210 ARCANE_ASSERT((old_size < m_family.groupSize()), ("Old size greater than new size!"));
211 m_values.resize(m_family.groupSize());
212 m_variables.resize(m_family.groupSize());
213 for (Integer i = old_size; i < m_family.groupSize(); ++i)
214 m_variables[i] = NULL;
215 }
216
217 private:
218
219 inline void _insertVariable(ItemGroup group, Array2View<DataType> v)
220 {
221 if (m_family.isPartial(group))
222 throw FatalErrorException(String::format("Group '{0}' defined partial", group.name()));
223 m_values[m_family.groupIndex(group)] = v;
224 }
225
226 inline void _insertPartialVariable(ItemGroup group, Array2View<DataType> v)
227 {
228 if (not m_family.isPartial(group))
229 throw FatalErrorException(String::format("Group '{0}' not defined partial", group.name()));
230 m_values[m_family.groupIndex(group)] = v;
231 }
232
233 inline void _insertInternalVariable(ItemGroup group, IVariable* v)
234 {
235 m_variables[m_family.groupIndex(group)] = v;
236 }
237
238 private:
239
242
245
248};
249
250/*---------------------------------------------------------------------------*/
251/*---------------------------------------------------------------------------*/
252
253} // End namespace Arcane::AnyItem
254
255/*---------------------------------------------------------------------------*/
256/*---------------------------------------------------------------------------*/
257
258#endif
AnyItem family (flyweight pattern) Aggregation of groups to describe variables / partial variables Co...
bool isPartial(const ItemGroup &group) const
Returns true if the group is associated with a partial variable.
void removeObserver(IFamilyObserver &observer) const
Remove an observer.
void registerObserver(IFamilyObserver &observer) const
Register an observer.
Integer groupIndex(const ItemGroup &group) const
Position of the group in the family.
Integer varIndex() const
localId() of the current entity.
Integer groupIndex() const
Index in the current AnyItem::Family group.
AnyItem family observer interface.
Integer groupIndex() const
Identifier of the group associated with the item referenced by this LinkData.
Integer varIndex() const
LocalId identifier of the item referenced in its original IItemFamily.
Tool for adding a variable to a group.
VariableArray< DataType > & m_variable
AnyItem variable.
void operator<<(MeshVariableArrayRefT< K, T > &v)
Binding of a variable.
void operator<<(MeshPartialVariableArrayRefT< K, T > &v)
Binding of a partial variable.
Aggregated variable array of arbitrary types (no variable arrays) WARNING Arcane variables must be re...
const Family m_family
AnyItem family of groups.
ArrayView< DataType > operator[](const LinkFamily::LinkData &item)
Direct accessor by a LinkFamily element (LinkData).
void notifyFamilyIsIncreased()
Notification of family enlargement.
const Family & family() const
Accessor to the family.
ArrayView< DataType > operator[](const Group::BlockItemEnumerator &item)
Direct accessor by an AnyItem enumerator.
ConstArray2View< DataType > valuesAtGroup(const Integer igrp) const
Raw data associated with a group identified relative to its family.
Array2View< DataType > valuesAtGroup(const Integer igrp)
Raw data associated with a group identified relative to its family.
ConstArrayView< DataType > operator[](const Group::BlockItemEnumerator &item) const
Direct accessor by an AnyItem enumerator.
ConstArrayView< IVariable * > variables() const
Array of variables.
void notifyFamilyIsInvalidate()
Notification of family invalidation.
VariableAdder operator[](const ItemGroup &group)
Addition of a variable for a group.
Arcane::UniqueArray< Array2View< DataType > > m_values
Container of generic variables.
Arcane::UniqueArray< IVariable * > m_variables
Container of variables.
ConstArrayView< DataType > operator[](const LinkFamily::LinkData &item) const
Direct accessor by a LinkFamily element (LinkData).
Modifiable view of an array of type T.
Constant view of an array of type T.
Reference to a group of a given kind.
Definition ItemGroup.h:420
Mesh entity group.
Definition ItemGroup.h:51
const String & name() const
Group name.
Definition ItemGroup.h:81
Scalar partial variable on a mesh entity type.
Array variable on a mesh entity type.
1D data vector with value semantics (STL style).
IVariable * variable() const
Associated variable.
Int32 Integer
Type representing an integer.
void arcaneCallFunctionAndTerminateIfThrow(std::function< void()> function)
Calls the function function and calls std::terminate() if an exception occurs.