MADNESS 0.10.1
vector_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_VECTOR_ARCHIVE_H__INCLUDED
33#define MADNESS_WORLD_VECTOR_ARCHIVE_H__INCLUDED
34
35/**
36 \file vector_archive.h
37 \brief Implements an archive wrapping an STL \c vector.
38 \ingroup serialization
39
40 \todo With a bit of thought this could be generalized to several STL containers.
41*/
42
43#include <type_traits>
44#include <vector>
45#include <cstring>
47
48namespace madness {
49 namespace archive {
50
51 /// \addtogroup serialization
52 /// @{
53
54 /// Wraps an archive around an STL \c vector for output.
56 public:
57 mutable std::vector<unsigned char>* v; ///< The STL vector being wrapped.
58
59 public:
60 /// Create a buffer to wrap the specified \c vector.
61
62 /// \param[in] v The \c vector.
63 /// \param[in] hint The minimum capacity of the vector.
64 VectorOutputArchive(std::vector<unsigned char>& v, std::size_t hint=262144)
65 : v(&v) {
66 open(hint);
67 };
68
69 /// Appends data to the end of the vector.
70
71 /// \todo Verify/complete the documentation.
72 /// \tparam T The type of data to be appended.
73 /// \param[in] t Pointer to the data to be appended.
74 /// \param[in] n The number of data items to be appended.
75 /// \return Description needed.
76 template <class T>
77 inline
78 typename std::enable_if< madness::is_trivially_serializable<T>::value, void >::type
79 store(const T* t, long n) const {
80 const unsigned char* ptr = (unsigned char*) t;
81 v->insert(v->end(),ptr,ptr+n*sizeof(T));
82 }
83
84 /// Clear any data in the vector and ensure its capacity is at least \c hint.
85
86 /// \param[in] hint The minimum capacity for the vector.
87 void open(std::size_t hint=262144) {
88 v->clear();
89 v->reserve(hint);
90 };
91
92 /// Close the archive.
93 void close() {};
94
95 /// Flush the archive.
96 void flush() {};
97 };
98
99
100 /// Wraps an archive around an STL \c vector for input.
102 mutable std::vector<unsigned char>* v; ///< The STL vector being wrapped.
103 mutable std::size_t i; ///< Current input location.
104
105 public:
106 /// Create a buffer to wrap the specified \c vector.
107
108 /// \param[in] v The \c vector.
109 VectorInputArchive(std::vector<unsigned char>& v) : v(&v) , i(0) {}
110
111 /// Load data from the vector.
112
113 /// The function only appears (due to \c enable_if) if \c T is
114 /// serializable.
115 /// \tparam T The type of data to be loaded.
116 /// \param[out] t Where to store the loaded data.
117 /// \param[in] n The number of data items to be loaded.
118 template <class T>
119 inline
120 typename std::enable_if< madness::is_trivially_serializable<T>::value, void >::type
121 load(T* t, long n) const {
122 std::size_t m = n*sizeof(T);
123 if (m+i > v->size()) MADNESS_EXCEPTION("VectorInputArchive: reading past end", m+1);
124 memcpy((unsigned char*) t, &((*v)[i]), m);
125 i += m;
126 }
127
128 /// Open the archive.
129 void open() {};
130
131 /// Reset the read location to the beginning of the \c vector.
132 void rewind() const {
133 i=0;
134 };
135
136 /// Get the amount of space left to be read from the \c vector.
137
138 /// \return The amount of space left to be read from the \c vector.
139 std::size_t nbyte_avail() const {
140 return v->size()-i;
141 };
142
143 /// Close the archive.
144 void close() {}
145 };
146
147 /// Implementation of functions for storing the pre/postamble in Vector archives.
148
149 /// \attention No type checking over Vector buffers, for efficiency.
150 /// \tparam T The data type.
151 template <class T>
153 /// Store the preamble.
154
155 /// \param[in] ar The archive.
156 static void preamble_store(const VectorOutputArchive& ar) {};
157
158 /// Store the postamble.
159
160 /// \param[in] ar The archive.
161 static inline void postamble_store(const VectorOutputArchive& ar) {};
162 };
163
164 /// Implementation of functions for loading the pre/postamble in Vector archives.
165
166 /// \attention No type checking over Vector buffers, for efficiency.
167 /// \tparam T The data type.
168 template <class T>
170 /// Load the preamble.
171
172 /// \param[in] ar The archive.
173 static inline void preamble_load(const VectorInputArchive& ar) {};
174
175 /// Load the postamble.
176
177 /// \param[in] ar The archive.
178 static inline void postamble_load(const VectorInputArchive& ar) {};
179 };
180
181 /// @}
182 }
183}
184
185#endif // MADNESS_WORLD_VECTOR_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 an STL vector for input.
Definition vector_archive.h:101
VectorInputArchive(std::vector< unsigned char > &v)
Create a buffer to wrap the specified vector.
Definition vector_archive.h:109
void open()
Open the archive.
Definition vector_archive.h:129
void close()
Close the archive.
Definition vector_archive.h:144
void rewind() const
Reset the read location to the beginning of the vector.
Definition vector_archive.h:132
std::enable_if< madness::is_trivially_serializable< T >::value, void >::type load(T *t, long n) const
Load data from the vector.
Definition vector_archive.h:121
std::size_t nbyte_avail() const
Get the amount of space left to be read from the vector.
Definition vector_archive.h:139
std::size_t i
Current input location.
Definition vector_archive.h:103
std::vector< unsigned char > * v
The STL vector being wrapped.
Definition vector_archive.h:102
Wraps an archive around an STL vector for output.
Definition vector_archive.h:55
void close()
Close the archive.
Definition vector_archive.h:93
std::enable_if< madness::is_trivially_serializable< T >::value, void >::type store(const T *t, long n) const
Appends data to the end of the vector.
Definition vector_archive.h:79
std::vector< unsigned char > * v
The STL vector being wrapped.
Definition vector_archive.h:57
void flush()
Flush the archive.
Definition vector_archive.h:96
VectorOutputArchive(std::vector< unsigned char > &v, std::size_t hint=262144)
Create a buffer to wrap the specified vector.
Definition vector_archive.h:64
void open(std::size_t hint=262144)
Clear any data in the vector and ensure its capacity is at least hint.
Definition vector_archive.h:87
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
std::string type(const PairType &n)
Definition PNOParameters.h:18
static const double m
Definition relops.cc:9
static void preamble_load(const VectorInputArchive &ar)
Load the preamble.
Definition vector_archive.h:173
static void postamble_load(const VectorInputArchive &ar)
Load the postamble.
Definition vector_archive.h:178
static void postamble_store(const VectorOutputArchive &ar)
Store the postamble.
Definition vector_archive.h:161
static void preamble_store(const VectorOutputArchive &ar)
Store the preamble.
Definition vector_archive.h:156
Default implementation of the pre/postamble for type checking.
Definition archive.h:509