3 #ifndef INCLUDE_NN_NLIB_BINARYREADER_H_
4 #define INCLUDE_NN_NLIB_BINARYREADER_H_
7 #include "nn/nlib/Swap.h"
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)); \
52 if (m_Cur == m_BufEnd)
return -1;
57 if (m_Cur + n <= m_BufEnd) {
61 return this->Skip_(n);
71 return this->
ReadArray(reinterpret_cast<unsigned long long*>(x), n);
73 return this->
ReadArray(reinterpret_cast<unsigned int*>(x), n);
81 return this->
ReadArray(reinterpret_cast<unsigned char*>(x), n);
84 return this->
ReadArray(reinterpret_cast<unsigned char*>(x), n);
87 return this->
ReadArray(reinterpret_cast<unsigned short*>(x), n);
90 return this->
ReadArray(reinterpret_cast<unsigned int*>(x), n);
94 return this->
ReadArray(reinterpret_cast<unsigned long long*>(x), n);
96 return this->
ReadArray(reinterpret_cast<unsigned int*>(x), n);
100 return this->
ReadArray(reinterpret_cast<unsigned long long*>(x), n);
105 template <
class T,
size_t N>
113 if (m_ErrorValue == 0) m_ErrorValue = e;
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 {
130 if (!this->Read_(&v.i))
return false;
134 bool Read_(
double* x) NLIB_NOEXCEPT {
139 if (!this->Read_(&v.i))
return false;
143 void FillBuffer() NLIB_NOEXCEPT;
146 unsigned char m_Buf[512];
147 unsigned char* m_Cur;
148 unsigned char* m_BufEnd;
149 InputStream* m_Stream;
151 unsigned char m_SwapEndian;
155 namespace binary_reader {
176 template <
class T,
size_t N>
183 #endif // INCLUDE_NN_NLIB_BINARYREADER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
bool Read(unsigned long long *x) noexcept
Reads unsigned long long-type data as binary from a stream. Returns true if successful.
bool Skip(size_t n) noexcept
Skips over n bytes when reading from stream.
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.
bool Read(signed char *x) noexcept
Reads signed char-type data as binary from a stream. Returns true if successful.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#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...
size_t ReadArray(char *x, size_t n) noexcept
Reads a char-type data string as binary from a stream.
void SetError(errno_t e) noexcept
Sets an error. If an error has been set already, this function does not set an error.
bool Read(int *x) noexcept
Reads int-type data as binary from a stream. Returns true if successful.
bool Read(double *x) noexcept
Reads double-type data as binary from a stream. Returns true if successful.
BinaryReader() noexcept
Instantiates the object with default parameters (default constructor).
bool Read(short *x) noexcept
Reads short-type data as binary from a stream. Returns true if successful.
size_t ReadArray(unsigned long *x, size_t n) noexcept
Reads an unsigned long-type data string as binary from a stream.
size_t ReadArray(int *x, size_t n) noexcept
Reads an int-type data string as binary from a stream.
bool Read(unsigned int *x) noexcept
Reads unsigned int-type data as binary from a stream. Returns true if successful. ...
bool Read(float *x) noexcept
Reads float-type data as binary from a stream. Returns true if successful.
size_t ReadArray(long long *x, size_t n) noexcept
Reads a long long-type data string as binary from a stream.
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.
size_t ReadArray(short *x, size_t n) noexcept
Reads a short-type data string as binary from a stream.
EndianSetting
Specifies the endianness.
bool Close() noexcept
Closes the binary reader.
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
size_t ReadArray(signed char *x, size_t n) noexcept
Reads a signed char-type data string as binary from a stream.
int Peek() noexcept
References the first byte of the stream.
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
The class for reading binary from streams (from InputStream).
bool Read(unsigned long *x) noexcept
Reads unsigned long-type data as binary from a stream. Returns true if successful.
bool Read(long *x) noexcept
Reads long-type data as binary from a stream. Returns true if successful.
~BinaryReader() noexcept
Destructor. This function does not close the stream.
size_t ReadArray(long *x, size_t n) noexcept
Reads a long-type data string as binary from a stream.
bool Read(long long *x) noexcept
Reads long long-type data as binary from a stream. Returns true if successful.
bool Read(char *x) noexcept
Reads char-type data as binary from a stream. Returns true if successful.
bool ReadArray(BinaryReader *r, T *x, size_t n)
You can read to user-defined class objects by specializing this function template.