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 : mos_buf_(NULL), mos_pos_(0), mos_size_(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  : mos_buf_(reinterpret_cast<unsigned char*>(&buf[0])),
17  mos_pos_(0),
18  mos_size_(N * sizeof(T)) {}
20  const void* data() const NLIB_NOEXCEPT { return mos_buf_; }
21  errno_t Init(void* buf, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
22  template <class T, size_t N>
23  errno_t Init(T (&buf)[N]) NLIB_NOEXCEPT {
24  return this->Init(&buf[0], N * sizeof(T));
25  }
26 
27  private:
28  virtual bool PushBuffer_(const void* p, size_t nbytes,
29  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE;
30  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE {
31  mos_buf_ = NULL;
32  mos_pos_ = 0;
33  mos_size_ = 0;
34  return true;
35  }
36  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE;
37 
38  private:
39  unsigned char* mos_buf_;
40  size_t mos_pos_;
41  size_t mos_size_;
43 };
44 
45 NLIB_NAMESPACE_END
46 
47 #endif // INCLUDE_NN_NLIB_MEMORYOUTPUTSTREAM_H_
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:210
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_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
The class for creating an output stream that makes use of memory.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
const void * data() const noexcept
Gets a pointer to the start of the output data.
errno_t Init(T(&buf)[N]) noexcept
Initializes MemoryOuputStream and makes it available for use.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
Defines the base class for output streams.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:76
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24