MADNESS 0.10.1
binary_fstream_archive.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30*/
31
32#ifndef MADNESS_WORLD_BINARY_FSTREAM_ARCHIVE_H__INCLUDED
33#define MADNESS_WORLD_BINARY_FSTREAM_ARCHIVE_H__INCLUDED
34
35/**
36 \file binary_fstream_archive.h
37 \brief Implements an archive wrapping a binary filestream.
38 \ingroup serialization
39*/
40
41#include <type_traits>
42#include <fstream>
43#include <memory>
45
46namespace madness {
47 namespace archive {
48
49 /// \addtogroup serialization
50 /// @{
51
52 /// Wraps an archive around a binary filestream for output.
54 static const std::size_t IOBUFSIZE = 4*1024*1024; ///< Buffer size.
55 std::shared_ptr<char> iobuf; ///< Buffer.
56 mutable std::ofstream os; ///< The filestream.
57
58 public:
59 /// Default constructor.
60
61 /// The filename and open modes are optional here; they can be
62 /// specified later by calling \c open().
63 /// \param[in] filename Name of the file to write to.
64 /// \param[in] mode I/O attributes for opening the file.
65 BinaryFstreamOutputArchive(const char* filename = nullptr,
66 std::ios_base::openmode mode = std::ios_base::binary | \
67 std::ios_base::out | std::ios_base::trunc);
68
70 std::ios_base::openmode mode = std::ios_base::binary | \
71 std::ios_base::out | std::ios_base::trunc)
72 : BinaryFstreamOutputArchive(name.c_str(),mode) {}
73
74 /// Write to the filestream.
75
76 /// The function only appears (due to \c enable_if) if \c T is
77 /// serializable.
78 /// \tparam T The type of data to be written.
79 /// \param[in] t Location of the data to be written.
80 /// \param[in] n The number of data items to be written.
81 template <class T>
82 inline
83 typename std::enable_if< is_trivially_serializable<T>::value, void >::type
84 store(const T* t, long n) const {
85 os.write((const char *) t, n*sizeof(T));
86 }
87
88 /// Open the filestream.
89
90 /// \param[in] filename The name of the file.
91 /// \param[in] mode I/O attributes for opening the file.
92 void open(const char* filename,
93 std::ios_base::openmode mode = std::ios_base::binary | \
94 std::ios_base::out | std::ios_base::trunc);
95
96 /// Close the filestream.
97 void close();
98
99 /// Flush the filestream.
100 void flush();
101 };
102
103 /// Wraps an archive around a binary filestream for input.
105 static const std::size_t IOBUFSIZE = 4*1024*1024; ///< Buffer size.
106 std::shared_ptr<char> iobuf; ///< Buffer.
107 mutable std::ifstream is; ///< The filestream.
108
109 public:
110 /// Default constructor.
111
112 /// The filename and open modes are optional here; they can be
113 /// specified later by calling \c open().
114 /// \param[in] filename Name of the file to read from.
115 /// \param[in] mode I/O attributes for opening the file.
116 BinaryFstreamInputArchive(const char* filename = nullptr, std::ios_base::openmode mode = std::ios_base::binary | std::ios_base::in);
117
118 /// Default constructor.
119
120 /// The filename and open modes are optional here; they can be
121 /// specified later by calling \c open().
122 /// \param[in] filename Name of the file to read from.
123 /// \param[in] mode I/O attributes for opening the file.
125 std::ios_base::openmode mode = std::ios_base::binary | \
126 std::ios_base::in)
127 : BinaryFstreamInputArchive(name.c_str(),mode) {}
128
129 /// Load from the filestream.
130
131 /// The function only appears (due to \c enable_if) if \c T is
132 /// serializable.
133 /// \tparam T The type of data to be read.
134 /// \param[out] t Where to put the loaded data.
135 /// \param[in] n The number of data items to be loaded.
136 template <class T>
137 inline
138 typename std::enable_if< is_trivially_serializable<T>::value, void >::type
139 load(T* t, long n) const {
140 is.read((char *) t, n*sizeof(T));
141 }
142
143 /// Open the filestream.
144
145 /// \param[in] filename Name of the file to read from.
146 /// \param[in] mode I/O attributes for opening the file.
147 void open(const char* filename, std::ios_base::openmode mode = std::ios_base::binary | std::ios_base::in);
148
149 /// Close the filestream.
150 void close();
151 };
152
153 /// @}
154 }
155}
156#endif // MADNESS_WORLD_BINARY_FSTREAM_ARCHIVE_H__INCLUDED
Interface templates for the archives (serialization).
Base class for input archive classes.
Definition archive.h:374
Base class for output archive classes.
Definition archive.h:382
Wraps an archive around a binary filestream for input.
Definition binary_fstream_archive.h:104
BinaryFstreamInputArchive(const std::string name, std::ios_base::openmode mode=std::ios_base::binary|std::ios_base::in)
Default constructor.
Definition binary_fstream_archive.h:124
static const std::size_t IOBUFSIZE
Buffer size.
Definition binary_fstream_archive.h:105
void close()
Close the filestream.
Definition binary_fstream_archive.cc:91
std::shared_ptr< char > iobuf
Buffer.
Definition binary_fstream_archive.h:106
std::enable_if< is_trivially_serializable< T >::value, void >::type load(T *t, long n) const
Load from the filestream.
Definition binary_fstream_archive.h:139
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::binary|std::ios_base::in)
Open the filestream.
Definition binary_fstream_archive.cc:79
std::ifstream is
The filestream.
Definition binary_fstream_archive.h:107
Wraps an archive around a binary filestream for output.
Definition binary_fstream_archive.h:53
void close()
Close the filestream.
Definition binary_fstream_archive.cc:62
std::shared_ptr< char > iobuf
Buffer.
Definition binary_fstream_archive.h:55
std::ofstream os
The filestream.
Definition binary_fstream_archive.h:56
static const std::size_t IOBUFSIZE
Buffer size.
Definition binary_fstream_archive.h:54
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::binary|std::ios_base::out|std::ios_base::trunc)
Open the filestream.
Definition binary_fstream_archive.cc:52
std::enable_if< is_trivially_serializable< T >::value, void >::type store(const T *t, long n) const
Write to the filestream.
Definition binary_fstream_archive.h:84
void flush()
Flush the filestream.
Definition binary_fstream_archive.cc:69
BinaryFstreamOutputArchive(const std::string name, std::ios_base::openmode mode=std::ios_base::binary|std::ios_base::out|std::ios_base::trunc)
Definition binary_fstream_archive.h:69
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
static const char * filename
Definition legendre.cc:96
std::string type(const PairType &n)
Definition PNOParameters.h:18
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28