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  errno_t SetStream(InputStream* istr) NLIB_NOEXCEPT;
40  InputStream* GetStream() const NLIB_NOEXCEPT { return stream_; }
41 
42  // NOTE:
43  // The destructor does not close its stream_, nor call InputTransform::OnClose().
44  // it is because OutputConverterStream does not own stream_ nor cvtr_.
45  virtual ~InputConverterStream() NLIB_NOEXCEPT NLIB_OVERRIDE {}
46 
47  protected:
48  // Note that SetTransform returns ENOMEM when cvtr->GetWorkBuffer() fails
49  errno_t SetTransform(InputTransform* cvtr) NLIB_NOEXCEPT;
50 
51  private:
52  virtual size_t FillBuffer_(void* p, size_t nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
53  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
54  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
55 
56  private:
57  InputStream* stream_;
58  InputTransform* cvtr_;
59  size_t ics_bufsize_;
60  unsigned char* ics_buf_;
61 
63 };
64 
65 template <class TRANSFORM>
67  public:
68  typedef TRANSFORM TransformType;
69 
70  InputConverterStreamTempl() NLIB_NOEXCEPT { this->SetTransform(&m_Transform); }
71  virtual ~InputConverterStreamTempl() NLIB_NOEXCEPT NLIB_OVERRIDE { m_Transform.OnClose(); }
72 
73  private:
74  void SetTransform(InputTransform* cvtr) NLIB_NOEXCEPT {
75  // to hide SetTransform()
76  errno_t e = InputConverterStream::SetTransform(cvtr);
77  if (e != 0) this->SetError(e);
78  }
79 
80  protected:
81  TRANSFORM m_Transform;
82 
83  private:
85 };
86 
87 NLIB_NAMESPACE_END
88 
89 #endif // INCLUDE_NN_NLIB_INPUTCONVERTERSTREAM_H_
#define NLIB_OVERRIDE
利用可能であればoverrideが定義されます。そうでない場合は空文字列です。
Definition: Config.h:210
内部でデータ変換を行う InputStream のように振る舞うクラスの基底です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:145
入力ストリームの基底クラスを定義しています。
InputTransformの派生クラスを保持するためのクラスです。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:61
InputStream * GetStream() const noexcept
変換前のデータを読み込む入力ストリームを取得します。
入力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: InputStream.h:16
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:86
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:211
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24