Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Configuration.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/* Configuration.cc (C) 2000-2014 */
9/* */
10/* Management of execution configuration options. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15#include "arcane/utils/TraceAccessor.h"
16#include "arcane/utils/ITraceMng.h"
17#include "arcane/utils/String.h"
18#include "arcane/utils/ValueConvert.h"
19#include "arcane/utils/FatalErrorException.h"
20#include "arcane/utils/TraceInfo.h"
21#include "arcane/utils/ScopedPtr.h"
22#include "arcane/utils/OStringStream.h"
23
24#include "arcane/core/Configuration.h"
25#include "arcane/core/XmlNode.h"
26#include "arcane/core/XmlNodeList.h"
27
28#include <map>
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33namespace Arcane
34{
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39class IApplication;
40class Configuration;
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
45class ConfigurationMng
46: public TraceAccessor
47, public IConfigurationMng
48{
49 public:
50
51 ConfigurationMng(ITraceMng* tm);
52 ~ConfigurationMng();
53
54 public:
55
56 virtual IConfiguration* defaultConfiguration() const;
58
59 private:
60
61 Configuration* m_default_configuration;
62
63 private:
64
65 Configuration* _createConfiguration();
66};
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71class ConfigurationSection
73{
74 public:
75
76 ConfigurationSection(const Configuration* configuration, const String& base_name)
77 : m_configuration(configuration)
78 , m_base_name(base_name)
79 {}
80
81 virtual ~ConfigurationSection() {}
82
83 public:
84
85 virtual Int32 value(const String& name, Int32 default_value) const;
86 virtual Int64 value(const String& name, Int64 default_value) const;
87 virtual Real value(const String& name, Real default_value) const;
88 virtual bool value(const String& name, bool default_value) const;
89 virtual String value(const String& name, const String& default_value) const;
90 virtual String value(const String& name, const char* default_value) const;
91
92 virtual Integer valueAsInteger(const String& name, Integer default_value) const
93 {
94 return value(name, default_value);
95 }
96 virtual Int32 valueAsInt32(const String& name, Int32 default_value) const
97 {
98 return value(name, default_value);
99 }
100 virtual Int64 valueAsInt64(const String& name, Int64 default_value) const
101 {
102 return value(name, default_value);
103 }
104 virtual Real valueAsReal(const String& name, Real default_value) const
105 {
106 return value(name, default_value);
107 }
108 virtual bool valueAsBool(const String& name, bool default_value) const
109 {
110 return value(name, default_value);
111 }
112 virtual String valueAsString(const String& name, const String& default_value) const
113 {
114 return value(name, default_value);
115 }
116
117 private:
118
119 const Configuration* m_configuration;
120 String m_base_name;
121};
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
125
126class Configuration
127: public TraceAccessor
128, public IConfiguration
129{
130 struct ConfigValue
131 {
132 ConfigValue(const String& value, Integer priority)
133 : m_value(value)
134 , m_priority(priority)
135 {}
136 const String& value() const { return m_value; }
137 Integer priority() const { return m_priority; }
138
139 private:
140
141 String m_value;
142 Integer m_priority;
143 };
144
145 typedef std::map<String, ConfigValue> KeyValueMap;
146
147 public:
148
149 Configuration(ConfigurationMng* cm, ITraceMng* tm);
150
151 public:
152
153 IConfigurationSection* createSection(const String& name) const override
154 {
155 return new ConfigurationSection(this, name);
156 }
157
158 IConfigurationSection* mainSection() const override { return m_main_section.get(); }
159
160 void addValue(const String& name, const String& value, Integer priority) override;
161 IConfiguration* clone() const override;
162 void merge(const IConfiguration* c) override;
163 void dump() const override;
164 void dump(std::ostream& o) const override;
165
166 public:
167
168 template <typename T> T getValue(const String& base_name, const String& name, T default_value) const
169 {
170 T v = T();
171 KeyValueMap::const_iterator i;
172 if (base_name.null())
173 i = m_values.find(name);
174 else
175 i = m_values.find(base_name + "." + name);
176
177 if (i == m_values.end())
178 return default_value;
179
180 String value = i->second.value();
181 if (builtInGetValue(v, value))
182 throw FatalErrorException(A_FUNCINFO, String::format("Can not convert '{0}' to type '{1}'", value, _typeName((T*)0)));
183
184 return v;
185 }
186
187 private:
188
189 static const char* _typeName(Int32*) { return "Int32"; }
190 static const char* _typeName(Int64*) { return "Int64"; }
191 static const char* _typeName(Real*) { return "Real"; }
192 static const char* _typeName(bool*) { return "bool"; }
193 static const char* _typeName(String*) { return "String"; }
194
195 void _checkAdd(const String& name, const String& value, Integer priority)
196 {
197 KeyValueMap::iterator i = m_values.find(name);
198 //info() << "CHECK_ADD name=" << name << " value=" << value << " priority=" << priority;
199 if (i == m_values.end()) {
200 //info() << "NOT_FOUND name=" << name << " value=" << value << " priority=" << priority;
201 m_values.insert(std::make_pair(name, ConfigValue(value, priority)));
202 }
203 else {
204 Integer orig_priority = i->second.priority();
205 //info() << "FOUND name=" << name << " value=" << value << " orig_priority=" << orig_priority;
206 if (priority < orig_priority) {
207 //info() << "REPLACING name=" << name << " value=" << value << " new_priority=" << priority;
208 i->second = ConfigValue(value, priority);
209 }
210 }
211 }
212
213 private:
214
215 ConfigurationMng* m_configuration_mng;
216 KeyValueMap m_values;
217 ScopedPtrT<IConfigurationSection> m_main_section;
218};
219
220/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
222
223/*---------------------------------------------------------------------------*/
224/*---------------------------------------------------------------------------*/
225
226Int32 ConfigurationSection::
227value(const String& name, Int32 default_value) const
228{
229 return m_configuration->getValue(m_base_name, name, default_value);
230}
231
232Int64 ConfigurationSection::
233value(const String& name, Int64 default_value) const
234{
235 return m_configuration->getValue(m_base_name, name, default_value);
236}
237
238Real ConfigurationSection::
239value(const String& name, Real default_value) const
240{
241 return m_configuration->getValue(m_base_name, name, default_value);
242}
243
244bool ConfigurationSection::
245value(const String& name, bool default_value) const
246{
247 return m_configuration->getValue(m_base_name, name, default_value);
248}
249
250String ConfigurationSection::
251value(const String& name, const String& default_value) const
252{
253 return m_configuration->getValue(m_base_name, name, default_value);
254}
255
256String ConfigurationSection::
257value(const String& name, const char* default_value) const
258{
259 return m_configuration->getValue(m_base_name, name, String(default_value));
260}
261
262/*---------------------------------------------------------------------------*/
263/*---------------------------------------------------------------------------*/
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268Configuration::
269Configuration(ConfigurationMng* cm, ITraceMng* tm)
270: TraceAccessor(tm)
271, m_configuration_mng(cm)
272, m_main_section(new ConfigurationSection(this, String()))
273{
274}
275
276/*---------------------------------------------------------------------------*/
277/*---------------------------------------------------------------------------*/
278
280clone() const
281{
282 Configuration* cf = new Configuration(m_configuration_mng, traceMng());
283
284 for (auto& i : m_values) {
285 cf->m_values.insert(std::make_pair(i.first, i.second));
286 }
287
288 return cf;
289}
290
291/*---------------------------------------------------------------------------*/
292/*---------------------------------------------------------------------------*/
293
295dump() const
296{
297 OStringStream ostr;
298 dump(ostr());
299 info() << ostr.str();
300}
301
302/*---------------------------------------------------------------------------*/
303/*---------------------------------------------------------------------------*/
304
306dump(std::ostream& o) const
307{
308 o << "Configuration:\n";
309 for (auto& i : m_values) {
310 String s1 = i.first;
311 String s2 = i.second.value();
312 o << " name=" << s1 << " value=" << s2 << " (" << i.second.priority() << ")\n";
313 }
314}
315
316/*---------------------------------------------------------------------------*/
317/*---------------------------------------------------------------------------*/
318
320merge(const IConfiguration* c)
321{
322 const Configuration* cc = ARCANE_CHECK_POINTER(dynamic_cast<const Configuration*>(c));
323 for (auto& i : cc->m_values) {
324 String s1 = i.first;
325 const ConfigValue& cv = i.second;
326 //String s2 = i->second.value();
327 _checkAdd(s1, cv.value(), cv.priority());
328 //info() << "MERGING CONFIGURATION name=" << s1 << " value=" << s2;
329 //m_values[s1] = s2;
330 }
331}
332
333/*---------------------------------------------------------------------------*/
334/*---------------------------------------------------------------------------*/
335
337addValue(const String& name, const String& value, Integer priority)
338{
339 _checkAdd(name, value, priority);
340}
341
342/*---------------------------------------------------------------------------*/
343/*---------------------------------------------------------------------------*/
344
345/*---------------------------------------------------------------------------*/
346/*---------------------------------------------------------------------------*/
347
348ConfigurationMng::
349ConfigurationMng(ITraceMng* tm)
350: TraceAccessor(tm)
351, m_default_configuration(0)
352{
353 m_default_configuration = _createConfiguration();
354}
355
356/*---------------------------------------------------------------------------*/
357/*---------------------------------------------------------------------------*/
358
359ConfigurationMng::
360~ConfigurationMng()
361{
362 delete m_default_configuration;
363}
364
365/*---------------------------------------------------------------------------*/
366/*---------------------------------------------------------------------------*/
367
368Configuration* ConfigurationMng::
369_createConfiguration()
370{
371 return new Configuration(this, traceMng());
372}
373
374/*---------------------------------------------------------------------------*/
375/*---------------------------------------------------------------------------*/
376
379{
380 return m_default_configuration;
381}
382
383/*---------------------------------------------------------------------------*/
384/*---------------------------------------------------------------------------*/
385
388{
389 return _createConfiguration();
390}
391
392/*---------------------------------------------------------------------------*/
393/*---------------------------------------------------------------------------*/
394
395/*---------------------------------------------------------------------------*/
396/*---------------------------------------------------------------------------*/
397
398extern "C++" IConfigurationMng*
399arcaneCreateConfigurationMng(ITraceMng* tm)
400{
402 return cm;
403}
404
405/*---------------------------------------------------------------------------*/
406/*---------------------------------------------------------------------------*/
407
408} // namespace Arcane
409
410/*---------------------------------------------------------------------------*/
411/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
virtual IConfiguration * defaultConfiguration() const
Default configuration.
virtual IConfiguration * createConfiguration()
Creates a new configuration.
void dump() const override
Displays the values of the configuration parameters via traceMng().
void merge(const IConfiguration *c) override
Merges this configuration with configuration c.
IConfigurationSection * createSection(const String &name) const override
Creates a configuration section.
IConfiguration * clone() const override
Clones this configuration.
IConfigurationSection * mainSection() const override
Main section.
void addValue(const String &name, const String &value, Integer priority) override
Adds a value to the configuration.
Application interface.
Configuration manager.
Interface for a configuration section.
Interface for a configuration.
Output stream linked to a String.
bool null() const
Returns true if the string is null.
Definition String.cc:306
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage info() const
Flow for an information message.
ITraceMng * traceMng() const
Trace manager.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.