-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsave.hpp
More file actions
79 lines (60 loc) · 2.2 KB
/
Copy pathsave.hpp
File metadata and controls
79 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef SAVE_HPP
#define SAVE_HPP
#include "binary.hpp"
#include "type_traits/extended/is_container.hpp"
#include <utility> // std::pair
#include <fstream> // write
// using namespace util;
namespace util { namespace serialize { namespace binary {
// forward declarations
template <typename T, EnableIfFundamental<T>...> T save(std::ofstream&, T);
template <typename T, EnableIfContainer <T>...> T save(std::ofstream&, T);
template <typename T, EnableIfArray <T>...> T save(std::ofstream&, T);
// template <typename T, EnableIfString <T>...> T save(std::ofstream&, T);
template<class K, class V> std::pair<K,V> save(std::ofstream&, std::pair<K,V>);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* S A V E */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
template <typename T, EnableIfFundamental<T>...>
T
save(std::ofstream & os, T t) {
os.write(reinterpret_cast<const char*>(&t), sizeof(t));
return t;
}
template <typename T, EnableIfContainer<T>...>
T
save(std::ofstream & os, T t) {
unsigned size = t.size();
save(os, size);
for (auto it = begin(t); it != end(t); ++it) {
save(os, *it);
}
return t;
}
template <typename T, EnableIfArray<T>...>
T
save(std::ofstream & os, T t) {
unsigned size = t.size();
save(os, size);
return t;
}
// template <typename T, EnableIfString<T>...>
// T
// save(std::ofstream & os, T t) {
// size_t size = t.size() + 1;
// // os.write(reinterpret_cast<const char*>(&size), sizeof(size));
// save(os, size);
// const char * data = t.data();
// os.write(reinterpret_cast<const char*>(&data), (unsigned)size);
//
// return t;
// }
template<class K, class V>
std::pair<K,V>
save(std::ofstream & os, std::pair<K,V> t) {
save(os, t.first);
save(os, t.second);
return t;
}
} } } /* namespace util::serialize::binary */
#endif /* end of include guard: SAVE_HPP */