nlib
BinaryReader.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_BINARYREADER_H_
17 #define INCLUDE_NN_NLIB_BINARYREADER_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 #include "nn/nlib/InputStream.h"
22 
23 NLIB_NAMESPACE_BEGIN
24 NLIB_EXPIMP_TEMPLATE(detail::MiniBufIn<512>);
25 
27  public:
29  kEndianDefault = 0,
32  DEFAULT = kEndianDefault,
33  ENDIAN_LITTLE = kEndianLittle,
34  ENDIAN_BIG = kEndianBig
35  };
36 
37  public:
38  BinaryReader() NLIB_NOEXCEPT : stream_(NULL),
39  errno_(-1),
40  swap_endian_(false) {}
41  ~BinaryReader() NLIB_NOEXCEPT { this->Close(); }
42  errno_t Init(EndianSetting endian) NLIB_NOEXCEPT;
43  errno_t Init() NLIB_NOEXCEPT { return Init(kEndianDefault); }
44  errno_t Open(InputStream* stream) NLIB_NOEXCEPT;
45 
46 #define NLIB_BINR(tp) return this->Read_(reinterpret_cast<tp*>(x))
47 
48  bool Read(char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
49  bool Read(signed char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
50  bool Read(unsigned char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
51  bool Read(short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); } // NOLINT
52  bool Read(unsigned short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); } // NOLINT
53  bool Read(int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
54  bool Read(unsigned int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
55  bool Read(long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); } // NOLINT
56  bool Read(unsigned long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); } // NOLINT
57  bool Read(long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); } // NOLINT
58  bool Read(unsigned long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); } // NOLINT
59  bool Read(float* x) NLIB_NOEXCEPT { NLIB_BINR(float); } // NOLINT
60  bool Read(double* x) NLIB_NOEXCEPT { NLIB_BINR(double); } // NOLINT
61 
62  template <class T>
63  bool Read(T* x) NLIB_NOEXCEPT NLIB_NONNULL;
64 
65  int Peek() NLIB_NOEXCEPT {
66  return mini_buf_.Peek(stream_);
67  }
68  bool Skip(size_t n) NLIB_NOEXCEPT {
69  size_t rem = mini_buf_.size();
70  if (rem <= n) {
71  mini_buf_.Advance(n);
72  return true;
73  } else {
74  mini_buf_.Advance(rem);
75  n -= rem;
76  return this->Skip_(n);
77  }
78  }
79 #undef NLIB_BINR
80 
81  size_t ReadArray(unsigned char* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
82  size_t ReadArray(unsigned short* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
83  size_t ReadArray(unsigned int* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
84  size_t ReadArray(unsigned long long* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
85  size_t ReadArray(unsigned long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
86 #ifdef NLIB_LP64
87  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
88 #else
89  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
90 #endif
91  }
92 
93  size_t ReadArray(float* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
94  size_t ReadArray(double* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
95 
96  size_t ReadArray(signed char* x, size_t n) NLIB_NOEXCEPT {
97  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
98  }
99  size_t ReadArray(char* x, size_t n) NLIB_NOEXCEPT {
100  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
101  }
102  size_t ReadArray(short* x, size_t n) NLIB_NOEXCEPT { // NOLINT
103  return this->ReadArray(reinterpret_cast<unsigned short*>(x), n); // NOLINT
104  }
105  size_t ReadArray(int* x, size_t n) NLIB_NOEXCEPT {
106  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
107  }
108  size_t ReadArray(long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
109 #ifdef NLIB_LP64
110  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
111 #else
112  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
113 #endif
114  }
115  size_t ReadArray(long long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
116  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
117  }
118 
119  template <class T>
120  size_t ReadArray(T* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
121  template <class T, size_t N>
122  size_t ReadArray(T (&a)[N]) NLIB_NOEXCEPT;
123  bool Close() NLIB_NOEXCEPT {
124  // Do not stream_->Close(). only detach it from BinaryReader.
125  stream_ = NULL;
126  return true;
127  }
128  void SetError(errno_t e) NLIB_NOEXCEPT {
129  if (errno_ == 0) errno_ = e;
130  }
131  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
132 
133  NLIB_SAFE_BOOL(BinaryReader, GetErrorValue() == 0)
134 
135  private:
136  bool Skip_(size_t n) NLIB_NOEXCEPT;
137  bool Read_(uint8_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
138  bool Read_(uint16_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
139  bool Read_(uint32_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
140  bool Read_(uint64_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
141  bool Read_(float* x) NLIB_NOEXCEPT {
142  union {
143  float f;
144  uint32_t i;
145  } v;
146  if (!this->Read_(&v.i)) return false;
147  *x = v.f;
148  return true;
149  }
150  bool Read_(double* x) NLIB_NOEXCEPT {
151  union {
152  double f;
153  uint64_t i;
154  } v;
155  if (!this->Read_(&v.i)) return false;
156  *x = v.f;
157  return true;
158  }
159 
160  private:
161  InputStream* stream_;
162  ErrnoT errno_;
163  bool swap_endian_;
164  detail::MiniBufIn<512> mini_buf_;
165 
167 };
168 
169 namespace binary_reader {
170 
171 template<class T>
172 NLIB_ALWAYS_INLINE bool Read(BinaryReader* r, T* x) { return r->Read(x); }
173 
174 template <class T>
175 NLIB_ALWAYS_INLINE bool ReadArray(BinaryReader* r, T* x, size_t n) {
176  return r->ReadArray(x, n);
177 }
178 }
179 
180 template <class T>
182  return binary_reader::Read(this, x);
183 }
184 
185 template <class T>
187  return binary_reader::ReadArray(this, x, n);
188 }
189 
190 template <class T, size_t N>
192  return this->ReadArray(&a[0], N);
193 }
194 
195 NLIB_NAMESPACE_END
196 
197 #endif // INCLUDE_NN_NLIB_BINARYREADER_H_
bool Read(unsigned long long *x) noexcept
ストリームからバイナリでunsigned long long型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:58
bool Skip(size_t n) noexcept
ストリームからn バイトを読み飛ばします。
Definition: BinaryReader.h:68
size_t ReadArray(unsigned char *x, size_t n) noexcept
ストリームからバイナリでunsigned char型のデータ列を読み取ります。
bool Read(unsigned char *x) noexcept
ストリームからバイナリでunsigned char型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:50
#define NLIB_ALWAYS_INLINE
コンパイラに関数をインライン展開するように強く示します。
Definition: Platform_unix.h:97
errno_t GetErrorValue() const noexcept
書き込み等が失敗した際に、エラーの原因を取得できます。
Definition: BinaryReader.h:131
bool Read(signed char *x) noexcept
ストリームからバイナリでsigned char型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:49
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:163
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:178
入力ストリームの基底クラスを定義しています。
size_t ReadArray(char *x, size_t n) noexcept
ストリームからバイナリでchar型のデータ列を読み取ります。
Definition: BinaryReader.h:99
void SetError(errno_t e) noexcept
エラーを設定します。既にエラーが設定されている場合にはエラーは設定されません。
Definition: BinaryReader.h:128
bool Read(int *x) noexcept
ストリームからバイナリでint型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:53
bool Read(double *x) noexcept
ストリームからバイナリでdouble型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:60
uint32_t nlib_ulong_compatible_t
unsigned longと互換性のある整数型がtypedefされています。
Definition: Platform.h:322
リトルエンディアンで書き込みます。
Definition: BinaryReader.h:30
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
BinaryReader() noexcept
デフォルトコンストラクタです。
Definition: BinaryReader.h:38
bool Read(short *x) noexcept
ストリームからバイナリでshort型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:51
size_t ReadArray(unsigned long *x, size_t n) noexcept
ストリームからバイナリでunsigned long型のデータ列を読み取ります。
Definition: BinaryReader.h:85
size_t ReadArray(int *x, size_t n) noexcept
ストリームからバイナリでint型のデータ列を読み取ります。
Definition: BinaryReader.h:105
入力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: InputStream.h:29
errno_tをラップするクラスです。Visual Studioのデバッガ上での表示を改善します。
Definition: Config.h:492
bool Read(unsigned int *x) noexcept
ストリームからバイナリでunsigned int型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:54
bool Read(float *x) noexcept
ストリームからバイナリでfloat型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:59
size_t ReadArray(long long *x, size_t n) noexcept
ストリームからバイナリでlong long型のデータ列を読み取ります。
Definition: BinaryReader.h:115
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
開発環境別の設定が書かれるファイルです。
bool Read(unsigned short *x) noexcept
ストリームからバイナリでunsigned short型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:52
size_t ReadArray(short *x, size_t n) noexcept
ストリームからバイナリでshort型のデータ列を読み取ります。
Definition: BinaryReader.h:102
EndianSetting
エンディアンを指定します。
Definition: BinaryReader.h:28
bool Close() noexcept
バイナリリーダをクローズします。
Definition: BinaryReader.h:123
size_t ReadArray(signed char *x, size_t n) noexcept
ストリームからバイナリでsigned char型のデータ列を読み取ります。
Definition: BinaryReader.h:96
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
int Peek() noexcept
ストリームの先頭1バイトを参照します。
Definition: BinaryReader.h:65
bool Read(BinaryReader *r, T *x)
この関数テンプレートを特殊化することで、ユーザー定義クラスに読み込むことができます。 ...
Definition: BinaryReader.h:172
ストリーム(InputStream)からバイナリを読み込むクラスです。
Definition: BinaryReader.h:26
bool Read(unsigned long *x) noexcept
ストリームからバイナリでunsigned long型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:56
errno_t Init() noexcept
プログラムが動作しているマシンと同じエンディアンでバイナリリーダーを初期化します。 ...
Definition: BinaryReader.h:43
bool Read(long *x) noexcept
ストリームからバイナリでlong型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:55
ビッグエンディアンで書き込みます。
Definition: BinaryReader.h:31
#define NLIB_NONNULL
全ての引数にNULLを指定することができないことを示します。
~BinaryReader() noexcept
デストラクタです。ストリームをクローズしません。
Definition: BinaryReader.h:41
size_t ReadArray(long *x, size_t n) noexcept
ストリームからバイナリでlong型のデータ列を読み取ります。
Definition: BinaryReader.h:108
bool Read(long long *x) noexcept
ストリームからバイナリでlong long型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:57
bool Read(char *x) noexcept
ストリームからバイナリでchar型のデータを読み取ります。成功した場合はtrueを返します。 ...
Definition: BinaryReader.h:48
bool ReadArray(BinaryReader *r, T *x, size_t n)
この関数テンプレートを特殊化することで、ユーザー定義クラスに読み込むことができます。 ...
Definition: BinaryReader.h:175
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37