DP3
BlobOStream.h
Go to the documentation of this file.
1 // BlobOStream.h: Output 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_BLOBOSTREAM_H
7 #define LOFAR_BLOB_BLOBOSTREAM_H
8 
9 #include "BlobOBuffer.h"
10 
11 #include <stack>
12 #include <vector>
13 #include <string>
14 #include <complex>
15 #include <cstring>
16 
17 namespace dp3 {
18 namespace blob {
19 
22 
24 
39 
40 class BlobOStream {
41  public:
46 
49 
51  void clear();
52 
54  uint64_t size() const { return itsCurLength; }
55 
64  //
69  //
72  unsigned int putStart(int objectVersion);
73  unsigned int putStart(const std::string& objectType, int objectVersion);
74  unsigned int putStart(const char* objectType, int objectVersion);
76 
79  uint64_t putEnd();
80 
84  BlobOStream& operator<<(const bool& value);
85  BlobOStream& operator<<(const char& value);
86  BlobOStream& operator<<(const int8_t& value);
87  BlobOStream& operator<<(const uint8_t& value);
88  BlobOStream& operator<<(const int16_t& value);
89  BlobOStream& operator<<(const uint16_t& value);
90  BlobOStream& operator<<(const int32_t& value);
91  BlobOStream& operator<<(const uint32_t& value);
92  BlobOStream& operator<<(const int64_t& value);
93  BlobOStream& operator<<(const uint64_t& value);
94  BlobOStream& operator<<(const float& value);
95  BlobOStream& operator<<(const double& value);
96  BlobOStream& operator<<(const std::complex<float>& value);
97  BlobOStream& operator<<(const std::complex<double>& value);
98  BlobOStream& operator<<(const std::string& value);
99  BlobOStream& operator<<(const char* value);
101 
105  void put(const bool* values, uint64_t nrval);
106  void put(const char* values, uint64_t nrval);
107  void put(const int8_t* values, uint64_t nrval);
108  void put(const uint8_t* values, uint64_t nrval);
109  void put(const int16_t* values, uint64_t nrval);
110  void put(const uint16_t* values, uint64_t nrval);
111  void put(const int32_t* values, uint64_t nrval);
112  void put(const uint32_t* values, uint64_t nrval);
113  void put(const int64_t* values, uint64_t nrval);
114  void put(const uint64_t* values, uint64_t nrval);
115  void put(const float* values, uint64_t nrval);
116  void put(const double* values, uint64_t nrval);
117  void put(const std::complex<float>* values, uint64_t nrval);
118  void put(const std::complex<double>* values, uint64_t nrval);
119  void put(const std::string* values, uint64_t nrval);
121 
125  void put(const std::vector<bool>& values);
126  template <class T>
127  void put(const std::vector<T>& values);
129 
131  void putBoolVec(const std::vector<bool>& values);
132 
140  int64_t setSpace(uint64_t nbytes);
141 
146  unsigned int align(unsigned int n);
147 
150  int64_t tellPos() const;
151 
152  private:
154  unsigned int doPutStart(const char* objectType, unsigned int nrc,
155  int objectVersion);
156 
159  void putBuf(const void* buf, uint64_t sz);
160 
163  void checkPut() const;
164  void throwPut() const;
166 
167  bool itsSeekable;
168  uint64_t itsCurLength;
169  unsigned int itsLevel;
171  std::stack<uint64_t> itsObjLen;
173  std::stack<int64_t> itsObjPtr;
175  BlobOBuffer* itsStream;
176 };
177 
179 
180 inline void BlobOStream::clear() {
181  itsCurLength = 0;
182  itsLevel = 0;
183 }
184 
185 inline unsigned int BlobOStream::putStart(int objectVersion) {
186  return doPutStart("", 0, objectVersion);
187 }
188 
189 inline unsigned int BlobOStream::putStart(const std::string& objectType,
190  int objectVersion) {
191  return doPutStart(objectType.data(), objectType.size(), objectVersion);
192 }
193 
194 inline unsigned int BlobOStream::putStart(const char* objectType,
195  int objectVersion) {
196  return doPutStart(objectType, strlen(objectType), objectVersion);
197 }
198 
199 inline int64_t BlobOStream::tellPos() const { return itsStream->tellPos(); }
200 
201 template <class T>
202 inline void BlobOStream::put(const std::vector<T>& vec) {
203  operator<<(uint64_t(vec.size()));
204  put(&vec[0], vec.size());
205 }
206 inline void BlobOStream::put(const std::vector<bool>& vec) {
207  operator<<(uint64_t(vec.size()));
208  putBoolVec(vec);
209 }
210 
211 inline void BlobOStream::checkPut() const {
212  if (itsLevel == 0) throwPut();
213 }
214 
215 } // namespace blob
216 } // namespace dp3
217 
218 #endif
Abstract base class for output buffer for a blob.
Definition: BlobOBuffer.h:32
virtual int64_t tellPos() const =0
Output stream for a blob.
Definition: BlobOStream.h:40
BlobOStream & operator<<(const int16_t &value)
void put(const uint8_t *values, uint64_t nrval)
BlobOStream & operator<<(const uint8_t &value)
BlobOStream & operator<<(const std::complex< float > &value)
void put(const bool *values, uint64_t nrval)
unsigned int align(unsigned int n)
void put(const float *values, uint64_t nrval)
BlobOStream & operator<<(const uint32_t &value)
BlobOStream(BlobOBuffer &)
void put(const char *values, uint64_t nrval)
BlobOStream & operator<<(const int32_t &value)
BlobOStream & operator<<(const char *value)
BlobOStream & operator<<(const float &value)
void put(const uint64_t *values, uint64_t nrval)
uint64_t size() const
Get the total size.
Definition: BlobOStream.h:54
unsigned int putStart(int objectVersion)
Definition: BlobOStream.h:185
BlobOStream & operator<<(const int64_t &value)
BlobOStream & operator<<(const char &value)
void put(const uint16_t *values, uint64_t nrval)
BlobOStream & operator<<(const uint16_t &value)
void put(const int8_t *values, uint64_t nrval)
BlobOStream & operator<<(const uint64_t &value)
void putBoolVec(const std::vector< bool > &values)
Put a vector of bools (without putting the size).
void put(const uint32_t *values, uint64_t nrval)
BlobOStream & operator<<(const std::string &value)
BlobOStream & operator<<(const bool &value)
int64_t setSpace(uint64_t nbytes)
BlobOStream & operator<<(const int8_t &value)
void put(const std::complex< double > *values, uint64_t nrval)
void put(const std::string *values, uint64_t nrval)
void put(const std::complex< float > *values, uint64_t nrval)
~BlobOStream()
Destructor.
void put(const double *values, uint64_t nrval)
void clear()
Clear the object. I.e., reset the current level and length.
Definition: BlobOStream.h:180
void put(const int64_t *values, uint64_t nrval)
void put(const int16_t *values, uint64_t nrval)
void put(const int32_t *values, uint64_t nrval)
int64_t tellPos() const
Definition: BlobOStream.h:199
BlobOStream & operator<<(const double &value)
BlobOStream & operator<<(const std::complex< double > &value)
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53