Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MemoryDataReaderWriter.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/* MemoryDataReaderWriter.cc (C) 2000-2009 */
9/* */
10/* Reading/writing data in memory. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Collection.h"
15#include "arcane/utils/Enumerator.h"
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/FatalErrorException.h"
18#include "arcane/utils/TraceInfo.h"
19#include "arcane/utils/Ref.h"
20
21#include "arcane/core/IData.h"
22#include "arcane/core/IVariable.h"
23#include "arcane/core/VariableCollection.h"
24
25#include "arcane/impl/MemoryDataReaderWriter.h"
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36MemoryDataReaderWriter::
37~MemoryDataReaderWriter()
38{
39 free();
40}
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
45void MemoryDataReaderWriter::
46free()
47{
48 m_vars_to_data.clear();
49}
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54void MemoryDataReaderWriter::
55beginWrite(const VariableCollection& vars)
56{
57 // Copies the current table and removes all references to the variables
58 // that will be saved. Once this is done, only references to
59 // more frequently used variables remain. Therefore,
60 // the corresponding IData is released.
61 VarToDataMap vars_to_data = m_vars_to_data;
62 for (VariableCollection::Enumerator ivar(vars); ++ivar;) {
63 IVariable* var = *ivar;
64 VarToDataMap::iterator i = vars_to_data.find(var->fullName());
65 if (i != vars_to_data.end())
66 vars_to_data.erase(i);
67 }
68
69 for (VarToDataMap::iterator i = vars_to_data.begin(); i != vars_to_data.end(); ++i) {
70 // Removes the reference to the current table and destroys the data
71 m_vars_to_data.erase(i->first);
72 }
73}
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
79write(IVariable* var, IData* data)
80{
81 Ref<IData> cdata = _findData(var);
82 if (!cdata.get()) {
83 cdata = data->cloneRef();
84 m_vars_to_data.insert(std::make_pair(var->fullName(), cdata));
85 }
86 else {
87 cdata->copy(data);
88 }
89}
90
91/*---------------------------------------------------------------------------*/
92/*---------------------------------------------------------------------------*/
93
95read(IVariable* var, IData* data)
96{
97 Ref<IData> cdata = _findData(var);
98 if (!cdata.get()) {
99 warning() << A_FUNCNAME << ": "
100 << String::format("can not find data for variable '{0}': variable will not be restored",
101 var->fullName());
102 return;
103 }
104 data->copy(cdata.get());
105}
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110Ref<IData> MemoryDataReaderWriter::
111_findData(IVariable* var)
112{
113 auto i = m_vars_to_data.find(var->fullName());
114 if (i == m_vars_to_data.end())
115 return {};
116 return i->second;
117}
118
119/*---------------------------------------------------------------------------*/
120/*---------------------------------------------------------------------------*/
121
122} // namespace Arcane
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
Interface of a data item.
Definition IData.h:34
virtual void copy(const IData *data)=0
Copy the data data into the current instance.
virtual Ref< IData > cloneRef()=0
Clone the data.
Interface of a variable.
Definition IVariable.h:40
virtual String fullName() const =0
Full variable name (with family prefix).
virtual void read(IVariable *var, IData *data)
Reads the data data of the variable var.
virtual void write(IVariable *var, IData *data)
Writes the data data of the variable var.
InstanceType * get() const
Associated instance or nullptr if none.
Reference to an instance.
TraceMessage warning() const
Flow for a warning message.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --