nlib
OutputConverterStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
4 #define INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
5 
6 #include "nn/nlib/OutputStream.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
10 class NLIB_VIS_PUBLIC OutputTransform {
11  // define a derived class and use it from OutputConverterStream
12  public:
13  virtual ~OutputTransform() {}
14  virtual errno_t Transform(OutputStream* os, const void* p, size_t nBytes, bool doFlush) = 0;
15 
16  // called only once at first, and pass the workbuffer which OutputConverterStream uses.
17  // The derived class has to manage the memory.
18  virtual void* GetWorkBuffer(size_t* n) = 0;
19 
20  // called when OutputConverterStream::SetStream() is called.
21  virtual errno_t OnSetStream(OutputStream* os) = 0;
22 
23  // called when the stream is closing.
24  virtual errno_t OnClose(OutputStream* os) = 0;
25 };
26 
28  public:
30 
31  errno_t SetStream(OutputStream* ostr) NLIB_NOEXCEPT;
32  OutputStream* GetStream() const NLIB_NOEXCEPT { return stream_; }
33 
34  // NOTE:
35  // The destructor does not close its stream_, nor call OutputTransform::OnClose().
36  // it is because OutputConverterStream does not own stream_ nor cvtr_.
37  virtual ~OutputConverterStream() NLIB_NOEXCEPT NLIB_OVERRIDE {}
38 
39  protected:
40  // Note that SetTransform returns ENOMEM when cvtr->GetWorkBuffer() fails
41  errno_t SetTransform(OutputTransform* cvtr) NLIB_NOEXCEPT;
42 
43  private:
44  virtual bool PushBuffer_(const void* p, size_t nbytes,
45  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
46  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
47  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
48 
49  private:
50  OutputStream* stream_;
51  OutputTransform* cvtr_;
52  size_t ocs_bufsize_;
53  unsigned char* ocs_buf_;
54 
56 };
57 
58 template <class TRANSFORM>
60  public:
61  typedef TRANSFORM TransformType;
62 
64  this->SetTransform(&m_Transform);
65  }
67  // note that OutputConverterStream::Close_() is final
68  bool result = this->Close();
69  NLIB_UNUSED(result);
70  }
71 
72  private:
73  void SetTransform(OutputTransform* cvtr) NLIB_NOEXCEPT {
74  // to hide SetTransform()
75  errno_t e = OutputConverterStream::SetTransform(cvtr);
76  if (e != 0) this->SetError(e);
77  }
78 
79  protected:
80  TRANSFORM m_Transform;
81 
82  private:
84 };
85 
86 NLIB_NAMESPACE_END
87 
88 #endif // INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:210
#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
OutputStream * GetStream() const noexcept
Gets the input stream that will be the base stream.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
The class for holding the OutputTransform derived class.
The base class for classes that act internally like OutputStream to convert data. ...
#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.
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