MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_byte_stream.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_BYTE_STREAM_H_1510422583
12#define INCGUARD_MGL_BYTE_STREAM_H_1510422583
13
14#include <cstdint>
15#include <cstdlib>
16#include <type_traits>
17
18namespace MGL
19{
22{
23public:
24 /* ------------------------------------------------------------------------- */
28 /* ------------------------------------------------------------------------- */
29 constexpr ByteStream() noexcept
30 : _address(nullptr)
31 , _size(0)
32 , _offset(0)
33 , _isOverflowed(false)
34 , _isReadOnly(false)
35 {
36 }
37
38 /* ------------------------------------------------------------------------- */
44 /* ------------------------------------------------------------------------- */
45 constexpr ByteStream(void *address, size_t size) noexcept
46 : _address(address)
47 , _size(size)
48 , _offset(0)
49 , _isOverflowed(false)
50 , _isReadOnly(false)
51 {
52 }
53
54 /* ------------------------------------------------------------------------- */
60 /* ------------------------------------------------------------------------- */
61 constexpr ByteStream(const void *address, size_t size) noexcept
62 : _address(const_cast<void *>(address))
63 , _size(size)
64 , _offset(0)
65 , _isOverflowed(false)
66 , _isReadOnly(true)
67 {
68 }
69
71 enum class ClearType : uint8_t
72 {
73 Zero,
74 Random,
75 };
76
77 void SetBuffer(void *address, size_t size) noexcept;
78 void SetBuffer(const void *address, size_t size) noexcept;
79 bool SetOffset(size_t offset) noexcept;
80
81 /* ------------------------------------------------------------------------- */
86 /* ------------------------------------------------------------------------- */
87 [[nodiscard]] constexpr size_t GetOffset() const noexcept { return _offset; };
88
89 /* ------------------------------------------------------------------------- */
94 /* ------------------------------------------------------------------------- */
95 [[nodiscard]] constexpr size_t GetSize() const noexcept { return _size; };
96
97 bool Clear(ClearType clearType) noexcept;
98
99 /* ------------------------------------------------------------------------- */
107 /* ------------------------------------------------------------------------- */
108 [[nodiscard]] constexpr void *GetCurrentPoint() const noexcept
109 {
110 if (_isReadOnly)
111 {
112 return nullptr;
113 }
114
115 return &static_cast<uint8_t *>(_address)[_offset];
116 }
117
118 /* ------------------------------------------------------------------------- */
123 /* ------------------------------------------------------------------------- */
124 [[nodiscard]] constexpr const void *GetCurrentConstPoint() const noexcept
125 {
126 return &static_cast<const uint8_t *>(_address)[_offset];
127 }
128
129 bool Read(void *dest, size_t size) noexcept;
130 bool Write(const void *source, size_t size) noexcept;
131
132 /* ------------------------------------------------------------------------- */
140 /* ------------------------------------------------------------------------- */
141 template<typename T>
142 constexpr bool Read(T &value) noexcept
143 {
144 if constexpr (std::is_same<bool, T>::value)
145 {
146 return ReadBool(value);
147 }
148 else
149 {
150 return Read(&value, sizeof(T));
151 }
152 }
153
154 /* ------------------------------------------------------------------------- */
162 /* ------------------------------------------------------------------------- */
163 template<typename T>
164 constexpr T Read() noexcept
165 {
166 T value = T();
167 if constexpr (std::is_same<bool, T>::value)
168 {
169 ReadBool(&value);
170 }
171 else
172 {
173 Read(&value, sizeof(T));
174 }
175 return value;
176 }
177
178 /* ------------------------------------------------------------------------- */
186 /* ------------------------------------------------------------------------- */
187 template<typename T>
188 constexpr bool Write(T value) noexcept
189 {
190 if constexpr (std::is_same<bool, T>::value)
191 {
192 return WriteBool(value);
193 }
194 else
195 {
196 return Write(&value, sizeof(T));
197 }
198 }
199
200 /* ------------------------------------------------------------------------- */
206 /* ------------------------------------------------------------------------- */
207 [[nodiscard]] constexpr bool IsOverflowed() const noexcept
208 {
209 return _isOverflowed;
210 }
211
212
213 bool ReadBool(bool &flag) noexcept;
214 bool ReadBool() noexcept;
215 bool WriteBool(bool flag) noexcept;
216
217 bool Fill(uint8_t value, size_t size) noexcept;
218 bool Skip(size_t size) noexcept;
219
220private:
221 void *_address;
222 size_t _size;
223 size_t _offset;
224 bool _isOverflowed;
225 bool _isReadOnly;
226};
227} // namespace MGL
228
229#endif // INCGUARD_MGL_BYTE_STREAM_H_1510422583
230
231// vim: et ts=4 sw=4 sts=4
バイトデータストリームクラス
Definition mgl_byte_stream.h:22
bool SetOffset(size_t offset) noexcept
オフセットの設定
Definition mgl_byte_stream.cc:60
bool Write(const void *source, size_t size) noexcept
書き込み
Definition mgl_byte_stream.cc:162
constexpr ByteStream(const void *address, size_t size) noexcept
コンストラクタ(読み込み専用)
Definition mgl_byte_stream.h:61
constexpr ByteStream(void *address, size_t size) noexcept
コンストラクタ
Definition mgl_byte_stream.h:45
constexpr ByteStream() noexcept
コンストラクタ
Definition mgl_byte_stream.h:29
bool Skip(size_t size) noexcept
ストリームのスキップ
Definition mgl_byte_stream.cc:281
constexpr size_t GetSize() const noexcept
バッファサイズの取得
Definition mgl_byte_stream.h:95
constexpr size_t GetOffset() const noexcept
オフセットの取得
Definition mgl_byte_stream.h:87
bool Clear(ClearType clearType) noexcept
データのクリア
Definition mgl_byte_stream.cc:83
constexpr bool Read(T &value) noexcept
値の読み込み
Definition mgl_byte_stream.h:142
ClearType
バッファクリア時の動作タイプ
Definition mgl_byte_stream.h:72
@ Random
ランダム初期化
constexpr T Read() noexcept
型を指定して値を読み込み
Definition mgl_byte_stream.h:164
void SetBuffer(void *address, size_t size) noexcept
バッファの設定
Definition mgl_byte_stream.cc:25
constexpr bool IsOverflowed() const noexcept
オーバーフローしているかを返す
Definition mgl_byte_stream.h:207
constexpr const void * GetCurrentConstPoint() const noexcept
現在の先頭位置を取得(const版)
Definition mgl_byte_stream.h:124
bool ReadBool() noexcept
論理値の読み込み
Definition mgl_byte_stream.cc:212
constexpr bool Write(T value) noexcept
値の書き込み
Definition mgl_byte_stream.h:188
bool WriteBool(bool flag) noexcept
論理値の書き込み
Definition mgl_byte_stream.cc:229
constexpr void * GetCurrentPoint() const noexcept
現在の先頭位置の取得
Definition mgl_byte_stream.h:108
bool Fill(uint8_t value, size_t size) noexcept
バッファを任意の値で埋める
Definition mgl_byte_stream.cc:251