Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ParameterList.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/* ParameterList.cc (C) 2000-2025 */
9/* */
10/* List of parameters. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/common/ParameterList.h"
15#include "arccore/common/StringDictionary.h"
16#include "arccore/base/String.h"
17#include "arccore/common/Array.h"
18#include "arccore/base/FatalErrorException.h"
19//#include "arccore/common/Ref.h"
20
21#include <algorithm>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
33{
34 public:
35
37 {
38 String name;
39 String value;
40 friend bool operator==(const NameValuePair& v1, const NameValuePair& v2)
41 {
42 return (v1.name == v2.name && v1.value == v2.value);
43 }
44 };
45
46 public:
47
48 Impl()
49 {}
50
51 public:
52
53 String getParameter(const String& key)
54 {
55 String x = m_parameters_dictionary.find(key);
56 return x;
57 }
58
59 void addParameter(const String& name, const String& value)
60 {
61 //std::cout << "__ADD_PARAMETER name='" << name << "' v='" << value << "'\n";
62 if (name.empty())
63 return;
64
65 m_parameters_dictionary.add(name, value);
66 m_parameters_list.add({ name, value });
67 }
68 void setParameter(const String& name, const String& value)
69 {
70 //std::cout << "__SET_PARAMETER name='" << name << "' v='" << value << "'\n";
71 if (name.empty())
72 return;
73
74 if (name.startsWith("//")) {
75 ARCCORE_FATAL("Set parameter not supported for ParameterOptions.");
76 }
77
78 m_parameters_dictionary.add(name, value);
79 // Remove all occurrences from the list having
80 // parameter \a name
81 auto comparer = [=](const NameValuePair& nv) { return nv.name == name; };
82 auto new_end = std::remove_if(m_parameters_list.begin(), m_parameters_list.end(), comparer);
83 m_parameters_list.resize(new_end - m_parameters_list.begin());
84 }
85 void removeParameter(const String& name, const String& value)
86 {
87 //std::cout << "__REMOVE_PARAMETER name='" << name << "' v='" << value << "'\n";
88 if (name.empty())
89 return;
90 if (name.startsWith("//")) {
91 ARCCORE_FATAL("Remove parameter not supported for ParameterOptions.");
92 }
93 // If the parameter \a name with the value \a value is found, it is removed.
94 // In this case, we must check if there is still
95 // in \a m_parameters_list a parameter \a name and if so
96 // we will take the value of that one
97 String x = m_parameters_dictionary.find(name);
98 bool need_fill = false;
99 if (x == value) {
100 m_parameters_dictionary.remove(name);
101 need_fill = true;
102 }
103 // Remove all occurrences from the list
104 // of the parameter with the desired value
105 NameValuePair ref_value{ name, value };
106 auto new_end = std::remove(m_parameters_list.begin(), m_parameters_list.end(), ref_value);
107 m_parameters_list.resize(new_end - m_parameters_list.begin());
108 if (need_fill)
109 _fillDictionaryWithValueInList(name);
110 }
111 void fillParameters(StringList& param_names, StringList& values) const
112 {
113 m_parameters_dictionary.fill(param_names, values);
114 }
115
116 private:
117
118 void _fillDictionaryWithValueInList(const String& name)
119 {
120 for (auto& nv : m_parameters_list)
121 if (nv.name == name)
122 m_parameters_dictionary.add(nv.name, nv.value);
123 }
124
125 private:
126
127 StringDictionary m_parameters_dictionary;
128 UniqueArray<NameValuePair> m_parameters_list;
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
136: m_p(new Impl())
137{
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
145: m_p(new Impl(*rhs.m_p))
146{
147}
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
154{
155 delete m_p;
156}
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
160
162getParameterOrNull(const String& param_name) const
163{
164 return m_p->getParameter(param_name);
165}
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
169
171addParameterLine(const String& line)
172{
173 Span<const Byte> bytes = line.bytes();
174 Int64 len = bytes.length();
175 for (Int64 i = 0; i < len; ++i) {
176 Byte c = bytes[i];
177 Byte cnext = ((i + 1) < len) ? bytes[i + 1] : '\0';
178 if (c == '=') {
179 m_p->addParameter(line.substring(0, i), line.substring(i + 1));
180 return false;
181 }
182 if (c == '+' && cnext == '=') {
183 m_p->addParameter(line.substring(0, i), line.substring(i + 2));
184 return false;
185 }
186 if (c == ':' && cnext == '=') {
187 m_p->setParameter(line.substring(0, i), line.substring(i + 2));
188 return false;
189 }
190 if (c == '-' && cnext == '=') {
191 m_p->removeParameter(line.substring(0, i), line.substring(i + 2));
192 return false;
193 }
194 }
195 return true;
196}
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
200
202fillParameters(StringList& param_names, StringList& values) const
203{
204 m_p->fillParameters(param_names, values);
205}
206
207/*---------------------------------------------------------------------------*/
208/*---------------------------------------------------------------------------*/
209
210} // End namespace Arcane
211
212/*---------------------------------------------------------------------------*/
213/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
void fillParameters(StringList &param_names, StringList &values) const
Retrieves the list of parameters and their values.
bool addParameterLine(const String &line)
Parses the line line.
String getParameterOrNull(const String &param_name) const
Retrieves the parameter with name param_name.
~ParameterList()
Frees resources.
ParameterList()
Implementation.
View of an array of elements of type T.
Definition Span.h:635
void add(const String &key, const String &value)
Adds the (key, value) pair to the dictionary.
String find(const String &key, bool throw_exception=false) const
Returns the value associated with key.
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:293
bool empty() const
True if the string is empty (null or "").
Definition String.cc:317
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