Arcane  v3.16.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ParameterOption.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* ParameterOption.cc (C) 2000-2025 */
9/* */
10/* Classe représentant l'ensemble des paramètres pouvant modifier les */
11/* options du jeu de données. */
12/*---------------------------------------------------------------------------*/
13/*---------------------------------------------------------------------------*/
14
15#include "arcane/utils/internal/ParameterOption.h"
16
17#include "arcane/utils/ApplicationInfo.h"
18#include "arcane/utils/ValueConvert.h"
19#include "arcane/utils/Array.h"
20#include "arcane/utils/FatalErrorException.h"
21#include "arcane/utils/ITraceMng.h"
22#include "arcane/utils/Ref.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
35: m_tag(ANY_TAG)
36, m_index(ANY_INDEX)
37{}
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
44: m_tag(tag)
45, m_index(1)
46{
47 ARCANE_ASSERT(tag != ANY_TAG, ("ANY_TAG without ANY_INDEX is forbidden"));
48 ARCANE_ASSERT(!tag.empty(), ("tag is empty"));
49}
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
55ParameterOptionAddrPart(const StringView tag, const Integer index)
56: m_tag(tag)
57, m_index(index)
58{
59 ARCANE_ASSERT(index == ANY_INDEX || tag != ANY_TAG, ("ANY_TAG without ANY_INDEX is forbidden"));
60 ARCANE_ASSERT(!tag.empty(), ("tag is empty"));
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66StringView ParameterOptionAddrPart::
67tag() const
68{
69 return m_tag;
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75Integer ParameterOptionAddrPart::
76index() const
77{
78 return m_index;
79}
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
85setTag(const StringView tag)
86{
87 ARCANE_ASSERT(m_index == ANY_INDEX || tag != ANY_TAG, ("ANY_TAG without ANY_INDEX is forbidden"));
88 ARCANE_ASSERT(!tag.empty(), ("tag is empty"));
89
90 m_tag = tag;
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96void ParameterOptionAddrPart::
97setIndex(const Integer index)
98{
99 m_index = index;
100}
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
106isAny() const
107{
108 return (m_tag == ANY_TAG && m_index == ANY_INDEX);
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
115operator==(const ParameterOptionAddrPart& other) const
116{
117 return (m_tag == other.m_tag || m_tag == ANY_TAG || other.m_tag == ANY_TAG) &&
118 (m_index == other.m_index || m_index == ANY_INDEX || other.m_index == ANY_INDEX || m_index == GET_INDEX || other.m_index == GET_INDEX);
119}
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124bool ParameterOptionAddrPart::
125operator!=(const ParameterOptionAddrPart& other) const
126{
127 return !operator==(other);
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136std::ostream& operator<<(std::ostream& o, const ParameterOptionAddrPart& h)
137{
138 o << (h.tag() == ParameterOptionAddrPart::ANY_TAG ? "ANY" : h.tag())
139 << "[" << (h.index() == ParameterOptionAddrPart::ANY_INDEX ? "ANY" : (h.index() == ParameterOptionAddrPart::GET_INDEX ? "GET" : std::to_string(h.index())))
140 << "]";
141 return o;
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
151ParameterOptionAddr(const StringView addr_str_view)
152{
153 Span span_line(addr_str_view.bytes());
154 Integer begin = 0;
155 Integer size = 0;
156 Integer index_begin = -1;
157 // On interdit les options qui s'appliquent à toutes les caseoptions.
158 bool have_a_no_any = false;
159
160 // aaa[0]
161 for (Integer i = 0; i < span_line.size(); ++i) {
162 if (span_line[i] == '[') {
163 index_begin = i + 1;
164 size = i - begin;
165 if (size == 0) {
166 const StringView current = addr_str_view.subView(0, i + 1);
167 ARCANE_FATAL("Invalid parameter option (empty tag) -- Current read : {0}", current);
168 }
169 if (index_begin >= span_line.size()) {
170 const StringView current = addr_str_view.subView(0, i + 1);
171 ARCANE_FATAL("Invalid parameter option (']' not found) -- Current read : {0}", current);
172 }
173 }
174 else if (span_line[i] == ']') {
175 if (index_begin == -1) {
176 const StringView current = addr_str_view.subView(0, i + 1);
177 ARCANE_FATAL("Invalid parameter option (']' found without '[' before) -- Current read : {0}", current);
178 }
179
180 // Motif spécial "[]" (= ANY_INDEX)
181 if (index_begin == i) {
182 m_parts.add(makeRef(new ParameterOptionAddrPart(addr_str_view.subView(begin, size), ParameterOptionAddrPart::ANY_INDEX)));
183 have_a_no_any = true;
184 }
185 else {
186 StringView index_str = addr_str_view.subView(index_begin, i - index_begin);
187 Integer index;
188 bool is_bad = builtInGetValue(index, index_str);
189 if (is_bad) {
190 const StringView current = addr_str_view.subView(0, i + 1);
191 ARCANE_FATAL("Invalid index in parameter option -- Current read : {0}", current);
192 }
193 m_parts.add(makeRef(new ParameterOptionAddrPart(addr_str_view.subView(begin, size), index)));
194 have_a_no_any = true;
195 }
196 }
197
198 else if (span_line[i] == '/') {
199 if (i + 1 == span_line.size()) {
200 const StringView current = addr_str_view.subView(0, i + 1);
201 ARCANE_FATAL("Invalid parameter option ('/' found at the end of the param option) -- Current read : {0}", current);
202 }
203
204 if (index_begin == -1) {
205 size = i - begin;
206 // Cas ou on a un any_tag any_index ("truc1//truc2").
207 if (size == 0) {
208 m_parts.add(makeRef(new ParameterOptionAddrPart()));
209 }
210 else {
211 m_parts.add(makeRef(new ParameterOptionAddrPart(addr_str_view.subView(begin, size))));
212 have_a_no_any = true;
213 }
214 }
215
216 begin = i + 1;
217 size = 0;
218 index_begin = -1;
219 }
220 }
221 if (index_begin == -1) {
222 size = static_cast<Integer>(span_line.size()) - begin;
223 if (size == 0) {
224 const StringView current = addr_str_view.subView(0, size);
225 ARCANE_FATAL("Invalid parameter option (empty tag) -- Current read : {0}", current);
226 }
227
228 m_parts.add(makeRef(new ParameterOptionAddrPart(addr_str_view.subView(begin, size))));
229 have_a_no_any = true;
230 }
231 if (!have_a_no_any) {
232 ARCANE_FATAL("Invalid option");
233 }
234}
235
236/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239// On ne doit pas bloquer les multiples ParameterOptionAddrPart(ANY) :
240// Construction par iteration : aaaa/bb/ANY/ANY/cc
243{
244 m_parts.add(makeRef(part));
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
249
251addrPart(const Integer index_of_part) const
252{
253 if (index_of_part >= m_parts.size()) {
254 if (m_parts[m_parts.size() - 1]->isAny()) {
255 return lastAddrPart();
256 }
257 ARCANE_FATAL("Invalid index");
258 }
259 return m_parts[index_of_part].get();
260}
261
262/*---------------------------------------------------------------------------*/
263/*---------------------------------------------------------------------------*/
264
265ParameterOptionAddrPart* ParameterOptionAddr::
266lastAddrPart() const
267{
268 return m_parts[m_parts.size() - 1].get();
269}
270
271/*---------------------------------------------------------------------------*/
272/*---------------------------------------------------------------------------*/
273
275nbAddrPart() const
276{
277 return m_parts.size();
278}
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
284getIndexInAddr(const ParameterOptionAddr& addr_with_get_index, ArrayView<Integer> indexes) const
285{
286 if (!operator==(addr_with_get_index))
287 return false;
288
289 ARCANE_ASSERT(indexes.size() == addr_with_get_index.nbIndexToGetInAddr(), ("ArrayView too small"));
290
291 Integer index = 0;
292 for (Integer i = 0; i < addr_with_get_index.nbAddrPart(); ++i) {
293 if (addr_with_get_index.addrPart(i)->index() == ParameterOptionAddrPart::GET_INDEX) {
294 Integer index_tag = addrPart(i)->index();
295 if (index_tag == ParameterOptionAddrPart::ANY_INDEX)
296 return false;
297 indexes[index++] = index_tag;
298 }
299 }
300 return true;
301}
302
303/*---------------------------------------------------------------------------*/
304/*---------------------------------------------------------------------------*/
305
307nbIndexToGetInAddr() const
308{
309 Integer count = 0;
310 for (const auto& elem : m_parts) {
311 if (elem->index() == ParameterOptionAddrPart::GET_INDEX) {
312 count++;
313 }
314 }
315 return count;
316}
317
318/*---------------------------------------------------------------------------*/
319/*---------------------------------------------------------------------------*/
320
322operator==(const ParameterOptionAddr& other) const
323{
324 Integer nb_iter = 0;
325 if (lastAddrPart()->isAny()) {
326 nb_iter = nbAddrPart() - 1;
327 }
328 else if (other.lastAddrPart()->isAny()) {
329 nb_iter = other.nbAddrPart() - 1;
330 }
331 else if (nbAddrPart() != other.nbAddrPart()) {
332 return false;
333 }
334 else {
335 nb_iter = nbAddrPart();
336 }
337
338 for (Integer i = 0; i < nb_iter; ++i) {
339 if (*addrPart(i) != *other.addrPart(i)) {
340 return false;
341 }
342 }
343 return true;
344}
345
346/*---------------------------------------------------------------------------*/
347/*---------------------------------------------------------------------------*/
348
349bool ParameterOptionAddr::
350operator!=(const ParameterOptionAddr& other) const
351{
352 return !operator==(other);
353}
354
355/*---------------------------------------------------------------------------*/
356/*---------------------------------------------------------------------------*/
357
358/*---------------------------------------------------------------------------*/
359/*---------------------------------------------------------------------------*/
360
361std::ostream& operator<<(std::ostream& o, const ParameterOptionAddr& h)
362{
363 Integer nb_part = h.nbAddrPart();
364 if (nb_part != 0)
365 o << *(h.addrPart(0));
366 for (Integer i = 1; i < nb_part; ++i) {
367 o << "/" << *(h.addrPart(i));
368 }
369 return o;
370}
371
372/*---------------------------------------------------------------------------*/
373/*---------------------------------------------------------------------------*/
374
375/*---------------------------------------------------------------------------*/
376/*---------------------------------------------------------------------------*/
377
378ParameterOptionElement::
379ParameterOptionElement(const StringView addr, const StringView value)
380: m_addr(addr)
381, m_value(value)
382{}
383
384/*---------------------------------------------------------------------------*/
385/*---------------------------------------------------------------------------*/
386
387ParameterOptionAddr ParameterOptionElement::
388addr() const
389{
390 return m_addr;
391}
392
393/*---------------------------------------------------------------------------*/
394/*---------------------------------------------------------------------------*/
395
396StringView ParameterOptionElement::
397value() const
398{
399 return m_value;
400}
401
402/*---------------------------------------------------------------------------*/
403/*---------------------------------------------------------------------------*/
404
405bool ParameterOptionElement::
406operator==(const ParameterOptionAddr& addr) const
407{
408 return m_addr == addr;
409}
410
411/*---------------------------------------------------------------------------*/
412/*---------------------------------------------------------------------------*/
413
415addParameter(const String& parameter, const String& value)
416{
417 if (parameter.startsWith("//")) {
418 addElement(parameter.view().subView(2), value.view());
419 }
420}
421
422/*---------------------------------------------------------------------------*/
423/*---------------------------------------------------------------------------*/
424
425void ParameterOptionElementsCollection::
426addElement(StringView addr, StringView value)
427{
428 m_elements.add({ addr, value });
429}
430
431/*---------------------------------------------------------------------------*/
432/*---------------------------------------------------------------------------*/
433
434// Un StringView "vide" est éqal à un StringView "nul".
435// Comme on travaille avec des String et que la distinction
436// vide/nul est importante, on passe par un std::optional.
437std::optional<StringView> ParameterOptionElementsCollection::
438value(const ParameterOptionAddr& addr)
439{
440 for (const auto& elem : m_elements) {
441 if (elem == addr)
442 return elem.value();
443 }
444 return {};
445}
446
447/*---------------------------------------------------------------------------*/
448/*---------------------------------------------------------------------------*/
449
452{
453 for (const auto& elem : m_elements) {
454 if (elem == addr)
455 return true;
456 }
457 return false;
458}
459
460/*---------------------------------------------------------------------------*/
461/*---------------------------------------------------------------------------*/
462
465{
466 Integer count = 0;
467 for (const auto& elem : m_elements) {
468 if (elem == addr)
469 count++;
470 }
471 return count;
472}
473
474/*---------------------------------------------------------------------------*/
475/*---------------------------------------------------------------------------*/
476
478getIndexInAddr(const ParameterOptionAddr& addr_with_get_index, UniqueArray<Integer>& indexes)
479{
480 UniqueArray<Integer> new_indexes(addr_with_get_index.nbIndexToGetInAddr());
481 for (const auto& elem : m_elements) {
482 if (elem.addr().getIndexInAddr(addr_with_get_index, new_indexes)) {
483 indexes.addRange(new_indexes);
484 }
485 }
486}
487
488/*---------------------------------------------------------------------------*/
489/*---------------------------------------------------------------------------*/
490
491} // End namespace Arcane
492
493/*---------------------------------------------------------------------------*/
494/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Vue modifiable d'un tableau d'un type T.
constexpr Integer size() const noexcept
Retourne la taille du tableau.
void addRange(ConstReferenceType val, Int64 n)
Ajoute n élément de valeur val à la fin du tableau.
Classe représentant une partie d'une adresse d'option du jeu de données. À noter qu'en XML,...
ParameterOptionAddrPart()
Constructeur. Définit le tag en ANY_TAG et l'index en ANY_INDEX.
bool isAny() const
isAny si ANY_TAG et ANY_INDEX.
bool operator==(const ParameterOptionAddrPart &other) const
Opérateur d'égalité. Le tag ANY_TAG est égal à tous les tags. L'index ANY_INDEX est égal à tous les i...
void setTag(const StringView tag)
Classe représentant une adresse d'option du jeu de données. Cette adresse doit être de la forme : "ta...
bool operator==(const ParameterOptionAddr &other) const
Opérateur d'égalité. Cet opérateur tient compte des ANY_TAG / ANY_INDEX. L'adresse "aaa[1]/bbb[2]/ANY...
ParameterOptionAddrPart * addrPart(const Integer index_of_part) const
Méthode permettant de récupérer une partie de l'adresse. Si l'adresse termine par un ANY_TAG[ANY_INDE...
ParameterOptionAddr(StringView addr_str_view)
Constructeur.
Integer nbAddrPart() const
Méthode permettant de récupérer le nombre de partie de l'adresse. Les parties égales à "ANY_TAG[ANY_I...
Integer nbIndexToGetInAddr() const
Méthode permettant de savoir combien il y a de "GET_INDEX" dans l'adresse.
bool getIndexInAddr(const ParameterOptionAddr &addr_with_get_index, ArrayView< Integer > indexes) const
Méthode permettant de récupérer un ou plusieurs indices dans l'adresse.
void addAddrPart(ParameterOptionAddrPart *part)
Méthode permettant d'ajouter une partie à la fin de l'adresse actuelle.
bool isExistAddr(const ParameterOptionAddr &addr)
Méthode permettant de savoir si une adresse est présente dans la liste d'éléments....
Integer countAddr(const ParameterOptionAddr &addr)
Méthode permettant de savoir combien de fois une adresse est présente dans la liste d'élements....
void addParameter(const String &parameter, const String &value)
Méthode permettant d'ajouter un paramètre d'option dans la liste des paramètres d'options.
void getIndexInAddr(const ParameterOptionAddr &addr_with_get_index, UniqueArray< Integer > &indexes)
Méthode permettant de récupérer un ou plusieurs indices dans la liste d'adresses.
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:212
Vue d'un tableau d'éléments de type T.
Definition Span.h:513
Vue sur une chaîne de caractères UTF-8.
Definition StringView.h:47
constexpr Span< const Byte > bytes() const ARCCORE_NOEXCEPT
Retourne la conversion de l'instance dans l'encodage UTF-8.
Definition StringView.h:96
StringView subView(Int64 pos) const
Sous-chaîne commençant à la position pos.
Definition StringView.cc:37
Chaîne de caractères unicode.
bool startsWith(const String &s) const
Indique si la chaîne commence par les caractères de s.
Definition String.cc:1099
StringView view() const
Retourne une vue sur la chaîne actuelle.
Definition String.cc:367
Vecteur 1D de données avec sémantique par valeur (style STL).
std::ostream & operator<<(std::ostream &o, eExecutionPolicy exec_policy)
Affiche le nom de la politique d'exécution.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Int32 Integer
Type représentant un entier.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.