1 #ifndef DIY_SERIALIZATION_HPP
2 #define DIY_SERIALIZATION_HPP
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <type_traits>
21 virtual void save_binary(
const char* x,
size_t count) =0;
29 position(position_) {}
31 virtual inline void save_binary(
const char* x,
size_t count)
override;
32 virtual inline void load_binary(
char* x,
size_t count)
override;
35 void clear() { buffer.clear(); reset(); }
36 void wipe() { std::vector<char>().swap(buffer); reset(); }
37 void reset() { position = 0; }
38 void skip(
size_t s) { position += s; }
39 void swap(
MemoryBuffer& o) { std::swap(position, o.position); buffer.swap(o.buffer); }
40 bool empty()
const {
return buffer.empty(); }
41 size_t size()
const {
return buffer.size(); }
42 void reserve(
size_t s) { buffer.reserve(s); }
43 operator bool()
const {
return position < buffer.size(); }
52 void write(
const std::string& fn)
const { std::ofstream out(fn.c_str()); out.write(&buffer[0], size()); }
53 void read(
const std::string& fn)
55 std::ifstream
in(fn.c_str(), std::ios::binary | std::ios::ate);
56 buffer.resize(static_cast<size_t>(
in.tellg()));
58 in.read(&buffer[0], static_cast<std::streamsize>(size()));
63 std::vector<char> buffer;
92 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 5)
93 static_assert(std::is_trivially_copyable<T>::value,
"Default serialization works only for trivially copyable types");
111 void save(BinaryBuffer& bb,
const T* x,
size_t n);
116 void load(BinaryBuffer& bb, T* x,
size_t n);
133 static yes test(Default*);
136 enum { value = (
sizeof(test((T*) 0)) ==
sizeof(yes)) };
144 for (
size_t i = 0; i < n; ++i)
154 for (
size_t i = 0; i < n; ++i)
174 x.buffer.resize(x.position);
183 typedef std::vector<U> Vector;
206 typedef std::valarray<U> ValArray;
230 typedef std::string String;
234 size_t sz = s.size();
244 for (
size_t i = 0; i < sz; ++i)
254 template<
class X,
class Y>
257 typedef std::pair<X,Y> Pair;
273 template<
class K,
class V>
276 typedef std::map<K,V> Map;
282 for (
typename std::map<K,V>::const_iterator it = m.begin(); it != m.end(); ++it)
290 for (
size_t i = 0; i < s; ++i)
303 typedef std::set<T> Set;
309 for (
typename std::set<T>::const_iterator it = m.begin(); it != m.end(); ++it)
317 for (
size_t i = 0; i < s; ++i)
327 template<
class K,
class V,
class H,
class E,
class A>
330 typedef std::unordered_map<K,V,H,E,A> Map;
344 for (
size_t i = 0; i < s; ++i)
348 m.emplace(std::move(p));
354 template<
class T,
class H,
class E,
class A>
357 typedef std::unordered_set<T,H,E,A> Set;
371 for (
size_t i = 0; i < s; ++i)
375 m.emplace(std::move(p));
383 template<
class... Args>
386 typedef std::tuple<Args...> Tuple;
390 template<std::
size_t I = 0>
392 typename std::enable_if<I ==
sizeof...(Args),
void>::type
395 template<std::
size_t I = 0>
397 typename std::enable_if<I <
sizeof...(Args),
void>::type
402 template<std::
size_t I = 0>
404 typename std::enable_if<I ==
sizeof...(Args),
void>::type
407 template<std::
size_t I = 0>
409 typename std::enable_if<I <
sizeof...(Args),
void>::type
419 if (position + count > buffer.capacity())
421 double newsize =
static_cast<double>(position + count) * growth_multiplier();
422 buffer.reserve(static_cast<size_t>(newsize));
425 if (position + count > buffer.size())
426 buffer.resize(position + count);
428 std::copy_n(x, count, &buffer[position]);
436 std::copy_n(&buffer[position], count, x);
444 std::copy_n(&buffer[buffer.size() - count], count, x);
445 buffer.resize(buffer.size() - count);
454 from.position -=
sizeof(size_t);
456 size_t total =
sizeof(size_t) + sz;
457 to.buffer.resize(to.position + total);
458 std::copy_n(&from.buffer[from.position], total, &to.buffer[to.position]);
459 to.position += total;
460 from.position += total;
void load(BinaryBuffer &bb, T &x)
Loads x from bb by calling diy::Serialization<T>::load(bb,x).
Definition: serialization.hpp:106
void load_back(BinaryBuffer &bb, T &x)
Supports only binary data copying (meant for simple footers).
Definition: serialization.hpp:120
virtual void save_binary(const char *x, size_t count)=0
copy count bytes from x into the buffer
virtual void load_binary(char *x, size_t count) override
copy count bytes into x from the buffer
Definition: serialization.hpp:434
static void copy(MemoryBuffer &from, MemoryBuffer &to)
copy a memory buffer from one buffer to another, bypassing making a temporary copy first ...
Definition: serialization.hpp:450
static float growth_multiplier()
multiplier used for the geometric growth of the container
Definition: serialization.hpp:49
void save(BinaryBuffer &bb, const T &x)
Saves x to bb by calling diy::Serialization<T>::save(bb,x).
Definition: serialization.hpp:102
virtual void load_binary_back(char *x, size_t count) override
copy count bytes into x from the back of the buffer
Definition: serialization.hpp:442
void in(const RegularLink< Bounds > &link, const Point &p, OutIter out, const Bounds &domain)
Finds the neighbor(s) containing the target point.
Definition: pick.hpp:102
Definition: serialization.hpp:26
virtual void load_binary(char *x, size_t count)=0
copy count bytes into x from the buffer
virtual void save_binary(const char *x, size_t count) override
copy count bytes from x into the buffer
Definition: serialization.hpp:417
Main interface to serialization, meant to be specialized for the types that require special handling...
Definition: serialization.hpp:90
virtual void load_binary_back(char *x, size_t count)=0
copy count bytes into x from the back of the buffer
A serialization buffer.
Definition: serialization.hpp:19