Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
CaseOptionSimple.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/* CaseOptionSimple.h (C) 2000-2025 */
9/* */
10/* Simple data set option. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CASEOPTIONSIMPLE_H
13#define ARCANE_CASEOPTIONSIMPLE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/core/ICaseOptions.h"
18#include "arcane/core/CaseOptionBase.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33#ifdef ARCANE_CHECK
34#define ARCANE_CASEOPTION_CHECK_IS_INITIALIZED _checkIsInitialized()
35#else
36#define ARCANE_CASEOPTION_CHECK_IS_INITIALIZED
37#endif
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42template <typename Type>
44{
45 public:
46
47 using ContainerType = Type;
48 using ReferenceType = Type&;
49 using ConstReferenceType = const Type&;
50 using ArrayViewType = ArrayView<Type>;
51 using ConstArrayViewType = ConstArrayView<Type>;
52};
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57/*!
58 * \brief Specialization for 'Array' options.
59 *
60 * This is necessary because the 'Array' class cannot be instantiated. For
61 * this type of option, it is forbidden to modify the option values, so
62 * the views must necessarily be constant.
63 */
64template <typename Type>
66{
67 public:
68
69 using ContainerType = UniqueArray<Type>;
70 using ReferenceType = const Array<Type>&;
71 using ConstReferenceType = const Array<Type>&;
72 using ArrayViewType = ConstArrayView<ContainerType>;
73 using ConstArrayViewType = ConstArrayView<ContainerType>;
74};
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
79/*!
80 * \brief Base class for simple options (single value).
81 * \ingroup CaseOption
82 */
83class ARCANE_CORE_EXPORT CaseOptionSimple
84: public CaseOptionBase
85{
86 public:
87
88 explicit CaseOptionSimple(const CaseOptionBuildInfo& cob);
89 CaseOptionSimple(const CaseOptionBuildInfo& cob, const String& physical_unit);
90 ~CaseOptionSimple();
91
92 public:
93
94 //! Returns \a true if the option is present
95 bool isPresent() const { return !m_element.null(); }
96
97 /*!
98 * \brief Returns the element of the option.
99 *
100 * \deprecated The internal implementation should not be used to allow
101 * for the use of a format other than XML in the future.
102 */
103 ARCANE_DEPRECATED_LONG_TERM("Y2022: Do not access XML item from option")
104 XmlNode element() const { return m_element; }
105
106 /*!
107 * \brief Function associated with this option (0 if none).
108 *
109 * If a function is associated with the option, the values of this
110 * latter are recalculated automatically at each iteration.
111 */
112 ICaseFunction* function() const override { return m_function; }
113 /*!
114 * \brief Standard function associated with this option (0 if none).
115 *
116 * A standard function has a specific prototype and can be called
117 * directly. Unlike function(), the presence of a standard function
118 * does not change the value of the option.
119 */
120 virtual IStandardFunction* standardFunction() const { return m_standard_function; }
121 /*!
122 * \brief Indicates if the value has changed since the last iteration.
123 *
124 * The value can only change if a function is associated with the option.
125 * The method returns true if the option's value is different from the
126 * previous iteration. This method also works in case of rollback.
127 */
128 bool hasChangedSinceLastIteration() const;
129 //! Full name in the format provided by the XPath standard.
130 String xpathFullName() const;
131 /*!
132 * \brief Default physical unit for this option (null if no unit),
133 * specified in the .axl file.
134 */
135 String defaultPhysicalUnit() const;
136 //! Physical unit specified in the data set (null if no unit)
137 String physicalUnit() const;
138 /*!
139 * \brief Physical unit converter.
140 *
141 * This converter only exists for 'Real' or 'RealArray' type options.
142 * It is null if the option does not have a unit.
143 */
144 IPhysicalUnitConverter* physicalUnitConverter() const { return m_unit_converter; }
145 /*!
146 * \brief Indicates if the option is optional.
147 *
148 * If an optional option is not provided,
149 * its value is undefined and should therefore not be used.
150 */
151 bool isOptional() const { return m_is_optional; }
152
153 /*!
154 * \brief Indicates if the option has an invalid value.
155 *
156 * This is always the case, unless the option is optional
157 * (isOptional()==true) and not provided.
158 */
159 bool hasValidValue() const { return m_has_valid_value; }
160
161 void visit(ICaseDocumentVisitor* visitor) const override;
162
163 protected:
164
165 void _search(bool is_phase1) override;
166 virtual bool _allowPhysicalUnit() = 0;
167 void _setChangedSinceLastIteration(bool has_changed);
168 void _searchFunction(XmlNode& velem);
169 void _setPhysicalUnit(const String& value);
170 void _setHasValidValue(bool v) { m_has_valid_value = v; }
171 XmlNode _element() const { return m_element; }
172
173 static String _convertFunctionRealToString(ICaseFunction* func, Real t);
174 static String _convertFunctionIntegerToString(ICaseFunction* func, Integer t);
175
176 private:
177
178 XmlNode m_element; //!< Option element
179 ICaseFunction* m_function = nullptr; //!< Associated function (or nullptr)
180 IStandardFunction* m_standard_function = nullptr; //!< Associated standard function (or nullpt)
181 //! Unit converter (nullptr if not needed). Valid only for 'Real' options
182 IPhysicalUnitConverter* m_unit_converter = nullptr;
183 bool m_changed_since_last_iteration = false;
184 bool m_is_optional = false;
185 bool m_has_valid_value = false;
186 String m_default_physical_unit;
187 String m_physical_unit;
188};
189
190/*---------------------------------------------------------------------------*/
191/*---------------------------------------------------------------------------*/
192
193/*!
194 * \ingroup CaseOption
195 * \brief Simple data set option (real, integer, boolean, ...).
196 *
197 * The most used method of this class is the operator()()
198 * which allows retrieving the option's value. If a function (ICaseFunction)
199 * is associated with the option, it is possible to retrieve the option's value
200 * at the physical time or at the iteration passed as a parameter to the method
201 * valueAtParameter().
202 * \code
203 * CaseOptionSimpleT<Real> real_option;
204 * Real v = real_option(); // uses operator()
205 * Real v = real_option; // uses implicit cast operator
206 * Real v = real_option.valueAtParameter(0.3); // value at physical time 0.3
207 * \endcode
208 */
209template <class T>
210class CaseOptionSimpleT
211: public CaseOptionSimple
212{
213 public:
214
215 typedef CaseOptionSimpleT<T> ThatClass;
216#ifndef SWIG
217 typedef typename CaseOptionTraitsT<T>::ContainerType Type; //!< Option type
218#else
219 typedef T Type; //!< Option type
220#endif
221 public:
222
223 ARCANE_CORE_EXPORT CaseOptionSimpleT(const CaseOptionBuildInfo& cob);
224 ARCANE_CORE_EXPORT CaseOptionSimpleT(const CaseOptionBuildInfo& cob, const String& physical_unit);
225
226 public:
227
228 ARCANE_CORE_EXPORT virtual void print(const String& lang, std::ostream& o) const;
229
230 //! Returns the value of the option
231 const Type& value() const
232 {
233 ARCANE_CASEOPTION_CHECK_IS_INITIALIZED;
234 return m_value;
235 }
236
237 //! Value of the option
238 operator const Type&() const { return value(); }
239
240 //! Returns the value of the option for the real parameter t.
241 ARCANE_CORE_EXPORT Type valueAtParameter(Real t) const;
242
243 //! Returns the value of the option for the integer parameter t.
244 ARCANE_CORE_EXPORT Type valueAtParameter(Integer t) const;
245
246 //! Returns the value of the option
247 //const Type& operator()() const { return value(); }
248 const Type& operator()() const { return value(); }
249
250#ifdef ARCANE_DOTNET
251 operator ThatClass*() { return this; }
252 operator const ThatClass*() const { return this; }
253 const ThatClass* operator->() const { return this; }
254 static Real castTo__Arcane_Real(const ThatClass& v)
255 {
256 return (Real)(v);
257 }
258#endif
259
260 //! Returns the value of the option for the real parameter t.
261 ARCANE_DEPRECATED Type operator()(Real t) const
262 {
263 return valueAtParameter(t);
264 }
265
266 //! Returns the value of the option for the integer parameter t.
267 ARCANE_DEPRECATED Type operator()(Integer t) const
268 {
269 return valueAtParameter(t);
270 }
271
272 /*!
273 * For internal use only
274 * \internal
275 */
276 ARCANE_CORE_EXPORT virtual void updateFromFunction(Real current_time, Integer current_iteration);
277
278 /*!
279 * \brief Sets the default value of the option.
280 *
281 * If the option is not present in the data set, its value will be
282 * that specified by the argument \a def_value; otherwise, calling this
283 * method has no effect.
284 */
285 ARCANE_CORE_EXPORT void setDefaultValue(const Type& def_value);
286
287 //! Returns the value of the option if isPresent()==true or otherwise \a arg_value
288 const Type& valueIfPresentOrArgument(const Type& arg_value)
289 {
290 ARCANE_CASEOPTION_CHECK_IS_INITIALIZED;
291 return isPresent() ? m_value : arg_value;
292 }
293
294 protected:
295
296 ARCANE_CORE_EXPORT virtual void _search(bool is_phase1);
297 ARCANE_CORE_EXPORT virtual bool _allowPhysicalUnit();
298
299 private:
300
301 Type m_value; //!< Option value
302};
303
304/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
307class ARCANE_CORE_EXPORT CaseOptionMultiSimple
308: public CaseOptionBase
309{
310 public:
311
312 CaseOptionMultiSimple(const CaseOptionBuildInfo& cob)
313 : CaseOptionBase(cob)
314 {}
315};
316
317/*---------------------------------------------------------------------------*/
318/*---------------------------------------------------------------------------*/
319
320/*!
321 * \ingroup CaseOption
322 * \brief Data set option of simple type list (real, integer, boolean, ...).
323 *
324 * \warning Using the base class `ArrayView<T>` is obsolete and
325 * should no longer be used. The view() method allows retrieving a view on the
326 * option values.
327 */
328#ifndef SWIG
329template <class T>
330class CaseOptionMultiSimpleT
331: public CaseOptionMultiSimple
332#ifdef ARCANE_HAS_PRIVATE_CASEOPTIONSMULTISIMPLE_BASE_CLASS
333, private ArrayView<T>
334#else
335, public ArrayView<T>
336#endif
337{
338 public:
339
340 //! Type of the option value
341 using Type = typename CaseOptionTraitsT<T>::ContainerType;
342 using ReferenceType = typename CaseOptionTraitsT<T>::ReferenceType;
343 using ConstReferenceType = typename CaseOptionTraitsT<T>::ConstReferenceType;
344 //! Type of the view on the option values
345 using ArrayViewType = typename CaseOptionTraitsT<T>::ArrayViewType;
346 //! Type of the constant view on the option values
347 using ConstArrayViewType = typename CaseOptionTraitsT<T>::ConstArrayViewType;
348
349 public:
350
351 ARCANE_CORE_EXPORT CaseOptionMultiSimpleT(const CaseOptionBuildInfo& cob);
352 ARCANE_CORE_EXPORT CaseOptionMultiSimpleT(const CaseOptionBuildInfo& cob, const String& physical_unit);
353 ARCANE_CORE_EXPORT ~CaseOptionMultiSimpleT();
354
355 public:
356
357 ARCCORE_DEPRECATED_2021("Use view() instead")
358 ArrayView<T> operator()()
359 {
360 return *this;
361 }
362 ARCCORE_DEPRECATED_2021("Use view() instead")
363 const ArrayView<T> operator()() const
364 {
365 return *this;
366 }
367
368 //! Conversion to the constant view
369 ARCCORE_DEPRECATED_2021("Use view() instead")
370 operator ArrayView<T>()
371 {
372 ArrayView<T>* v = this;
373 return *v;
374 }
375
376 //! Conversion to the constant view
377 ARCCORE_DEPRECATED_2021("Use view() instead")
378 operator ConstArrayView<T>() const
379 {
380 const ArrayView<T>* v = this;
381 return *v;
382 }
383
384 //! Constant view on the option elements
386 {
387 return m_view;
388 }
389 //! View on the option elements
391 {
392 return m_view;
393 }
394
395 ConstReferenceType operator[](Integer i) const { return m_view[i]; }
396 ReferenceType operator[](Integer i) { return m_view[i]; }
397
398 public:
399
400 ARCANE_CORE_EXPORT void print(const String& lang, std::ostream& o) const override;
401 ICaseFunction* function() const override { return 0; }
402 void updateFromFunction(Real, Integer) override {}
403
404 ConstArrayView<T> values() const
405 {
406 const ArrayView<T>* v = this;
407 return *v;
408 }
409 const T& value(Integer index) const { return this->operator[](index); }
410 Integer size() const { return ArrayView<T>::size(); }
411 ARCANE_CORE_EXPORT void visit(ICaseDocumentVisitor* visitor) const override;
412 bool isPresent() const { return !m_view.empty(); }
413
414 protected:
415
416 void _search(bool is_phase1) override;
417 virtual bool _allowPhysicalUnit();
418
419 private:
420
421 ArrayViewType m_view;
422};
423#endif
424
425/*---------------------------------------------------------------------------*/
426/*---------------------------------------------------------------------------*/
427
428typedef CaseOptionSimpleT<Real> CaseOptionReal;
429typedef CaseOptionSimpleT<Real2> CaseOptionReal2;
430typedef CaseOptionSimpleT<Real3> CaseOptionReal3;
431typedef CaseOptionSimpleT<Real2x2> CaseOptionReal2x2;
432typedef CaseOptionSimpleT<Real3x3> CaseOptionReal3x3;
433typedef CaseOptionSimpleT<bool> CaseOptionBool;
434typedef CaseOptionSimpleT<Integer> CaseOptionInteger;
435typedef CaseOptionSimpleT<Int32> CaseOptionInt32;
436typedef CaseOptionSimpleT<Int64> CaseOptionInt64;
437typedef CaseOptionSimpleT<String> CaseOptionString;
438
439typedef CaseOptionSimpleT<RealArray> CaseOptionRealArray;
440typedef CaseOptionSimpleT<Real2Array> CaseOptionReal2Array;
441typedef CaseOptionSimpleT<Real3Array> CaseOptionReal3Array;
442typedef CaseOptionSimpleT<Real2x2Array> CaseOptionReal2x2Array;
443typedef CaseOptionSimpleT<Real3x3Array> CaseOptionReal3x3Array;
444typedef CaseOptionSimpleT<BoolArray> CaseOptionBoolArray;
445typedef CaseOptionSimpleT<IntegerArray> CaseOptionIntegerArray;
446typedef CaseOptionSimpleT<Int32Array> CaseOptionInt32Array;
447typedef CaseOptionSimpleT<Int64Array> CaseOptionInt64Array;
448typedef CaseOptionSimpleT<StringArray> CaseOptionStringArray;
449
450/*---------------------------------------------------------------------------*/
451/*---------------------------------------------------------------------------*/
452
453} // End namespace Arcane
454
455/*---------------------------------------------------------------------------*/
456/*---------------------------------------------------------------------------*/
457
458#endif
Modifiable view of an array of type T.
constexpr ArrayView() noexcept
Constructs an empty view.
constexpr Integer size() const noexcept
Returns the size of the array.
Base class for 1D data vectors.
typename CaseOptionTraitsT< T >::ConstArrayViewType ConstArrayViewType
Type of the constant view on the option values.
ConstArrayViewType view() const
Constant view on the option elements.
typename CaseOptionTraitsT< T >::ContainerType Type
Type of the option value.
typename CaseOptionTraitsT< T >::ArrayViewType ArrayViewType
Type of the view on the option values.
void updateFromFunction(Real, Integer) override
Updates the option value from a function.
void print(const String &lang, std::ostream &o) const override
Prints the option value in the language lang, to the stream o.
ICaseFunction * function() const override
Returns the function linked to this option or nullptr if none exists.
void visit(ICaseDocumentVisitor *visitor) const override
Applies the visitor to this option.
void _search(bool is_phase1) override
Searches for the option value in the dataset.
ArrayViewType view()
View on the option elements.
Simple data set option (real, integer, boolean, ...).
const Type & valueIfPresentOrArgument(const Type &arg_value)
Returns the value of the option if isPresent()==true or otherwise arg_value.
virtual void _search(bool is_phase1)
Searches for the option value in the data set.
ARCANE_DEPRECATED Type operator()(Real t) const
Returns the value of the option for the real parameter t.
Type valueAtParameter(Real t) const
Returns the value of the option for the real parameter t.
CaseOptionTraitsT< T >::ContainerType Type
Option type.
virtual void print(const String &lang, std::ostream &o) const
Prints the option value in the language lang, to the stream o.
Type valueAtParameter(Integer t) const
Returns the value of the option for the integer parameter t.
const Type & value() const
Returns the value of the option.
const Type & operator()() const
Returns the value of the option.
ARCANE_DEPRECATED Type operator()(Integer t) const
Returns the value of the option for the integer parameter t.
virtual void updateFromFunction(Real current_time, Integer current_iteration)
void setDefaultValue(const Type &def_value)
Sets the default value of the option.
bool hasValidValue() const
Indicates if the option has an invalid value.
ICaseFunction * function() const override
Function associated with this option (0 if none).
bool isOptional() const
Indicates if the option is optional.
virtual IStandardFunction * standardFunction() const
Standard function associated with this option (0 if none).
ARCANE_DEPRECATED_LONG_TERM("Y2022: Do not access XML item from option") XmlNode element() const
Returns the element of the option.
IPhysicalUnitConverter * physicalUnitConverter() const
Physical unit converter.
bool isPresent() const
Returns true if the option is present.
Constant view of an array of type T.
Visitor interface for a dataset option.
Interface managing a standard function.
1D data vector with value semantics (STL style).
Node of a DOM tree.
Definition XmlNode.h:51
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.