Arcane  v4.1.4.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
FieldProperty.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/* FieldProperty.h (C) 2000-2026 */
9/* */
10/* Gestion des propriétés comme champ de classes. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_COMMON_INTERNAL_FIELDPROPERTY_H
13#define ARCCORE_COMMON_INTERNAL_FIELDPROPERTY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/String.h"
18#include "arccore/base/PlatformUtils.h"
19#include "arccore/base/internal/ConvertInternal.h"
20
21#include "arccore/common/Array.h"
22#include "arccore/common/List.h"
23
24#include <functional>
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
36{
37 public:
38
39 template <typename DataType>
40 class FieldProperty
41 {
42 public:
43
44 explicit FieldProperty(const DataType& default_value)
45 : m_value(default_value)
46 , m_default_value(default_value)
47 {}
48 FieldProperty()
49 : FieldProperty(DataType())
50 {}
51 FieldProperty& operator=(const DataType& v)
52 {
53 setValue(v);
54 return (*this);
55 }
56 explicit(false) operator DataType() const { return m_value; }
57
58 public:
59
60 void setValue(const DataType& v)
61 {
62 if (m_validator) {
63 DataType copy(v);
64 m_validator(copy);
65 m_value = copy;
66 }
67 else
68 m_value = v;
69 m_has_value = true;
70 }
71 DataType value() const { return m_value; }
72 bool isValueSet() const { return m_has_value; }
73 void setValidator(std::function<void(DataType&)>&& func) { m_validator = func; }
74
75 private:
76
77 DataType m_value;
78 DataType m_default_value;
79 bool m_has_value = false;
80 std::function<void(DataType&)> m_validator;
81 };
82
83 class Int32Value
84 {
85 public:
86
87 explicit Int32Value(Int32 v)
88 : value(v)
89 {}
90 explicit(false) operator Int32() const { return value; }
91
92 public:
93
94 Int32Value minValue(Int32 x) const
95 {
96 return Int32Value(std::max(value, x));
97 }
98 Int32Value maxValue(Int32 x) const
99 {
100 return Int32Value(std::min(value, x));
101 }
102
103 public:
104
105 Int32 value;
106 };
107
108 static Int32Value getInt32(const String& str_value, Int32 default_value)
109 {
110 Int32 v = default_value;
111 if (!str_value.null()) {
112 bool is_bad = Convert::Impl::StringViewToIntegral::getValue(v, str_value);
113 if (is_bad)
114 v = default_value;
115 }
116 return Int32Value(v);
117 }
118 static void checkSet(FieldProperty<bool>& p, const String& str_value)
119 {
120 if (p.isValueSet())
121 return;
122 if (str_value.null())
123 return;
124 bool v = false;
125 bool is_bad = Convert::Impl::StringViewToIntegral::getValue(v, str_value);
126 if (!is_bad)
127 p.setValue(v);
128 }
129 static void checkSet(FieldProperty<Int32>& p, const String& str_value)
130 {
131 if (p.isValueSet())
132 return;
133 if (str_value.null())
134 return;
135 Int32 v = 0;
136 bool is_bad = Convert::Impl::StringViewToIntegral::getValue(v, str_value);
137 if (!is_bad)
138 p.setValue(v);
139 }
140 static void checkSet(FieldProperty<StringList>& p, const String& str_value)
141 {
142 if (p.isValueSet())
143 return;
144 if (str_value.null())
145 return;
146 StringList s;
147 s.add(str_value);
148 p.setValue(s);
149 }
150 static void checkSet(FieldProperty<StringList>& p, const StringList& str_values)
151 {
152 if (p.isValueSet())
153 return;
154 p.setValue(str_values);
155 }
156 static void checkSet(FieldProperty<String>& p, const String& str_value)
157 {
158 if (p.isValueSet())
159 return;
160 if (str_value.null())
161 return;
162 p.setValue(str_value);
163 }
164
165 /*---------------------------------------------------------------------------*/
166 /*---------------------------------------------------------------------------*/
167
169 {
170 private:
171
172 class NameValuePair
173 {
174 public:
175
176 NameValuePair(const String& n, const String& v)
177 : name(n)
178 , value(v)
179 {}
180 String name;
181 String value;
182 };
183
184 public:
185
198 String getValue(const UniqueArray<String>& env_values, const String& param_name,
199 const String& default_value)
200 {
201 if (!param_name.null()) {
202 String v = _searchParam(param_name);
203 if (!v.null())
204 return v;
205 }
206 for (const auto& x : env_values) {
208 if (!ev.null())
209 return ev;
210 }
211 return default_value;
212 }
213 void add(const String& name, const String& value)
214 {
215 m_values.add(NameValuePair(name, value));
216 }
217
218 private:
219
221
222 private:
223
224 String _searchParam(const String& param_name)
225 {
226 String v;
227 // Une option peut être présente plusieurs fois. Prend la dernière.
228 for (const auto& x : m_values) {
229 if (x.name == param_name)
230 v = x.value;
231 }
232 return v;
233 }
234 };
235};
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
240} // namespace Arcane
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245#endif
String getValue(const UniqueArray< String > &env_values, const String &param_name, const String &default_value)
Récupère la valeur d'une option.
Chaîne de caractères unicode.
bool null() const
Retourne true si la chaîne est nulle.
Definition String.cc:305
Vecteur 1D de données avec sémantique par valeur (style STL).
ARCCORE_BASE_EXPORT String getEnvironmentVariable(const String &name)
Variable d'environnement du nom name.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
List< String > StringList
Tableau de chaînes de caractères unicode.
Definition UtilsTypes.h:509
std::int32_t Int32
Type entier signé sur 32 bits.