nlib
OutputConverterStream.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_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  : stream_(nullptr), cvtr_(nullptr), ocs_bufsize_(0), ocs_buf_(nullptr) {}
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
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:244
#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:179
OutputStream * GetStream() const noexcept
Gets the input stream that will be the base stream.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
The class for holding the OutputTransform derived class.
The base class for classes that act internally like OutputStream to convert data. ...
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
Defines the base class for output streams.
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:30
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37