nlib
OutputConverterStream.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
17 #define INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
18 
19 #include "nn/nlib/OutputStream.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 
23 class NLIB_VIS_PUBLIC OutputTransform {
24  // define a derived class and use it from OutputConverterStream
25  public:
26  virtual ~OutputTransform() {}
27  virtual errno_t Transform(OutputStream* os, const void* p, size_t nBytes, bool doFlush) = 0;
28 
29  // called only once at first, and pass the workbuffer which OutputConverterStream uses.
30  // The derived class has to manage the memory.
31  virtual void* GetWorkBuffer(size_t* n) = 0;
32 
33  // called when OutputConverterStream::SetStream() is called.
34  virtual errno_t OnSetStream(OutputStream* os) = 0;
35 
36  // called when the stream is closing.
37  virtual errno_t OnClose(OutputStream* os) = 0;
38 };
39 
41  public:
43 
44  errno_t SetStream(OutputStream* ostr) NLIB_NOEXCEPT;
45  OutputStream* GetStream() const NLIB_NOEXCEPT { return stream_; }
46 
47  // NOTE:
48  // The destructor does not close its stream_, nor call OutputTransform::OnClose().
49  // it is because OutputConverterStream does not own stream_ nor cvtr_.
50  virtual ~OutputConverterStream() NLIB_NOEXCEPT NLIB_OVERRIDE {}
51 
52  protected:
53  // Note that SetTransform returns ENOMEM when cvtr->GetWorkBuffer() fails
54  errno_t SetTransform(OutputTransform* cvtr) NLIB_NOEXCEPT;
55 
56  private:
57  virtual bool PushBuffer_(const void* p, size_t nbytes,
58  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
59  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
60  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
61 
62  private:
63  OutputStream* stream_;
64  OutputTransform* cvtr_;
65  size_t ocs_bufsize_;
66  unsigned char* ocs_buf_;
67 
69 };
70 
71 template <class TRANSFORM>
73  public:
74  typedef TRANSFORM TransformType;
75 
77  this->SetTransform(&m_Transform);
78  }
80  // note that OutputConverterStream::Close_() is final
81  bool result = this->Close();
82  NLIB_UNUSED(result);
83  }
84 
85  private:
86  void SetTransform(OutputTransform* cvtr) NLIB_NOEXCEPT {
87  // to hide SetTransform()
88  errno_t e = OutputConverterStream::SetTransform(cvtr);
89  if (e != 0) this->SetError(e);
90  }
91 
92  protected:
93  TRANSFORM m_Transform;
94 
95  private:
97 };
98 
99 NLIB_NAMESPACE_END
100 
101 #endif // INCLUDE_NN_NLIB_OUTPUTCONVERTERSTREAM_H_
#define NLIB_OVERRIDE
利用可能であればoverrideが定義されます。そうでない場合は空文字列です。
Definition: Config.h:228
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:163
OutputStream * GetStream() const noexcept
ベースとなる入力ストリームを取得します。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
OutputTransformの派生クラスを保持するためのクラスです。
内部でデータ変換を行うOutputStreamのように振る舞うクラスの基底です。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
出力ストリームの基底クラスを定義しています。
出力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: OutputStream.h:30
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37