Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ISimpleTableReaderWriter.h
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/* ISimpleTableReaderWriter.h (C) 2000-2025 */
9/* */
10/* Interface representing a simple table reader/writer. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ISIMPLETABLEREADERWRITER_H
13#define ARCANE_CORE_ISIMPLETABLEREADERWRITER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/core/ISimpleTableInternalMng.h"
18
19#include "arcane/core/Directory.h"
20#include "arcane/core/IParallelMng.h"
22#include "arcane/utils/Iostream.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33/**
34 * \brief Class containing two static methods
35 * useful for implementations.
36 *
37 */
38class ARCANE_CORE_EXPORT SimpleTableReaderWriterUtils
39{
40 public:
41
42 /**
43 * \brief Static method allowing the creation of a directory with multiple
44 * processes.
45 * \note This is a collective method that must be called by all processes.
46 *
47 * \param parallel_mng The parallel manager of the current context.
48 * \param directory The directory to create.
49 * \return true If the directory was successfully created.
50 * \return false If the directory could not be created.
51 */
52 static bool createDirectoryOnlyProcess0(IParallelMng* parallel_mng, const Directory& directory)
53 {
54 int sf = 0;
55 if (parallel_mng->commRank() == 0) {
56 sf = directory.createDirectory();
57 }
58 if (parallel_mng->commSize() > 1) {
59 sf = parallel_mng->reduce(Parallel::ReduceMax, sf);
60 }
61 return sf == 0;
62 };
63
64 /**
65 * \brief Static method allowing verification of file existence.
66 *
67 * \param directory The directory where the file is located.
68 * \param file The file name (with extension).
69 * \return true If the file already exists.
70 * \return false If the file does not exist.
71 */
72 static bool isFileExist(const Directory& directory, const String& file)
73 {
74 std::ifstream stream;
75 stream.open(directory.file(file).localstr(), std::ifstream::in);
76 bool fin = stream.good();
77 stream.close();
78 return fin;
79 };
80};
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85/**
86 * \brief Class interface allowing reading a file and writing
87 * a file with or from a SimpleTableInternal.
88 *
89 * The file read must preferably have been written by an implementation
90 * of this same interface.
91 *
92 * Imperatively, a file written by an implementation of this
93 * interface must be readable by this same implementation.
94 *
95 * The implementation must not destroy the SimpleTableInternal object
96 * pointed to by the pointer used. The caller is responsible for managing this.
97 */
98class ARCANE_CORE_EXPORT ISimpleTableReaderWriter
99{
100 public:
101
102 virtual ~ISimpleTableReaderWriter() = default;
103
104 public:
105
106 /**
107 * \brief Method allowing writing a simple table to a file.
108 *
109 * The extension will be added by the implementation.
110 *
111 * The destination directory will be created by the implementation if
112 * it does not exist.
113 *
114 * The SimpleTableInternal elements that must be written are:
115 * - row names (m_row_names),
116 * - column names (m_column_names),
117 * - table name (m_table_name),
118 * - table values (m_values).
119 *
120 * Other SimpleTableInternal elements are not mandatory.
121 *
122 * \param dst The destination directory.
123 * \param file_name The file name (without extension).
124 * \return true If the file was successfully written.
125 * \return false If the file could not be written.
126 */
127 virtual bool writeTable(const Directory& dst, const String& file_name) = 0;
128
129 /**
130 * \brief Method allowing reading a file containing a simple table.
131 *
132 * The extension will be added by the implementation.
133 *
134 * A call to SimpleTableInternal::clear() must be performed before reading.
135 *
136 * The elements that must be retrieved are:
137 * - row names (m_row_names),
138 * - column names (m_column_names),
139 * - table name (m_table_name),
140 * - table values (m_values).
141 *
142 * The elements that must be deduced if not retrieved are:
143 * - row sizes (m_row_sizes),
144 * - column sizes (m_column_sizes).
145 *
146 * Default deduction for m_row_sizes:
147 * - len(m_row_sizes) = len(m_row_names)
148 * - m_row_sizes[*] = m_values.dim2Size()
149 *
150 * Default deduction for m_column_sizes:
151 * - len(m_column_sizes) = len(m_column_names)
152 * - m_column_sizes[*] = m_values.dim1Size()
153 *
154 *
155 * \param src The source directory.
156 * \param file_name The file name (without extension).
157 * \return true If the file was successfully read.
158 * \return false If the file could not be read.
159 */
160 virtual bool readTable(const Directory& src, const String& file_name) = 0;
161
162 /**
163 * \brief Method allowing clearing the content of the
164 * SimpleTableInternal object.
165 */
166 virtual void clearInternal() = 0;
167
168 /**
169 * \brief Method allowing writing the table to the
170 * standard output.
171 *
172 * The writing format is free (for the csv implementation, the
173 * writing is done the same way as in a csv file).
174 */
175 virtual void print() = 0;
176
177 /**
178 * \brief Method allowing retrieval of the precision currently
179 * used for writing values.
180 *
181 * \return Integer The precision.
182 */
183 virtual Integer precision() = 0;
184
185 /**
186 * \brief Method allowing modification of the print precision.
187 *
188 * For both the 'print()' method and the 'writetable()' method.
189 *
190 * \warning The "std::fixed" flag modifies the behavior of "setPrecision()";
191 * if the "std::fixed" flag is disabled, the precision defines the
192 * total number of digits (before and after the comma);
193 * if the "std::fixed" flag is enabled, the precision defines the
194 * number of digits after the comma. Therefore, attention must be paid when
195 * using "std::numeric_limits<Real>::max_digits10"
196 * (for writing) or "std::numeric_limits<Real>::digits10"
197 * (for reading), which should be used without the "std::fixed" flag.
198 *
199 * \param precision The new precision.
200 */
201 virtual void setPrecision(Integer precision) = 0;
202
203 /**
204 * \brief Method allowing checking if the 'std::fixed' flag is
205 * active or not for writing values.
206 *
207 * \return true If yes.
208 * \return false If no.
209 */
210 virtual bool isFixed() = 0;
211 /**
212 * \brief Method allowing setting or unsetting the 'std::fixed' flag.
213 *
214 * For both the 'print()' method and the 'writetable()' method.
215 *
216 * This flag allows 'forcing' the number of digits after the comma to
217 * the desired precision. For example, if 'setPrecision(4)' was called,
218 * and 'setFixed(true)' is called, the print of '6.1' will yield '6.1000'.
219 *
220 * \warning The "std::fixed" flag modifies the behavior of "setPrecision()";
221 * if the "std::fixed" flag is disabled, the precision defines the
222 * total number of digits (before and after the comma);
223 * if the "std::fixed" flag is enabled, the precision defines the
224 * number of digits after the comma. Therefore, attention must be paid when
225 * using "std::numeric_limits<Real>::max_digits10"
226 * (for writing) or "std::numeric_limits<Real>::digits10"
227 * (for reading), which should be used without the "std::fixed" flag.
228 *
229 * \param fixed Whether the 'std::fixed' flag should be set or not.
230 */
231 virtual void setFixed(bool fixed) = 0;
232
233 /**
234 * \brief Method allowing checking if the 'std::scientific' flag is
235 * active or not for writing values.
236 *
237 * \return true If yes.
238 * \return false If no.
239 */
241 /**
242 * \brief Method allowing setting or unsetting the 'std::scientific' flag.
243 *
244 * For both the 'print()' method and the 'writetable()' method.
245 *
246 * This flag allows 'forcing' the display of values in scientific
247 * notation during writing.
248 *
249 * \param use_scientific Whether the 'std::scientific' flag should be set or not.
250 */
251 virtual void setForcedToUseScientificNotation(bool use_scientific) = 0;
252
253 /**
254 * \brief Method allowing retrieval of the file type
255 * that will be written by the implementation. ("csv" will be returned
256 * for the csv implementation).
257 *
258 * \return String The file type/extension used.
259 */
260 virtual String fileType() = 0;
261
262 /**
263 * \brief Method allowing retrieval of a reference to the
264 * SimpleTableInternal object used.
265 *
266 * \return Ref<SimpleTableInternal> A copy of the reference.
267 */
269
270 /**
271 * \brief Method allowing setting a reference to a
272 * SimpleTableInternal.
273 *
274 * \param simple_table_internal The reference to a SimpleTableInternal.
275 */
276 virtual void setInternal(const Ref<SimpleTableInternal>& simple_table_internal) = 0;
277};
278
279/*---------------------------------------------------------------------------*/
280/*---------------------------------------------------------------------------*/
281
282} // End namespace Arcane
283
284/*---------------------------------------------------------------------------*/
285/*---------------------------------------------------------------------------*/
286
287#endif
Declarations of types on entities.
Class managing a directory.
Definition Directory.h:36
String file(const String &file_name) const override
Returns the full path of the file file_name in the directory.
Definition Directory.cc:120
bool createDirectory() const override
Creates the directory.
Definition Directory.cc:102
Interface of the parallelism manager for a subdomain.
virtual Int32 commRank() const =0
Rank of this instance in the communicator.
virtual Int32 commSize() const =0
Number of instances in the communicator.
virtual char reduce(eReduceType rt, char v)=0
Performs a reduction of type rt on the real v and returns the value.
Class interface allowing reading a file and writing a file with or from a SimpleTableInternal.
virtual bool isForcedToUseScientificNotation()=0
Method allowing checking if the 'std::scientific' flag is active or not for writing values.
virtual bool writeTable(const Directory &dst, const String &file_name)=0
Method allowing writing a simple table to a file.
virtual void setPrecision(Integer precision)=0
Method allowing modification of the print precision.
virtual void setInternal(const Ref< SimpleTableInternal > &simple_table_internal)=0
Method allowing setting a reference to a SimpleTableInternal.
virtual Integer precision()=0
Method allowing retrieval of the precision currently used for writing values.
virtual bool isFixed()=0
Method allowing checking if the 'std::fixed' flag is active or not for writing values.
virtual bool readTable(const Directory &src, const String &file_name)=0
Method allowing reading a file containing a simple table.
virtual void setFixed(bool fixed)=0
Method allowing setting or unsetting the 'std::fixed' flag.
virtual Ref< SimpleTableInternal > internal()=0
Method allowing retrieval of a reference to the SimpleTableInternal object used.
virtual void setForcedToUseScientificNotation(bool use_scientific)=0
Method allowing setting or unsetting the 'std::scientific' flag.
virtual String fileType()=0
Method allowing retrieval of the file type that will be written by the implementation....
virtual void clearInternal()=0
Method allowing clearing the content of the SimpleTableInternal object.
virtual void print()=0
Method allowing writing the table to the standard output.
Reference to an instance.
Class containing two static methods useful for implementations.
static bool createDirectoryOnlyProcess0(IParallelMng *parallel_mng, const Directory &directory)
Static method allowing the creation of a directory with multiple processes.
static bool isFileExist(const Directory &directory, const String &file)
Static method allowing verification of file existence.
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:229
@ ReduceMax
Maximum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.