Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
VariableRef.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/* VariableRef.h (C) 2000-2025 */
9/* */
10/* Class managing a reference to a variable. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_VARIABLEREF_H
13#define ARCANE_CORE_VARIABLEREF_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/String.h"
18
19#include "arcane/core/IVariable.h"
20#include "arcane/core/VariableComputeFunction.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31class IModule;
34typedef VariableBuildInfo VariableBuilder;
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39/*!
40 * \ingroup Variable
41 * \brief Reference to a variable.
42 *
43 * This class manages a reference to a variable (IVariable).
44 *
45 * If the variable is not associated with a module, the module() method returns 0.
46 *
47 * This class must be derived.
48 *
49 * The most derived class of this class must call _internalInit()
50 * in its constructor. Only it must do so, and in the constructor
51 * to ensure that the reference to the variable is valid as soon as
52 * the object is constructed and that the virtual methods that must be called
53 * during this initialization correspond to the instance being created.
54 */
55class ARCANE_CORE_EXPORT VariableRef
56{
57 public:
58
60 friend class UpdateNotifyFunctorList;
61
62 protected:
63
64 //! Constructs a reference to a variable with the infos \a vbi
65 explicit VariableRef(const VariableBuildInfo& vbi);
66 //! Copy constructor
67 VariableRef(const VariableRef& from);
68 //! Constructs a reference to a variable \a var
69 explicit VariableRef(IVariable* var);
70 //! Copy assignment operator
71 VariableRef& operator=(const VariableRef& from);
72 //! Default constructor
74
75 public:
76
77 //! Releases resources
78 virtual ~VariableRef();
79
80 public:
81
82 //! Sub-domain associated with the variable (TODO deprecate end of 2023)
83 ISubDomain* subDomain() const;
84
85 public:
86
87 //! Variable manager associated with the variable.
89
90 //! Variable name
91 String name() const;
92
93 public:
94
95 //TODO Remove virtual
96 //! Variable type (Real, Integer, ...)
97 virtual eDataType dataType() const;
98
99 //! Prints the variable value
100 virtual void print(std::ostream& o) const;
101
102 //TODO Remove virtual
103 //! Module associated with the variable (or nullptr, if none)
104 virtual IModule* module() const { return m_module; }
105
106 //TODO Remove virtual
107 //! Variable properties
108 virtual int property() const;
109
110 //! Reference properties (internal)
111 virtual int referenceProperty() const;
112
113 //! Sets the property \a property
114 virtual void setProperty(int property);
115
116 //! Unsets the property \a property
117 virtual void unsetProperty(int property);
118
119 //! Registers the variable (internal)
120 virtual void registerVariable();
121
122 //! Unregisters the variable (internal)
123 virtual void unregisterVariable();
124
125 //! Associated variable
126 IVariable* variable() const { return m_variable; }
127
128 /*! \brief Checks if the variable is synchronized.
129 * \sa IVariable::checkIfSync()
130 */
131 virtual Integer checkIfSync(int max_print = 0);
132
133 /*!
134 * \brief Checks if the variable has the same values on all replicas.
135 * \sa IVariable::checkIfSameOnAllReplica()
136 */
137 virtual Integer checkIfSameOnAllReplica(int max_print = 0);
138
139 //! Updates from the internal part
140 virtual void updateFromInternal();
141
142 //! If the variable is an array, returns its dimension, otherwise returns 0
143 virtual Integer arraySize() const { return 0; }
144
145 public:
146
147 void setUsed(bool v) { m_variable->setUsed(v); }
148 bool isUsed() const { return m_variable->isUsed(); }
149
150 virtual void internalSetUsed(bool /*v*/) {}
151
152 public:
153
154 /*!
155 * \brief Call stack at the time of assigning this instance.
156 *
157 * The stack is only accessible in verification or debug mode. If
158 * not, it returns a null string.
159 */
160 const String& assignmentStackTrace() const { return m_assignment_stack_trace; }
161
162 public:
163
164 //@{ @name Tag Management
165 //! Adds the tag \a tagname with the value \a tagvalue
166 void addTag(const String& tagname, const String& tagvalue);
167 /*! \brief Removes the tag \a tagname
168 *
169 * If the tag \a tagname is not in the list, nothing happens.
170 */
171 void removeTag(const String& tagname);
172 //! \a true if the variable has the tag \a tagname
173 bool hasTag(const String& tagname) const;
174 //! Value of the tag \a tagname. The string is null if the tag does not exist.
175 String tagValue(const String& tagname) const;
176 //@}
177
178 public:
179
180 /*!
181 * \name Dependency Management
182 *
183 * Operations related to variable dependency management.
184 */
185 //@{
186 /*! \brief Recalculates the variable if necessary
187 *
188 * Through the dependency mechanism, this operation is called recursively
189 * on all variables that the instance depends on. The recalculation function
190 * computeFunction() is then called if it turns out that one of the variables
191 * it depends on has been modified more recently.
192 */
193 void update();
194
195 /*! \brief Indicates that the variable has just been updated.
196 *
197 * For correct dependency management, this property
198 * must be called every time a variable update has been performed.
199 */
200 void setUpToDate();
201
202 //! Time when the variable was updated
203 Int64 modifiedTime();
204
205 //! Adds \a var to the dependency list at the current time
206 void addDependCurrentTime(const VariableRef& var);
207
208 //! Adds \a var to the dependency list at the current time with trace info \a tinfo
209 void addDependCurrentTime(const VariableRef& var, const TraceInfo& tinfo);
210
211 //! Adds \a var to the dependency list at the previous time
212 void addDependPreviousTime(const VariableRef& var);
213
214 //! Adds \a var to the dependency list at the previous time with trace info \a tinfo
215 void addDependPreviousTime(const VariableRef& var, const TraceInfo& tinfo);
216
217 /*! \brief Removes \a var from the dependency list
218 */
219 void removeDepend(const VariableRef& var);
220
221 /*!
222 * \brief Sets the variable's recalculation function.
223 *
224 * If a recalculation function already existed, it is destroyed
225 * and replaced by this one.
226 */
227 template <typename ClassType> void
228 setComputeFunction(ClassType* instance, void (ClassType::*func)())
229 {
230 _setComputeFunction(new VariableComputeFunction(instance, func));
231 }
232
233 /*!
234 * \brief Sets the variable's recalculation function.
235 *
236 * If a recalculation function already existed, it is destroyed
237 * and replaced by this one.
238 * \a tinfo contains the information allowing to know where the function is defined (for debugging)
239 */
240 template <typename ClassType> void
241 setComputeFunction(ClassType* instance, void (ClassType::*func)(), const TraceInfo& tinfo)
242 {
243 _setComputeFunction(new VariableComputeFunction(instance, func, tinfo));
244 }
245 //@}
246
247 public:
248
249 //! Previous reference (or null) to variable()
250 VariableRef* previousReference();
251
252 //! Next reference (or null) to variable()
253 VariableRef* nextReference();
254
255 /*!
256 * \internal
257 * \brief Sets the previous reference.
258 *
259 * For internal use only.
260 */
261 void setPreviousReference(VariableRef* v);
262
263 /*!
264 * \internal
265 * \brief Sets the next reference.
266 *
267 * For internal use only.
268 */
269 void setNextReference(VariableRef* v);
270
271 public:
272
273 static void setTraceCreation(bool v);
274 static bool hasTraceCreation();
275
276 protected:
277
278 void _setComputeFunction(IVariableComputeFunction* v);
279
280 /*!
281 * \brief Internal initialization of the variable.
282 *
283 * \warning This method must <strong >obligatorily</strong > be
284 * called in the derived class constructor
285 * before any use of the reference.
286 */
287 void _internalInit(IVariable*);
288
289 /*!
290 * \brief Referenced variable.
291 *
292 * This method checks that a variable is properly referenced.
293 */
295 {
296 _checkValid();
297 return m_variable;
298 }
299
300 private:
301
302 //! Associated variable
303 IVariable* m_variable = nullptr;
304
305 //! Associated module (or 0 if none)
306 IModule* m_module = nullptr;
307
308 //! \a true if the variable has been registered
309 bool m_is_registered = false;
310
311 //! Reference properties
312 int m_reference_property = 0;
313
314 //! Previous reference on \a m_variable
315 VariableRef* m_previous_reference = nullptr;
316
317 //! Next reference on \a m_variable
318 VariableRef* m_next_reference = nullptr;
319
320 /*!
321 * \brief Call stack during variable assignment.
322 *
323 * Used only when traces are active.
324 */
325 String m_assignment_stack_trace;
326
327 protected:
328
329 void _executeUpdateFunctors();
330
331 bool m_has_trace = false;
332
333 private:
334
335 void _checkValid() const
336 {
337#ifdef ARCANE_CHECK
338 if (!m_variable)
339 _throwInvalid();
340#endif
341 }
342 void _throwInvalid() const;
343 bool _checkValidPropertyChanged(int property);
344 void _setAssignmentStackTrace();
345
346 protected:
347
348 void _internalAssignVariable(const VariableRef& var);
349
350 private:
351
352 static bool m_static_has_trace_creation;
353 UpdateNotifyFunctorList* m_notify_functor_list = nullptr;
354};
355
356/*---------------------------------------------------------------------------*/
357/*---------------------------------------------------------------------------*/
358
359} // namespace Arcane
360
361/*---------------------------------------------------------------------------*/
362/*---------------------------------------------------------------------------*/
363
364#endif
365
366//TODO: to be removed when all codes include this file directly
367#include "arcane/core/VariableList.h"
Interface of a module.
Definition IModule.h:40
Interface of the subdomain manager.
Definition ISubDomain.h:75
Interface of the functor class for recalculating a variable.
Variable manager interface.
Interface of a variable.
Definition IVariable.h:40
Parameters necessary for building a variable.
Interface of the variable recalculation functor class.
Internal class to manage functors called when the variable is updated.
Reference to a variable.
Definition VariableRef.h:56
VariableRef(const VariableBuildInfo &vbi)
Constructs a reference to a variable with the infos vbi.
VariableRef & operator=(const VariableRef &from)
Copy assignment operator.
virtual eDataType dataType() const
Variable type (Real, Integer, ...).
virtual void print(std::ostream &o) const
Prints the variable value.
virtual IModule * module() const
Module associated with the variable (or nullptr, if none).
void setComputeFunction(ClassType *instance, void(ClassType::*func)(), const TraceInfo &tinfo)
Sets the variable's recalculation function.
IVariable * variable() const
Associated variable.
String name() const
Variable name.
const String & assignmentStackTrace() const
Call stack at the time of assigning this instance.
ISubDomain * subDomain() const
Sub-domain associated with the variable (TODO deprecate end of 2023).
virtual Integer arraySize() const
If the variable is an array, returns its dimension, otherwise returns 0.
void setComputeFunction(ClassType *instance, void(ClassType::*func)())
Sets the variable's recalculation function.
IVariableMng * variableMng() const
Variable manager associated with the variable.
IVariable * _variable() const
Referenced variable.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
eDataType
Data type.
Definition DataTypes.h:41