Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
dataio.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DATAIO_HH
5#define DATAIO_HH
6
7
8#ifdef HAVE_BOOST_SERIALIZATION
9
10#include <fstream>
11
13
14#include <boost/archive/text_oarchive.hpp>
15#include <boost/archive/text_iarchive.hpp>
16#include <boost/archive/binary_oarchive.hpp>
17#include <boost/archive/binary_iarchive.hpp>
18
19
24class DataIO
25{
26 public:
28 enum FileFormat { BINARY, ASCII, XML };
29
38 template <class DataContainer>
39 static void writeData(const DataContainer& data, std::string filename, FileFormat format=BINARY)
40 {
41 std::ofstream ofs(filename.c_str());
42
43
44 switch (format) {
45 case BINARY:
46 {
47 boost::archive::binary_oarchive oa(ofs);
48 oa << data;
49 break;
50 }
51 case ASCII:
52 {
53 boost::archive::text_oarchive oa(ofs);
54 oa << data;
55 break;
56 }
57 case XML:
58 DUNE_THROW(Dune::NotImplemented, "Serialized output in XML format not yet implemented.");
59 }
60 }
61
70 template <class DataContainer>
71 static void loadData(DataContainer& data, std::string filename, FileFormat format=BINARY)
72 {
73 std::ifstream ifs(filename.c_str());
74
75 switch (format) {
76 case BINARY:
77 {
78 boost::archive::binary_iarchive ia(ifs);
79 ia >> data;
80 break;
81 }
82 case ASCII:
83 {
84 boost::archive::text_iarchive ia(ifs);
85 ia >> data;
86 break;
87 }
88 case XML:
89 DUNE_THROW(Dune::NotImplemented, "Serialized output in XML format not yet implemented.");
90 }
91 }
92};
93
94#else
95#warning dataio.hh was included but boost_serialization was not found !
96#endif
97
98#endif
99
#define DUNE_THROW(E,...)
T c_str(T... args)
T data(T... args)