mFrame
載入中...
搜尋中...
無符合項目
ByteBuffer.h
1
8#ifndef MFRAME_C76BAF3F_EE1F_46A3_A050_9CD1057055E5
9#define MFRAME_C76BAF3F_EE1F_46A3_A050_9CD1057055E5
10
11/* ***************************************************************************************
12 * Include
13 */
14
15//----------------------------------------------------------------------------------------
16
17//----------------------------------------------------------------------------------------
18#include "./../io/Buffer.h"
19#include "./../lang/Memory.h"
20#include "./../lang/Strings.h"
21
22/* ***************************************************************************************
23 * Namespace
24 */
25namespace ufm::io {
26 class ByteBuffer;
27} // namespace ufm::io
28
29/* ***************************************************************************************
30 * Class/Interface/Struct
31 */
32
45 public ufm::io::Buffer {
46 /* *************************************************************************************
47 * Variable
48 */
49 protected:
50 ufm::lang::Memory vMemory;
51 int vPosition;
52 int vMark;
53 int vLimit;
54
55 /* *************************************************************************************
56 * Abstract Method
57 */
58
59 /* *************************************************************************************
60 * Construct Method
61 */
62 public:
69
76 inline ByteBuffer(void* pointer, int length)
77 : ByteBuffer(ufm::lang::Memory(pointer, length)) {
78 return;
79 }
80
84 virtual ~ByteBuffer(void) override;
85
86 /* *************************************************************************************
87 * Operator Method
88 */
89 public:
96 inline ByteBuffer& operator<<(char c) {
97 this->putByte(c);
98 return *this;
99 }
100
107 inline ByteBuffer& operator<<(short s) {
108 this->putShort(s);
109 return *this;
110 }
111
118 inline ByteBuffer& operator<<(int v) {
119 this->putInt(v);
120 return *this;
121 }
122
129 inline ByteBuffer& operator<<(const char* string) {
130 this->put(string);
131 return *this;
132 }
133
141 this->put(string);
142 return *this;
143 }
144
151 inline int operator=(int v) {
152 this->position(v);
153 return this->position();
154 }
155
161 inline void operator+=(int shift) {
162 this->position(this->position() + shift);
163 }
164
170 inline void operator-=(int shift) {
171 this->position(this->position() - shift);
172 }
173
179 inline uint32 operator++(void) {
180 this->position(this->position() + 1);
181 return static_cast<uint32>(this->position());
182 }
183
189 inline uint32 operator--(void) {
190 this->position(this->position() - 1);
191 return static_cast<uint32>(this->position());
192 }
193
199 inline uint32 operator++(int) {
200 uint32 result = static_cast<uint32>(this->position());
201 this->position(this->position() + 1);
202 return result;
203 }
204
210 inline uint32 operator--(int) {
211 uint32 result = static_cast<uint32>(this->position());
212 this->position(this->position() - 1);
213 return result;
214 }
215
216 /* *************************************************************************************
217 * Override Method - ufm::io::Buffer
218 */
219 public:
231 virtual void clear(void) override;
232
238 virtual ufm::lang::Readable& getReadable(void) override;
239
245 virtual ufm::lang::Appendable& getAppendable(void) override;
246
247 /* *************************************************************************************
248 * Override Method - ufm::lang::Readable
249 */
250 public:
255 virtual int avariable(void) const override;
256
263 virtual int pollByte(char& result) override;
264
270 virtual int poll(ufm::lang::Appendable& appendable) override;
271
278 virtual int poll(void* pointer, int length) override;
279
280 /* *************************************************************************************
281 * Override Method - ufm::lang::Iterable<const char>
282 */
283 public:
292 virtual void forEach(ufm::func::Consumer<const char&>& action) override;
293
300 virtual const char* elementAt(int index) const override;
301
310 virtual int nextIndex(int index) const override;
311
318
324 virtual ufm::util::Iterator<const char> end(void) override;
325
326 /* *************************************************************************************
327 * Override Method - ufm::lang::Appendable
328 */
329 public:
335 virtual int remaining(void) const override;
336
343 virtual int putByte(const char data) override;
344
353 virtual int put(ufm::lang::Readable& readable) override;
354
364 virtual int put(const void* pointer, int length) override;
365
366 /* *************************************************************************************
367 * Public Method
368 */
369 public:
375 inline int limit(void) const {
376 return this->vLimit;
377 }
378
384 inline int capacity(void) const {
385 return this->vMemory.length();
386 }
387
393 inline int position(void) const {
394 return this->vPosition;
395 }
396
402 inline ByteBuffer& reset(void) {
403 this->position(this->vMark);
404 return *this;
405 }
406
412 inline ByteBuffer& mark(void) {
413 this->vMark = vPosition;
414 return *this;
415 }
416
422 inline ByteBuffer& rewind(void) {
423 this->vPosition = 0;
424 this->vMark = 0;
425 return *this;
426 }
427
434 inline bool pollByte(uint8& result) {
435 return this->pollByte(reinterpret_cast<char&>(result));
436 }
437
444 inline bool pollShort(uint16& result) {
445 return this->pollShort(reinterpret_cast<short&>(result));
446 }
447
454 inline bool pollInt(uint32& result) {
455 return this->pollInt(reinterpret_cast<int&>(result));
456 }
457
463 inline ufm::lang::Memory& memory(void) {
464 return this->vMemory;
465 }
466
473 bool limit(int newLimit);
474
481 bool position(int newPosition);
482
489 void flip(void);
490
498 void move(int position);
499
506 bool put(const char* string);
507
514 bool put(const ufm::lang::Strings& string);
515
523 int putFormat(const char* format, ...);
524
532 int putFormat(const char* format, va_list args);
533
540 bool putShort(const short value);
541
548 bool putInt(const int value);
549
556 bool putFloat(const float value);
557
564 bool pollShort(short& result);
565
572 bool pollInt(int& result);
573
580 bool pollFloat(float& result);
581
582 /* *************************************************************************************
583 * Protected Method
584 */
585
586 /* *************************************************************************************
587 * Private Method
588 */
589
590 /* *************************************************************************************
591 * Static Variable
592 */
593
594 /* *************************************************************************************
595 * Static Method
596 */
597};
598
599/* ***************************************************************************************
600 * End of file
601 */
602
603#endif /* MFRAME_C76BAF3F_EE1F_46A3_A050_9CD1057055E5 */
靈活高效的位元組緩衝區實現
Definition ByteBuffer.h:45
bool position(int newPosition)
設定 ByteBuffer 的位置。
int operator=(int v)
重載運算子 =,設定 ByteBuffer 的位置。
Definition ByteBuffer.h:151
bool put(const char *string)
將字串寫入 ByteBuffer。
virtual int putByte(const char data) override
輸入單一字節至緩衝區
ByteBuffer & rewind(void)
將 ByteBuffer 的位置重置為 0。
Definition ByteBuffer.h:422
virtual ufm::util::Iterator< const char > begin(void) override
返回一個迭代器,用於遍歷集合中的元素
bool pollShort(short &result)
從 ByteBuffer 中提取一個 short。
virtual void clear(void) override
清空緩衝區內容
ByteBuffer & operator<<(char c)
重載運算子 <<,將 char 資料寫入 ByteBuffer。
Definition ByteBuffer.h:96
bool limit(int newLimit)
設定 ByteBuffer 的限制。
virtual int avariable(void) const override
取得可讀取的資料數量
bool putFloat(const float value)
將 float 資料寫入 ByteBuffer。
virtual ufm::lang::Readable & getReadable(void) override
獲取 ByteBuffer 的可讀物件。
virtual int poll(void *pointer, int length) override
從緩衝區讀取指定長度的資料到指定的記憶體位置
ByteBuffer & operator<<(ufm::lang::Strings &string)
重載運算子 <<,將 ufm::lang::Strings 物件寫入 ByteBuffer。
Definition ByteBuffer.h:140
bool pollFloat(float &result)
從 ByteBuffer 中提取一個 float。
bool putInt(const int value)
將 int 資料寫入 ByteBuffer。
int limit(void) const
獲取 ByteBuffer 的限制。
Definition ByteBuffer.h:375
void move(int position)
將位置移動到指定位置
bool pollInt(uint32 &result)
從 ByteBuffer 中提取一個 int。
Definition ByteBuffer.h:454
uint32 operator--(int)
重載運算子 –,將 ByteBuffer 的位置向後移動一位(後置)。
Definition ByteBuffer.h:210
virtual ufm::util::Iterator< const char > end(void) override
返回一個迭代器,用於遍歷集合的結尾
virtual int poll(ufm::lang::Appendable &appendable) override
從緩衝區讀取資料並寫入到Appendable物件中
virtual ufm::lang::Appendable & getAppendable(void) override
獲取 ByteBuffer 的可追加物件。
ByteBuffer & operator<<(short s)
重載運算子 <<,將 short 資料寫入 ByteBuffer。
Definition ByteBuffer.h:107
ByteBuffer & reset(void)
重置 ByteBuffer 的位置到標記。
Definition ByteBuffer.h:402
virtual const char * elementAt(int index) const override
獲取集合中指定索引處的元素。
int putFormat(const char *format, va_list args)
將格式化字串寫入 ByteBuffer。
void flip(void)
翻轉緩衝區,為讀取已寫入資料做準備
virtual int remaining(void) const override
取得緩衝區剩餘空閒字節數。
virtual ~ByteBuffer(void) override
釋放緩衝區資源
bool pollInt(int &result)
從 ByteBuffer 中提取一個 int。
void operator+=(int shift)
重載運算子 +=,將 ByteBuffer 的位置向前移動指定的位移量。
Definition ByteBuffer.h:161
virtual int put(const void *pointer, int length) override
從記憶體指標附加資料至緩衝區
uint32 operator--(void)
重載運算子 –,將 ByteBuffer 的位置向後移動一位。
Definition ByteBuffer.h:189
virtual void forEach(ufm::func::Consumer< const char & > &action) override
遍歷集合中所有元素,對每個元素執行指定操作。 若所有元素處理完畢或操作中發生異常則停止。
ByteBuffer(ufm::lang::Memory memory) noexcept
使用現有記憶體建立位元組緩衝區
ByteBuffer & mark(void)
標記 ByteBuffer 的當前位置。
Definition ByteBuffer.h:412
bool pollShort(uint16 &result)
從 ByteBuffer 中提取一個 short。
Definition ByteBuffer.h:444
bool putShort(const short value)
將 short 資料寫入 ByteBuffer。
ByteBuffer & operator<<(int v)
重載運算子 <<,將 int 資料寫入 ByteBuffer。
Definition ByteBuffer.h:118
uint32 operator++(int)
重載運算子 ++,將 ByteBuffer 的位置向前移動一位(後置)。
Definition ByteBuffer.h:199
int capacity(void) const
獲取 ByteBuffer 的容量。
Definition ByteBuffer.h:384
bool pollByte(uint8 &result)
從 ByteBuffer 中提取一個 byte。
Definition ByteBuffer.h:434
virtual int pollByte(char &result) override
讀取一個字節並將其從緩衝區移除
uint32 operator++(void)
重載運算子 ++,將 ByteBuffer 的位置向前移動一位。
Definition ByteBuffer.h:179
int putFormat(const char *format,...)
將格式化字串寫入 ByteBuffer。
ufm::lang::Memory & memory(void)
獲取 ByteBuffer 的記憶體。
Definition ByteBuffer.h:463
int position(void) const
獲取 ByteBuffer 的位置。
Definition ByteBuffer.h:393
virtual int nextIndex(int index) const override
返回當前索引的下一個有效索引。
virtual int put(ufm::lang::Readable &readable) override
從 Readable 物件附加資料至緩衝區
ByteBuffer(void *pointer, int length)
使用常數指標建立位元組緩衝區
Definition ByteBuffer.h:76
bool put(const ufm::lang::Strings &string)
將 ufm::lang::Strings 物件寫入 ByteBuffer。
void operator-=(int shift)
重載運算子 -=,將 ByteBuffer 的位置向後移動指定的位移量。
Definition ByteBuffer.h:170
ByteBuffer & operator<<(const char *string)
重載運算子 <<,將字串寫入 ByteBuffer。
Definition ByteBuffer.h:129
int length(void) const
取得資料長度
Definition Data.h:153
動態記憶體管理類別
Definition Memory.h:38
物件基底類別
Definition Object.h:63
字串類別,提供字串操作和記憶體管理功能
Definition Strings.h:33
迭代器類別,提供遍歷集合元素的標準介面。
Definition Iterator.h:257
Definition Buffer.h:24
[Interface] 消費者函數式介面模板
Definition Consumer.h:43
[Interface] 緩衝區基礎介面,定義資料存取的核心功能
Definition Buffer.h:41
資料附加介面 (Appendable)
Definition Appendable.h:43
[Interface] 定義資料讀取介面
Definition Readable.h:46