ADTF
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1
7#ifndef _STREAM_CLASS_HEADER_
8#define _STREAM_CLASS_HEADER_
9
10namespace adtf
11{
12namespace ucom
13{
14namespace ant
15{
16
20class DOEXPORT cStream : public catwo::object<IInputStream, IOutputStream, IStream>
21
22{
23 A_UTILS_D(cStream)
24
25 public:
29 typedef enum : uint8_t
30 {
35 } tStreamType;
36
37 protected:
38
43 typedef enum // untyped for combinations
44 {
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
111 tFileSize GetCurrentSize() const;
112
118 tFileSize GetHighestPosIndex() const;
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;
203 tFileSize GetRemainingBytes() const;
204};
205
206
207/**********************************************************************************************/
208// Writer Helper functions
209/**********************************************************************************************/
210extern tResult ucom_write_to_stream_buffer(IOutputStream& oOutStream,
211 const void* pBuffer,
212 size_t szBuffer,
213 size_t& szCount);
214
215extern 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/**********************************************************************************************/
222extern tResult ucom_read_from_stream_buffer(const IInputStream& oInStream,
223 void* pBuffer,
224 size_t szBuffer,
225 size_t& szCount);
226
227extern tResult ucom_read_from_stream_string(const IInputStream& oInStream,
228 adtf_util::cString& strValue,
229 size_t& szReadCount);
230
235template<typename T>
237{
238public:
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
286template<>
287struct ucom_type_streamer<adtf_util::cString>
288{
289public:
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
305template<typename T>
306tResult 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
311template<typename T>
312tResult 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
317template<typename T>
318IOutputStream& 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
325template<typename T>
326const 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
336using ant::cStream;
337
339
340using ant::ucom_write_to_stream;
341using ant::ucom_read_from_stream;
342
343using ant::ucom_write_to_stream_buffer;
344using ant::ucom_read_from_stream_buffer;
345
346using ant::ucom_write_to_stream_string;
347using ant::ucom_read_from_stream_string;
348
349}//ns ucom
350}//ns adtf
351
352#endif // _STREAM_CLASS_HEADER_
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
Definition stream_intf.h:24
virtual tResult Read(void *pvBuffer, size_t nBufferSize, size_t *pnBytesRead=nullptr) const =0
Definition stream_intf.h:54
virtual tResult Write(const void *pvBuffer, size_t nBufferSize, size_t *pnBytesWritten=nullptr)=0
Definition stream.h:22
const void * GetCurrentPtr() const
tResult Read(void *pvBuffer, size_t nBufferSize, size_t *pnBytesRead=nullptr) const override
tResult Write(const void *pvBuffer, size_t nBufferSize, size_t *pnBytesWritten=nullptr) override
@ FLG_DONT_GROW
Will only have the at construction time decided size.
Definition stream.h:48
@ FLG_KEEP_POSITION
Sets correct end of file marker at.
Definition stream.h:46
tResult Reset() override
tResult SetSize(tFileSize szBuffer, uint32_t ui32Flags=0)
tFileSize GetCurrentSize() const
tFileSize GetRemainingBytes() const
tResult CopyFrom(cStream &strmInput)
cStream(tStreamType eType, tFileSize szBuffer=0, uint32_t ui32Flags=0)
tStreamType
Definition stream.h:30
@ INPUT_STREAM
Is a Input Stream (can be read).
Definition stream.h:32
@ OUTPUT_STREAM
Is a Output Stream (can be written).
Definition stream.h:34
tFileSize GetHighestPosIndex() const
tResult AttachReference(void *pReferenceBuffer, tFileSize szBufferSize)
tResult CopyTo(cStream &strmOutput) const
tResult Flush() override
tResult Seek(tFilePos nPos, tSeekOrigin eOrigin, tFilePos *pNewPos=nullptr) const override
tStreamType m_eType
Type of Stream, see tStreamType.
Definition stream.h:39
Definition object.h:397
Namespace for all functionality of the ADTF UCOM SDK provided since v3.0.
Definition test_runtime.h:14
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
Definition stream.h:237