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 #include "nn/nlib/OutputStream.h"
9 
10 NLIB_NAMESPACE_BEGIN
11 NLIB_EXPIMP_TEMPLATE(detail::MiniBufOut<512>);
12 
14  public:
15  enum EndianSetting { DEFAULT = 0, ENDIAN_LITTLE, ENDIAN_BIG };
16 
17  public:
18  BinaryWriter() NLIB_NOEXCEPT : stream_(NULL),
19  errno_(-1),
20  swap_endian_(false) {}
21  ~BinaryWriter() NLIB_NOEXCEPT { this->Close(); }
22  errno_t Init(EndianSetting endian) NLIB_NOEXCEPT;
23  errno_t Init() NLIB_NOEXCEPT { return Init(DEFAULT); }
24  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT;
25 
26 #define NLIB_BINW(tp) return this->Write_(static_cast<tp>(x))
27  bool Write(char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
28  bool Write(signed char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
29  bool Write(unsigned char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
30  bool Write(short x) NLIB_NOEXCEPT { NLIB_BINW(uint16_t); } // NOLINT
31  bool Write(unsigned short x) NLIB_NOEXCEPT { NLIB_BINW(uint16_t); } // NOLINT
32  bool Write(int x) NLIB_NOEXCEPT { NLIB_BINW(uint32_t); }
33  bool Write(unsigned int x) NLIB_NOEXCEPT { NLIB_BINW(uint32_t); }
34  bool Write(long x) NLIB_NOEXCEPT { NLIB_BINW(nlib_ulong_compatible_t); } // NOLINT
35  bool Write(unsigned long x) NLIB_NOEXCEPT { NLIB_BINW(nlib_ulong_compatible_t); } // NOLINT
36  bool Write(long long x) NLIB_NOEXCEPT { NLIB_BINW(uint64_t); } // NOLINT
37  bool Write(unsigned long long x) NLIB_NOEXCEPT { NLIB_BINW(uint64_t); } // NOLINT
38  bool Write(float x) NLIB_NOEXCEPT { NLIB_BINW(float); } // NOLINT
39  bool Write(double x) NLIB_NOEXCEPT { NLIB_BINW(double); } // NOLINT
40 #undef NLIB_BW
41 
42  template <class T>
43  bool Write(T x) NLIB_NOEXCEPT;
44 
45  bool WriteArray(const unsigned char* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
46  bool WriteArray(const unsigned short* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
47  bool WriteArray(const unsigned int* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
48  bool WriteArray(const unsigned long long* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
49  bool WriteArray(const unsigned long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
50 #ifdef NLIB_LP64
51  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
52 #else
53  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
54 #endif
55  }
56 
57  bool WriteArray(const float* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
58  bool WriteArray(const double* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
59  bool WriteArray(const signed char* x, size_t n) NLIB_NOEXCEPT {
60  return this->WriteArray(reinterpret_cast<const unsigned char*>(x), n);
61  }
62  bool WriteArray(const char* x, size_t n) NLIB_NOEXCEPT {
63  return this->WriteArray(reinterpret_cast<const unsigned char*>(x), n);
64  }
65  bool WriteArray(const short* x, size_t n) NLIB_NOEXCEPT { // NOLINT
66  return this->WriteArray(reinterpret_cast<const unsigned short*>(x), n); // NOLINT
67  }
68  bool WriteArray(const int* x, size_t n) NLIB_NOEXCEPT {
69  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
70  }
71  bool WriteArray(const long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
72 #ifdef NLIB_LP64
73  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
74 #else
75  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
76 #endif
77  }
78  bool WriteArray(const long long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
79  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
80  }
81  template <class T>
82  bool WriteArray(const T* x, size_t n) NLIB_NOEXCEPT;
83  template <class T, size_t N>
84  bool WriteArray(const T(&a)[N]) NLIB_NOEXCEPT;
85  bool Flush() NLIB_NOEXCEPT;
86  bool Close() NLIB_NOEXCEPT;
87  void SetError(errno_t e) NLIB_NOEXCEPT {
88  if (errno_ == 0) errno_ = e;
89  }
90  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
91  OutputStream* GetStream() NLIB_NOEXCEPT { return stream_; }
92 
93  NLIB_SAFE_BOOL(BinaryWriter, GetErrorValue() == 0)
94 
95  private:
96  bool Write_(uint8_t x) NLIB_NOEXCEPT;
97  bool Write_(uint16_t x) NLIB_NOEXCEPT;
98  bool Write_(uint32_t x) NLIB_NOEXCEPT;
99  bool Write_(uint64_t x) NLIB_NOEXCEPT;
100  bool Write_(float x) NLIB_NOEXCEPT;
101  bool Write_(double x) NLIB_NOEXCEPT;
102  NLIB_VIS_HIDDEN bool WriteStream() NLIB_NOEXCEPT;
103 
104  private:
105  OutputStream* stream_;
106  ErrnoT errno_;
107  bool swap_endian_;
108  detail::MiniBufOut<512> mini_buf_;
109 
111 };
112 
113 namespace binary_writer {
114 
115 template<class T>
116 NLIB_ALWAYS_INLINE bool Write(BinaryWriter* w, T x) { return w->Write(x); }
117 
118 template <class T>
119 NLIB_ALWAYS_INLINE bool WriteArray(BinaryWriter* w, const T* x, size_t n) {
120  return w->WriteArray(x, n);
121 }
122 }
123 
124 template <class T>
126  return binary_writer::Write(this, x);
127 }
128 
129 template <class T>
131  return binary_writer::WriteArray(this, x, n);
132 }
133 
134 template <class T, size_t N>
136  return this->WriteArray(&a[0], N);
137 }
138 
139 NLIB_NAMESPACE_END
140 
141 #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:35
OutputStream * GetStream() noexcept
Gets the stream for the text writer to write to.
Definition: BinaryWriter.h:91
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:69
#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:145
#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:160
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.
bool Write(float x) noexcept
Writes float-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:38
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
Definition: BinaryWriter.h:90
bool Write(unsigned short x) noexcept
Writes unsigned short-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:31
uint32_t nlib_ulong_compatible_t
Defines an integer type that is compatible with unsigned long using typedef.
Definition: Platform.h:617
errno_t Init() noexcept
Initializes the binary writer using the same endianness as the machine the program is running on...
Definition: BinaryWriter.h:23
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
~BinaryWriter() noexcept
Destructor. This function does not close the stream.
Definition: BinaryWriter.h:21
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
bool Write(char x) noexcept
Writes char-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:27
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:116
bool Write(unsigned int x) noexcept
Writes unsigned int-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:33
bool Write(long long x) noexcept
Writes long long-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:36
bool Write(unsigned char x) noexcept
Writes unsigned char-type data as binary to a stream. Returns true if successful. ...
Definition: BinaryWriter.h:29
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:119
void SetError(errno_t e) noexcept
Sets an error. If an error has been set already, this function does not set an error.
Definition: BinaryWriter.h:87
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:78
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:71
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
Definition: Config.h:474
bool Write(long x) noexcept
Writes long-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:34
bool Write(int x) noexcept
Writes int-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:32
bool Write(short x) noexcept
Writes short-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:30
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
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:49
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:68
The class for writing binary to streams (to OutputStream).
Definition: BinaryWriter.h:13
bool Write(signed char x) noexcept
Writes signed char-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:28
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
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:59
Defines the base class for output streams.
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:62
BinaryWriter() noexcept
Instantiates the object with default parameters (default constructor).
Definition: BinaryWriter.h:18
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:76
bool Write(double x) noexcept
Writes double-type data as binary to a stream. Returns true if successful.
Definition: BinaryWriter.h:39
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:37
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:65
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24