nlib
BinaryWriter.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
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:
29  kEndianDefault = 0,
32  DEFAULT = kEndianDefault,
33  ENDIAN_LITTLE = kEndianLittle,
34  ENDIAN_BIG = kEndianBig
35  };
36 
37  public:
38  BinaryWriter() NLIB_NOEXCEPT : stream_(nullptr),
39  errno_(-1),
40  swap_endian_(false) {}
41  ~BinaryWriter() NLIB_NOEXCEPT { this->Close(); }
42  errno_t Init(EndianSetting endian) NLIB_NOEXCEPT;
43  errno_t Init() NLIB_NOEXCEPT { return Init(kEndianDefault); }
44  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT;
45 
46 #define NLIB_BINW(tp) return this->Write_(static_cast<tp>(x))
47  bool Write(char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
48  bool Write(signed char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
49  bool Write(unsigned char x) NLIB_NOEXCEPT { NLIB_BINW(uint8_t); }
50  bool Write(short x) NLIB_NOEXCEPT { NLIB_BINW(uint16_t); } // NOLINT
51  bool Write(unsigned short x) NLIB_NOEXCEPT { NLIB_BINW(uint16_t); } // NOLINT
52  bool Write(int x) NLIB_NOEXCEPT { NLIB_BINW(uint32_t); }
53  bool Write(unsigned int x) NLIB_NOEXCEPT { NLIB_BINW(uint32_t); }
54  bool Write(long x) NLIB_NOEXCEPT { NLIB_BINW(nlib_ulong_compatible_t); } // NOLINT
55  bool Write(unsigned long x) NLIB_NOEXCEPT { NLIB_BINW(nlib_ulong_compatible_t); } // NOLINT
56  bool Write(long long x) NLIB_NOEXCEPT { NLIB_BINW(uint64_t); } // NOLINT
57  bool Write(unsigned long long x) NLIB_NOEXCEPT { NLIB_BINW(uint64_t); } // NOLINT
58  bool Write(float x) NLIB_NOEXCEPT { NLIB_BINW(float); } // NOLINT
59  bool Write(double x) NLIB_NOEXCEPT { NLIB_BINW(double); } // NOLINT
60 #undef NLIB_BINW
61 
62  template <class T>
63  bool Write(T x) NLIB_NOEXCEPT;
64 
65  bool WriteArray(const unsigned char* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
66  bool WriteArray(const unsigned short* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
67  bool WriteArray(const unsigned int* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
68  bool WriteArray(const unsigned long long* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL; // NOLINT
69  bool WriteArray(const unsigned long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
70 #ifdef NLIB_LP64
71  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
72 #else
73  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
74 #endif
75  }
76 
77  bool WriteArray(const float* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
78  bool WriteArray(const double* x, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
79  bool WriteArray(const signed char* x, size_t n) NLIB_NOEXCEPT {
80  return this->WriteArray(reinterpret_cast<const unsigned char*>(x), n);
81  }
82  bool WriteArray(const char* x, size_t n) NLIB_NOEXCEPT {
83  return this->WriteArray(reinterpret_cast<const unsigned char*>(x), n);
84  }
85  bool WriteArray(const short* x, size_t n) NLIB_NOEXCEPT { // NOLINT
86  return this->WriteArray(reinterpret_cast<const unsigned short*>(x), n); // NOLINT
87  }
88  bool WriteArray(const int* x, size_t n) NLIB_NOEXCEPT {
89  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
90  }
91  bool WriteArray(const long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
92 #ifdef NLIB_LP64
93  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
94 #else
95  return this->WriteArray(reinterpret_cast<const unsigned int*>(x), n);
96 #endif
97  }
98  bool WriteArray(const long long* x, size_t n) NLIB_NOEXCEPT { // NOLINT
99  return this->WriteArray(reinterpret_cast<const unsigned long long*>(x), n); // NOLINT
100  }
101  template <class T>
102  bool WriteArray(const T* x, size_t n) NLIB_NOEXCEPT;
103  template <class T, size_t N>
104  bool WriteArray(const T(&a)[N]) NLIB_NOEXCEPT;
105  bool Flush() NLIB_NOEXCEPT;
106  bool Close() NLIB_NOEXCEPT;
107  void SetError(errno_t e) NLIB_NOEXCEPT {
108  if (errno_ == 0) errno_ = e;
109  }
110  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
111  OutputStream* GetStream() NLIB_NOEXCEPT { return stream_; }
112 
113  NLIB_SAFE_BOOL(BinaryWriter, GetErrorValue() == 0)
114 
115  private:
116  bool Write_(uint8_t x) NLIB_NOEXCEPT;
117  bool Write_(uint16_t x) NLIB_NOEXCEPT;
118  bool Write_(uint32_t x) NLIB_NOEXCEPT;
119  bool Write_(uint64_t x) NLIB_NOEXCEPT;
120  bool Write_(float x) NLIB_NOEXCEPT;
121  bool Write_(double x) NLIB_NOEXCEPT;
122  NLIB_VIS_HIDDEN bool WriteStream() NLIB_NOEXCEPT;
123 
124  private:
125  OutputStream* stream_;
126  ErrnoT errno_;
127  bool swap_endian_;
128  detail::MiniBufOut<512> mini_buf_;
129 
131 };
132 
133 namespace binary_writer {
134 
135 template<class T>
136 NLIB_ALWAYS_INLINE bool Write(BinaryWriter* w, T x) { return w->Write(x); }
137 
138 template <class T>
139 NLIB_ALWAYS_INLINE bool WriteArray(BinaryWriter* w, const T* x, size_t n) {
140  return w->WriteArray(x, n);
141 }
142 }
143 
144 template <class T>
146  return binary_writer::Write(this, x);
147 }
148 
149 template <class T>
151  return binary_writer::WriteArray(this, x, n);
152 }
153 
154 template <class T, size_t N>
156  return this->WriteArray(&a[0], N);
157 }
158 
159 NLIB_NAMESPACE_END
160 
161 #endif // INCLUDE_NN_NLIB_BINARYWRITER_H_
bool Write(unsigned long x) noexcept
ストリームにバイナリでunsigned long型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:55
OutputStream * GetStream() noexcept
テキストライタが書き込みを行うストリームを取得します。
Definition: BinaryWriter.h:111
#define NLIB_ALWAYS_INLINE
コンパイラに関数をインライン展開するように強く示します。
Definition: Platform_unix.h:97
リトルエンディアンで書き込みます。
Definition: BinaryWriter.h:30
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:194
bool WriteArray(const unsigned char *x, size_t n) noexcept
ストリームにバイナリでunsigned char型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も...
bool Write(float x) noexcept
ストリームにバイナリでfloat型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:58
errno_t GetErrorValue() const noexcept
書き込み等が失敗した際に、エラーの原因を取得できます。
Definition: BinaryWriter.h:110
bool Write(unsigned short x) noexcept
ストリームにバイナリでunsigned short型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:51
uint32_t nlib_ulong_compatible_t
unsigned longと互換性のある整数型がtypedefされています。
Definition: Platform.h:329
errno_t Init() noexcept
プログラムが動作しているマシンと同じエンディアンでバイナリライタを初期化します。
Definition: BinaryWriter.h:43
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:88
~BinaryWriter() noexcept
デストラクタです。ストリームをクローズしません。
Definition: BinaryWriter.h:41
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
bool Write(char x) noexcept
ストリームにバイナリでchar型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:47
ビッグエンディアンで書き込みます。
Definition: BinaryWriter.h:31
bool Write(BinaryWriter *w, T x)
この関数テンプレートを特殊化することで、ユーザー定義クラスを書きこむことができます。 ...
Definition: BinaryWriter.h:136
bool Write(unsigned int x) noexcept
ストリームにバイナリでunsigned int型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:53
bool Write(long long x) noexcept
ストリームにバイナリでlong long型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:56
bool Write(unsigned char x) noexcept
ストリームにバイナリでunsigned char型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:49
bool WriteArray(BinaryWriter *w, const T *x, size_t n)
この関数テンプレートを特殊化することで、ユーザー定義クラスを書きこむことができます。 ...
Definition: BinaryWriter.h:139
void SetError(errno_t e) noexcept
エラーを設定します。既にエラーが設定されている場合にはエラーは設定されません。
Definition: BinaryWriter.h:107
bool WriteArray(const long long *x, size_t n) noexcept
ストリームにバイナリでlong long型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も成功...
Definition: BinaryWriter.h:98
bool WriteArray(const long *x, size_t n) noexcept
ストリームにバイナリでlong型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も成功しま...
Definition: BinaryWriter.h:91
errno_tをラップするクラスです。Visual Studioのデバッガ上での表示を改善します。
Definition: Config.h:406
bool Write(long x) noexcept
ストリームにバイナリでlong型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:54
bool Write(int x) noexcept
ストリームにバイナリでint型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:52
bool Write(short x) noexcept
ストリームにバイナリでshort型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:50
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
開発環境別の設定が書かれるファイルです。
EndianSetting
エンディアンを指定します。
Definition: BinaryWriter.h:28
bool WriteArray(const unsigned long *x, size_t n) noexcept
ストリームにバイナリでunsigned long型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も...
Definition: BinaryWriter.h:69
bool WriteArray(const int *x, size_t n) noexcept
ストリームにバイナリでint型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も成功します...
Definition: BinaryWriter.h:88
ストリーム(OutputStream)にバイナリを書き込むクラスです。
Definition: BinaryWriter.h:26
bool Write(signed char x) noexcept
ストリームにバイナリでsigned char型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:48
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
bool WriteArray(const signed char *x, size_t n) noexcept
ストリームにバイナリでsigned char型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も成...
Definition: BinaryWriter.h:79
出力ストリームの基底クラスを定義しています。
bool WriteArray(const char *x, size_t n) noexcept
ストリームにバイナリでchar型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も成功しま...
Definition: BinaryWriter.h:82
BinaryWriter() noexcept
デフォルトコンストラクタです。
Definition: BinaryWriter.h:38
#define NLIB_NONNULL
全ての引数にNULLを指定することができないことを示します。
bool Write(double x) noexcept
ストリームにバイナリでdouble型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:59
出力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: OutputStream.h:30
bool Write(unsigned long long x) noexcept
ストリームにバイナリでunsigned long long型のデータを書き込みます。成功した場合はtrueを返します。 ...
Definition: BinaryWriter.h:57
bool WriteArray(const short *x, size_t n) noexcept
ストリームにバイナリでshort型の配列を書き込みます。成功した場合はtrueを返します。nが0の場合も成功しま...
Definition: BinaryWriter.h:85
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37