Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
CaseOptionEnum.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/* CaseOptionEnum.h (C) 2000-2023 */
9/* */
10/* Enumerated data set option. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CASEOPTIONENUM_H
13#define ARCANE_CASEOPTIONENUM_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/core/CaseOptionSimple.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28/*!
29 * \internal
30 * \brief Name of a data set option.
31 * This class allows storing the name of an option in multiple languages.
32 */
33class ARCANE_CORE_EXPORT CaseOptionName
34{
35 public:
36
37 //! Constructs a name option \a true_name
38 CaseOptionName(const String& true_name);
39 //! Copy constructor
41 //! Releases resources
42 virtual ~CaseOptionName();
43
44 public:
45
46 /*! \brief returns the name of the option in the language \a lang.
47 * If no translation is available in the language \a lang,
48 * trueName() is returned.
49 */
50 String name(const String& lang) const;
51 //! Returns the true name (non-translated) of the option
52 String trueName() const { return m_true_name; }
53 /*!
54 \brief Adds a translation for the option name.
55 Adds the name \a tname corresponding to the language \a lang.
56 If a translation already exists for this language, it is replaced by
57 this one.
58 \param tname translation of the name
59 \param lang language of the translation
60 */
61 void addAlternativeNodeName(const String& lang, const String& tname);
62
63 private:
64
65 String m_true_name; //!< Option name
66 StringDictionary* m_translations; //!< Translations.
67};
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
71
72/*!
73 * \internal
74 * \brief Name and value of a data set enumeration.
75 */
76class ARCANE_CORE_EXPORT CaseOptionEnumValue
77: public CaseOptionName
78{
79 public:
80
81 CaseOptionEnumValue(const String& name, int value);
82 //! Copy constructor
83 CaseOptionEnumValue(const CaseOptionEnumValue& rhs);
84 ~CaseOptionEnumValue();
85
86 public:
87
88 int value() const { return m_value; }
89
90 private:
91
92 int m_value;
93};
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
98/*!
99 * \internal
100 * \brief Set of values for an enumeration.
101 */
102class ARCANE_CORE_EXPORT CaseOptionEnumValues
103{
104 public:
105
106 //! Type of the value list
108
109 public:
110
111 //! Constructs the instance
113 ~CaseOptionEnumValues(); //!< Releases resources
114
115 public:
116
117 /*! \brief Adds the enumeration value \a value.
118 * The instance becomes the owner of \a value, which is destroyed
119 * when it is no longer used.
120 * This function should only be called during initialization.
121 * If \a do_clone is true, a copy of \a value is used
122 */
123 void addEnumValue(CaseOptionEnumValue* value, bool do_clone);
124
125 //! Returns the number of enumeration values
126 Integer nbEnumValue() const;
127
128 //! Returns the i-th value
130
131 /*! \brief Returns the value of the enumeration having the name \a name
132 *
133 * The value is returned in \a index.
134 * \param name name of the enumeration
135 * \param lang is the language of the data set
136 * \param value is the enumeration value (returned)
137 * \retval true in case of error,
138 * \retval false in case of success.
139 */
140 bool valueOfName(const String& name, const String& lang, int& value) const;
141
142 //! Returns the name corresponding to the value \a value for the language \a lang
143 String nameOfValue(int value, const String& lang) const;
144
145 /*!
146 * \brief Fills \a names with valid names for the language \a lang.
147 */
148 void getValidNames(const String& lang, StringArray& names) const;
149
150 private:
151
152 EnumValueList* m_enum_values; //!< Enumeration values
153};
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158/*!
159 * \brief Enumerated data set option.
160 * \ingroup CaseOption
161 */
162class ARCANE_CORE_EXPORT CaseOptionEnum
163: public CaseOptionSimple
164{
165 public:
166
167 CaseOptionEnum(const CaseOptionBuildInfo& cob, const String& type_name);
168 ~CaseOptionEnum();
169
170 public:
171
172 virtual void print(const String& lang, std::ostream& o) const;
173 virtual void updateFromFunction(Real current_time, Integer current_iteration)
174 {
175 _updateFromFunction(current_time, current_iteration);
176 }
177
178 void addEnumValue(CaseOptionEnumValue* value, bool do_clone)
179 {
180 m_enum_values->addEnumValue(value, do_clone);
181 }
182 CaseOptionEnumValues* enumValues() const { return m_enum_values; }
183
184 virtual void visit(ICaseDocumentVisitor* visitor) const;
185
186 int enumValueAsInt() const { return _optionValue(); }
187
188 public:
189 protected:
190
191 virtual void _search(bool is_phase1);
192 virtual bool _allowPhysicalUnit() { return false; }
193
194 //! Sets the option value to \a v
195 virtual void _setOptionValue(int v) = 0;
196 //! Returns the option value
197 virtual int _optionValue() const = 0;
198
199 protected:
200
201 void _setEnumDefaultValue(int def_value);
202
203 private:
204
205 String m_type_name; //!< Enumeration name
206 CaseOptionEnumValues* m_enum_values;
207 void _updateFromFunction(Real current_time, Integer current_iteration);
208};
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
212
213/*!
214 * \ingroup CaseOption
215 * \brief Enumerated data set option.
216 *
217 * \a T is the computer type of the enumeration.
218 */
219template <class EnumType>
220class CaseOptionEnumT
221: public CaseOptionEnum
222{
223 public:
224
225 CaseOptionEnumT(const CaseOptionBuildInfo& cob, const String& type_name)
226 : CaseOptionEnum(cob, type_name)
227 , m_value(EnumType())
228 {}
229
230 public:
231
232 //! Option value
233 EnumType value() const
234 {
235 ARCANE_CASEOPTION_CHECK_IS_INITIALIZED;
236 return m_value;
237 }
238
239 //! Option value
240 operator EnumType() const { return value(); }
241
242 //! Option value
243 EnumType operator()() const { return value(); }
244
245 /*!
246 * \brief Sets the default value of the option.
247 *
248 * If the option is not present in the data set, its value will be
249 * that specified by the argument \a def_value; otherwise, calling this method has no effect.
250 */
251 void setDefaultValue(EnumType def_value)
252 {
253 _setEnumDefaultValue(static_cast<int>(def_value));
254 }
255
256 //! Returns the option value if isPresent()==true, otherwise \a arg_value
257 EnumType valueIfPresentOrArgument(EnumType arg_value)
258 {
259 ARCANE_CASEOPTION_CHECK_IS_INITIALIZED;
260 return isPresent() ? m_value : arg_value;
261 }
262
263 private:
264
265 EnumType m_value; //!< Option value
266
267 public:
268 protected:
269
270 virtual void _setOptionValue(int i)
271 {
272 m_value = static_cast<EnumType>(i);
273 }
274 virtual int _optionValue() const
275 {
276 return static_cast<int>(m_value);
277 }
278};
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
283/*!
284 * \brief Multi-enumeration data set option.
285 * \ingroup CaseOption
286 */
287class ARCANE_CORE_EXPORT CaseOptionMultiEnum
288: public CaseOptionBase
289{
290 public:
291 public:
292
293 CaseOptionMultiEnum(const CaseOptionBuildInfo& cob, const String& type_name);
294 ~CaseOptionMultiEnum();
295
296 public:
297
298 virtual void print(const String& lang, std::ostream& o) const;
299 virtual ICaseFunction* function() const { return 0; }
300 virtual void updateFromFunction(Real /*current_time*/, Integer /*current_iteration*/) {}
301
302 void addEnumValue(CaseOptionEnumValue* value, bool do_clone)
303 {
304 m_enum_values->addEnumValue(value, do_clone);
305 }
306
307 CaseOptionEnumValues* enumValues() const { return m_enum_values; }
308
309 virtual void visit(ICaseDocumentVisitor* visitor) const;
310
311 protected:
312
313 virtual void _search(bool is_phase1);
314 virtual bool _allowPhysicalUnit() { return false; }
315
316 //! Allocates an array for \a size elements
317 virtual void _allocate(Integer size) = 0;
318 //! Returns the number of elements in the array.
319 virtual Integer _nbElem() const = 0;
320 /*! Sets the option value to the value \a v.
321 * \a v is directly converted to the enumeration value.
322 */
323 virtual void _setOptionValue(Integer index, int v) = 0;
324 //! Returns the enumeration value for index \a index.
325 virtual int _optionValue(Integer index) const = 0;
326
327 private:
328
329 String m_type_name; //!< Enumeration name
330 CaseOptionEnumValues* m_enum_values;
331};
332
333/*---------------------------------------------------------------------------*/
334/*---------------------------------------------------------------------------*/
335
336/*!
337 * \brief Multi-enumeration data set option.
338 * \ingroup CaseOption
339 */
340template <class T>
341class CaseOptionMultiEnumT
342: public CaseOptionMultiEnum
343, public ArrayView<T>
344{
345 public:
346
347 typedef T Type; //!< Option type.
348
349 public:
350
351 CaseOptionMultiEnumT(const CaseOptionBuildInfo& cob, const String type_name)
352 : CaseOptionMultiEnum(cob, type_name)
353 {}
354
355 protected:
356
357 virtual void _allocate(Integer size)
358 {
359 m_values.resize(size);
360 ArrayView<T>* view = this;
361 *view = m_values.view();
362 }
363 virtual Integer _nbElem() const
364 {
365 return this->size();
366 }
367 virtual void _setOptionValue(Integer index, int v)
368 {
369 (*this)[index] = static_cast<T>(v);
370 }
371 virtual int _optionValue(Integer index) const
372 {
373 return static_cast<int>((*this)[index]);
374 }
375
376 private:
377
378 UniqueArray<T> m_values;
379};
380
381/*---------------------------------------------------------------------------*/
382/*---------------------------------------------------------------------------*/
383
384} // End namespace Arcane
385
386/*---------------------------------------------------------------------------*/
387/*---------------------------------------------------------------------------*/
388
389#endif
constexpr ArrayView() noexcept
Constructs an empty view.
constexpr Integer size() const noexcept
Returns the size of the array.
EnumType value() const
Option value.
EnumType operator()() const
Option value.
void setDefaultValue(EnumType def_value)
Sets the default value of the option.
virtual void _setOptionValue(int i)
Sets the option value to v.
EnumType valueIfPresentOrArgument(EnumType arg_value)
Returns the option value if isPresent()==true, otherwise arg_value.
virtual int _optionValue() const
Returns the option value.
String nameOfValue(int value, const String &lang) const
Returns the name corresponding to the value value for the language lang.
CaseOptionEnumValues()
Constructs the instance.
UniqueArray< CaseOptionEnumValue * > EnumValueList
Type of the value list.
void getValidNames(const String &lang, StringArray &names) const
Fills names with valid names for the language lang.
bool valueOfName(const String &name, const String &lang, int &value) const
Returns the value of the enumeration having the name name.
Integer nbEnumValue() const
Returns the number of enumeration values.
CaseOptionEnumValue * enumValue(Integer index) const
Returns the i-th value.
void addEnumValue(CaseOptionEnumValue *value, bool do_clone)
Adds the enumeration value value. The instance becomes the owner of value, which is destroyed when it...
virtual void print(const String &lang, std::ostream &o) const
Prints the option value in the language lang, to the stream o.
virtual void _setOptionValue(int v)=0
Sets the option value to v.
virtual void updateFromFunction(Real current_time, Integer current_iteration)
Updates the option value from a function.
virtual int _optionValue() const =0
Returns the option value.
virtual void _setOptionValue(Integer index, int v)
virtual Integer _nbElem() const
Returns the number of elements in the array.
virtual int _optionValue(Integer index) const
Returns the enumeration value for index index.
virtual void _allocate(Integer size)
Allocates an array for size elements.
virtual Integer _nbElem() const =0
Returns the number of elements in the array.
virtual void print(const String &lang, std::ostream &o) const
Prints the option value in the language lang, to the stream o.
virtual void _allocate(Integer size)=0
Allocates an array for size elements.
virtual ICaseFunction * function() const
Returns the function linked to this option or nullptr if none exists.
virtual void updateFromFunction(Real, Integer)
Updates the option value from a function.
virtual int _optionValue(Integer index) const =0
Returns the enumeration value for index index.
virtual void _setOptionValue(Integer index, int v)=0
String name(const String &lang) const
returns the name of the option in the language lang. If no translation is available in the language l...
CaseOptionName(const String &true_name)
Constructs a name option true_name.
String trueName() const
Returns the true name (non-translated) of the option.
bool isPresent() const
Returns true if the option is present.
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Array< String > StringArray
Dynamic one-dimensional array of strings.
Definition UtilsTypes.h:145
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.