Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AnyItemVariable.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/* AnyItemVariable.h (C) 2000-2025 */
9/* */
10/* Aggregated variable of arbitrary types. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ANYITEM_ANYITEMVARIABLE_H
13#define ARCANE_CORE_ANYITEM_ANYITEMVARIABLE_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"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane::AnyItem
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
53template <typename DataType>
54class Variable : public IFamilyObserver
55{
56 public:
57
61 class VariableAdder
62 {
63 public:
64
65 VariableAdder(Variable<DataType>& variable, const ItemGroup& group)
66 : m_variable(variable)
67 , m_group(group)
68 , m_used(false)
69 {}
70
71 ~VariableAdder()
72 {
73 ARCANE_ASSERT((m_used == true), ("VariableAdder never used"));
74 }
75
77 template <typename K, typename T>
79 {
80 ARCANE_ASSERT((m_used == false), ("VariableAdder already used"));
81 m_variable._insertVariable(m_group, v.asArray());
82 m_variable._insertInternalVariable(m_group, v.variable());
83 m_used = true;
84 }
85
87 template <typename K, typename T>
89 {
90 ARCANE_ASSERT((m_used == false), ("VariableAdder already used"));
91 m_variable._insertPartialVariable(m_group, v.asArray());
92 m_variable._insertInternalVariable(m_group, v.variable());
93 m_used = true;
94 }
95
96 private:
97
99 Variable<DataType>& m_variable;
100
103
105 bool m_used;
106 };
107
108 public:
109
110 Variable(const Family& family)
112 , m_values(m_family.groupSize())
113 , m_variables(m_family.groupSize())
114 {
115 // The family registers the carried variables
116 m_family.registerObserver(*this);
117 for (Integer i = 0; i < m_family.groupSize(); ++i)
118 m_variables[i] = NULL;
119 }
120
121 Variable(const Variable& v)
122 : m_family(v.m_family)
123 , m_values(v.m_values)
125 {
126 // The family registers the carried variables
128 }
129
130 ~Variable()
131 {
132 // The family deregisters the variable
134 }
135
137 inline DataType& operator[](const Group::BlockItemEnumerator& item)
138 {
139 return m_values[item.groupIndex()][item.varIndex()];
140 }
141
143 inline const DataType& operator[](const Group::BlockItemEnumerator& item) const
144 {
145 return m_values[item.groupIndex()][item.varIndex()];
146 }
147
149 inline DataType& operator[](const LinkFamily::LinkData& item)
150 {
151 return m_values[item.groupIndex()][item.varIndex()];
152 }
153
155 inline const DataType& operator[](const LinkFamily::LinkData& item) const
156 {
157 return m_values[item.groupIndex()][item.varIndex()];
158 }
159
161 inline VariableAdder operator[](const ItemGroup& group)
162 {
163 return VariableAdder(*this, group);
164 }
165
166 template <typename T>
167 inline VariableAdder operator[](const ItemGroupT<T>& group)
168 {
169 return VariableAdder(*this, group);
170 }
171
173 inline const Family& family() const { return m_family; }
174
177 {
178 return m_variables;
179 }
180
183 {
184 return m_values[igrp];
185 }
186
189 {
190 return m_values[igrp];
191 }
192
195 {
196 // If the family changes, we invalidate the variables and resize
197 m_values.resize(m_family.groupSize());
198 m_variables.resize(m_family.groupSize());
199 for (Integer i = 0; i < m_family.groupSize(); ++i)
200 m_variables[i] = NULL;
201 }
202
205 {
206 // If the family is enlarged, we simply resize
207 const Integer old_size = m_values.size();
208 ARCANE_ASSERT((old_size < m_family.groupSize()), ("Old size greater than new size!"));
209 m_values.resize(m_family.groupSize());
210 m_variables.resize(m_family.groupSize());
211 for (Integer i = old_size; i < m_family.groupSize(); ++i)
212 m_variables[i] = NULL;
213 }
214
215 private:
216
217 inline void _insertVariable(ItemGroup group, ArrayView<DataType> v)
218 {
219 if (m_family.isPartial(group))
220 throw FatalErrorException(String::format("Group '{0}' defined partial", group.name()));
221 m_values[m_family.groupIndex(group)] = v;
222 }
223
224 inline void _insertPartialVariable(ItemGroup group, ArrayView<DataType> v)
225 {
226 if (not m_family.isPartial(group))
227 throw FatalErrorException(String::format("Group '{0}' not defined partial", group.name()));
228 m_values[m_family.groupIndex(group)] = v;
229 }
230
231 inline void _insertInternalVariable(ItemGroup group, IVariable* v)
232 {
233 m_variables[m_family.groupIndex(group)] = v;
234 }
235
236 private:
237
240
243
246};
247
248/*---------------------------------------------------------------------------*/
249/*---------------------------------------------------------------------------*/
250
251} // End namespace Arcane::AnyItem
252
253/*---------------------------------------------------------------------------*/
254/*---------------------------------------------------------------------------*/
255
256#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.
Variable< DataType > & m_variable
AnyItem Variable.
void operator<<(MeshVariableScalarRefT< K, T > &v)
Binding of a variable.
void operator<<(MeshPartialVariableScalarRefT< K, T > &v)
Binding of a partial variable.
const ItemGroup & m_group
Variable group.
bool m_used
Indicator of Adder usage.
Aggregated variable of arbitrary types (no array variables).
VariableAdder operator[](const ItemGroup &group)
Adding a variable for a group.
Arcane::UniqueArray< ArrayView< DataType > > m_values
Container of generic variables.
Arcane::UniqueArray< IVariable * > m_variables
Container of variables.
void notifyFamilyIsInvalidate()
Notification of family invalidation.
ArrayView< DataType > valuesAtGroup(const Integer igrp)
Raw data associated with a group identified relative to its family.
ConstArrayView< IVariable * > variables() const
Array of variables.
const DataType & operator[](const Group::BlockItemEnumerator &item) const
Direct accessor by an AnyItem enumerator.
ConstArrayView< DataType > valuesAtGroup(const Integer igrp) const
Raw data associated with a group identified relative to its family.
const DataType & operator[](const LinkFamily::LinkData &item) const
Direct accessor by a LinkFamily element (LinkData).
DataType & operator[](const LinkFamily::LinkData &item)
Direct accessor by a LinkFamily element (LinkData).
const Family & family() const
Accessor to the family.
void notifyFamilyIsIncreased()
Notification of family enlargement.
DataType & operator[](const Group::BlockItemEnumerator &item)
Direct accessor by an AnyItem enumerator.
const Family m_family
AnyItem Family of groups.
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 variable on a mesh entity type.
Scalar variable on a mesh entity type.
1D data vector with value semantics (STL style).
IVariable * variable() const
Associated variable.
Instance of a variable.
Definition Variable.h:77
Int32 Integer
Type representing an integer.
void arcaneCallFunctionAndTerminateIfThrow(std::function< void()> function)
Calls the function function and calls std::terminate() if an exception occurs.