nlib
InputConverterStream.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_INPUTCONVERTERSTREAM_H_
17 #define INCLUDE_NN_NLIB_INPUTCONVERTERSTREAM_H_
18 
19 #include "nn/nlib/InputStream.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 
23 class NLIB_VIS_PUBLIC InputTransform {
24  // define a derived class and use it from InputConverterStream
25  public:
26  virtual ~InputTransform() {}
27 
28  // is, p, n are not nullptr, and size > 0 when called.
29  // p can be the buffer memory the user specified.
30  // The data size to write can be less than 'size'.
31  // *n becomes the size of written data.
32  //
33  // return 0 if it is all ok. otherwise, an error has occured.
34  // *n == 0 if and only if EOF or error.
35  virtual errno_t Transform(InputStream* is, void* p, size_t size, size_t* n) = 0;
36 
37  // called only once at first, and pass the workbuffer which OutputConverterStream uses.
38  // The derived class has to manage the memory.
39  virtual void* GetWorkBuffer(size_t* n) = 0;
40 
41  // called when InputConverterStream::SetStream() is called.
42  virtual errno_t OnSetStream(InputStream* is) = 0;
43 
44  // called when the stream is closing.
45  virtual errno_t OnClose() = 0;
46 };
47 
49  public:
51  : stream_(nullptr), cvtr_(nullptr), ics_bufsize_(0), ics_buf_(nullptr) {}
52  errno_t SetStream(InputStream* istr) NLIB_NOEXCEPT;
53  InputStream* GetStream() const NLIB_NOEXCEPT { return stream_; }
54 
55  // NOTE:
56  // The destructor does not close its stream_, nor call InputTransform::OnClose().
57  // it is because OutputConverterStream does not own stream_ nor cvtr_.
58  virtual ~InputConverterStream() NLIB_NOEXCEPT NLIB_OVERRIDE {}
59 
60  protected:
61  // Note that SetTransform returns ENOMEM when cvtr->GetWorkBuffer() fails
62  errno_t SetTransform(InputTransform* cvtr) NLIB_NOEXCEPT;
63 
64  private:
65  virtual size_t FillBuffer_(void* p, size_t nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
66  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
67  virtual void* GetWorkBuffer_(size_t* nbytes) NLIB_NOEXCEPT NLIB_OVERRIDE NLIB_FINAL;
68 
69  private:
70  InputStream* stream_;
71  InputTransform* cvtr_;
72  size_t ics_bufsize_;
73  unsigned char* ics_buf_;
74 
76 };
77 
78 template <class TRANSFORM>
80  public:
81  typedef TRANSFORM TransformType;
82 
83  InputConverterStreamTempl() NLIB_NOEXCEPT { this->SetTransform(&m_Transform); }
84  virtual ~InputConverterStreamTempl() NLIB_NOEXCEPT NLIB_OVERRIDE { m_Transform.OnClose(); }
85 
86  private:
87  void SetTransform(InputTransform* cvtr) NLIB_NOEXCEPT {
88  // to hide SetTransform()
89  errno_t e = InputConverterStream::SetTransform(cvtr);
90  if (e != 0) this->SetError(e);
91  }
92 
93  protected:
94  TRANSFORM m_Transform;
95 
96  private:
98 };
99 
100 NLIB_NAMESPACE_END
101 
102 #endif // INCLUDE_NN_NLIB_INPUTCONVERTERSTREAM_H_
#define NLIB_OVERRIDE
利用可能であればoverrideが定義されます。そうでない場合は空文字列です。
Definition: Config.h:244
内部でデータ変換を行う InputStream のように振る舞うクラスの基底です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
入力ストリームの基底クラスを定義しています。
InputTransformの派生クラスを保持するためのクラスです。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
InputStream * GetStream() const noexcept
変換前のデータを読み込む入力ストリームを取得します。
入力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: InputStream.h:29
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:107
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37