Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Configuration.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* Gestion des options de configuration de l'exécution. */
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/Configuration.h"
25#include "arcane/XmlNode.h"
26#include "arcane/XmlNodeList.h"
27
28#include <map>
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33ARCANE_BEGIN_NAMESPACE
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38class IApplication;
39class Configuration;
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
45: public TraceAccessor
46, public IConfigurationMng
47{
48 public:
49
52
53 public:
54
55 virtual IConfiguration* defaultConfiguration() const;
56 virtual IConfiguration* createConfiguration();
57
58 private:
59
60 Configuration* m_default_configuration;
61
62 private:
63
64 Configuration* _createConfiguration();
65};
66
67/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69
72{
73 public:
74
75 ConfigurationSection(const Configuration* configuration,const String& base_name)
76 : m_configuration(configuration), m_base_name(base_name){}
77
78 virtual ~ConfigurationSection() {}
79
80 public:
81
82 virtual Int32 value(const String& name,Int32 default_value) const;
83 virtual Int64 value(const String& name,Int64 default_value) const;
84 virtual Real value(const String& name,Real default_value) const;
85 virtual bool value(const String& name,bool default_value) const;
86 virtual String value(const String& name,const String& default_value) const;
87 virtual String value(const String& name,const char* default_value) const;
88
89 virtual Integer valueAsInteger(const String& name,Integer default_value) const
90 { return value(name,default_value); }
91 virtual Int32 valueAsInt32(const String& name,Int32 default_value) const
92 { return value(name,default_value); }
93 virtual Int64 valueAsInt64(const String& name,Int64 default_value) const
94 { return value(name,default_value); }
95 virtual Real valueAsReal(const String& name,Real default_value) const
96 { return value(name,default_value); }
97 virtual bool valueAsBool(const String& name,bool default_value) const
98 { return value(name,default_value); }
99 virtual String valueAsString(const String& name,const String& default_value) const
100 { return value(name,default_value); }
101
102 private:
103
104 const Configuration* m_configuration;
105 String m_base_name;
106};
107
108/*---------------------------------------------------------------------------*/
109/*---------------------------------------------------------------------------*/
110
112: public TraceAccessor
113, public IConfiguration
114{
116 {
117 ConfigValue(const String& value,Integer priority)
118 : m_value(value), m_priority(priority){}
119 const String& value() const { return m_value; }
120 Integer priority() const { return m_priority; }
121 private:
122 String m_value;
123 Integer m_priority;
124 };
125
126 typedef std::map<String,ConfigValue> KeyValueMap;
127
128 public:
129
131
132 public:
133
134 IConfigurationSection* createSection(const String& name) const override
135 {
136 return new ConfigurationSection(this,name);
137 }
138
139 IConfigurationSection* mainSection() const override { return m_main_section.get(); }
140
141 void addValue(const String& name,const String& value,Integer priority) override;
142 IConfiguration* clone() const override;
143 void merge(const IConfiguration* c) override;
144 void dump() const override;
145 void dump(std::ostream& o) const override;
146
147 public:
148
149 template<typename T> T getValue(const String& base_name,const String& name,T default_value) const
150 {
151 T v = T();
152 KeyValueMap::const_iterator i;
153 if (base_name.null())
154 i = m_values.find(name);
155 else
156 i = m_values.find(base_name+"."+name);
157
158 if (i==m_values.end())
159 return default_value;
160
161 String value = i->second.value();
162 if (builtInGetValue(v,value))
163 throw FatalErrorException(A_FUNCINFO,String::format("Can not convert '{0}' to type '{1}'",value,_typeName((T*)0)));
164
165 return v;
166 }
167
168 private:
169
170 static const char* _typeName(Int32*) { return "Int32"; }
171 static const char* _typeName(Int64*) { return "Int64"; }
172 static const char* _typeName(Real*) { return "Real"; }
173 static const char* _typeName(bool*) { return "bool"; }
174 static const char* _typeName(String*) { return "String"; }
175
176 void _checkAdd(const String& name,const String& value,Integer priority)
177 {
178 KeyValueMap::iterator i = m_values.find(name);
179 //info() << "CHECK_ADD name=" << name << " value=" << value << " priority=" << priority;
180 if (i==m_values.end()){
181 //info() << "NOT_FOUND name=" << name << " value=" << value << " priority=" << priority;
182 m_values.insert(std::make_pair(name,ConfigValue(value,priority)));
183 }
184 else{
185 Integer orig_priority = i->second.priority();
186 //info() << "FOUND name=" << name << " value=" << value << " orig_priority=" << orig_priority;
187 if (priority<orig_priority){
188 //info() << "REPLACING name=" << name << " value=" << value << " new_priority=" << priority;
189 i->second = ConfigValue(value,priority);
190 }
191 }
192 }
193
194 private:
195
196 ConfigurationMng* m_configuration_mng;
197 KeyValueMap m_values;
198 ScopedPtrT<IConfigurationSection> m_main_section;
199};
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
204/*---------------------------------------------------------------------------*/
205/*---------------------------------------------------------------------------*/
206
207Int32 ConfigurationSection::
208value(const String& name,Int32 default_value) const
209{
210 return m_configuration->getValue(m_base_name,name,default_value);
211}
212
213Int64 ConfigurationSection::
214value(const String& name,Int64 default_value) const
215{
216 return m_configuration->getValue(m_base_name,name,default_value);
217}
218
219Real ConfigurationSection::
220value(const String& name,Real default_value) const
221{
222 return m_configuration->getValue(m_base_name,name,default_value);
223}
224
225bool ConfigurationSection::
226value(const String& name,bool default_value) const
227{
228 return m_configuration->getValue(m_base_name,name,default_value);
229}
230
231String ConfigurationSection::
232value(const String& name,const String& default_value) const
233{
234 return m_configuration->getValue(m_base_name,name,default_value);
235}
236
237String ConfigurationSection::
238value(const String& name,const char* default_value) const
239{
240 return m_configuration->getValue(m_base_name,name,String(default_value));
241}
242
243/*---------------------------------------------------------------------------*/
244/*---------------------------------------------------------------------------*/
245
246/*---------------------------------------------------------------------------*/
247/*---------------------------------------------------------------------------*/
248
249Configuration::
250Configuration(ConfigurationMng* cm,ITraceMng* tm)
251: TraceAccessor(tm),
252 m_configuration_mng(cm),
253 m_main_section(new ConfigurationSection(this,String()))
254{
255}
256
257/*---------------------------------------------------------------------------*/
258/*---------------------------------------------------------------------------*/
259
261clone() const
262{
263 Configuration* cf = new Configuration(m_configuration_mng,traceMng());
264
265 for( auto& i : m_values ){
266 cf->m_values.insert(std::make_pair(i.first,i.second));
267 }
268
269 return cf;
270}
271
272/*---------------------------------------------------------------------------*/
273/*---------------------------------------------------------------------------*/
274
276dump() const
277{
279 dump(ostr());
280 info() << ostr.str();
281}
282
283/*---------------------------------------------------------------------------*/
284/*---------------------------------------------------------------------------*/
285
287dump(std::ostream& o) const
288{
289 o << "Configuration:\n";
290 for( auto& i : m_values ){
291 String s1 = i.first;
292 String s2 = i.second.value();
293 o << " name=" << s1 << " value=" << s2 << " (" << i.second.priority() << ")\n";
294 }
295}
296
297/*---------------------------------------------------------------------------*/
298/*---------------------------------------------------------------------------*/
299
301merge(const IConfiguration* c)
302{
303 const Configuration* cc = ARCANE_CHECK_POINTER(dynamic_cast<const Configuration*>(c));
304 for( auto& i : cc->m_values ){
305 String s1 = i.first;
306 const ConfigValue& cv = i.second;
307 //String s2 = i->second.value();
308 _checkAdd(s1,cv.value(),cv.priority());
309 //info() << "MERGING CONFIGURATION name=" << s1 << " value=" << s2;
310 //m_values[s1] = s2;
311 }
312}
313
314/*---------------------------------------------------------------------------*/
315/*---------------------------------------------------------------------------*/
316
318addValue(const String& name,const String& value,Integer priority)
319{
320 _checkAdd(name,value,priority);
321}
322
323/*---------------------------------------------------------------------------*/
324/*---------------------------------------------------------------------------*/
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329ConfigurationMng::
330ConfigurationMng(ITraceMng* tm)
332, m_default_configuration(0)
333{
334 m_default_configuration = _createConfiguration();
335}
336
337/*---------------------------------------------------------------------------*/
338/*---------------------------------------------------------------------------*/
339
340ConfigurationMng::
341~ConfigurationMng()
342{
343 delete m_default_configuration;
344}
345
346/*---------------------------------------------------------------------------*/
347/*---------------------------------------------------------------------------*/
348
349Configuration* ConfigurationMng::
350_createConfiguration()
351{
352 return new Configuration(this,traceMng());
353}
354
355/*---------------------------------------------------------------------------*/
356/*---------------------------------------------------------------------------*/
357
360{
361 return m_default_configuration;
362}
363
364/*---------------------------------------------------------------------------*/
365/*---------------------------------------------------------------------------*/
366
369{
370 return _createConfiguration();
371}
372
373/*---------------------------------------------------------------------------*/
374/*---------------------------------------------------------------------------*/
375
376/*---------------------------------------------------------------------------*/
377/*---------------------------------------------------------------------------*/
378
379extern "C++" IConfigurationMng*
380arcaneCreateConfigurationMng(ITraceMng* tm)
381{
383 return cm;
384}
385
386/*---------------------------------------------------------------------------*/
387/*---------------------------------------------------------------------------*/
388
389ARCANE_END_NAMESPACE
390
391/*---------------------------------------------------------------------------*/
392/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
virtual IConfiguration * defaultConfiguration() const
Configuration par défaut.
virtual IConfiguration * createConfiguration()
Créé une nouvelle configuration.
void dump() const override
Affiche les valeurs des paramètres de configuration via le traceMng()
void merge(const IConfiguration *c) override
Fusionne cette configuration avec la configuration c.
IConfigurationSection * createSection(const String &name) const override
Créé une section de configuration.
IConfiguration * clone() const override
Clone cette configuration.
IConfigurationSection * mainSection() const override
Section principale.
void addValue(const String &name, const String &value, Integer priority) override
Ajout une valeur à la configuration.
Gestionnaire de configuration.
Interface d'une section de configuration.
Interface d'une configuration.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Flot de sortie lié à une String.
Exception lorsqu'une erreur fatale est survenue.
Interface du gestionnaire de traces.
Chaîne de caractères unicode.
ITraceMng * traceMng() const
Gestionnaire de trace.
TraceMessage info() const
Flot pour un message d'information.
Int32 Integer
Type représentant un entier.