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