DP3
BlobIStream.h
Go to the documentation of this file.
1 // BlobIStream.h: Input stream for a blob
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #ifndef LOFAR_BLOB_BLOBISTREAM_H
7 #define LOFAR_BLOB_BLOBISTREAM_H
8 
9 #include "common/DataFormat.h"
10 #include "common/DataConvert.h"
11 
12 #include "BlobIBuffer.h"
13 
14 #include <stack>
15 #include <vector>
16 #include <string>
17 #include <complex>
18 
19 namespace dp3 {
20 namespace blob {
21 
24 
26 
42 
43 class BlobIStream {
44  public:
49 
52 
54  void clear();
55 
65  int getStart(const std::string& objectType);
66  int getStart(const char* objectType = "");
68 
73  uint64_t getEnd();
74 
81  const std::string& getNextType();
82  const std::string& getNextType(uint64_t& size);
84 
88  BlobIStream& operator>>(bool& value);
89  BlobIStream& operator>>(char& value);
90  BlobIStream& operator>>(int8_t& value);
91  BlobIStream& operator>>(uint8_t& value);
92  BlobIStream& operator>>(int16_t& value);
93  BlobIStream& operator>>(uint16_t& value);
94  BlobIStream& operator>>(int32_t& value);
95  BlobIStream& operator>>(uint32_t& value);
96  BlobIStream& operator>>(int64_t& value);
97  BlobIStream& operator>>(uint64_t& value);
98  BlobIStream& operator>>(float& value);
99  BlobIStream& operator>>(double& value);
100  template <class T>
101  BlobIStream& operator>>(std::complex<T>& value);
102  // BlobIStream& operator>> (i4complex& value);
103  // BlobIStream& operator>> (i16complex& value);
104  // BlobIStream& operator>> (u16complex& value);
105  BlobIStream& operator>>(std::complex<float>& value);
106  BlobIStream& operator>>(std::complex<double>& value);
107  BlobIStream& operator>>(std::string& value);
109 
113  void get(bool* values, uint64_t nrval);
114  void get(char* values, uint64_t nrval);
115  void get(int8_t* values, uint64_t nrval);
116  void get(uint8_t* values, uint64_t nrval);
117  void get(int16_t* values, uint64_t nrval);
118  void get(uint16_t* values, uint64_t nrval);
119  void get(int32_t* values, uint64_t nrval);
120  void get(uint32_t* values, uint64_t nrval);
121  void get(int64_t* values, uint64_t nrval);
122  void get(uint64_t* values, uint64_t nrval);
123  void get(float* values, uint64_t nrval);
124  void get(double* values, uint64_t nrval);
125  template <class T>
126  void get(std::complex<T>* values, uint64_t nrval);
127  void get(std::complex<float>* values, uint64_t nrval);
128  void get(std::complex<double>* values, uint64_t nrval);
129  void get(std::string* values, uint64_t nrval);
131 
135  void get(std::vector<bool>& values);
136  template <typename T>
137  void get(std::vector<T>& values);
139 
142  void getBoolVec(std::vector<bool>& values, uint64_t size);
143 
150  int64_t getSpace(uint64_t nbytes);
151 
156  unsigned int align(unsigned int n);
157 
160  int64_t tellPos() const;
161 
165  bool mustConvert() const;
168 
169  private:
171  void getBuf(void* buf, uint64_t sz);
172 
175  void checkGet() const;
176  void throwGet() const;
178 
179  bool itsSeekable;
180  bool itsMustConvert;
181  bool itsHasCachedType;
182  uint64_t itsCurLength;
183  unsigned int itsLevel;
184  int itsVersion;
186  dp3::common::DataFormat itsDataFormat;
188  std::string itsObjectType;
190  std::stack<uint64_t> itsObjTLN;
192  std::stack<uint64_t> itsObjLen;
194  BlobIBuffer* itsStream;
195 };
196 
198 
199 inline void BlobIStream::clear() {
200  itsCurLength = 0;
201  itsLevel = 0;
202  itsHasCachedType = false;
203 }
204 
205 inline int BlobIStream::getStart(const char* objectType) {
206  return getStart(std::string(objectType));
207 }
208 
209 inline int64_t BlobIStream::tellPos() const { return itsStream->tellPos(); }
210 
211 inline bool BlobIStream::mustConvert() const { return itsMustConvert; }
212 
214  return itsDataFormat;
215 }
216 
217 template <typename T>
218 inline BlobIStream& BlobIStream::operator>>(std::complex<T>& value) {
219  getBuf(&value, sizeof(std::complex<T>));
220  if (itsMustConvert) {
221  dp3::common::dataConvert(itsDataFormat, &value, 1);
222  }
223  return *this;
224 }
225 template <typename T>
226 inline void BlobIStream::get(std::complex<T>* values, uint64_t nrval) {
227  getBuf(values, nrval * sizeof(std::complex<T>));
228  if (itsMustConvert) {
229  dp3::common::dataConvert(itsDataFormat, values, nrval);
230  }
231 }
232 
233 template <typename T>
234 inline void BlobIStream::get(std::vector<T>& vec) {
235  uint64_t sz;
236  operator>>(sz);
237  vec.resize(sz);
238  get(&vec[0], sz);
239 }
240 inline void BlobIStream::get(std::vector<bool>& vec) {
241  uint64_t sz;
242  operator>>(sz);
243  getBoolVec(vec, sz);
244 }
245 
246 inline void BlobIStream::checkGet() const {
247  if (itsLevel == 0) throwGet();
248 }
249 
250 } // namespace blob
251 } // namespace dp3
252 
253 #endif
Abstract base class for input buffer for a blob.
Definition: BlobIBuffer.h:36
virtual int64_t tellPos() const =0
Input stream for a blob.
Definition: BlobIStream.h:43
BlobIStream(BlobIBuffer &)
const std::string & getNextType(uint64_t &size)
~BlobIStream()
Destructor.
void get(char *values, uint64_t nrval)
void get(uint8_t *values, uint64_t nrval)
BlobIStream & operator>>(double &value)
int64_t tellPos() const
Definition: BlobIStream.h:209
BlobIStream & operator>>(uint8_t &value)
void get(uint64_t *values, uint64_t nrval)
BlobIStream & operator>>(std::string &value)
BlobIStream & operator>>(uint16_t &value)
unsigned int align(unsigned int n)
BlobIStream & operator>>(std::complex< double > &value)
BlobIStream & operator>>(char &value)
BlobIStream & operator>>(uint32_t &value)
void get(double *values, uint64_t nrval)
void get(int8_t *values, uint64_t nrval)
void getBoolVec(std::vector< bool > &values, uint64_t size)
void get(uint16_t *values, uint64_t nrval)
void get(int64_t *values, uint64_t nrval)
BlobIStream & operator>>(int8_t &value)
int getStart(const std::string &objectType)
BlobIStream & operator>>(uint64_t &value)
int64_t getSpace(uint64_t nbytes)
void get(int16_t *values, uint64_t nrval)
BlobIStream & operator>>(float &value)
void clear()
Clear the object. I.e., reset the current level and position.
Definition: BlobIStream.h:199
void get(std::complex< float > *values, uint64_t nrval)
BlobIStream & operator>>(std::complex< float > &value)
void get(int32_t *values, uint64_t nrval)
BlobIStream & operator>>(int32_t &value)
void get(std::complex< double > *values, uint64_t nrval)
bool mustConvert() const
Definition: BlobIStream.h:211
BlobIStream & operator>>(int16_t &value)
const std::string & getNextType()
void get(bool *values, uint64_t nrval)
BlobIStream & operator>>(int64_t &value)
void get(uint32_t *values, uint64_t nrval)
void get(std::string *values, uint64_t nrval)
void get(float *values, uint64_t nrval)
dp3::common::DataFormat dataFormat() const
Definition: BlobIStream.h:213
BlobIStream & operator>>(bool &value)
void dataConvert(DataFormat, char *inout, unsigned int nrval)
Definition: DataConvert.h:175
DataFormat
This file defines an enum for the possible machine data formats.
Definition: DataFormat.h:24
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53