Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ParameterListWithCaseOption.cc
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/* ParameterListWithCaseOption.cc (C) 2000-2025 */
9/* */
10/* Parameter list with support for dataset options. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ParameterList.h"
15#include "arcane/utils/StringDictionary.h"
16#include "arcane/utils/String.h"
17#include "arcane/utils/Array.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/Ref.h"
20
21#include "arcane/utils/internal/ParameterOption.h"
22#include "arcane/utils/internal/ParameterListWithCaseOption.h"
23
24#include <algorithm>
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/*
30 * This class manages command-line parameters that allow overriding
31 * dataset options.
32 *
33 * It is a copy of the ParameterList class.
34 *
35 * TODO: This class should only retain options that start with '//' and are
36 * related to the dataset.
37 */
38namespace Arcane
39{
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
45{
46 public:
47
49 {
50 String name;
51 String value;
52 friend bool operator==(const NameValuePair& v1, const NameValuePair& v2)
53 {
54 return (v1.name == v2.name && v1.value == v2.value);
55 }
56 };
57
58 public:
59
60 Impl()
61 : m_parameter_option(makeRef(new ParameterOptionElementsCollection()))
62 {}
63
64 public:
65
66 String getParameter(const String& key)
67 {
68 if (key.startsWith("//")) {
69 if (const auto value = m_parameter_option->value(ParameterOptionAddr(key.view().subView(2))))
70 return value.value();
71 return {};
72 }
73 String x = m_parameters_dictionary.find(key);
74 return x;
75 }
76
77 void addParameter(const String& name, const String& value)
78 {
79 //std::cout << "__ADD_PARAMETER name='" << name << "' v='" << value << "'\n";
80 if (name.empty())
81 return;
82
83 if (name.startsWith("//")) {
84 m_parameters_option_list.add({ name, value });
85 m_parameter_option->addParameter(m_parameters_option_list[m_parameters_option_list.size() - 1].name, m_parameters_option_list[m_parameters_option_list.size() - 1].value);
86 return;
87 }
88
89 m_parameters_dictionary.add(name, value);
90 m_parameters_list.add({ name, value });
91 m_parameter_option->addParameter(m_parameters_list[m_parameters_list.size() - 1].name, m_parameters_list[m_parameters_list.size() - 1].value);
92 }
93
94 void setParameter(const String& name, const String& value)
95 {
96 //std::cout << "__SET_PARAMETER name='" << name << "' v='" << value << "'\n";
97 if (name.empty())
98 return;
99
100 if (name.startsWith("//")) {
101 ARCANE_FATAL("Set parameter not supported for ParameterOptions.");
102 }
103
104 m_parameters_dictionary.add(name, value);
105 // Remove all occurrences from the list having \a name as the parameter
106 auto comparer = [=](const NameValuePair& nv) { return nv.name == name; };
107 auto new_end = std::remove_if(m_parameters_list.begin(), m_parameters_list.end(), comparer);
108 m_parameters_list.resize(new_end - m_parameters_list.begin());
109 }
110
111 void removeParameter(const String& name, const String& value)
112 {
113 //std::cout << "__REMOVE_PARAMETER name='" << name << "' v='" << value << "'\n";
114 if (name.empty())
115 return;
116 if (name.startsWith("//")) {
117 ARCANE_FATAL("Remove parameter not supported for ParameterOptions.");
118 }
119 // If the parameter \a name with the value \a value is found, it is removed.
120 // In this case, we must check if there is still a parameter \a name in
121 // \a m_parameters_list, and if so, we will take its value.
122 String x = m_parameters_dictionary.find(name);
123 bool need_fill = false;
124 if (x == value) {
125 m_parameters_dictionary.remove(name);
126 need_fill = true;
127 }
128 // Remove all occurrences
129 // of the parameter with the desired value
130 NameValuePair ref_value{ name, value };
131 auto new_end = std::remove(m_parameters_list.begin(), m_parameters_list.end(), ref_value);
132 m_parameters_list.resize(new_end - m_parameters_list.begin());
133 if (need_fill)
134 _fillDictionaryWithValueInList(name);
135 }
136 void fillParameters(StringList& param_names, StringList& values) const
137 {
138 m_parameters_dictionary.fill(param_names, values);
139 for (const auto& [name, value] : m_parameters_option_list) {
140 param_names.add(name);
141 values.add(value);
142 std::cout << "FILL name='" << name << "' value='" << value << "'\n";
143 }
144 }
145
146 ParameterOptionElementsCollection* getParameterOption() const
147 {
148 return m_parameter_option.get();
149 }
150
151 private:
152
153 void _fillDictionaryWithValueInList(const String& name)
154 {
155 for (auto& nv : m_parameters_list)
156 if (nv.name == name)
157 m_parameters_dictionary.add(nv.name, nv.value);
158 }
159
160 private:
161
162 StringDictionary m_parameters_dictionary;
163 UniqueArray<NameValuePair> m_parameters_list;
164 UniqueArray<NameValuePair> m_parameters_option_list;
165 Ref<ParameterOptionElementsCollection> m_parameter_option;
166};
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
185
186/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
197
199getParameterOrNull(const String& param_name) const
200{
201 return m_p->getParameter(param_name);
202}
203
204/*---------------------------------------------------------------------------*/
205/*---------------------------------------------------------------------------*/
206
208addParameterLine(const String& line)
209{
210 Span<const Byte> bytes = line.bytes();
211 Int64 len = bytes.length();
212 for (Int64 i = 0; i < len; ++i) {
213 Byte c = bytes[i];
214 Byte cnext = ((i + 1) < len) ? bytes[i + 1] : '\0';
215 if (c == '=') {
216 m_p->addParameter(line.substring(0, i), line.substring(i + 1));
217 return false;
218 }
219 if (c == '+' && cnext == '=') {
220 m_p->addParameter(line.substring(0, i), line.substring(i + 2));
221 return false;
222 }
223 if (c == ':' && cnext == '=') {
224 m_p->setParameter(line.substring(0, i), line.substring(i + 2));
225 return false;
226 }
227 if (c == '-' && cnext == '=') {
228 m_p->removeParameter(line.substring(0, i), line.substring(i + 2));
229 return false;
230 }
231 }
232 return true;
233}
234
235/*---------------------------------------------------------------------------*/
236/*---------------------------------------------------------------------------*/
237
239getParameterCaseOption(const String& language) const
240{
241 return { m_p->getParameterOption(), language };
242}
243
244/*---------------------------------------------------------------------------*/
245/*---------------------------------------------------------------------------*/
246
248addParameters(const ParameterList& parameters)
249{
250 // TODO: Only consider options that start with '//'
251 StringList names;
252 StringList values;
253 parameters.fillParameters(names, values);
254 Int32 size = names.count();
255 for (Int32 i = 0; i < size; ++i)
256 m_p->addParameter(names[i], values[i]);
257}
258
259/*---------------------------------------------------------------------------*/
260/*---------------------------------------------------------------------------*/
261
262} // End namespace Arcane
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Integer count() const
Number of elements in the collection.
Class representing the set of parameters that can modify the dataset options.
bool addParameterLine(const String &line)
Parses the line line.
void addParameters(const ParameterList &parameters)
Adds the parameters from parameters to the instance's parameters.
ParameterCaseOption getParameterCaseOption(const String &language) const
Method to retrieve an object of type ParameterCaseOption.
String getParameterOrNull(const String &param_name) const
Retrieves the parameter with name param_name.
void fillParameters(StringList &param_names, StringList &values) const
Retrieves the list of parameters and their values.
Class representing a data set option address. This address must be in the form: "tag/tag[index]/tag" ...
Class representing a collection of XML elements (a set of Arcane options).
View of an array of elements of type T.
Definition Span.h:635
String find(const String &key, bool throw_exception=false) const
Returns the value associated with key.
StringView subView(Int64 pos) const
Substring starting at position pos.
Definition StringView.cc:37
bool startsWith(const String &s) const
Indicates if the string starts with the characters of s.
Definition String.cc:1111
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:293
StringView view() const
Returns a view of the current string.
Definition String.cc:369
String substring(Int64 pos) const
Substring starting at position pos.
Definition String.cc:1126
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
List< String > StringList
Unicode string list.
Definition UtilsTypes.h:509
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.