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