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