ADTF  3.18.2
stream.h
Go to the documentation of this file.
1 
7 #ifndef _STREAM_CLASS_HEADER_
8 #define _STREAM_CLASS_HEADER_
9 
10 namespace adtf
11 {
12 namespace ucom
13 {
14 namespace ant
15 {
16 
20 class DOEXPORT cStream : public catwo::object<IInputStream, IOutputStream, IStream>
21 
22 {
24 
25  public:
29  typedef enum : uint8_t
30  {
32  INPUT_STREAM = 1,
34  OUTPUT_STREAM = 2
35  } tStreamType;
36 
37  protected:
38 
43  typedef enum // untyped for combinations
44  {
46  FLG_KEEP_POSITION = 1,
48  FLG_DONT_GROW = 2
49  } tStreamFlags;
50 
51  private:
52  cStream() = delete;
53  //USE CopyFrom
54  cStream(const cStream& oStream) = delete;
55  //USE CopyFrom or CopyTo
56  // We need an return value because
57  cStream& operator=(const cStream& oStream) = delete;
58  cStream& operator=(cStream&& oStream) = delete;
59  cStream(cStream&& oStream) = delete;
60 
61  public:
68  explicit cStream(tStreamType eType,
69  tFileSize szBuffer=0,
70  uint32_t ui32Flags=0);;
71 
75  virtual ~cStream();
76 
83  tResult SetSize(tFileSize szBuffer, uint32_t ui32Flags=0);
84 
90  tResult CopyTo(cStream& strmOutput) const;
91 
97  tResult CopyFrom(cStream& strmInput);
98 
104  const void* GetCurrentPtr() const;
105 
112 
119 
127  tResult AttachReference(void* pReferenceBuffer, tFileSize szBufferSize);
128 
134 
135 
136  public: // implements IStream
146  tResult Read(void* pvBuffer, size_t nBufferSize, size_t* pnBytesRead=nullptr) const override;
147 
156  tResult Write(const void* pvBuffer, size_t nBufferSize, size_t* pnBytesWritten=nullptr) override;
157 
168  tResult Seek(tFilePos nPos, tSeekOrigin eOrigin, tFilePos* pNewPos=nullptr) const override;
169 
177  tResult Reset() override;
178 
184  tResult Flush() override;
185 
186  protected:
195  bool IsEof() const;
204 };
205 
206 
207 /**********************************************************************************************/
208 // Writer Helper functions
209 /**********************************************************************************************/
210 extern tResult ucom_write_to_stream_buffer(IOutputStream& oOutStream,
211  const void* pBuffer,
212  size_t szBuffer,
213  size_t& szCount);
214 
215 extern tResult ucom_write_to_stream_string(IOutputStream& oOutStream,
216  const adtf_util::cString& strValue,
217  size_t& szCount);
218 
219 /**********************************************************************************************/
220 // READER Helper functions
221 /**********************************************************************************************/
222 extern tResult ucom_read_from_stream_buffer(const IInputStream& oInStream,
223  void* pBuffer,
224  size_t szBuffer,
225  size_t& szCount);
226 
227 extern tResult ucom_read_from_stream_string(const IInputStream& oInStream,
228  adtf_util::cString& strValue,
229  size_t& szReadCount);
230 
235 template<typename T>
237 {
238 public:
239  static tResult Write(IOutputStream& oOutStream,
240  const T& oValue,
241  size_t& szCount)
242  {
243  //we need access to swap value in memory
244  T oValueToWrite = oValue;
245  if (IStream::m_ui8PlatformByteOrder == PLATFORM_BIG_ENDIAN_8)
246  {
247  RETURN_IF_FAILED(adtf_util::cSystem::SwapEndian(&oValueToWrite));
248  }
249 
250  auto pReadPosition = reinterpret_cast<const uint8_t*>(&oValueToWrite);
251  size_t nBytesToWrite = sizeof(oValueToWrite);
252  while (nBytesToWrite)
253  {
254  size_t szWrittenSize = 0;
255  RETURN_IF_FAILED(oOutStream.Write(pReadPosition, nBytesToWrite, &szWrittenSize));
256  szCount += szWrittenSize;
257  pReadPosition += szWrittenSize;
258  nBytesToWrite -= szWrittenSize;
259  }
260 
262  }
263 
264  static tResult Read(const IInputStream& oInStream,
265  T& oValue,
266  size_t& szCount)
267  {
268  auto pWritePosition = reinterpret_cast<uint8_t*>(&oValue);
269  size_t nBytesToRead = sizeof(oValue);
270  while (nBytesToRead)
271  {
272  size_t szReadSize = 0;
273  RETURN_IF_FAILED(oInStream.Read(pWritePosition, nBytesToRead, &szReadSize));
274  szCount += szReadSize;
275  pWritePosition += szReadSize;
276  nBytesToRead -= szReadSize;
277  }
278 
280  }
281 };
282 
286 template<>
288 {
289 public:
290  static tResult Write(IOutputStream& oOutStream,
291  const adtf_util::cString& strValue,
292  size_t& szCount)
293  {
294  return ucom_write_to_stream_string(oOutStream, strValue, szCount);
295  }
296  static tResult Read(const IInputStream& oInStream,
297  adtf_util::cString& strValue,
298  size_t& szReadCount)
299  {
300  return ucom_read_from_stream_string(oInStream, strValue, szReadCount);
301  }
302 };
303 
304 //Function template for writing and reading types to and from a IStream
305 template<typename T>
306 tResult ucom_write_to_stream(IOutputStream& oOutStream, const T& oValue, size_t& szCount)
307 {
308  return ucom_type_streamer<T>::Write(oOutStream, oValue, szCount);
309 }
310 
311 template<typename T>
312 tResult ucom_read_from_stream(const IInputStream& oInStream, T& oValue, size_t& szCount)
313 {
314  return ucom_type_streamer<T>::Read(oInStream, oValue, szCount);
315 }
316 
317 template<typename T>
318 IOutputStream& operator<<(IOutputStream& oWriteStream, const T& oValue)
319 {
320  size_t szCount = 0;
321  ucom_type_streamer<T>::Write(oWriteStream, oValue, szCount);
322  return oWriteStream;
323 }
324 
325 template<typename T>
326 const IInputStream& operator>>(const IInputStream& oReadStream, T& oValue)
327 {
328  size_t szCount = 0;
329  ucom_type_streamer<T>::Read(oReadStream, oValue, szCount);
330  return oReadStream;
331 }
332 
333 
334 }//ns ant
335 
336 using ant::cStream;
337 
338 using ant::ucom_type_streamer;
339 
340 using ant::ucom_write_to_stream;
341 using ant::ucom_read_from_stream;
342 
343 using ant::ucom_write_to_stream_buffer;
344 using ant::ucom_read_from_stream_buffer;
345 
346 using ant::ucom_write_to_stream_string;
347 using ant::ucom_read_from_stream_string;
348 
349 }//ns ucom
350 }//ns adtf
351 
352 #endif // _STREAM_CLASS_HEADER_
tInt64 tFileSize
type definition for a file or stream size value (platform and compiler independent type).
tInt64 tFilePos
type definition for a file or stream position value (platform and compiler independent type).
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
An interface for reading data from a stream.
Definition: stream_intf.h:24
virtual tResult Read(void *pvBuffer, size_t nBufferSize, size_t *pnBytesRead=nullptr) const =0
Read data from stream.
An interface for writing data to a stream.
Definition: stream_intf.h:54
virtual tResult Write(const void *pvBuffer, size_t nBufferSize, size_t *pnBytesWritten=nullptr)=0
Write data to stream.
The cStream Class provides a basic, simple implementation for IStream interface.
Definition: stream.h:22
const void * GetCurrentPtr() const
Gets the address of the buffers current seek position.
tResult SetSize(tFileSize szBuffer, uint32_t ui32Flags=0)
Sets the current size of the stream.
tFileSize GetCurrentSize() const
Returns the current buffer size.
tResult AttachReference(void *pReferenceBuffer, tFileSize szBufferSize)
Reference to a extern buffer (no intern buffer will be used to write to or read from).
tResult CopyTo(cStream &strmOutput) const
Copies a Stream to another.
tStreamType
Stream direction type.
Definition: stream.h:30
tResult Reset() override
Implements.
cStream(tStreamType eType, tFileSize szBuffer=0, uint32_t ui32Flags=0)
Constructs a Stream with the current size.
tResult CopyFrom(cStream &strmInput)
Copies content from another Stream.
virtual ~cStream()
Destructor.
tResult Seek(tFilePos nPos, tSeekOrigin eOrigin, tFilePos *pNewPos=nullptr) const override
Implements IStream::Seek.
tResult Flush() override
Implements.
tResult Write(const void *pvBuffer, size_t nBufferSize, size_t *pnBytesWritten=nullptr) override
Implements.
tResult Read(void *pvBuffer, size_t nBufferSize, size_t *pnBytesRead=nullptr) const override
Implements.
tFileSize GetHighestPosIndex() const
Returns the current buffer size which was written (without a gap at the end).
tStreamType m_eType
Type of Stream, see tStreamType.
Definition: stream.h:39
bool IsEof() const
Checks if end of stream is reached.
tFileSize GetRemainingBytes() const
tResult DetachReference()
Reference to a buffer will be detached.
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:379
#define PLATFORM_BIG_ENDIAN_8
defines the big endianess value, that will be retrieved by
Definition: constants.h:121
#define A_UTILS_D(__pclassname_)
Helper macro for d-pattern definitions.
Definition: d_ptr.h:270
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2778
ADTF adtf_util Namespace - Within adtf this is used as adtf::util or adtf_util and also defined as A_...
Namespace for entire ADTF SDK.
Helper template for writing and reading types.
Definition: stream.h:237