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
75 inline ByteBuffer(int length) : ByteBuffer(ufm::lang::Memory(length)) {
76 return;
77 }
78
85 inline ByteBuffer(void* pointer, int length)
86 : ByteBuffer(ufm::lang::Memory(pointer, length)) {
87 return;
88 }
89
93 virtual ~ByteBuffer(void) override;
94
95 /* *************************************************************************************
96 * Operator Method
97 */
98 public:
105 inline ByteBuffer& operator<<(char c) {
106 this->putByte(c);
107 return *this;
108 }
109
116 inline ByteBuffer& operator<<(short s) {
117 this->putShort(s);
118 return *this;
119 }
120
127 inline ByteBuffer& operator<<(int v) {
128 this->putInt(v);
129 return *this;
130 }
131
138 inline ByteBuffer& operator<<(const char* string) {
139 this->put(string);
140 return *this;
141 }
142
150 this->put(string);
151 return *this;
152 }
153
160 inline int operator=(int v) {
161 this->position(v);
162 return this->position();
163 }
164
170 inline void operator+=(int shift) {
171 this->position(this->position() + shift);
172 }
173
179 inline void operator-=(int shift) {
180 this->position(this->position() - shift);
181 }
182
188 inline uint32 operator++(void) {
189 this->position(this->position() + 1);
190 return static_cast<uint32>(this->position());
191 }
192
198 inline uint32 operator--(void) {
199 this->position(this->position() - 1);
200 return static_cast<uint32>(this->position());
201 }
202
208 inline uint32 operator++(int) {
209 uint32 result = static_cast<uint32>(this->position());
210 this->position(this->position() + 1);
211 return result;
212 }
213
219 inline uint32 operator--(int) {
220 uint32 result = static_cast<uint32>(this->position());
221 this->position(this->position() - 1);
222 return result;
223 }
224
225 /* *************************************************************************************
226 * Override Method - ufm::io::Buffer
227 */
228 public:
240 virtual void clear(void) override;
241
247 virtual ufm::lang::Readable& getReadable(void) override;
248
254 virtual ufm::lang::Appendable& getAppendable(void) override;
255
256 /* *************************************************************************************
257 * Override Method - ufm::lang::Readable
258 */
259 public:
264 virtual int avariable(void) const override;
265
272 virtual int pollByte(char& result) override;
273
279 virtual int poll(ufm::lang::Appendable& appendable) override;
280
287 virtual int poll(void* pointer, int length) override;
288
289 /* *************************************************************************************
290 * Override Method - ufm::lang::Iterable<const char>
291 */
292 public:
301 virtual void forEach(ufm::func::Consumer<const char&>& action) override;
302
309 virtual const char* elementAt(int index) const override;
310
319 virtual int nextIndex(int index) const override;
320
327
333 virtual ufm::util::Iterator<const char> end(void) override;
334
335 /* *************************************************************************************
336 * Override Method - ufm::lang::Appendable
337 */
338 public:
344 virtual int remaining(void) const override;
345
352 virtual int putByte(const char data) override;
353
362 virtual int put(ufm::lang::Readable& readable) override;
363
373 virtual int put(const void* pointer, int length) override;
374
375 /* *************************************************************************************
376 * Public Method
377 */
378 public:
384 inline int limit(void) const {
385 return this->vLimit;
386 }
387
393 inline int capacity(void) const {
394 return this->vMemory.length();
395 }
396
402 inline int position(void) const {
403 return this->vPosition;
404 }
405
411 inline ByteBuffer& reset(void) {
412 this->position(this->vMark);
413 return *this;
414 }
415
421 inline ByteBuffer& mark(void) {
422 this->vMark = vPosition;
423 return *this;
424 }
425
431 inline ByteBuffer& rewind(void) {
432 this->vPosition = 0;
433 this->vMark = 0;
434 return *this;
435 }
436
443 inline bool pollByte(uint8& result) {
444 return this->pollByte(reinterpret_cast<char&>(result));
445 }
446
453 inline bool pollShort(uint16& result) {
454 return this->pollShort(reinterpret_cast<short&>(result));
455 }
456
463 inline bool pollInt(uint32& result) {
464 return this->pollInt(reinterpret_cast<int&>(result));
465 }
466
472 inline ufm::lang::Memory& memory(void) {
473 return this->vMemory;
474 }
475
482 bool limit(int newLimit);
483
490 bool position(int newPosition);
491
498 void flip(void);
499
507 void move(int position);
508
515 bool put(const char* string);
516
523 bool put(const ufm::lang::Strings& string);
524
532 int putFormat(const char* format, ...);
533
541 int putFormat(const char* format, va_list args);
542
549 bool putShort(const short value);
550
557 bool putInt(const int value);
558
565 bool putFloat(const float value);
566
573 bool pollShort(short& result);
574
581 bool pollInt(int& result);
582
589 bool pollFloat(float& result);
590
591 /* *************************************************************************************
592 * Protected Method
593 */
594
595 /* *************************************************************************************
596 * Private Method
597 */
598
599 /* *************************************************************************************
600 * Static Variable
601 */
602
603 /* *************************************************************************************
604 * Static Method
605 */
606};
607
608/* ***************************************************************************************
609 * End of file
610 */
611
612#endif /* MFRAME_C76BAF3F_EE1F_46A3_A050_9CD1057055E5 */
靈活高效的位元組緩衝區實現
Definition ByteBuffer.h:45
bool position(int newPosition)
設定 ByteBuffer 的位置。
int operator=(int v)
重載運算子 =,設定 ByteBuffer 的位置。
Definition ByteBuffer.h:160
bool put(const char *string)
將字串寫入 ByteBuffer。
virtual int putByte(const char data) override
輸入單一字節至緩衝區
ByteBuffer & rewind(void)
將 ByteBuffer 的位置重置為 0。
Definition ByteBuffer.h:431
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:105
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:149
bool pollFloat(float &result)
從 ByteBuffer 中提取一個 float。
bool putInt(const int value)
將 int 資料寫入 ByteBuffer。
int limit(void) const
獲取 ByteBuffer 的限制。
Definition ByteBuffer.h:384
void move(int position)
將位置移動到指定位置
bool pollInt(uint32 &result)
從 ByteBuffer 中提取一個 int。
Definition ByteBuffer.h:463
uint32 operator--(int)
重載運算子 –,將 ByteBuffer 的位置向後移動一位(後置)。
Definition ByteBuffer.h:219
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:116
ByteBuffer & reset(void)
重置 ByteBuffer 的位置到標記。
Definition ByteBuffer.h:411
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:170
virtual int put(const void *pointer, int length) override
從記憶體指標附加資料至緩衝區
uint32 operator--(void)
重載運算子 –,將 ByteBuffer 的位置向後移動一位。
Definition ByteBuffer.h:198
virtual void forEach(ufm::func::Consumer< const char & > &action) override
遍歷集合中所有元素,對每個元素執行指定操作。 若所有元素處理完畢或操作中發生異常則停止。
ByteBuffer(ufm::lang::Memory memory) noexcept
使用現有記憶體建立位元組緩衝區
ByteBuffer & mark(void)
標記 ByteBuffer 的當前位置。
Definition ByteBuffer.h:421
bool pollShort(uint16 &result)
從 ByteBuffer 中提取一個 short。
Definition ByteBuffer.h:453
bool putShort(const short value)
將 short 資料寫入 ByteBuffer。
ByteBuffer & operator<<(int v)
重載運算子 <<,將 int 資料寫入 ByteBuffer。
Definition ByteBuffer.h:127
uint32 operator++(int)
重載運算子 ++,將 ByteBuffer 的位置向前移動一位(後置)。
Definition ByteBuffer.h:208
int capacity(void) const
獲取 ByteBuffer 的容量。
Definition ByteBuffer.h:393
bool pollByte(uint8 &result)
從 ByteBuffer 中提取一個 byte。
Definition ByteBuffer.h:443
virtual int pollByte(char &result) override
讀取一個字節並將其從緩衝區移除
uint32 operator++(void)
重載運算子 ++,將 ByteBuffer 的位置向前移動一位。
Definition ByteBuffer.h:188
int putFormat(const char *format,...)
將格式化字串寫入 ByteBuffer。
ufm::lang::Memory & memory(void)
獲取 ByteBuffer 的記憶體。
Definition ByteBuffer.h:472
int position(void) const
獲取 ByteBuffer 的位置。
Definition ByteBuffer.h:402
ByteBuffer(int length)
建立指定大小的位元組緩衝區
Definition ByteBuffer.h:75
virtual int nextIndex(int index) const override
返回當前索引的下一個有效索引。
virtual int put(ufm::lang::Readable &readable) override
從 Readable 物件附加資料至緩衝區
ByteBuffer(void *pointer, int length)
使用常數指標建立位元組緩衝區
Definition ByteBuffer.h:85
bool put(const ufm::lang::Strings &string)
將 ufm::lang::Strings 物件寫入 ByteBuffer。
void operator-=(int shift)
重載運算子 -=,將 ByteBuffer 的位置向後移動指定的位移量。
Definition ByteBuffer.h:179
ByteBuffer & operator<<(const char *string)
重載運算子 <<,將字串寫入 ByteBuffer。
Definition ByteBuffer.h:138
int length(void) const
取得資料有效長度
Definition Data.h:139
存儲資料的類別,提供動態記憶體管理功能。
Definition Memory.h:44
物件基底類別
Definition Object.h:63
字串類別,提供字串操作和記憶體管理功能
Definition Strings.h:33
[Class] 迭代器介面
Definition Iterator.h:42
Definition Buffer.h:24
[Interface] 消費者函數式介面模板
Definition Consumer.h:43
[Interface] 緩衝區基礎介面,定義資料存取的核心功能
Definition Buffer.h:41
資料附加介面 (Appendable)
Definition Appendable.h:43
[Interface] 定義資料讀取介面
Definition Readable.h:46