Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
AlinaUtils.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/* AlinaUtils.cc (C) 2000-2026 */
9/* */
10/* Classes utilitaires. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arccore/alina/AlinaUtils.h"
17
18#include <boost/property_tree/ptree.hpp>
19#include <boost/property_tree/json_parser.hpp>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane::Alina
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29using BoostPTree = boost::property_tree::ptree;
30
31namespace detail
32{
33 const BoostPTree& empty_ptree()
34 {
35 static const BoostPTree p;
36 return p;
37 }
38
39 // To access PropertyTree::m_property_tree as 'boost::property_tree::ptree'
41 {
42 public:
43
44 static const BoostPTree& toBoostPTree(const PropertyTree& p)
45 {
46 return *(static_cast<const BoostPTree*>(p.m_property_tree));
47 }
48 static BoostPTree& toBoostPTree(PropertyTree& p)
49 {
50 return *(static_cast<BoostPTree*>(p.m_property_tree));
51 }
52 };
53
54} // namespace detail
55
56namespace
57{
58 const BoostPTree& toBoostPTree(const PropertyTree& p)
59 {
60 return detail::PropertyWrapper::toBoostPTree(p);
61 }
62 const BoostPTree& toBoostPTree(const PropertyTree* p)
63 {
64 return detail::PropertyWrapper::toBoostPTree(*p);
65 }
66 BoostPTree& toBoostPTree(PropertyTree* p)
67 {
68 return detail::PropertyWrapper::toBoostPTree(*p);
69 }
70} // namespace
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75PropertyTree::
76PropertyTree()
77: m_property_tree(new BoostPTree())
78, m_is_own(true)
79{
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85PropertyTree::
86PropertyTree(const PropertyTree& rhs)
87{
88 if (rhs.m_is_own) {
89 m_property_tree = new BoostPTree(toBoostPTree(rhs));
90 m_is_own = true;
91 }
92 else {
93 m_property_tree = rhs.m_property_tree;
94 m_is_own = false;
95 }
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101/*PropertyTree::
102PropertyTree(const BoostPTree& x)
103: m_property_tree(new BoostPTree(x))
104, m_is_own(true)
105{}*/
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110PropertyTree::
111~PropertyTree()
112{
113 if (m_is_own)
114 delete &toBoostPTree(this);
115}
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120PropertyTree PropertyTree::
121get_child_empty(const std::string& path) const
122{
123 const BoostPTree& child = toBoostPTree(this).get_child(path, detail::empty_ptree());
124 PropertyTree p;
125 p.m_property_tree = const_cast<BoostPTree*>(&child);
126 p.m_is_own = false;
127 return p;
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
133bool PropertyTree::
134erase(const char* name)
135{
136 return toBoostPTree(this).erase(name);
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
142size_t PropertyTree::
143count(const char* name) const
144{
145 return toBoostPTree(this).count(name);
146}
147
148/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151void PropertyTree::
152read_json(const std::string& filename)
153{
154 BoostPTree& p = toBoostPTree(this);
155 boost::property_tree::json_parser::read_json(filename, p);
156}
157
158Int32 PropertyTree::get(const char* param_type, Int32 default_value) const
159{
160 return toBoostPTree(this).get(param_type, default_value);
161}
162Int64 PropertyTree::get(const char* param_type, Int64 default_value) const
163{
164 return toBoostPTree(this).get(param_type, default_value);
165}
166double PropertyTree::get(const char* param_type, double default_value) const
167{
168 return toBoostPTree(this).get(param_type, default_value);
169}
170double* PropertyTree::get(const char* param_type, double* default_value) const
171{
172 return toBoostPTree(this).get(param_type, default_value);
173}
174void* PropertyTree::get(const char* param_type, void* default_value) const
175{
176 return toBoostPTree(this).get(param_type, default_value);
177}
178std::string PropertyTree::get(const char* param_type, const std::string& default_value) const
179{
180 return toBoostPTree(this).get(param_type, default_value);
181}
182
183void PropertyTree::put(const std::string& path, Int32 value)
184{
185 toBoostPTree(this).put(path, value);
186}
187void PropertyTree::put(const std::string& path, Int64 value)
188{
189 toBoostPTree(this).put(path, value);
190}
191void PropertyTree::put(const std::string& path, double value)
192{
193 toBoostPTree(this).put(path, value);
194}
195void PropertyTree::put(const std::string& path, const std::string& value)
196{
197 toBoostPTree(this).put(path, value);
198}
199void PropertyTree::put(const std::string& path, double* value)
200{
201 toBoostPTree(this).put(path, value);
202}
203void PropertyTree::put(const std::string& path, void* value)
204{
205 toBoostPTree(this).put(path, value);
206}
207
208/*---------------------------------------------------------------------------*/
209/*---------------------------------------------------------------------------*/
210
211void PropertyTree::
212_addChild(const std::string& path, const char* name,
213 const PropertyTree& obj)
214{
215 auto& p = toBoostPTree(this);
216 p.add_child(std::string(path) + name, toBoostPTree(obj));
217}
218
219/*---------------------------------------------------------------------------*/
220/*---------------------------------------------------------------------------*/
221
222void PropertyTree::
223check_params(const std::set<std::string>& names) const
224{
225 const auto& p = toBoostPTree(this);
226 bool has_error = false;
227 for (const auto& n : names) {
228 if (!p.count(n)) {
229 ARCCORE_ALINA_PARAM_MISSING(n);
230 }
231 }
232 for (const auto& v : p) {
233 if (!names.count(v.first)) {
234 std::cerr << "WARNING: unknown parameter " << v.first << "\n";
235 has_error = true;
236 }
237 }
238 if (has_error)
239 ARCANE_FATAL("Invalid parameters");
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245void PropertyTree::
246check_params(const std::set<std::string>& names,
247 const std::set<std::string>& opt_names) const
248{
249 const auto& p = toBoostPTree(this);
250 bool has_error = false;
251
252 for (const auto& n : names) {
253 if (!p.count(n)) {
254 ARCCORE_ALINA_PARAM_MISSING(n);
255 }
256 }
257 for (const auto& n : opt_names) {
258 if (!p.count(n)) {
259 ARCCORE_ALINA_PARAM_MISSING(n);
260 }
261 }
262 for (const auto& v : p) {
263 if (!names.count(v.first) && !opt_names.count(v.first)) {
264 std::cerr << "WARNING: unknown parameter " << v.first << "\n";
265 has_error = true;
266 }
267 }
268 if (has_error)
269 ARCANE_FATAL("Invalid parameters");
270}
271
272/*---------------------------------------------------------------------------*/
273/*---------------------------------------------------------------------------*/
274
275void PropertyTree::
276putKeyValue(const std::string& param)
277{
278 auto& p = toBoostPTree(this);
279 size_t eq_pos = param.find('=');
280 if (eq_pos == std::string::npos)
281 ARCANE_FATAL("param in put() should have \"key=value\" format (param='{0}')", param);
282 p.put(param.substr(0, eq_pos), param.substr(eq_pos + 1));
283}
284
285/*---------------------------------------------------------------------------*/
286/*---------------------------------------------------------------------------*/
287
288detail::empty_params::
289empty_params(const PropertyTree& ap)
290{
291 const auto& p = toBoostPTree(ap);
292 for (const auto& v : p) {
293 std::cerr << "Alina: unknown parameter " << v.first << "\n";
294 }
295}
296
297/*---------------------------------------------------------------------------*/
298/*---------------------------------------------------------------------------*/
299
300std::ostream& operator<<(std::ostream& o, const PropertyTree& obj)
301{
302 const auto& p = toBoostPTree(obj);
303 std::ostringstream ostr;
304 boost::property_tree::json_parser::write_json(ostr, p);
305 o << ostr.str();
306 return o;
307}
308
309/*---------------------------------------------------------------------------*/
310/*---------------------------------------------------------------------------*/
311
312} // namespace Arcane::Alina
313
314/*---------------------------------------------------------------------------*/
315/*---------------------------------------------------------------------------*/
Fichier de configuration d'Arcane.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
std::int64_t Int64
Type entier signé sur 64 bits.
std::int32_t Int32
Type entier signé sur 32 bits.