nlib
ZlibOutputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_ZLIBOUTPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_ZLIBOUTPUTSTREAM_H_
5 #ifndef NLIB_NO_ZLIB
6 
7 #include <stdlib.h>
8 #include <zlib.h>
10 
11 NLIB_NAMESPACE_BEGIN
12 
13 class OutputStream;
14 
16  typedef void* (*ReallocFunc)(void* p, size_t n);
17  void* buf_in;
18  void* buf_out;
19  size_t buffer_size;
20  int level;
21  ReallocFunc realloc_func;
22 
23  public:
25  buf_out(NULL),
26  buffer_size(64 * 1024),
27  level(Z_DEFAULT_COMPRESSION),
28  realloc_func(::nlib_realloc) {}
29  NLIB_CEXPR ZlibOutputStreamSettings(void* buf_in_, void* buf_out_, size_t buffer_size_,
30  int level_) NLIB_NOEXCEPT : buf_in(buf_in_),
31  buf_out(buf_out_),
32  buffer_size(buffer_size_),
33  level(level_),
34  realloc_func(::nlib_realloc) {}
35  NLIB_CEXPR ZlibOutputStreamSettings(void* buf_in_, void* buf_out_, size_t buffer_size_,
36  int level_, ReallocFunc realloc_func_) NLIB_NOEXCEPT
37  : buf_in(buf_in_),
38  buf_out(buf_out_),
39  buffer_size(buffer_size_),
40  level(level_),
41  realloc_func(realloc_func_) {}
42 };
43 
44 namespace detail {
45 
46 class NLIB_VIS_PUBLIC ZlibOutputTransform NLIB_FINAL : public OutputTransform {
47  public:
48  typedef ZlibOutputStreamSettings::ReallocFunc ReallocFunc;
49  ZlibOutputTransform() NLIB_NOEXCEPT;
50  virtual ~ZlibOutputTransform() NLIB_NOEXCEPT NLIB_OVERRIDE;
51 
52  virtual errno_t Transform(OutputStream* os, const void* p, size_t nBytes,
53  bool doFlush) NLIB_NOEXCEPT NLIB_OVERRIDE;
54  virtual void* GetWorkBuffer(size_t* n) NLIB_NOEXCEPT NLIB_OVERRIDE;
55  virtual errno_t OnSetStream(OutputStream* os) NLIB_NOEXCEPT NLIB_OVERRIDE;
56  virtual errno_t OnClose(OutputStream* os) NLIB_NOEXCEPT NLIB_OVERRIDE;
57 
58  errno_t Init(const ZlibOutputStreamSettings& settings) NLIB_NOEXCEPT;
59 
60  private:
61  z_stream m_Strm;
62  ReallocFunc m_Realloc;
63  unsigned char* m_BufIn; // to z_stream::next_in
64  unsigned char* m_BufOut; // to z_stream::next_out
65  size_t m_BufSize; // for both m_BufIn and m_BufOut
66  size_t m_BufInCur;
67  int m_Level;
68  bool m_IsInternalBuffer;
69  unsigned char m_BaseBuf[256]; // buffer for the base class
70 };
71 
72 } // namespace detail
73 
75  : public OutputConverterStreamTempl<detail::ZlibOutputTransform> {
76  public:
78  virtual ~ZlibOutputStream() NLIB_NOEXCEPT NLIB_OVERRIDE;
79  errno_t Init() NLIB_NOEXCEPT {
81  return this->Init(settings);
82  }
83  errno_t Init(const ZlibOutputStreamSettings& settings) NLIB_NOEXCEPT {
84  return m_Transform.Init(settings);
85  }
86 
87  private:
89 };
90 
91 NLIB_NAMESPACE_END
92 
93 #endif
94 #endif // INCLUDE_NN_NLIB_ZLIBOUTPUTSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
errno_t Init() noexcept
Initializes using the default settings.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#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
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
void * buf_out
The buffer set for z_stream::next_out.
void * buf_in
The buffer set for z_stream::next_in.
The class for holding the OutputTransform derived class.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
size_t buffer_size
The size of ZlibOutputStreamSettings::buf_in and ZlibOutputStreamSettings::buf_out.
ReallocFunc realloc_func
The realloc function or its equivalent that ZlibOutputStream uses internally.
constexpr ZlibOutputStreamSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets the default values...
constexpr ZlibOutputStreamSettings(void *buf_in_, void *buf_out_, size_t buffer_size_, int level_) noexcept
Sets the individual data members.
NLIB_CHECK_RESULT void * nlib_realloc(void *ptr, size_t size)
A weak function that calls the C standard function realloc. nlib calls realloc via this function...
The structure that stores the various ZlibOutputStream settings. At construction time, the default settings are stored.
The class that uses zlib to decompress data and write the data to a stream.
int level
The compression setting to pass to the deflateInit2 function of zlib.
errno_t Init(const ZlibOutputStreamSettings &settings) noexcept
Configures the buffer and the other settings to use for streaming according to the values specified i...
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
constexpr ZlibOutputStreamSettings(void *buf_in_, void *buf_out_, size_t buffer_size_, int level_, ReallocFunc realloc_func_) noexcept
Sets the individual data members.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24