map_io.h
Go to the documentation of this file.
1 
16 #pragma once
17 
19 #include "interop/util/length_of.h"
20 
21 namespace illumina { namespace interop { namespace io
22 {
23 
26  template<typename Source, typename Destination>
27  void copy_from(std::ostream &, const Source &, const Destination &)
28  { }
29 
35  template<typename Source, typename Destination>
36  void copy_from(std::istream &, Destination &dst, const Source &src)
37  {
38  dst = src;
39  }
45  template<typename Source, typename Destination>
46  void copy_from(const char*, Destination &dst, const Source &src) // TODO: Simplify this entire file with templates
47  {
48  dst = src;
49  }
50 
57  template<typename ReadType, typename ValueType>
58  std::streamsize stream_map(std::istream &in, ValueType &val)
59  {
60  ReadType read_val;
61  read_binary(in, read_val);
62  val = static_cast<ValueType>(read_val);
63  return in.gcount();
64  }
71  template<typename ReadType, typename ValueType>
72  std::streamsize stream_map(char*& in, ValueType &val)
73  {
74  ReadType read_val;
75  read_binary(in, read_val);
76  val = static_cast<ValueType>(read_val);
77  return sizeof(ReadType);
78  }
79 
86  inline std::streamsize stream_map(std::istream &in, std::string &val)
87  {
88  read_binary(in, val, "");
89  return in.gcount();
90  }
97  inline std::streamsize stream_map(char*& in, std::string &val)
98  {
99  read_binary(in, val);
100  return val.size();
101  }
102 
105  template<typename ReadType, typename ValueType, bool SameSize=sizeof(ReadType)==sizeof(ValueType)>
107  {
118  static std::streamsize read_array_from_stream(std::istream &in, ValueType* vals, const size_t n, const size_t offset=0)
119  {
120  std::streamsize tot = 0;
121  for (size_t i = 0; i < n; i++)
122  {
123  ReadType read_val;
124  read_binary(in, read_val);
125  vals[offset + i] = read_val;
126  tot += in.gcount();
127  }
128  return tot;
129  }
140  static std::streamsize read_array_from_stream(char* &in, ValueType* vals, const size_t n, const size_t offset=0)
141  {
142  for (size_t i = 0; i < n; i++)
143  {
144  ReadType read_val;
145  read_binary(in, read_val);
146  vals[offset + i] = read_val;
147  }
148  return n*sizeof(ReadType);
149  }
150  };
155  template<typename ReadType, typename ValueType>
156  struct read_array_helper<ReadType, ValueType, true>
157  {
168  static std::streamsize read_array_from_stream(std::istream &in, ValueType* vals, const size_t n, const size_t offset=0)
169  {
170  read_binary(in, vals+offset, n);
171  return in.gcount();
172  }
183  static std::streamsize read_array_from_stream(char* &in, ValueType* vals, const size_t n, const size_t offset=0)
184  {
185  read_binary(in, vals+offset, n);
186  return n*sizeof(ReadType);
187  }
188  };
189 
199  template<typename ReadType, typename ValueType>
200  std::streamsize stream_map(std::istream &in, std::vector<ValueType>&vals, const size_t n)
201  {
202  vals.resize(n);
203  INTEROP_ASSERTMSG(!vals.empty(), "n="<<n);
205  }
206 
207 
217  template<typename ReadType, typename ValueType>
218  std::streamsize stream_map(char*& in, std::vector<ValueType>&vals, const size_t n)
219  {
220  vals.resize(n);
221  INTEROP_ASSERT(!vals.empty());
223  }
224 
234  template<typename ReadType, typename ValueType>
235  std::streamsize padded_stream_map(std::istream &in, std::vector<ValueType>&vals, const size_t n, const ReadType)
236  {
237  vals.resize(n);
238  INTEROP_ASSERTMSG(!vals.empty(), "n="<<n);
240  }
241 
242 
252  template<typename ReadType, typename ValueType>
253  std::streamsize padded_stream_map(char*& in, std::vector<ValueType>&vals, const size_t n, const ReadType)
254  {
255  vals.resize(n);
256  INTEROP_ASSERT(!vals.empty());
258  }
259 
260 
261 
272  template<typename ReadType, typename ValueType>
273  std::streamsize stream_map(std::istream &in, std::vector<ValueType> &vals, const size_t offset, const size_t n)
274  {
275  vals.resize(offset + n);
276  INTEROP_ASSERT(!vals.empty());
277  return read_array_helper<ReadType,ValueType>::read_array_from_stream(in, &vals.front(), n, offset);
278  }
279 
290  template<typename ReadType, typename ValueType>
291  std::streamsize stream_map(char*& in, std::vector<ValueType> &vals, const size_t offset, const size_t n)
292  {
293  vals.resize(offset + n);
294  INTEROP_ASSERT(!vals.empty());
295  return read_array_helper<ReadType,ValueType>::read_array_from_stream(in, &vals.front(), n, offset);
296  }
297 
307  template<typename ReadType, typename ValueType, size_t N>
308  std::streamsize stream_map(std::istream &in, ValueType (&vals)[N], const size_t n)
309  {
311  }
312 
322  template<typename ReadType, typename ValueType, size_t N>
323  std::streamsize stream_map(char* &in, ValueType (&vals)[N], const size_t n)
324  {
326  }
327 
334  template<typename WriteType, typename ValueType>
335  std::streamsize stream_map(std::ostream &out, const ValueType val)
336  {
337  write_binary(out, static_cast<WriteType>(val));
338  return out.tellp();
339  }
340 
349  inline std::streamsize stream_map(std::ostream &out, const std::string &str)
350  {
351  write_binary(out, str);
352  return out.tellp();
353  }
354 
364  template<typename WriteType, typename ValueType>
365  std::streamsize stream_map(std::ostream &out, const ValueType &vals, const size_t n)
366  {
367  INTEROP_ASSERT(util::length_of(vals) >= n);
369  "Write bug: expected values is greater than array size");
370  for (size_t i = 0; i < n; i++)
371  {
372  WriteType write_val = static_cast<WriteType>(vals[i]);
373  write_binary(out, write_val);
374  }
375  return out.tellp();
376  }
377 
388  template<typename WriteType, typename ValueType>
389  std::streamsize padded_stream_map(std::ostream &out, const ValueType &vals, const size_t n, const WriteType pad)
390  {
391  for (size_t i = 0; i < util::length_of(vals); i++)
392  {
393  WriteType write_val = static_cast<WriteType>(vals[i]);
394  write_binary(out, write_val);
395  }
396  for (size_t i = util::length_of(vals); i < n; i++)
397  {
398  write_binary(out, pad);
399  }
400  return out.tellp();
401  }
402 
413  template<typename WriteType, typename ValueType>
414  std::streamsize stream_map(std::ostream &out, const ValueType &vals, const size_t offset, const size_t n)
415  {
416  INTEROP_ASSERT(util::length_of(vals) >= (offset+n));
418  "Write bug: expected values is greater than array size");
419  for (size_t i = 0; i < n; i++)
420  {
421  WriteType write_val = static_cast<WriteType>(vals[offset + i]);
422  write_binary(out, write_val);
423  }
424  return out.tellp();
425  }
426 
429  template<typename Layout>
430  void map_resize(const Layout &, size_t)
431  {
432  }
433 
439  template<typename Layout>
440  void map_resize(Layout &layout, const size_t n)
441  {
442  layout.resize(n);
443  }
444 
445 }}}
446 
447 
#define INTEROP_ASSERTMSG(TST, MSG)
Definition: assert.h:32
Definition: enum_description.h:15
static std::streamsize read_array_from_stream(std::istream &in, ValueType *vals, const size_t n, const size_t offset=0)
Definition: map_io.h:168
void map_resize(const Layout &, size_t)
Definition: map_io.h:430
static std::streamsize read_array_from_stream(std::istream &in, ValueType *vals, const size_t n, const size_t offset=0)
Definition: map_io.h:118
std::streamsize padded_stream_map(std::istream &in, std::vector< ValueType > &vals, const size_t n, const ReadType)
Definition: map_io.h:235
void copy_from(std::ostream &, const Source &, const Destination &)
Definition: map_io.h:27
#define INTEROP_RANGE_CHECK_GT(VALUE, RANGE, EXCEPTION, MESSAGE)
Definition: exception.h:38
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
void write_binary(std::ostream &out, const T *buffer, const size_t n)
Definition: stream_util.h:158
static std::streamsize read_array_from_stream(char *&in, ValueType *vals, const size_t n, const size_t offset=0)
Definition: map_io.h:183
std::streamsize stream_map(std::istream &in, ValueType &val)
Definition: map_io.h:58
Definition: stream_exceptions.h:59
static std::streamsize read_array_from_stream(char *&in, ValueType *vals, const size_t n, const size_t offset=0)
Definition: map_io.h:140
size_t length_of(const T &val)
Definition: length_of.h:55
void read_binary(std::istream &in, T *buffer, const size_t n)
Definition: stream_util.h:31