nlib
InputConverterStream.h
[詳解]
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
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Platform.h:2151
内部でデータ変換を行う InputStream のように振る舞うクラスの基底です。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:126
入力ストリームの基底クラスを定義しています。
InputTransformの派生クラスを保持するためのクラスです。
#define NLIB_OVERRIDE
利用可能であればoverrideが定義されます。そうでない場合は空文字列です。
void SetError(errno_t e) const noexcept
InputStreamにエラーを設定します。
Definition: InputStream.h:86
入力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: InputStream.h:15
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
InputStream * GetStream() const noexcept
変換前のデータを読み込む入力ストリームを取得します。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24