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(
in.tellg());
58 in.read(&buffer[0], 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;
204 typedef std::valarray<U> ValArray;
226 typedef std::string String;
230 size_t sz = s.size();
240 for (
size_t i = 0; i < sz; ++i)
250 template<
class X,
class Y>
253 typedef std::pair<X,Y> Pair;
269 template<
class K,
class V>
272 typedef std::map<K,V> Map;
278 for (
typename std::map<K,V>::const_iterator it = m.begin(); it != m.end(); ++it)
286 for (
size_t i = 0; i < s; ++i)
299 typedef std::set<T> Set;
305 for (
typename std::set<T>::const_iterator it = m.begin(); it != m.end(); ++it)
313 for (
size_t i = 0; i < s; ++i)
323 template<
class K,
class V,
class H,
class E,
class A>
326 typedef std::unordered_map<K,V,H,E,A> Map;
340 for (
size_t i = 0; i < s; ++i)
344 m.emplace(std::move(p));
350 template<
class T,
class H,
class E,
class A>
353 typedef std::unordered_set<T,H,E,A> Set;
367 for (
size_t i = 0; i < s; ++i)
371 m.emplace(std::move(p));
379 template<
class... Args>
382 typedef std::tuple<Args...> Tuple;
386 template<std::
size_t I = 0>
388 typename std::enable_if<I ==
sizeof...(Args),
void>::type
391 template<std::
size_t I = 0>
393 typename std::enable_if<I <
sizeof...(Args),
void>::type
398 template<std::
size_t I = 0>
400 typename std::enable_if<I ==
sizeof...(Args),
void>::type
403 template<std::
size_t I = 0>
405 typename std::enable_if<I <
sizeof...(Args),
void>::type
415 if (position + count > buffer.capacity())
416 buffer.reserve((position + count) * growth_multiplier());
418 if (position + count > buffer.size())
419 buffer.resize(position + count);
421 std::copy(x, x + count, &buffer[position]);
429 std::copy(&buffer[position], &buffer[position + count], x);
437 std::copy(&buffer[buffer.size() - count], &buffer[buffer.size()], x);
438 buffer.resize(buffer.size() - count);
447 from.position -=
sizeof(size_t);
449 size_t total =
sizeof(size_t) + sz;
450 to.buffer.resize(to.position + total);
451 std::copy(&from.buffer[from.position], &from.buffer[from.position + total], &to.buffer[to.position]);
452 to.position += total;
453 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:427
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:443
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:435
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:413
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