Free Electron
MemStream.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 
8 /**************************************************************************//**
9  @brief Implementation of std::istream for raw byte buffer
10 
11  @ingroup core
12 
13  Usage:
14  @code
15  MemStream memstream(pBuffer, bufferSize);
16  @endcode
17 
18  See https://tuttlem.github.io/2014/08/18/getting-istream-to-work-off-a-byte-array.html
19 *//***************************************************************************/
20 class MemStream: public std::istream
21 {
22  private:
23 
24  class MemBuf: public std::basic_streambuf<char>
25  {
26  public:
27  MemBuf(const uint8_t *p, size_t l)
28  {
29  setg((char*)p, (char*)p, (char*)p + l);
30  }
31 
32  };
33 
34  public:
35  MemStream(const uint8_t *p, size_t l):
36  std::istream(&m_memBuf),
37  m_memBuf(p, l)
38  {
39  rdbuf(&m_memBuf);
40  }
41 
42  MemBuf m_memBuf;
43 };
Implementation of std::istream for raw byte buffer.
Definition: MemStream.h:20