21
21
22
22
#include < iostream>
23
23
#include < fstream>
24
- #include < sstream >
24
+ #include < functional >
25
25
#include " phmap.h"
26
26
namespace phmap
27
27
{
@@ -168,22 +168,32 @@ bool parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, Eq, Alloc>::phmap_load(Inp
168
168
class BinaryOutputArchive {
169
169
public:
170
170
BinaryOutputArchive (const char *file_path) {
171
- ofs_.open (file_path, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
171
+ os_ = new std::ofstream (file_path, std::ofstream::out |
172
+ std::ofstream::trunc |
173
+ std::ofstream::binary);
174
+ destruct_ = [this ]() { delete os_; };
172
175
}
173
176
174
- ~BinaryOutputArchive () = default ;
177
+ BinaryOutputArchive (std::ostream &os) : os_(&os) {}
178
+
179
+ ~BinaryOutputArchive () {
180
+ if (destruct_) {
181
+ destruct_ ();
182
+ }
183
+ }
184
+
175
185
BinaryOutputArchive (const BinaryOutputArchive&) = delete ;
176
186
BinaryOutputArchive& operator =(const BinaryOutputArchive&) = delete ;
177
187
178
188
bool saveBinary (const void *p, size_t sz) {
179
- ofs_. write (reinterpret_cast <const char *>(p), (std::streamsize)sz);
189
+ os_-> write (reinterpret_cast <const char *>(p), (std::streamsize)sz);
180
190
return true ;
181
191
}
182
192
183
193
template <typename V>
184
194
typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool >::type
185
195
saveBinary (const V& v) {
186
- ofs_. write (reinterpret_cast <const char *>(&v), sizeof (V));
196
+ os_-> write (reinterpret_cast <const char *>(&v), sizeof (V));
187
197
return true ;
188
198
}
189
199
@@ -194,29 +204,39 @@ class BinaryOutputArchive {
194
204
}
195
205
196
206
private:
197
- std::ofstream ofs_;
207
+ std::ostream* os_;
208
+ std::function<void ()> destruct_;
198
209
};
199
210
200
211
201
212
class BinaryInputArchive {
202
213
public:
203
214
BinaryInputArchive (const char * file_path) {
204
- ifs_.open (file_path, std::ofstream::in | std::ofstream::binary);
215
+ is_ = new std::ifstream (file_path,
216
+ std::ifstream::in | std::ifstream::binary);
217
+ destruct_ = [this ]() { delete is_; };
205
218
}
219
+
220
+ BinaryInputArchive (std::istream& is) : is_(&is) {}
206
221
207
- ~BinaryInputArchive () = default ;
222
+ ~BinaryInputArchive () {
223
+ if (destruct_) {
224
+ destruct_ ();
225
+ }
226
+ }
227
+
208
228
BinaryInputArchive (const BinaryInputArchive&) = delete ;
209
229
BinaryInputArchive& operator =(const BinaryInputArchive&) = delete ;
210
230
211
231
bool loadBinary (void * p, size_t sz) {
212
- ifs_. read (reinterpret_cast <char *>(p), (std::streamsize)sz);
232
+ is_-> read (reinterpret_cast <char *>(p), (std::streamsize)sz);
213
233
return true ;
214
234
}
215
235
216
236
template <typename V>
217
237
typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool >::type
218
238
loadBinary (V* v) {
219
- ifs_. read (reinterpret_cast <char *>(v), sizeof (V));
239
+ is_-> read (reinterpret_cast <char *>(v), sizeof (V));
220
240
return true ;
221
241
}
222
242
@@ -227,7 +247,8 @@ class BinaryInputArchive {
227
247
}
228
248
229
249
private:
230
- std::ifstream ifs_;
250
+ std::istream* is_;
251
+ std::function<void ()> destruct_;
231
252
};
232
253
233
254
} // namespace phmap
0 commit comments