nlib
MemoryOutputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MEMORYOUTPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_MEMORYOUTPUTSTREAM_H_
5 
6 #include "nn/nlib/OutputStream.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
11  public:
12  MemoryOutputStream() NLIB_NOEXCEPT : m_MemBuf(NULL), m_MemPos(0), m_MemSize(0) {}
13  MemoryOutputStream(void* buf, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
14  template <class T, size_t N>
15  explicit MemoryOutputStream (T(&buf)[N]) NLIB_NOEXCEPT
16  : m_MemBuf(reinterpret_cast<unsigned char*>(&buf[0])),
17  m_MemPos(0),
18  m_MemSize(N * sizeof(T)) {}
20  const void* data() const NLIB_NOEXCEPT { return m_MemBuf; }
21  void Init(void* buf, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
22  template <class T, size_t N>
23  void Init(T (&buf)[N]) NLIB_NOEXCEPT {
24  this->Init(&buf[0], N * sizeof(T));
25  }
26 
27  private:
28  virtual bool PushBuffer_(const void* p, size_t nBytes,
29  bool doFlush) NLIB_NOEXCEPT NLIB_OVERRIDE;
30  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE {
31  m_MemBuf = NULL;
32  m_MemPos = 0;
33  m_MemSize = 0;
34  return true;
35  }
36  virtual void* GetWorkBuffer_(size_t* nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
37 
38  private:
39  unsigned char* m_MemBuf;
40  size_t m_MemPos;
41  size_t m_MemSize;
42  NLIB_DISALLOW_COPY_AND_ASSIGN(MemoryOutputStream);
43 };
44 
45 NLIB_NAMESPACE_END
46 
47 #endif // INCLUDE_NN_NLIB_MEMORYOUTPUTSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
MemoryOutputStream() noexcept
Instantiates the object with default parameters (default constructor). You must call the Init functio...
MemoryOutputStream(T(&buf)[N]) noexcept
Constructs and initializes MemoryOutputStream.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
The class for creating an output stream that makes use of memory.
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
void Init(T(&buf)[N]) noexcept
Initializes MemoryOuputStream and makes it available for use.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
Defines the base class for output streams.
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17