Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
vector_io.h
Go to the documentation of this file.
1#ifndef ROUTING_KIT_VECTOR_IO_H
2#define ROUTING_KIT_VECTOR_IO_H
3
5
6#include <string>
7#include <vector>
8#include <stdexcept>
9#include <fstream>
10#include <type_traits>
11#include <functional>
12
13namespace RoutingKit{
14
15template<class T>
16void save_vector(const std::string&file_name, const std::vector<T>&vec){
17 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
18 std::ofstream out(file_name, std::ios::binary);
19 if(!out)
20 throw std::runtime_error("Can not open \""+file_name+"\" for writing.");
21 out.write(reinterpret_cast<const char*>(&vec[0]), vec.size()*sizeof(T));
22}
23
24template<class T>
25std::vector<T>load_vector(const std::string&file_name){
26 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
27 std::ifstream in(file_name, std::ios::binary);
28 if(!in)
29 throw std::runtime_error("Can not open \""+file_name+"\" for reading.");
30 in.seekg(0, std::ios::end);
31 unsigned long long file_size = in.tellg();
32 if(file_size % sizeof(T) != 0)
33 throw std::runtime_error("File \""+file_name+"\" can not be a vector of the requested type because it's size is no multiple of the element type's size.");
34 in.seekg(0, std::ios::beg);
35 std::vector<T>vec(file_size / sizeof(T));
36 in.read(reinterpret_cast<char*>(&vec[0]), file_size);
37 return vec; // NVRO
38}
39
40
41template<> void save_vector<std::string>(const std::string&file_name, const std::vector<std::string>&vec);
42extern template void save_vector<std::string>(const std::string&file_name, const std::vector<std::string>&vec);
43
44template<> std::vector<std::string>load_vector<std::string>(const std::string&file_name);
45extern template std::vector<std::string> load_vector<std::string>(const std::string&file_name);
46
47void save_bit_vector(const std::string&file_name, const BitVector&vec);
48BitVector load_bit_vector(const std::string&file_name);
49
50
51
52
53template<class T>
54void save_value(const std::string&file_name, const T&val){
55 save_vector(file_name, std::vector<T>{val});
56}
57
58
59template<class T>
60T load_value(const std::string&file_name){
61 auto v = load_vector<T>(file_name);
62 if(v.empty())
63 throw std::runtime_error(file_name+" is empty");
64 if(v.size() > 1)
65 throw std::runtime_error(file_name+" contains more than one element");
66 return v.front();
67}
68
69
70template<class T>
71void write_value(std::ostream&out, const T&val){
72 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
73 out.write((const char*)&val, sizeof(T));
74 if(!out)
75 throw std::runtime_error("Could not write value to file");
76}
77
78template<class T>
79T read_value(std::istream&in){
80 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
81 T val;
82 in.read((char*)&val, sizeof(T));
83 if(!in)
84 throw std::runtime_error("Could not read value to file");
85 return val; // NVRO
86}
87
88template<class T>
89void write_vector(std::ostream&out, const std::vector<T>&v){
90 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
91 out.write((const char*)&v[0], sizeof(T)*v.size());
92 if(!out)
93 throw std::runtime_error("Could not write vector data");
94
95}
96
97template<class T>
98std::vector<T> read_vector(std::istream&in, unsigned long long size){
99 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
100 std::vector<T>v(size);
101 in.read((char*)&v[0], sizeof(T)*size);
102 if(!in)
103 throw std::runtime_error("Could not read vector data");
104 return v; // NVRO
105}
106
107
108
109
110template<class T>
111void write_value(const std::function<void(const char*, unsigned long long)>&out, const T&val){
112 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
113 out((const char*)&val, sizeof(T));
114}
115
116template<class T>
117T read_value(const std::function<void(char*, unsigned long long)>&in){
118 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
119 T val;
120 in((char*)&val, sizeof(T));
121 return val; // NVRO
122}
123
124template<class T>
125void write_vector(const std::function<void(const char*, unsigned long long)>&out, const std::vector<T>&v){
126 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
127 out((const char*)&v[0], sizeof(T)*v.size());
128}
129
130template<class T>
131std::vector<T> read_vector(const std::function<void(char*, unsigned long long)>&in, unsigned long long size){
132 static_assert(std::is_pod<T>::value, "Cannot find non-trivial serialization code for this type, maybe a header is missing or serialization is simply not available");
133 std::vector<T>v(size);
134 in((char*)&v[0], sizeof(T)*size);
135 return v; // NVRO
136}
137
138
139
140
141inline
142BitVector read_bit_vector(const std::function<void(char*, unsigned long long)>&in, unsigned long long size){
143 BitVector v(size);
144 in((char*)v.data(), ((size+511)/512)*64);
145 return v;
146}
147
148inline
149void write_bit_vector(const std::function<void(const char*, unsigned long long)>&out, const BitVector&v){
150 out((const char*)v.data(), ((v.size()+511)/512)*64);
151}
152
153
154
155template<class F>
156void open_file_for_saving(const std::string&file_name, const F&f){
157 std::ofstream out(file_name, std::ios::binary);
158 if(!out)
159 throw std::runtime_error("Can not open \""+file_name+"\" for writing.");
160 f(out);
161}
162
163template<class F>
164void open_file_for_loading(const std::string&file_name, const F&f){
165 std::ifstream in(file_name, std::ios::binary);
166 if(!in)
167 throw std::runtime_error("Can not open \""+file_name+"\" for reading.");
168
169 in.seekg(0, std::ios_base::end);
170 unsigned long long file_size = in.tellg();
171 in.seekg(0, std::ios_base::beg);
172 f(in, file_size);
173}
174
175} // RoutingKit
176
177#endif
uint64_t size() const
Definition bit_vector.h:26
void write_bit_vector(const std::function< void(const char *, unsigned long long)> &out, const BitVector &v)
Definition vector_io.h:149
void write_value(std::ostream &out, const T &val)
Definition vector_io.h:71
void save_vector(const std::string &file_name, const std::vector< T > &vec)
Definition vector_io.h:16
void save_vector< std::string >(const std::string &file_name, const std::vector< std::string > &vec)
BitVector load_bit_vector(const std::string &file_name)
void save_bit_vector(const std::string &file_name, const BitVector &vec)
T read_value(std::istream &in)
Definition vector_io.h:79
T load_value(const std::string &file_name)
Definition vector_io.h:60
std::vector< T > read_vector(std::istream &in, unsigned long long size)
Definition vector_io.h:98
std::vector< T > load_vector(const std::string &file_name)
Definition vector_io.h:25
void open_file_for_loading(const std::string &file_name, const F &f)
Definition vector_io.h:164
void write_vector(std::ostream &out, const std::vector< T > &v)
Definition vector_io.h:89
std::vector< std::string > load_vector< std::string >(const std::string &file_name)
BitVector read_bit_vector(const std::function< void(char *, unsigned long long)> &in, unsigned long long size)
Definition vector_io.h:142
void save_value(const std::string &file_name, const T &val)
Definition vector_io.h:54
void open_file_for_saving(const std::string &file_name, const F &f)
Definition vector_io.h:156