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  // Note that SetTransform returns ENOMEM when cvtr->GetWorkBuffer() fails
32  errno_t SetTransform(OutputTransform* cvtr) NLIB_NOEXCEPT;
33  errno_t SetStream(OutputStream* ostr) NLIB_NOEXCEPT;
34  OutputStream* GetStream() const NLIB_NOEXCEPT { return m_Ostr; }
35 
36  // NOTE:
37  // The destructor does not close its m_Ostr, nor call OutputTransform::OnClose().
38  // it is because OutputConverterStream does not own m_Ostr nor m_Cvtr.
39  virtual ~OutputConverterStream() NLIB_NOEXCEPT NLIB_OVERRIDE {}
40 
41  private:
42  virtual bool PushBuffer_(const void* p, size_t nBytes,
43  bool doFlush) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
44  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
45  virtual void* GetWorkBuffer_(size_t* nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
46 
47  private:
48  OutputStream* m_Ostr;
49  OutputTransform* m_Cvtr;
50  size_t m_BufSize;
51  unsigned char* m_Buf;
52 
53  NLIB_DISALLOW_COPY_AND_ASSIGN(OutputConverterStream);
54 };
55 
56 template <class TRANSFORM>
57 class OutputConverterStreamTempl : public OutputConverterStream {
58  public:
59  typedef TRANSFORM TransformType;
60 
61  OutputConverterStreamTempl() NLIB_NOEXCEPT {
62  this->SetTransform(&m_Transform);
63  }
64  virtual ~OutputConverterStreamTempl() NLIB_NOEXCEPT NLIB_OVERRIDE {
65  // note that OutputConverterStream::Close_() is final
66  bool result = this->Close();
67  NLIB_UNUSED(result);
68  }
69 
70  private:
71  void SetTransform(OutputTransform* cvtr) NLIB_NOEXCEPT {
72  // to hide SetTransform()
73  errno_t e = OutputConverterStream::SetTransform(cvtr);
74  if (e != 0) this->SetError(e);
75  }
76 
77  protected:
78  TRANSFORM m_Transform;
79 
80  private:
82 };
83 
84 NLIB_NAMESPACE_END
85 
86 #endif // INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#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.
bool Close() noexcept
Closes the stream after it has been flushed. Returns true if successful.
The class for holding the OutputTransform derived class.
The base class for classes that act internally like OutputStream to convert data. ...
OutputStream * GetStream() const noexcept
Gets the input stream that will be the base stream.
void SetError(errno_t e) const noexcept
Sets an error to OutputStream.
Definition: OutputStream.h:99
#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
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24