1 #ifndef teca_binary_stream_h
2 #define teca_binary_stream_h
4 #include "teca_config.h"
33 {
return m_size != 0; }
37 void clear() noexcept;
40 void resize(
unsigned long n_bytes);
43 void grow(
unsigned long n_bytes);
46 unsigned char *get_data() noexcept
49 const unsigned char *get_data()
const noexcept
54 unsigned long size()
const noexcept
55 {
return m_write_p - m_data; }
59 unsigned long capacity()
const noexcept
64 void set_read_pos(
unsigned long n) noexcept
65 { m_read_p = m_data + n; }
67 void set_write_pos(
unsigned long n) noexcept
68 { m_write_p = m_data + n; }
74 template <
typename T>
void pack(T *val);
75 template <
typename T>
void pack(
const T &val);
76 template <
typename T>
void unpack(T &val);
77 template <
typename T>
void pack(
const T *val,
unsigned long n);
78 template <
typename T>
void unpack(T *val,
unsigned long n);
81 void pack(
const std::string *v,
unsigned long n);
82 void unpack(std::string *v,
unsigned long n);
84 void pack(
const std::string &str);
85 void unpack(std::string &str);
87 void pack(
const std::vector<std::string> &v);
88 void unpack(std::vector<std::string> &v);
90 template<
typename T>
void pack(
const std::vector<T> &v);
91 template<
typename T>
void unpack(std::vector<T> &v);
93 template<
typename T,
size_t N>
void pack(
const std::array<T,N> &v);
94 template<
typename T,
size_t N>
void unpack(std::array<T,N> &v);
96 template<
typename KT,
typename VT>
void pack(
const std::map<KT, VT> &v);
97 template<
typename KT,
typename VT>
void unpack(std::map<KT, VT> &v);
99 template<
typename T1,
typename T2>
void pack(
const std::pair<T1, T2> &v);
100 template<
typename T1,
typename T2>
void unpack(std::pair<T1, T2> &v);
105 template <
typename T>
int expect(
const T &val);
106 template <
typename T>
int expect(
const T *val,
unsigned long n);
107 int expect(
const char *str);
110 int broadcast(MPI_Comm comm,
int root_rank=0);
115 constexpr
unsigned int get_block_size()
119 unsigned long m_size;
120 unsigned char *m_data;
121 unsigned char *m_read_p;
122 unsigned char *m_write_p;
126 template <
typename T>
127 void teca_binary_stream::pack(T *val)
134 template <
typename T>
135 void teca_binary_stream::pack(
const T &val)
137 this->grow(
sizeof(T));
138 *((T *)m_write_p) = val;
139 m_write_p +=
sizeof(T);
143 template <
typename T>
144 void teca_binary_stream::unpack(T &val)
146 val = *((T *)m_read_p);
147 m_read_p +=
sizeof(T);
151 template <
typename T>
152 void teca_binary_stream::pack(
const T *val,
unsigned long n)
154 unsigned long n_bytes = n*
sizeof(T);
157 unsigned long nn = n*
sizeof(T);
158 memcpy(m_write_p, val, nn);
163 template <
typename T>
164 void teca_binary_stream::unpack(T *val,
unsigned long n)
166 unsigned long nn = n*
sizeof(T);
167 memcpy(val, m_read_p, nn);
173 void teca_binary_stream::pack(
const std::string *v,
unsigned long n)
175 for (
unsigned long i = 0; i < n; ++i)
181 void teca_binary_stream::unpack(std::string *v,
unsigned long n)
183 for (
unsigned long i = 0; i < n; ++i)
189 void teca_binary_stream::pack(
const std::string &str)
191 unsigned long slen = str.size();
193 this->pack(str.c_str(), slen);
198 void teca_binary_stream::unpack(std::string &str)
200 unsigned long slen = 0;
204 str.assign(
reinterpret_cast<char*
>(m_read_p), slen);
211 void teca_binary_stream::pack(
const std::vector<std::string> &v)
213 unsigned long vlen = v.size();
215 for (
unsigned long i = 0; i < vlen; ++i)
221 void teca_binary_stream::unpack(std::vector<std::string> &v)
227 for (
unsigned long i = 0; i < vlen; ++i)
233 void teca_binary_stream::pack(
const std::vector<T> &v)
235 const unsigned long vlen = v.size();
237 this->pack(v.data(), vlen);
242 void teca_binary_stream::unpack(std::vector<T> &v)
248 this->unpack(v.data(), vlen);
252 template<
typename T,
size_t N>
253 void teca_binary_stream::pack(
const std::array<T,N> &v)
256 this->pack(v.data(), N);
260 template<
typename T,
size_t N>
261 void teca_binary_stream::unpack(std::array<T,N> &v)
266 this->unpack(v.data(), N);
270 template<
typename KT,
typename VT>
271 void teca_binary_stream::pack(
const std::map<KT, VT> &m)
273 unsigned long n_elem = m.size();
276 typename std::map<KT,VT>::const_iterator it = m.begin();
278 for (
unsigned long i = 0; i < n_elem; ++i)
280 this->pack(it->first);
281 this->pack(it->second);
286 template<
typename KT,
typename VT>
287 void teca_binary_stream::unpack(std::map<KT, VT> &m)
289 unsigned long n_elem = 0;
290 this->unpack(n_elem);
292 for (
unsigned long i = 0; i < n_elem; ++i)
300 m.emplace(std::move(key), std::move(val));
305 template<
typename T1,
typename T2>
306 void teca_binary_stream::pack(
const std::pair<T1, T2> &p)
309 this->pack(p.second);
313 template<
typename T1,
typename T2>
314 void teca_binary_stream::unpack(std::pair<T1, T2> &p)
316 this->unpack(p.first);
317 this->unpack(p.second);
322 int teca_binary_stream::expect(
const T &val)
335 int teca_binary_stream::expect(
const T *val,
unsigned long n)
338 T *tmp = (T*)malloc(n*
sizeof(T));
339 this->unpack(tmp, n);
340 for (
unsigned long i = 0; i < n; ++i)
342 if (tmp[i] != val[i])
354 int teca_binary_stream::expect(
const char *str)
356 unsigned long n = strlen(str);
357 char *tmp = (
char*)malloc(n);
358 this->unpack(tmp, n);
359 int same = strncmp(str, tmp, n);
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:17
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:161