nlib
BinaryWriter.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_BINARYWRITER_H_
4 #define INCLUDE_NN_NLIB_BINARYWRITER_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/Swap.h"
8 
9 NLIB_NAMESPACE_BEGIN
10 
11 class OutputStream;
12 
14  public:
15  enum EndianSetting { DEFAULT = 0, ENDIAN_LITTLE, ENDIAN_BIG };
16 
17  public:
18  BinaryWriter() NLIB_NOEXCEPT : m_Cur(&m_Buf[0]),
19  m_Stream(NULL),
20  m_ErrorValue(0),
21  m_SwapEndian(0) {}
22  ~BinaryWriter() NLIB_NOEXCEPT { this->Close(); }
23 
24  bool Init(OutputStream* stream, EndianSetting endian = DEFAULT) NLIB_NOEXCEPT NLIB_NONNULL;
25 
26 #define NLIB_BINW(tp) \
27  if (NLIB_LIKELY(m_Stream)) return this->Write_(static_cast<tp>(x)); \
28  SetError(EBADF); \
29  return false
30 
31  bool Write(char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
32  bool Write(signed char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
33  bool Write(unsigned char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
34  bool Write(short x) NLIB_NOEXCEPT { NLIB_BINW(uint16_t); } // NOLINT
35  bool Write(unsigned short x) NLIB_NOEXCEPT { NLIB_BINW(uint16_t); } // NOLINT
36  bool Write(int x) NLIB_NOEXCEPT { NLIB_BINW(uint32_t); }
37  bool Write(unsigned int x) NLIB_NOEXCEPT { NLIB_BINW(uint32_t); }
38  bool Write(long x) NLIB_NOEXCEPT { NLIB_BINW(nlib_ulong_compatible_t); } // NOLINT
39  bool Write(unsigned long x) NLIB_NOEXCEPT { NLIB_BINW(nlib_ulong_compatible_t); } // NOLINT
40  bool Write(long long x) NLIB_NOEXCEPT { NLIB_BINW(uint64_t); } // NOLINT
41  bool Write(unsigned long long x) NLIB_NOEXCEPT { NLIB_BINW(uint64_t); } // NOLINT
42  bool Write(float x) NLIB_NOEXCEPT { NLIB_BINW(float); } // NOLINT
43  bool Write(double x) NLIB_NOEXCEPT { NLIB_BINW(double); } // NOLINT
44 #undef NLIB_BW
45 
46  template <class T>
48 
49  bool WriteArray(const unsigned char* x, size_t n) NLIB_NOEXCEPT;
50  bool WriteArray(const unsigned short* x, size_t n) NLIB_NOEXCEPT; // NOLINT
51  bool WriteArray(const unsigned int* x, size_t n) NLIB_NOEXCEPT;
52  bool WriteArray(const unsigned long long* x, size_t n) NLIB_NOEXCEPT; // NOLINT
53  bool WriteArray(const unsigned long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
54 #ifdef NLIB_LP64
55  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
56 #else
57  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
58 #endif
59  }
60 
61  bool WriteArray(const float* x, size_t n) NLIB_NOEXCEPT;
62  bool WriteArray(const double* x, size_t n) NLIB_NOEXCEPT;
63  bool WriteArray(const signed char* x, size_t n) NLIB_NOEXCEPT {
64  return this->WriteArray(reinterpret_cast<const unsigned char*>(x), n);
65  }
66  bool WriteArray(const char* x, size_t n) NLIB_NOEXCEPT {
67  return this->WriteArray(reinterpret_cast<const unsigned char*>(x), n);
68  }
69  bool WriteArray(const short* x, size_t n) NLIB_NOEXCEPT { // NOLINT
70  return this->WriteArray(reinterpret_cast<const unsigned short*>(x), n); // NOLINT
71  }
72  bool WriteArray(const int* x, size_t n) NLIB_NOEXCEPT {
73  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
74  }
75  bool WriteArray(const long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
76 #ifdef NLIB_LP64
77  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
78 #else
79  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
80 #endif
81  }
82  bool WriteArray(const long long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
83  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
84  }
85  template <class T>
86  NLIB_VIS_HIDDEN bool WriteArray(const T* x, size_t n) NLIB_NOEXCEPT;
87  template <class T, size_t N>
88  NLIB_VIS_HIDDEN bool WriteArray(const T(&a)[N]) NLIB_NOEXCEPT;
89  bool Flush() NLIB_NOEXCEPT;
90  bool Close() NLIB_NOEXCEPT;
91  void SetError(errno_t e) NLIB_NOEXCEPT {
92  if (m_ErrorValue == 0) m_ErrorValue = e;
93  }
94  errno_t GetErrorValue() const NLIB_NOEXCEPT { return m_ErrorValue; }
95  OutputStream* GetStream() NLIB_NOEXCEPT { return m_Stream; }
96 
97  NLIB_SAFE_BOOL(BinaryWriter, GetErrorValue() == 0)
98 
99  private:
100  bool Write_(uint8_t x) NLIB_NOEXCEPT;
101  bool Write_(uint16_t x) NLIB_NOEXCEPT;
102  bool Write_(uint32_t x) NLIB_NOEXCEPT;
103  bool Write_(uint64_t x) NLIB_NOEXCEPT;
104  bool Write_(float x) NLIB_NOEXCEPT;
105  bool Write_(double x) NLIB_NOEXCEPT;
106  bool WriteStream() NLIB_NOEXCEPT;
107 
108  private:
109  unsigned char m_Buf[512];
110  unsigned char* m_Cur;
111  OutputStream* m_Stream;
112  errno_t m_ErrorValue;
113  unsigned char m_SwapEndian;
114 
116 };
117 
118 namespace binary_writer {
119 
120 template<class T>
121 NLIB_ALWAYS_INLINE bool Write(BinaryWriter* w, T x) { return w->Write(x); }
122 
123 template <class T>
124 NLIB_ALWAYS_INLINE bool WriteArray(BinaryWriter* w, const T* x, size_t n) {
125  return w->WriteArray(x, n);
126 }
127 }
128 
129 template <class T>
130 NLIB_ALWAYS_INLINE bool BinaryWriter::Write(T x) NLIB_NOEXCEPT {
131  return binary_writer::Write(this, x);
132 }
133 
134 template <class T>
135 NLIB_ALWAYS_INLINE bool BinaryWriter::WriteArray(const T* x, size_t n) NLIB_NOEXCEPT {
136  return binary_writer::WriteArray(this, x, n);
137 }
138 
139 template <class T, size_t N>
140 NLIB_ALWAYS_INLINE bool BinaryWriter::WriteArray(const T (&a)[N]) NLIB_NOEXCEPT {
141  return this->WriteArray(&a[0], N);
142 }
143 
144 NLIB_NAMESPACE_END
145 
146 #endif // INCLUDE_NN_NLIB_BINARYWRITER_H_
bool Write(unsigned long x) noexcept
Writes unsigned long-type data as binary to a stream. Returns true if successful. ...
Definition: BinaryWriter.h:39
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
OutputStream * GetStream() noexcept
Gets the stream for the text writer to write to.
Definition: BinaryWriter.h:95
#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
bool WriteArray(const unsigned char *x, size_t n) noexcept
Writes an unsigned char-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
Definition: BinaryWriter.h:94
bool Write(float x) noexcept
Writes float-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:42
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
bool Write(unsigned short x) noexcept
Writes unsigned short-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:35
uint32_t nlib_ulong_compatible_t
Defines an integer type that is compatible with unsigned long using typedef.
Definition: Platform.h:425
~BinaryWriter() noexcept
Destructor. This function does not close the stream.
Definition: BinaryWriter.h:22
bool Write(char x) noexcept
Writes char-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:31
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:121
bool Write(unsigned int x) noexcept
Writes unsigned int-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:37
bool Write(long long x) noexcept
Writes long long-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:40
bool Write(unsigned char x) noexcept
Writes unsigned char-type data as binary to a stream. Returns true if successful. ...
Definition: BinaryWriter.h:33
bool WriteArray(BinaryWriter *w, const T *x, size_t n)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:124
bool WriteArray(const long long *x, size_t n) noexcept
Writes a long long-type array as binary to a stream. Returns true if successful. If n is 0...
Definition: BinaryWriter.h:82
bool WriteArray(const long *x, size_t n) noexcept
Writes a long-type array as binary to a stream. Returns true if successful. If n is 0...
Definition: BinaryWriter.h:75
bool Write(long x) noexcept
Writes long-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:38
bool Write(int x) noexcept
Writes int-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:36
bool Write(short x) noexcept
Writes short-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:34
A file that contains the configuration information for each development environment.
EndianSetting
Specifies the endianness.
Definition: BinaryWriter.h:15
bool WriteArray(const unsigned long *x, size_t n) noexcept
Writes an unsigned long-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
Definition: BinaryWriter.h:53
bool WriteArray(const int *x, size_t n) noexcept
Writes an int-type array as binary to a stream. Returns true if successful. If n is 0...
Definition: BinaryWriter.h:72
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:59
The class for writing binary to streams (to OutputStream).
Definition: BinaryWriter.h:13
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
bool Write(signed char x) noexcept
Writes signed char-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:32
bool WriteArray(const signed char *x, size_t n) noexcept
Writes a signed char-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
Definition: BinaryWriter.h:63
bool WriteArray(const char *x, size_t n) noexcept
Writes a char-type array as binary to a stream. Returns true if successful. If n is 0...
Definition: BinaryWriter.h:66
BinaryWriter() noexcept
Instantiates the object with default parameters (default constructor).
Definition: BinaryWriter.h:18
bool Write(double x) noexcept
Writes double-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:43
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17
bool Write(unsigned long long x) noexcept
Writes unsigned long long-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:41
bool WriteArray(const short *x, size_t n) noexcept
Writes a short-type array as binary to a stream. Returns true if successful. If n is 0...
Definition: BinaryWriter.h:69
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24