DOLFIN
DOLFIN C++ interface
File.h
1 // Copyright (C) 2002-2012 Johan Hoffman, Anders Logg and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __FILE_H
19 #define __FILE_H
20 
21 #include <memory>
22 #include <ostream>
23 #include <string>
24 #include <utility>
25 #include <dolfin/common/MPI.h>
26 #include "GenericFile.h"
27 
28 namespace dolfin
29 {
30 
34 
44 
45  class File
46  {
47  public:
48 
50  enum class Type {x3d, xml, vtk, raw, xyz, binary, svg};
51 
74  File(const std::string filename, std::string encoding="ascii");
75 
101  File(MPI_Comm comm, const std::string filename,
102  std::string encoding="ascii");
103 
119  File(const std::string filename, Type type, std::string encoding="ascii");
120 
138  File(MPI_Comm comm, const std::string filename, Type type,
139  std::string encoding="ascii");
140 
146  File(std::ostream& outstream);
147 
149  ~File();
150 
152  template<typename T> void operator>>(T& t)
153  {
154  _file->_read();
155  _file->read(t);
156  }
157 
159  template<typename T> void read(T& t)
160  {
161  _file->_read();
162  _file->read(t);
163  }
164 
173  void operator<<(const std::pair<const Mesh*, double> mesh);
174 
183  void operator<<(const std::pair<const MeshFunction<int>*, double> f);
184 
193  void operator<<
194  (const std::pair<const MeshFunction<std::size_t>*, double> f);
195 
204  void operator<< (const std::pair<const MeshFunction<double>*, double> f);
205 
214  void operator<<(const std::pair<const MeshFunction<bool>*, double> f);
215 
224  void operator<<(const std::pair<const Function*, double> u);
225 
227  template<typename X>
228  void operator<<(const X& x)
229  {
230  _file->_write(_mpi_comm.rank());
231  _file->write(x);
232  }
233 
235  template<typename X>
236  void write(const X& x)
237  {
238  _file->_write(_mpi_comm.rank());
239  _file->write(x);
240  }
241 
243  template<typename X>
244  void write(const X& x, double time)
245  {
246  _file->_write(_mpi_comm.rank());
247  _file->write(x, time);
248  }
249 
259  static bool exists(std::string filename);
260 
261  // Create parent path for file if file has a parent path
266  static void create_parent_path(std::string filename);
267 
268  private:
269 
270  // Initialise GenericFile (using file extension to determine type)
271  void init(MPI_Comm comm, const std::string filename, std::string encoding);
272 
273  // Initialise GenericFile (with specified type)
274  void init(MPI_Comm comm, const std::string filename, Type type,
275  std::string encoding);
276 
277  // FIXME: Remove when GenericFile::write is cleaned up
278  // MPI communicator
279  dolfin::MPI::Comm _mpi_comm;
280 
281  // Pointer to implementation (envelope-letter design)
282  std::unique_ptr<GenericFile> _file;
283 
284  };
285 
286 }
287 
288 #endif
void operator>>(T &t)
Read from file.
Definition: File.h:152
void read(T &t)
Read from file.
Definition: File.h:159
double time()
Return wall time elapsed since some implementation dependent epoch.
Definition: timing.cpp:48
Definition: adapt.h:29
File(const std::string filename, std::string encoding="ascii")
Definition: File.cpp:38
~File()
Destructor.
Definition: File.cpp:67
Definition: File.h:45
Type
File formats.
Definition: File.h:50
static void create_parent_path(std::string filename)
Definition: File.cpp:121
void write(const X &x, double time)
Write object to file with time.
Definition: File.h:244
void init(int argc, char *argv[])
Definition: init.cpp:27
void operator<<(const X &x)
Write object to file.
Definition: File.h:228
void write(const X &x)
Write object to file.
Definition: File.h:236
Definition: MPI.h:76
static bool exists(std::string filename)
Definition: File.cpp:109