Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
VariableDataInfo.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/* VariableDataInfo.h (C) 2000-2024 */
9/* */
10/* Informations sur les données d'une variable. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/std/internal/VariableDataInfo.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/JSONWriter.h"
18#include "arcane/utils/JSONReader.h"
19#include "arcane/utils/Array.h"
20#include "arcane/utils/ValueConvert.h"
21#include "arcane/utils/Ref.h"
22
23#include "arcane/core/ISerializedData.h"
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane::impl
29{
30
31// TODO: utiliser version avec exception pour la lecture JSON si les
32// conversions ne sont pas valides (par exemple on attend un réel et on a
33// une chaîne de caractère.
34
35namespace
36{
37 static void _addAttribute(XmlNode& node, const String& attr_name, Int64 value)
38 {
39 node.setAttrValue(attr_name, String::fromNumber(value));
40 }
41
42 static void _addAttribute(XmlNode& node, const String& attr_name, const String& value)
43 {
44 node.setAttrValue(attr_name, value);
45 }
46
47 static Integer _readInteger(const XmlNode& node, const String& attr_name)
48 {
49 return node.attr(attr_name, true).valueAsInteger(true);
50 }
51
52 static Int64 _readInt64(const XmlNode& node, const String& attr_name)
53 {
54 return node.attr(attr_name, true).valueAsInt64(true);
55 }
56
57 static bool _readBool(const XmlNode& node, const String& attr_name)
58 {
59 return node.attr(attr_name, true).valueAsBoolean(true);
60 }
61
62 static void _addAttribute(JSONWriter& o, const String& attr_name, Int64 value)
63 {
64 o.write(attr_name, value);
65 }
66
67 static void _addAttribute(JSONWriter& o, const String& attr_name, Int32 value)
68 {
69 o.write(attr_name, value);
70 }
71
72 static void _addAttribute(JSONWriter& o, const String& attr_name, const String& value)
73 {
74 o.write(attr_name, value);
75 }
76
77 static void _addAttribute(JSONWriter& o, const String& attr_name, bool value)
78 {
79 o.write(attr_name, value);
80 }
81
82 static Int32 _readInteger(const JSONValue& jvalue, const String& attr_name)
83 {
84 return jvalue.expectedChild(attr_name).valueAsInt32();
85 }
86
87 static Int64 _readInt64(const JSONValue& jvalue, const String& attr_name)
88 {
89 return jvalue.expectedChild(attr_name).valueAsInt64();
90 }
91
92 static bool _readBool(const JSONValue& jvalue, const String& attr_name)
93 {
94 return jvalue.expectedChild(attr_name).valueAsBool();
95 }
96} // namespace
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101VariableDataInfo::
102VariableDataInfo(const String& full_name, const ISerializedData* sdata)
103: m_full_name(full_name)
104, m_nb_dimension(sdata->nbDimension())
105, m_nb_element(sdata->nbElement())
106, m_nb_base_element(sdata->nbBaseElement())
107, m_is_multi_size(sdata->isMultiSize())
108{
109 Int64ConstArrayView extents = sdata->extents();
110
111 if (m_nb_dimension == 2 && !m_is_multi_size) {
112 m_dim1_size = extents[0];
113 m_dim2_size = extents[1];
114 }
115 m_dimension_array_size = extents.size();
116 m_base_data_type = sdata->baseDataType();
117 m_memory_size = sdata->memorySize();
118 m_shape = sdata->shape();
119}
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124VariableDataInfo::
125VariableDataInfo(const String& full_name, const XmlNode& element)
126: m_full_name(full_name)
127{
128 m_nb_dimension = _readInteger(element, V_NB_DIMENSION);
129 m_dim1_size = _readInt64(element, V_DIM1_SIZE);
130 m_dim2_size = _readInt64(element, V_DIM2_SIZE);
131 m_nb_element = _readInt64(element, V_NB_ELEMENT);
132 m_nb_base_element = _readInt64(element, V_NB_BASE_ELEMENT);
133 m_dimension_array_size = _readInteger(element, V_DIMENSION_ARRAY_SIZE);
134 m_is_multi_size = _readBool(element, V_IS_MULTI_SIZE);
135 m_base_data_type = (eDataType)_readInteger(element, V_BASE_DATA_TYPE);
136 m_memory_size = _readInt64(element, V_MEMORY_SIZE);
137 m_file_offset = _readInt64(element, V_FILE_OFFSET);
138 // L'élément est nul si on repart d'une veille protection (avant Arcane 3.7)
139 XmlNode shape_attr = element.attr(V_SHAPE);
140 if (!shape_attr.null()) {
141 String shape_str = shape_attr.value();
142 if (!shape_str.empty()) {
143 UniqueArray<Int32> values;
144 if (builtInGetValue(values, shape_str))
145 ARCANE_FATAL("Can not read values '{0}' for attribute 'shape'", shape_str);
146 m_shape.setDimensions(values);
147 }
148 }
149 {
150 // L'attribut 'compare-hash' est nul si on repart d'une veille protection (avant Arcane 3.12)
151 XmlNode hash_attr = element.attr(V_COMPARISON_HASH);
152 if (!hash_attr.null())
153 m_comparison_hash_value = hash_attr.value();
154 }
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160VariableDataInfo::
161VariableDataInfo(const String& full_name, const JSONValue& jvalue)
162: m_full_name(full_name)
163{
164 // NOTE: Le format JSON n'est valide qu'à partir de la version 3.12 de Arcane.
165 m_nb_dimension = _readInteger(jvalue, V_NB_DIMENSION);
166 m_dim1_size = _readInt64(jvalue, V_DIM1_SIZE);
167 m_dim2_size = _readInt64(jvalue, V_DIM2_SIZE);
168 m_nb_element = _readInt64(jvalue, V_NB_ELEMENT);
169 m_nb_base_element = _readInt64(jvalue, V_NB_BASE_ELEMENT);
170 m_dimension_array_size = _readInteger(jvalue, V_DIMENSION_ARRAY_SIZE);
171 m_is_multi_size = _readBool(jvalue, V_IS_MULTI_SIZE);
172 m_base_data_type = (eDataType)_readInteger(jvalue, V_BASE_DATA_TYPE);
173 m_memory_size = _readInt64(jvalue, V_MEMORY_SIZE);
174 m_file_offset = _readInt64(jvalue, V_FILE_OFFSET);
175 // L'élément est nul si on repart d'une veille protection (avant Arcane 3.7)
176 {
177 String shape_str = jvalue.expectedChild(V_SHAPE).valueAsStringView();
178 if (!shape_str.empty()) {
179 UniqueArray<Int32> values;
180 if (builtInGetValue(values, shape_str))
181 ARCANE_FATAL("Can not read values '{0}' for attribute 'shape'", shape_str);
182 m_shape.setDimensions(values);
183 }
184 }
185 m_comparison_hash_value = jvalue.expectedChild(V_COMPARISON_HASH).valueAsStringView();
186}
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
190
191void VariableDataInfo::
192write(XmlNode element, JSONWriter& writer) const
193{
194 _write(element);
195 _write(writer);
196}
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
200
201void VariableDataInfo::
202_write(XmlNode element) const
203{
204 _addAttribute(element, V_NB_DIMENSION, m_nb_dimension);
205 _addAttribute(element, V_DIM1_SIZE, m_dim1_size);
206 _addAttribute(element, V_DIM2_SIZE, m_dim2_size);
207 _addAttribute(element, V_NB_ELEMENT, m_nb_element);
208 _addAttribute(element, V_NB_BASE_ELEMENT, m_nb_base_element);
209 _addAttribute(element, V_DIMENSION_ARRAY_SIZE, m_dimension_array_size);
210 _addAttribute(element, V_IS_MULTI_SIZE, (m_is_multi_size) ? 1 : 0);
211 _addAttribute(element, V_BASE_DATA_TYPE, (Integer)m_base_data_type);
212 _addAttribute(element, V_MEMORY_SIZE, m_memory_size);
213 _addAttribute(element, V_FILE_OFFSET, m_file_offset);
214 _addAttribute(element, V_SHAPE_SIZE, m_shape.dimensions().size());
215 _addAttribute(element, V_COMPARISON_HASH, m_comparison_hash_value);
216 {
217 String s;
218 if (builtInPutValue(m_shape.dimensions().smallView(), s))
219 ARCANE_FATAL("Can not write '{0}'", m_shape.dimensions());
220 _addAttribute(element, V_SHAPE, s);
221 }
222}
223
224/*---------------------------------------------------------------------------*/
225/*---------------------------------------------------------------------------*/
226
227void VariableDataInfo::
228_write(JSONWriter& writer) const
229{
230 JSONWriter::Object o(writer, m_full_name);
231 _addAttribute(writer, V_NB_DIMENSION, m_nb_dimension);
232 _addAttribute(writer, V_DIM1_SIZE, m_dim1_size);
233 _addAttribute(writer, V_DIM2_SIZE, m_dim2_size);
234 _addAttribute(writer, V_NB_ELEMENT, m_nb_element);
235 _addAttribute(writer, V_NB_BASE_ELEMENT, m_nb_base_element);
236 _addAttribute(writer, V_DIMENSION_ARRAY_SIZE, m_dimension_array_size);
237 _addAttribute(writer, V_IS_MULTI_SIZE, m_is_multi_size);
238 _addAttribute(writer, V_BASE_DATA_TYPE, (Integer)m_base_data_type);
239 _addAttribute(writer, V_MEMORY_SIZE, m_memory_size);
240 _addAttribute(writer, V_FILE_OFFSET, m_file_offset);
241 _addAttribute(writer, V_SHAPE_SIZE, m_shape.dimensions().size());
242 _addAttribute(writer, V_COMPARISON_HASH, m_comparison_hash_value);
243 {
244 String s;
245 if (builtInPutValue(m_shape.dimensions().smallView(), s))
246 ARCANE_FATAL("Can not write '{0}'", m_shape.dimensions());
247 _addAttribute(writer, V_SHAPE, s);
248 }
249}
250
251/*---------------------------------------------------------------------------*/
252/*---------------------------------------------------------------------------*/
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257Ref<VariableDataInfo> VariableDataInfoMap::
258_add(VariableDataInfo* v)
259{
260 auto vref = makeRef(v);
261 m_data_info_map.insert(std::make_pair(v->fullName(), vref));
262 return vref;
263}
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268Ref<VariableDataInfo> VariableDataInfoMap::
269add(const String& full_name, const ISerializedData* sdata)
270{
271 return _add(new VariableDataInfo(full_name, sdata));
272}
273
274/*---------------------------------------------------------------------------*/
275/*---------------------------------------------------------------------------*/
276
277Ref<VariableDataInfo> VariableDataInfoMap::
278add(const String& full_name, const XmlNode& node)
279{
280 return _add(new VariableDataInfo(full_name, node));
281}
282
283/*---------------------------------------------------------------------------*/
284/*---------------------------------------------------------------------------*/
285
286Ref<VariableDataInfo> VariableDataInfoMap::
287add(const String& full_name, const JSONValue& jvalue)
288{
289 return _add(new VariableDataInfo(full_name, jvalue));
290}
291
292/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
294
295Ref<VariableDataInfo> VariableDataInfoMap::
296find(const String& full_name) const
297{
298 auto ivar = m_data_info_map.find(full_name);
299 if (ivar != m_data_info_map.end())
300 return ivar->second;
301 return {};
302}
303
304/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
307} // namespace Arcane::impl
308
309/*---------------------------------------------------------------------------*/
310/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Interface d'une donnée sérialisée.
Représente une valeur JSON.
Definition JSONReader.h:43
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Noeud d'un arbre DOM.
Definition XmlNode.h:51
Informations sur les données d'une variable.
constexpr Integer size() const noexcept
Nombre d'éléments du tableau.
Chaîne de caractères unicode.
bool builtInPutValue(const String &v, String &s)
Converti la valeur v dans la chaîne s.
Definition Convert.cc:291
eDataType
Type d'une donnée.
Definition DataTypes.h:39
ConstArrayView< Int64 > Int64ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 64 bits.
Definition UtilsTypes.h:638
Int32 Integer
Type représentant un entier.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.