nlib
BinaryReader.h
Go to the documentation of this file.
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 
9 NLIB_NAMESPACE_BEGIN
10 
11 class InputStream;
12 
14  public:
15  enum EndianSetting { DEFAULT = 0, ENDIAN_LITTLE, ENDIAN_BIG };
16 
17  public:
18  BinaryReader() NLIB_NOEXCEPT : m_Cur(NULL),
19  m_BufEnd(NULL),
20  m_Stream(NULL),
21  m_ErrorValue(0),
22  m_SwapEndian(0) {}
23  ~BinaryReader() NLIB_NOEXCEPT { this->Close(); }
24  bool Init(InputStream* stream, EndianSetting endian = DEFAULT) NLIB_NOEXCEPT NLIB_NONNULL;
25 
26 #define NLIB_BINR(tp) \
27  if (NLIB_LIKELY(x)) return this->Read_(reinterpret_cast<tp*>(x)); \
28  NLIB_STATIC_ASSERT(sizeof(*x) == sizeof(tp)); \
29  SetError(EINVAL); \
30  return false
31 
32  bool Read(char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
33  bool Read(signed char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
34  bool Read(unsigned char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
35  bool Read(short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); } // NOLINT
36  bool Read(unsigned short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); } // NOLINT
37  bool Read(int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
38  bool Read(unsigned int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
39  bool Read(long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); } // NOLINT
40  bool Read(unsigned long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); } // NOLINT
41  bool Read(long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); } // NOLINT
42  bool Read(unsigned long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); } // NOLINT
43  bool Read(float* x) NLIB_NOEXCEPT { NLIB_BINR(float); } // NOLINT
44  bool Read(double* x) NLIB_NOEXCEPT { NLIB_BINR(double); } // NOLINT
45 
46  template <class T>
48 
50  if (NLIB_UNLIKELY(m_Cur == m_BufEnd)) {
51  this->FillBuffer();
52  if (m_Cur == m_BufEnd) return -1;
53  }
54  return *m_Cur;
55  }
56  bool Skip(size_t n) NLIB_NOEXCEPT {
57  if (m_Cur + n <= m_BufEnd) {
58  m_Cur += n;
59  return true;
60  }
61  return this->Skip_(n);
62  }
63 #undef NLIB_BINR
64 
65  size_t ReadArray(unsigned char* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
66  size_t ReadArray(unsigned short* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
67  size_t ReadArray(unsigned int* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
68  size_t ReadArray(unsigned long long* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
69  size_t ReadArray(unsigned long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
70 #ifdef NLIB_LP64
71  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
72 #else
73  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
74 #endif
75  }
76 
77  size_t ReadArray(float* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
78  size_t ReadArray(double* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
79 
80  size_t ReadArray(signed char* x, size_t n) NLIB_NOEXCEPT {
81  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
82  }
83  size_t ReadArray(char* x, size_t n) NLIB_NOEXCEPT {
84  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
85  }
86  size_t ReadArray(short* x, size_t n) NLIB_NOEXCEPT { // NOLINT
87  return this->ReadArray(reinterpret_cast<unsigned short*>(x), n); // NOLINT
88  }
89  size_t ReadArray(int* x, size_t n) NLIB_NOEXCEPT {
90  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
91  }
92  size_t ReadArray(long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
93 #ifdef NLIB_LP64
94  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
95 #else
96  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
97 #endif
98  }
99  size_t ReadArray(long long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
100  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
101  }
102 
103  template <class T>
104  NLIB_VIS_HIDDEN size_t ReadArray(T* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
105  template <class T, size_t N>
106  NLIB_VIS_HIDDEN size_t ReadArray(T(&a)[N]) NLIB_NOEXCEPT;
107  bool Close() NLIB_NOEXCEPT {
108  // Do not m_Stream->Close(). only detach it from BinaryReader.
109  m_Stream = NULL;
110  return true;
111  }
112  void SetError(errno_t e) NLIB_NOEXCEPT {
113  if (m_ErrorValue == 0) m_ErrorValue = e;
114  }
115  errno_t GetErrorValue() const NLIB_NOEXCEPT { return m_ErrorValue; }
116 
117  NLIB_SAFE_BOOL(BinaryReader, GetErrorValue() == 0)
118 
119  private:
120  bool Skip_(size_t n) NLIB_NOEXCEPT;
121  bool Read_(uint8_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
122  bool Read_(uint16_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
123  bool Read_(uint32_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
124  bool Read_(uint64_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
125  bool Read_(float* x) NLIB_NOEXCEPT {
126  union {
127  float f;
128  uint32_t i;
129  } v;
130  if (!this->Read_(&v.i)) return false;
131  *x = v.f;
132  return true;
133  }
134  bool Read_(double* x) NLIB_NOEXCEPT {
135  union {
136  double f;
137  uint64_t i;
138  } v;
139  if (!this->Read_(&v.i)) return false;
140  *x = v.f;
141  return true;
142  }
143  void FillBuffer() NLIB_NOEXCEPT;
144 
145  private:
146  unsigned char m_Buf[512];
147  unsigned char* m_Cur;
148  unsigned char* m_BufEnd;
149  InputStream* m_Stream;
150  errno_t m_ErrorValue;
151  unsigned char m_SwapEndian;
152  NLIB_DISALLOW_COPY_AND_ASSIGN(BinaryReader);
153 };
154 
155 namespace binary_reader {
156 
157 template<class T>
158 NLIB_ALWAYS_INLINE bool Read(BinaryReader* r, T* x) { return r->Read(x); }
159 
160 template <class T>
161 NLIB_ALWAYS_INLINE bool ReadArray(BinaryReader* r, T* x, size_t n) {
162  return r->ReadArray(x, n);
163 }
164 }
165 
166 template <class T>
167 NLIB_ALWAYS_INLINE bool BinaryReader::Read(T* x) NLIB_NOEXCEPT {
168  return binary_reader::Read(this, x);
169 }
170 
171 template <class T>
172 NLIB_ALWAYS_INLINE size_t BinaryReader::ReadArray(T* x, size_t n) NLIB_NOEXCEPT {
173  return binary_reader::ReadArray(this, x, n);
174 }
175 
176 template <class T, size_t N>
177 NLIB_ALWAYS_INLINE size_t BinaryReader::ReadArray(T (&a)[N]) NLIB_NOEXCEPT {
178  return this->ReadArray(&a[0], N);
179 }
180 
181 NLIB_NAMESPACE_END
182 
183 #endif // INCLUDE_NN_NLIB_BINARYREADER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
bool Read(unsigned long long *x) noexcept
Reads unsigned long long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:42
bool Skip(size_t n) noexcept
Skips over n bytes when reading from stream.
Definition: BinaryReader.h:56
size_t ReadArray(unsigned char *x, size_t n) noexcept
Reads an unsigned char-type data string as binary from a stream.
bool Read(unsigned char *x) noexcept
Reads unsigned char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:34
#define NLIB_UNLIKELY(x)
Indicates to the compiler that condition x is likely to be false.
Definition: Platform_unix.h:62
bool Read(signed char *x) noexcept
Reads signed char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:33
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
#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:126
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:141
size_t ReadArray(char *x, size_t n) noexcept
Reads a char-type data string as binary from a stream.
Definition: BinaryReader.h:83
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
void SetError(errno_t e) noexcept
Sets an error. If an error has been set already, this function does not set an error.
Definition: BinaryReader.h:112
bool Read(int *x) noexcept
Reads int-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:37
bool Read(double *x) noexcept
Reads double-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:44
uint32_t nlib_ulong_compatible_t
Defines an integer type that is compatible with unsigned long using typedef.
Definition: Platform.h:425
BinaryReader() noexcept
Instantiates the object with default parameters (default constructor).
Definition: BinaryReader.h:18
bool Read(short *x) noexcept
Reads short-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:35
size_t ReadArray(unsigned long *x, size_t n) noexcept
Reads an unsigned long-type data string as binary from a stream.
Definition: BinaryReader.h:69
size_t ReadArray(int *x, size_t n) noexcept
Reads an int-type data string as binary from a stream.
Definition: BinaryReader.h:89
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:15
bool Read(unsigned int *x) noexcept
Reads unsigned int-type data as binary from a stream. Returns true if successful. ...
Definition: BinaryReader.h:38
bool Read(float *x) noexcept
Reads float-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:43
size_t ReadArray(long long *x, size_t n) noexcept
Reads a long long-type data string as binary from a stream.
Definition: BinaryReader.h:99
A file that contains the configuration information for each development environment.
bool Read(unsigned short *x) noexcept
Reads unsigned short-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:36
size_t ReadArray(short *x, size_t n) noexcept
Reads a short-type data string as binary from a stream.
Definition: BinaryReader.h:86
EndianSetting
Specifies the endianness.
Definition: BinaryReader.h:15
bool Close() noexcept
Closes the binary reader.
Definition: BinaryReader.h:107
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
Definition: BinaryReader.h:115
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:59
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
size_t ReadArray(signed char *x, size_t n) noexcept
Reads a signed char-type data string as binary from a stream.
Definition: BinaryReader.h:80
int Peek() noexcept
References the first byte of the stream.
Definition: BinaryReader.h:49
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:158
The class for reading binary from streams (from InputStream).
Definition: BinaryReader.h:13
bool Read(unsigned long *x) noexcept
Reads unsigned long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:40
bool Read(long *x) noexcept
Reads long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:39
~BinaryReader() noexcept
Destructor. This function does not close the stream.
Definition: BinaryReader.h:23
size_t ReadArray(long *x, size_t n) noexcept
Reads a long-type data string as binary from a stream.
Definition: BinaryReader.h:92
bool Read(long long *x) noexcept
Reads long long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:41
bool Read(char *x) noexcept
Reads char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:32
bool ReadArray(BinaryReader *r, T *x, size_t n)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:161
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24