16 #ifndef INCLUDE_NN_NLIB_BINARYREADER_H_ 17 #define INCLUDE_NN_NLIB_BINARYREADER_H_ 20 #include "nn/nlib/Swap.h" 24 NLIB_EXPIMP_TEMPLATE(detail::MiniBufIn<512>);
32 DEFAULT = kEndianDefault,
33 ENDIAN_LITTLE = kEndianLittle,
34 ENDIAN_BIG = kEndianBig
40 swap_endian_(false) {}
46 #define NLIB_BINR(tp) return this->Read_(reinterpret_cast<tp*>(x)) 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); }
52 bool Read(
unsigned short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); }
53 bool Read(
int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
54 bool Read(
unsigned int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
57 bool Read(
long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); }
58 bool Read(
unsigned long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); }
59 bool Read(
float* x) NLIB_NOEXCEPT { NLIB_BINR(
float); }
60 bool Read(
double* x) NLIB_NOEXCEPT { NLIB_BINR(
double); }
66 return mini_buf_.Peek(stream_);
68 bool Skip(
size_t n) NLIB_NOEXCEPT {
69 size_t rem = mini_buf_.size();
74 mini_buf_.Advance(rem);
76 return this->Skip_(n);
85 size_t ReadArray(
unsigned long* x,
size_t n) NLIB_NOEXCEPT {
87 return this->
ReadArray(reinterpret_cast<unsigned long long*>(x), n);
89 return this->
ReadArray(reinterpret_cast<unsigned int*>(x), n);
96 size_t ReadArray(
signed char* x,
size_t n) NLIB_NOEXCEPT {
97 return this->
ReadArray(reinterpret_cast<unsigned char*>(x), n);
100 return this->
ReadArray(reinterpret_cast<unsigned char*>(x), n);
103 return this->
ReadArray(reinterpret_cast<unsigned short*>(x), n);
106 return this->
ReadArray(reinterpret_cast<unsigned int*>(x), n);
110 return this->
ReadArray(reinterpret_cast<unsigned long long*>(x), n);
112 return this->
ReadArray(reinterpret_cast<unsigned int*>(x), n);
115 size_t ReadArray(
long long* x,
size_t n) NLIB_NOEXCEPT {
116 return this->
ReadArray(reinterpret_cast<unsigned long long*>(x), n);
121 template <
class T,
size_t N>
129 if (errno_ == 0) errno_ = e;
141 bool Read_(
float* x) NLIB_NOEXCEPT {
146 if (!this->Read_(&v.i))
return false;
150 bool Read_(
double* x) NLIB_NOEXCEPT {
155 if (!this->Read_(&v.i))
return false;
164 detail::MiniBufIn<512> mini_buf_;
169 namespace binary_reader {
190 template <
class T,
size_t N>
197 #endif // INCLUDE_NN_NLIB_BINARYREADER_H_ 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.
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
bool Read(signed char *x) noexcept
Reads signed char-type data as binary from a stream. Returns true if successful.
#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.
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
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.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
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.
size_t ReadArray(signed char *x, size_t n) noexcept
Reads a signed char-type data string as binary from a stream.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
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.
errno_t Init() noexcept
Initializes the binary reader using the same endianness as the machine the program is running on...
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.