Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
StringScalarData.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/* StringScalarData.cc (C) 2000-2023 */
9/* */
10/* Scalar data of type 'String'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arcane/utils/ArgumentException.h"
17#include "arcane/utils/NotSupportedException.h"
18#include "arcane/utils/TraceInfo.h"
19#include "arcane/utils/IHashAlgorithm.h"
20#include "arcane/utils/NotImplementedException.h"
21#include "arcane/utils/Array.h"
22#include "arcane/utils/ArrayShape.h"
23
24#include "arcane/core/datatype/DataAllocationInfo.h"
25#include "arcane/core/datatype/DataStorageTypeInfo.h"
26#include "arcane/core/datatype/DataStorageBuildInfo.h"
27#include "arcane/core/datatype/DataTypeTraits.h"
28
29#include "arcane/impl/SerializedData.h"
30#include "arcane/impl/DataStorageFactory.h"
31
32#include "arcane/core/ISerializer.h"
33#include "arcane/core/IDataVisitor.h"
34
35#include "arcane/core/internal/IDataInternal.h"
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40namespace Arcane
41{
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
49class StringScalarData
51, public IScalarDataT<String>
52{
54
55 public:
56
57 typedef String DataType;
58 typedef StringScalarData ThatClass;
59 typedef IScalarDataT<String> DataInterfaceType;
60
61 public:
62
63 class Internal
64 : public IDataInternal
65 {
66 public:
67
68 explicit Internal(StringScalarData* p)
69 : m_p(p)
70 {}
71
72 public:
73
74 void computeHash(DataHashInfo& hash_info) override
75 {
76 m_p->computeHash(hash_info);
77 }
78
79 private:
80
81 StringScalarData* m_p = nullptr;
82 };
83
84 public:
85
86 explicit StringScalarData(ITraceMng* trace)
87 : m_trace(trace)
88 , m_internal(this)
89 {}
90 explicit StringScalarData(const DataStorageBuildInfo& dsbi);
91 StringScalarData(const StringScalarData& rhs)
92 : m_value(rhs.m_value)
93 , m_trace(rhs.m_trace)
94 , m_internal(this)
95 , m_allocation_info(rhs.m_allocation_info)
96 {}
97
98 public:
99
100 Integer dimension() const override { return 0; }
101 Integer multiTag() const override { return 0; }
103 void serialize(ISerializer* sbuf, IDataOperation* operation) override;
104 void serialize(ISerializer* sbuf, Int32ConstArrayView ids, IDataOperation* operation) override;
105 DataType& value() override { return m_value; }
106 const DataType& value() const override { return m_value; }
107 void resize(Integer) override {}
108 IData* clone() override { return cloneTrue(); }
109 IData* cloneEmpty() override { return cloneTrueEmpty(); };
110 Ref<IData> cloneRef() override { return makeRef(cloneTrue()); }
112 DataStorageTypeInfo storageTypeInfo() const override;
113 StringScalarData* cloneTrue() override { return new ThatClass(*this); }
114 StringScalarData* cloneTrueEmpty() override { return new ThatClass(m_trace); }
116 {
117 DataInterfaceType* d = new ThatClass(*this);
118 return makeRef(d);
119 }
121 {
122 DataInterfaceType* d = new ThatClass(m_trace);
123 return makeRef(d);
124 }
125 void fillDefault() override
126 {
127 m_value = String();
128 }
129 void setName(const String& name) override;
130 Ref<ISerializedData> createSerializedDataRef(bool use_basic_type) const override;
132 void assignSerializedData(const ISerializedData* sdata) override;
133 void copy(const IData* data) override;
134 void swapValues(IData* data) override;
135 void computeHash(IHashAlgorithm* algo, ByteArray& output) const override;
136 void computeHash(DataHashInfo& hash_info) const;
137 ArrayShape shape() const override { return {}; }
138 void setShape(const ArrayShape&) override {}
139 void setAllocationInfo(const DataAllocationInfo& v) override { m_allocation_info = v; }
140 DataAllocationInfo allocationInfo() const override { return m_allocation_info; }
141 void visit(IScalarDataVisitor* visitor) override
142 {
143 visitor->applyVisitor(this);
144 }
145 void visit(IDataVisitor* visitor) override
146 {
147 visitor->applyDataVisitor(this);
148 }
149 void visitScalar(IScalarDataVisitor* visitor) override;
150 void visitArray(IArrayDataVisitor* visitor) override;
151 void visitArray2(IArray2DataVisitor* visitor) override;
152 IDataInternal* _commonInternal() override { return &m_internal; }
153
154 public:
155
156 static DataStorageTypeInfo staticStorageTypeInfo();
157
158 private:
159
160 DataType m_value;
161 ITraceMng* m_trace;
162 Internal m_internal;
163 DataAllocationInfo m_allocation_info;
164};
165
166/*---------------------------------------------------------------------------*/
167/*---------------------------------------------------------------------------*/
168
169StringScalarData::
170StringScalarData(const DataStorageBuildInfo& dsbi)
171: m_trace(dsbi.traceMng())
172, m_internal(this)
173{
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179DataStorageTypeInfo StringScalarData::
180staticStorageTypeInfo()
181{
183 Int32 nb_basic_type = 0;
184 Int32 dimension = 1;
185 Int32 multi_tag = 1;
186 return DataStorageTypeInfo(bdt, nb_basic_type, dimension, multi_tag);
187}
188
189/*---------------------------------------------------------------------------*/
190/*---------------------------------------------------------------------------*/
191
193storageTypeInfo() const
194{
195 return staticStorageTypeInfo();
196}
197
198/*---------------------------------------------------------------------------*/
199/*---------------------------------------------------------------------------*/
200
202createSerializedDataRef(bool use_basic_type) const
203{
204 ARCANE_UNUSED(use_basic_type);
205
206 ByteConstArrayView local_values(m_value.utf8());
207 Int64 nb_element = 1;
208 Int64 len = local_values.size();
209 Int64 nb_base_element = len;
210 Int64UniqueArray extents;
211 extents.add(nb_element);
212 Span<const Byte> base_values = local_values;
213 auto sd = arcaneCreateSerializedDataRef(DT_Byte, base_values.size(), 1, nb_element,
214 nb_base_element, false, extents);
215 sd->setConstBytes(base_values);
216 //m_trace->info() << " WRITE STRING " << m_value << " len=" << len;
217 return sd;
218}
219
220/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
222
225{
226 if (sdata->baseDataType() != DT_Byte)
227 throw ArgumentException(A_FUNCINFO, "Bad serialized type");
228
229 sdata->allocateMemory(sdata->memorySize());
230}
231
232/*---------------------------------------------------------------------------*/
233/*---------------------------------------------------------------------------*/
234
237{
238 if (sdata->baseDataType() != DT_Byte)
239 throw ArgumentException(A_FUNCINFO, "Bad serialized type");
240
241 Span<const Byte> byte_values = sdata->constBytes();
242 Int64 len = sdata->nbBaseElement();
243 //m_trace->info() << " ASSIGN STRING n=" << len
244 // << " ptr=" << (void*)byte_values.begin();
245 if (len != 0)
246 m_value = String(byte_values);
247 else
248 m_value = String();
249 //m_trace->info() << " READ STRING = " << m_value;
250}
251
252/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
254
256serialize(ISerializer* sbuf, IDataOperation* operation)
257{
258 ARCANE_UNUSED(operation);
259
260 ISerializer::eMode mode = sbuf->mode();
261 if (mode == ISerializer::ModeReserve) {
262 sbuf->reserve(m_value);
263 }
264 else if (mode == ISerializer::ModePut) {
265 sbuf->put(m_value);
266 }
267 else if (mode == ISerializer::ModeGet) {
268 switch (sbuf->readMode()) {
270 sbuf->get(m_value);
271 break;
273 throw NotSupportedException(A_FUNCINFO, "option 'ReadAdd'");
274 }
275 }
276 else
277 throw NotSupportedException(A_FUNCINFO, "Invalid mode");
278}
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
285{
286 ARCANE_UNUSED(sbuf);
287 ARCANE_UNUSED(ids);
288 ARCANE_UNUSED(operation);
289 // Nothing to do for this serialization type.
290}
291
292/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
294
296setName(const String& name)
297{
298 ARCANE_UNUSED(name);
299}
300
301/*---------------------------------------------------------------------------*/
302/*---------------------------------------------------------------------------*/
303
305computeHash(IHashAlgorithm* algo, ByteArray& output) const
306{
307 ByteConstArrayView input = m_value.utf8();
308 algo->computeHash64(input, output);
309}
310
311/*---------------------------------------------------------------------------*/
312/*---------------------------------------------------------------------------*/
313
315computeHash(DataHashInfo& hash_info) const
316{
317 hash_info.setVersion(2);
318 hash_info.context()->updateHash(asBytes(m_value.bytes()));
319}
320
321/*---------------------------------------------------------------------------*/
322/*---------------------------------------------------------------------------*/
323
325copy(const IData* data)
326{
327 const DataInterfaceType* true_data = dynamic_cast<const DataInterfaceType*>(data);
328 if (!true_data)
329 throw ArgumentException(A_FUNCINFO, "Can not cast 'IData' to 'IScalarDataT'");
330 m_value = true_data->value();
331}
332
333/*---------------------------------------------------------------------------*/
334/*---------------------------------------------------------------------------*/
335
337swapValues(IData* data)
338{
339 DataInterfaceType* true_data = dynamic_cast<DataInterfaceType*>(data);
340 if (!true_data)
341 throw ArgumentException(A_FUNCINFO, "Can not cast 'IData' to 'IScalarDataT'");
342 std::swap(m_value, true_data->value());
343}
344
345/*---------------------------------------------------------------------------*/
346/*---------------------------------------------------------------------------*/
347
350{
351 visitor->applyVisitor(this);
352}
353
354/*---------------------------------------------------------------------------*/
355/*---------------------------------------------------------------------------*/
356
359{
360 throw NotSupportedException(A_FUNCINFO, "Can not visit array data with scalar data");
361}
362
363/*---------------------------------------------------------------------------*/
364/*---------------------------------------------------------------------------*/
365
368{
369 throw NotSupportedException(A_FUNCINFO, "Can not visit array2 data with scalar data");
370}
371
372/*---------------------------------------------------------------------------*/
373/*---------------------------------------------------------------------------*/
374
375extern "C++" void
376registerStringScalarDataFactory(IDataFactoryMng* dfm)
377{
379}
380
381/*---------------------------------------------------------------------------*/
382/*---------------------------------------------------------------------------*/
383
384} // End namespace Arcane
385
386/*---------------------------------------------------------------------------*/
387/*---------------------------------------------------------------------------*/
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro to define methods managing counters of references.
Array shape.
Definition ArrayShape.h:42
void add(ConstReferenceType val)
Adds element val to the end of the array.
constexpr Integer size() const noexcept
Number of elements in the array.
Information on data allocation.
Information for calculating data hash.
Information to construct an instance of 'IData'.
static void registerDataFactory(IDataFactoryMng *dfm)
Registers a factory for the data DataType in dfm.
Type information for a data container.
Interface of the visitor pattern for a 2D array data item.
Interface of the visitor pattern for an array data item.
Interface of the data factory manager.
Internal part of IData.
Interface of an operation on a data.
Interface of the visitor pattern for a data item.
Interface of a data item.
Definition IData.h:34
virtual void updateHash(Span< const std::byte > input)=0
Adds the array input to the calculated hash.
Interface of a hashing algorithm.
virtual void computeHash64(Span< const Byte > input, ByteArray &output)
Calculates the hash value for the array input.
Interface of a scalar data item of type T.
Definition IData.h:246
virtual DataType & value()=0
Data value.
Interface of the visitor pattern for a scalar data item.
Interface of a serialized data.
virtual eDataType baseDataType() const =0
Data type.
virtual Int64 memorySize() const =0
Indicates the number of bytes that must be allocated to store or read the data.
virtual void allocateMemory(Int64 size)=0
Allocates an array to hold the serialized elements.
virtual Int64 nbBaseElement() const =0
Number of base elements.
virtual Span< const Byte > constBytes() const =0
Serialized values.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual void put(Span< const Real > values)=0
Add the array values.
virtual eReadMode readMode() const =0
Read mode.
virtual eMode mode() const =0
Current operating mode.
@ ReadReplace
Replace current elements with those read.
@ ReadAdd
Add those read to the current elements.
virtual void get(ArrayView< Real > values)=0
Retrieve the array values.
Reference to an instance.
Thread-safe implementation of a reference counter.
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
void computeHash(DataHashInfo &hash_info) override
Calculates the hash of the data.
Scalar data of a unicode string.
DataStorageTypeInfo storageTypeInfo() const override
Information about the data container type.
IDataInternal * _commonInternal() override
StringScalarData * cloneTrue() override
Clone the data.
DataAllocationInfo allocationInfo() const override
Allocation information.
Ref< IData > cloneEmptyRef() override
Clone the data but without elements.
void visitArray(IArrayDataVisitor *visitor) override
Apply the visitor to the data.
eDataType dataType() const override
Data type.
DataType & value() override
Data value.
void resize(Integer) override
Resize the data.
void serialize(ISerializer *sbuf, IDataOperation *operation) override
Serializes the data by applying the operation.
Ref< DataInterfaceType > cloneTrueRef() override
Clone the data.
Ref< ISerializedData > createSerializedDataRef(bool use_basic_type) const override
Serialize the data.
void assignSerializedData(const ISerializedData *sdata) override
Assign the serialized values sdata to the data.
Integer multiTag() const override
Multi-tag. 0 if not multiple, 1 if multiple, 2 if multiple for MultiArray variables (obsolete).
void setAllocationInfo(const DataAllocationInfo &v) override
Sets the allocation information.
void visit(IScalarDataVisitor *visitor) override
Applies the visitor to the data.
void allocateBufferForSerializedData(ISerializedData *sdata) override
Allocate memory to read the serialized values sdata.
void setShape(const ArrayShape &) override
Sets the array shape.
IData * cloneEmpty() override
Clone the data but without elements. The created instance must be destroyed by the 'delete' operator.
void visitScalar(IScalarDataVisitor *visitor) override
Apply the visitor to the data.
IData * clone() override
Clone the data. The created instance must be destroyed by the 'delete' operator.
void computeHash(IHashAlgorithm *algo, ByteArray &output) const override
Compute a hash key on this data.
ArrayShape shape() const override
Array shape for a 1D or 2D data item.
void visitArray2(IArray2DataVisitor *visitor) override
Apply the visitor to the data.
void swapValues(IData *data) override
Swap the values of data with those of the instance.
Ref< IData > cloneRef() override
Clone the data.
void visit(IDataVisitor *visitor) override
Applies the visitor to the data.
const DataType & value() const override
Data value.
Integer dimension() const override
Dimension. 0 for a scalar, 1 for a mono-dim array, 2 for a bi-dim array.
void fillDefault() override
Fills the data with its default value.
void copy(const IData *data) override
Copy the data data into the current instance.
StringScalarData * cloneTrueEmpty() override
Clone the data but without elements.
void setName(const String &name) override
Sets the name of the data (internal).
Ref< DataInterfaceType > cloneTrueEmptyRef() override
Clone the data but without elements.
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:293
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Ref< ISerializedData > arcaneCreateSerializedDataRef(eDataType data_type, Int64 memory_size, Integer nb_dim, Int64 nb_element, Int64 nb_base_element, bool is_multi_size, Int64ConstArrayView dimensions)
Creates serialized data.
UniqueArray< Int64 > Int64UniqueArray
Dynamic 1D array of 64-bit integers.
Definition UtilsTypes.h:339
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
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
eBasicDataType
Type of a basic data item.
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1032
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:476
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
eDataType
Data type.
Definition DataTypes.h:41
@ DT_Byte
Byte data type.
Definition DataTypes.h:42