Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
PropertyMng.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/* PropertyMng.cc (C) 2000-2024 */
9/* */
10/* Property Manager. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/TraceAccessor.h"
15#include "arcane/utils/String.h"
16#include "arcane/utils/Ref.h"
17
18#include "arcane/core/IPropertyMng.h"
19#include "arcane/core/Properties.h"
20#include "arcane/core/ISerializer.h"
21#include "arcane/core/SerializeBuffer.h"
22#include "arcane/core/VariableTypes.h"
23#include "arcane/core/Observable.h"
24
25#include <map>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
39class PropertyMng
40: public TraceAccessor
41, public IPropertyMng
42{
43 public:
44
45 static const Int32 SERIALIZE_VERSION = 1;
46
47 public:
48
49 explicit PropertyMng(ITraceMng* tm);
50 ~PropertyMng() override;
51
52 public:
53
54 void build();
55
56 public:
57
58 ITraceMng* traceMng() const override { return TraceAccessor::traceMng(); }
59
60 public:
61
62 PropertiesImpl* getPropertiesImpl(const String& full_name) override;
63 void destroyProperties(const Properties& p) override;
64 void registerProperties(const Properties& p) override;
65 void serialize(ISerializer* serializer) override;
66 void writeTo(ByteArray& bytes) override;
67 void readFrom(Span<const Byte> bytes) override;
68 void print(std::ostream& o) const override;
69 IObservable* writeObservable() override { return &m_write_observable; }
70 IObservable* readObservable() override { return &m_read_observable; }
71
72 private:
73 private:
74
75 typedef std::map<String, Properties> PropertiesMapType;
76
77 PropertiesMapType m_properties_map;
78 VariableArrayByte* m_property_values_var;
79 AutoDetachObservable m_write_observable;
80 AutoDetachObservable m_read_observable;
81};
82
83/*---------------------------------------------------------------------------*/
84/*---------------------------------------------------------------------------*/
85
86extern "C++" IPropertyMng*
87arcaneCreatePropertyMng(ITraceMng* tm)
88{
89 auto pm = new PropertyMng(tm);
90 pm->build();
91 return pm;
92}
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
97extern "C++" Ref<IPropertyMng>
98arcaneCreatePropertyMngReference(ITraceMng* tm)
99{
100 auto pm = arcaneCreatePropertyMng(tm);
101 return makeRef(pm);
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107PropertyMng::
108PropertyMng(ITraceMng* tm)
109: TraceAccessor(tm)
110, m_property_values_var(nullptr)
111{
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117PropertyMng::
118~PropertyMng()
119{
120 delete m_property_values_var;
121}
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
129void PropertyMng::
130build()
131{
132}
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
138getPropertiesImpl(const String& full_name)
139{
140 //info() << "GETTING PROPERTIES name=" << full_name;
141 auto v = m_properties_map.find(full_name);
142 if (v != m_properties_map.end())
143 return v->second.impl();
144 return 0;
145}
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
152{
153 //TODO: check if not yet present.
154 m_properties_map.insert(std::make_pair(p.fullName(), p));
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
162{
163 auto v = m_properties_map.find(p.fullName());
164 if (v != m_properties_map.end())
165 m_properties_map.erase(v);
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
172serialize(ISerializer* serializer)
173{
174 switch (serializer->mode()) {
175 case ISerializer::ModeReserve:
176
177 {
178 serializer->reserveInt32(1); // SERIALIZE_VERSION
179 serializer->reserveInt64(1); // Nombre d'éléments dans la map
180
181 for (auto& v : m_properties_map) {
182 serializer->reserve(v.first);
183 //info() << "SERIALIZE RESERVE name=" << v->first;
184 v.second.serialize(serializer);
185 }
186 } break;
188 serializer->putInt32(SERIALIZE_VERSION); // SERIALIZE_VERSION
189 serializer->putInt64(m_properties_map.size()); // Nombre d'éléments dans la map
190
191 for (auto& v : m_properties_map) {
192 serializer->put(v.first);
193 //info() << "SERIALIZE PUT name=" << v->first;
194 v.second.serialize(serializer);
195 }
196 } break;
198
199 Int64 version = serializer->getInt32();
200 if (version != SERIALIZE_VERSION) {
201 // Reading is done with a protection from an old version of Arcane
202 // which is not compatible. Displays a warning and does nothing.
203 // (NOTE: this still risks causing problems for those who use the
204 // properties, so perhaps a fatal error should be raised?)
205 pwarning() << "Can not reading properties from imcompatible checkpoint";
206 return;
207 }
208
209 Int64 n = serializer->getInt64();
210 String name;
211 for (Integer i = 0; i < n; ++i) {
212 serializer->get(name);
213 Properties p(this, name);
214 //info() << "SERIALIZE GET name=" << name;
215 p.serialize(serializer);
216 }
217 break;
218 }
219}
220
221/*---------------------------------------------------------------------------*/
222/*---------------------------------------------------------------------------*/
223
225writeTo(ByteArray& bytes)
226{
227 m_write_observable.notifyAllObservers();
228
230 sb.setSerializeTypeInfo(true);
231 sb.setMode(ISerializer::ModeReserve);
232 this->serialize(&sb);
233 sb.allocateBuffer();
235 this->serialize(&sb);
236
237 Span<const Byte> buf_bytes = sb.globalBuffer();
238 info(4) << "SaveProperties nb_byte=" << buf_bytes.size();
239 bytes.copy(buf_bytes);
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
247{
248 info(4) << "ReadProperties nb_read_byte=" << bytes.size();
249
251 sb.setSerializeTypeInfo(true);
252 sb.initFromBuffer(bytes);
253 this->serialize(&sb);
254
255 m_read_observable.notifyAllObservers();
256}
257
258/*---------------------------------------------------------------------------*/
259/*---------------------------------------------------------------------------*/
260
265print(std::ostream& o) const
266{
267 for (const auto& v : m_properties_map) {
268 const Properties& p = v.second;
269 o << '[' << p.fullName() << "]\n";
270 p.print(o);
271 o << '\n';
272 }
273}
274
275/*---------------------------------------------------------------------------*/
276/*---------------------------------------------------------------------------*/
277
278} // End namespace Arcane
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
Observable that automatically calls IObservable::detachAllObservers() in the destructor.
void initFromBuffer(Span< const Byte > buf)
Initializes the serializer for reading from the data buf.
void setSerializeTypeInfo(bool v)
Indicates whether to serialize the data type to ensure consistency.
void allocateBuffer() override
Allocates the serializer memory.
void setMode(eMode new_mode) override
Sets the current mode.
Interface of the property manager.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual Int64 getInt64()=0
Retrieve a size.
virtual void put(Span< const Real > values)=0
Add the array values.
virtual void putInt32(Int32 value)=0
Add the integer value.
virtual eMode mode() const =0
Current operating mode.
virtual Int32 getInt32()=0
Retrieve an integer.
virtual void get(ArrayView< Real > values)=0
Retrieve the array values.
virtual void putInt64(Int64 value)=0
Add the integer value.
List of properties.
Definition Properties.h:65
void serialize(ISerializer *serializer)
Performs the serialization of the properties.
const String & fullName() const
Full name of the property.
void print(std::ostream &o) const
Prints the properties and their values to the stream o.
void print(std::ostream &o) const override
Displays the properties and their values on the stream o.
void destroyProperties(const Properties &p) override
Deletes the properties referenced by p.
IObservable * readObservable() override
Observable for reading.
IObservable * writeObservable() override
Observable for writing.
void serialize(ISerializer *serializer) override
Performs serialization.
PropertiesImpl * getPropertiesImpl(const String &full_name) override
Retrieves the list of properties by full name full_name.
void writeTo(ByteArray &bytes) override
Serializes property information into bytes.
void registerProperties(const Properties &p) override
Registers the properties referenced by p.
void readFrom(Span< const Byte > bytes) override
Reads the serialized information contained in bytes.
Reference to an instance.
Implementation of a buffer for serialization.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage info() const
Flow for an information message.
ITraceMng * traceMng() const
Trace manager.
TraceMessage pwarning() const
VariableRefArrayT< Byte > VariableArrayByte
Array variable of byte type.
-- 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.
Array< Byte > ByteArray
Dynamic one-dimensional array of characters.
Definition UtilsTypes.h:121
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.