nlib
InputConverterStream.h
Go to the documentation of this file.
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 NULL, 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 
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
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:228
The base class for classes that act internally like InputStream to convert data.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:163
Defines the base class for input streams.
The class for holding the InputTransform derived class.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
InputStream * GetStream() const noexcept
Gets the input stream for reading the pre-converted data.
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:29
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37