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