nlib
BinaryReader.h
Go to the documentation of this file.
1 
2 /*---------------------------------------------------------------------------*
3 
4  Project: CrossRoad
5  Copyright (C)2012-2016 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
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:
28  enum EndianSetting { DEFAULT = 0, ENDIAN_LITTLE, ENDIAN_BIG };
29 
30  public:
31  BinaryReader() NLIB_NOEXCEPT : stream_(NULL),
32  errno_(-1),
33  swap_endian_(false) {}
34  ~BinaryReader() NLIB_NOEXCEPT { this->Close(); }
35  errno_t Init(EndianSetting endian) NLIB_NOEXCEPT;
36  errno_t Init() NLIB_NOEXCEPT { return Init(DEFAULT); }
37  errno_t Open(InputStream* stream) NLIB_NOEXCEPT;
38 
39 #define NLIB_BINR(tp) return this->Read_(reinterpret_cast<tp*>(x))
40 
41  bool Read(char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
42  bool Read(signed char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
43  bool Read(unsigned char* x) NLIB_NOEXCEPT { NLIB_BINR(uint8_t); }
44  bool Read(short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); } // NOLINT
45  bool Read(unsigned short* x) NLIB_NOEXCEPT { NLIB_BINR(uint16_t); } // NOLINT
46  bool Read(int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
47  bool Read(unsigned int* x) NLIB_NOEXCEPT { NLIB_BINR(uint32_t); }
48  bool Read(long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); } // NOLINT
49  bool Read(unsigned long* x) NLIB_NOEXCEPT { NLIB_BINR(nlib_ulong_compatible_t); } // NOLINT
50  bool Read(long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); } // NOLINT
51  bool Read(unsigned long long* x) NLIB_NOEXCEPT { NLIB_BINR(uint64_t); } // NOLINT
52  bool Read(float* x) NLIB_NOEXCEPT { NLIB_BINR(float); } // NOLINT
53  bool Read(double* x) NLIB_NOEXCEPT { NLIB_BINR(double); } // NOLINT
54 
55  template <class T>
56  bool Read(T* x) NLIB_NOEXCEPT NLIB_NONNULL;
57 
58  int Peek() NLIB_NOEXCEPT {
59  return mini_buf_.Peek(stream_);
60  }
61  bool Skip(size_t n) NLIB_NOEXCEPT {
62  size_t rem = mini_buf_.size();
63  if (rem <= n) {
64  mini_buf_.Advance(n);
65  return true;
66  } else {
67  mini_buf_.Advance(rem);
68  n -= rem;
69  return this->Skip_(n);
70  }
71  }
72 #undef NLIB_BINR
73 
74  size_t ReadArray(unsigned char* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
75  size_t ReadArray(unsigned short* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
76  size_t ReadArray(unsigned int* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
77  size_t ReadArray(unsigned long long* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
78  size_t ReadArray(unsigned long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
79 #ifdef NLIB_LP64
80  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
81 #else
82  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
83 #endif
84  }
85 
86  size_t ReadArray(float* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
87  size_t ReadArray(double* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
88 
89  size_t ReadArray(signed char* x, size_t n) NLIB_NOEXCEPT {
90  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
91  }
92  size_t ReadArray(char* x, size_t n) NLIB_NOEXCEPT {
93  return this->ReadArray(reinterpret_cast<unsigned char*>(x), n);
94  }
95  size_t ReadArray(short* x, size_t n) NLIB_NOEXCEPT { // NOLINT
96  return this->ReadArray(reinterpret_cast<unsigned short*>(x), n); // NOLINT
97  }
98  size_t ReadArray(int* x, size_t n) NLIB_NOEXCEPT {
99  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
100  }
101  size_t ReadArray(long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
102 #ifdef NLIB_LP64
103  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
104 #else
105  return this->ReadArray(reinterpret_cast<unsigned int*>(x), n);
106 #endif
107  }
108  size_t ReadArray(long long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
109  return this->ReadArray(reinterpret_cast<unsigned long long*>(x), n); // NOLINT
110  }
111 
112  template <class T>
113  size_t ReadArray(T* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
114  template <class T, size_t N>
115  size_t ReadArray(T (&a)[N]) NLIB_NOEXCEPT;
116  bool Close() NLIB_NOEXCEPT {
117  // Do not stream_->Close(). only detach it from BinaryReader.
118  stream_ = NULL;
119  return true;
120  }
121  void SetError(errno_t e) NLIB_NOEXCEPT {
122  if (errno_ == 0) errno_ = e;
123  }
124  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
125 
126  NLIB_SAFE_BOOL(BinaryReader, GetErrorValue() == 0)
127 
128  private:
129  bool Skip_(size_t n) NLIB_NOEXCEPT;
130  bool Read_(uint8_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
131  bool Read_(uint16_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
132  bool Read_(uint32_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
133  bool Read_(uint64_t* x) NLIB_NOEXCEPT NLIB_NONNULL;
134  bool Read_(float* x) NLIB_NOEXCEPT {
135  union {
136  float f;
137  uint32_t i;
138  } v;
139  if (!this->Read_(&v.i)) return false;
140  *x = v.f;
141  return true;
142  }
143  bool Read_(double* x) NLIB_NOEXCEPT {
144  union {
145  double f;
146  uint64_t i;
147  } v;
148  if (!this->Read_(&v.i)) return false;
149  *x = v.f;
150  return true;
151  }
152 
153  private:
154  InputStream* stream_;
155  ErrnoT errno_;
156  bool swap_endian_;
157  detail::MiniBufIn<512> mini_buf_;
158 
160 };
161 
162 namespace binary_reader {
163 
164 template<class T>
165 NLIB_ALWAYS_INLINE bool Read(BinaryReader* r, T* x) { return r->Read(x); }
166 
167 template <class T>
168 NLIB_ALWAYS_INLINE bool ReadArray(BinaryReader* r, T* x, size_t n) {
169  return r->ReadArray(x, n);
170 }
171 }
172 
173 template <class T>
175  return binary_reader::Read(this, x);
176 }
177 
178 template <class T>
180  return binary_reader::ReadArray(this, x, n);
181 }
182 
183 template <class T, size_t N>
185  return this->ReadArray(&a[0], N);
186 }
187 
188 NLIB_NAMESPACE_END
189 
190 #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.
Definition: BinaryReader.h:51
bool Skip(size_t n) noexcept
Skips over n bytes when reading from stream.
Definition: BinaryReader.h:61
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:43
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:95
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
Definition: BinaryReader.h:124
bool Read(signed char *x) noexcept
Reads signed char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:42
#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:158
#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:173
Defines the base class for input streams.
size_t ReadArray(char *x, size_t n) noexcept
Reads a char-type data string as binary from a stream.
Definition: BinaryReader.h:92
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:121
bool Read(int *x) noexcept
Reads int-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:46
bool Read(double *x) noexcept
Reads double-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:53
uint32_t nlib_ulong_compatible_t
Defines an integer type that is compatible with unsigned long using typedef.
Definition: Platform.h:627
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
BinaryReader() noexcept
Instantiates the object with default parameters (default constructor).
Definition: BinaryReader.h:31
bool Read(short *x) noexcept
Reads short-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:44
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:78
size_t ReadArray(int *x, size_t n) noexcept
Reads an int-type data string as binary from a stream.
Definition: BinaryReader.h:98
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:29
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
Definition: Config.h:487
bool Read(unsigned int *x) noexcept
Reads unsigned int-type data as binary from a stream. Returns true if successful. ...
Definition: BinaryReader.h:47
bool Read(float *x) noexcept
Reads float-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:52
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:108
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.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:45
size_t ReadArray(short *x, size_t n) noexcept
Reads a short-type data string as binary from a stream.
Definition: BinaryReader.h:95
EndianSetting
Specifies the endianness.
Definition: BinaryReader.h:28
bool Close() noexcept
Closes the binary reader.
Definition: BinaryReader.h:116
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:89
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:224
int Peek() noexcept
References the first byte of the stream.
Definition: BinaryReader.h:58
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:165
The class for reading binary from streams (from InputStream).
Definition: BinaryReader.h:26
bool Read(unsigned long *x) noexcept
Reads unsigned long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:49
errno_t Init() noexcept
Initializes the binary reader using the same endianness as the machine the program is running on...
Definition: BinaryReader.h:36
bool Read(long *x) noexcept
Reads long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:48
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
~BinaryReader() noexcept
Destructor. This function does not close the stream.
Definition: BinaryReader.h:34
size_t ReadArray(long *x, size_t n) noexcept
Reads a long-type data string as binary from a stream.
Definition: BinaryReader.h:101
bool Read(long long *x) noexcept
Reads long long-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:50
bool Read(char *x) noexcept
Reads char-type data as binary from a stream. Returns true if successful.
Definition: BinaryReader.h:41
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:168
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37