nlib
InputConverterStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_INPUTCONVERTERSTREAM_H_
4 #define INCLUDE_NN_NLIB_INPUTCONVERTERSTREAM_H_
5 
6 #include "nn/nlib/InputStream.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
10 class NLIB_VIS_PUBLIC InputTransform {
11  // define a derived class and use it from InputConverterStream
12  public:
13  virtual ~InputTransform() {}
14 
15  // is, p, n are not NULL, and size > 0 when called.
16  // p can be the buffer memory the user specified.
17  // The data size to write can be less than 'size'.
18  // *n becomes the size of written data.
19  //
20  // return 0 if it is all ok. otherwise, an error has occured.
21  // *n == 0 if and only if EOF or error.
22  virtual errno_t Transform(InputStream* is, void* p, size_t size, size_t* n) = 0;
23 
24  // called only once at first, and pass the workbuffer which OutputConverterStream uses.
25  // The derived class has to manage the memory.
26  virtual void* GetWorkBuffer(size_t* n) = 0;
27 
28  // called when InputConverterStream::SetStream() is called.
29  virtual errno_t OnSetStream(InputStream* is) = 0;
30 
31  // called when the stream is closing.
32  virtual errno_t OnClose() = 0;
33 };
34 
36  public:
38 
39  // Note that SetTransform returns ENOMEM when cvtr->GetWorkBuffer() fails
40  errno_t SetTransform(InputTransform* cvtr) NLIB_NOEXCEPT;
41  errno_t SetStream(InputStream* istr) NLIB_NOEXCEPT;
42  InputStream* GetStream() const NLIB_NOEXCEPT { return m_Istr; }
43 
44  // NOTE:
45  // The destructor does not close its m_Istr, nor call InputTransform::OnClose().
46  // it is because OutputConverterStream does not own m_Istr nor m_Cvtr.
47  virtual ~InputConverterStream() NLIB_NOEXCEPT NLIB_OVERRIDE {}
48 
49  private:
50  virtual size_t FillBuffer_(void* p, size_t nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
51  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
52  virtual void* GetWorkBuffer_(size_t* nBytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
53 
54  private:
55  InputStream* m_Istr;
56  InputTransform* m_Cvtr;
57  size_t m_BufSize;
58  unsigned char* m_Buf;
59 
60  NLIB_DISALLOW_COPY_AND_ASSIGN(InputConverterStream);
61 };
62 
63 template <class TRANSFORM>
64 class InputConverterStreamTempl : public InputConverterStream {
65  public:
66  typedef TRANSFORM TransformType;
67 
68  InputConverterStreamTempl() NLIB_NOEXCEPT { this->SetTransform(&m_Transform); }
69  virtual ~InputConverterStreamTempl() NLIB_NOEXCEPT NLIB_OVERRIDE { m_Transform.OnClose(); }
70 
71  private:
72  void SetTransform(InputTransform* cvtr) NLIB_NOEXCEPT {
73  // to hide SetTransform()
74  errno_t e = InputConverterStream::SetTransform(cvtr);
75  if (e != 0) this->SetError(e);
76  }
77 
78  protected:
79  TRANSFORM m_Transform;
80 
81  private:
83 };
84 
85 NLIB_NAMESPACE_END
86 
87 #endif // INCLUDE_NN_NLIB_INPUTCONVERTERSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
The base class for classes that act internally like InputStream to convert data.
#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
Defines the base class for input streams.
The class for holding the InputTransform derived class.
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
void SetError(errno_t e) const noexcept
Sets an error to InputStream.
Definition: InputStream.h:86
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:15
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
InputStream * GetStream() const noexcept
Gets the input stream for reading the pre-converted data.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24