1-
2-
31#ifndef __BYTEBUFFER_H__
42#define __BYTEBUFFER_H__
53
64#include < stdlib.h>
75#include < stdint.h>
86#include < string.h>
9- #include < errno.h >
7+ #include < cstring >
108
119#include < string>
1210#include < iostream>
1311
1412// Default size of the buffer
15- # define DEFAULT_BUFFER_SIZE 2048
13+ constexpr uint32_t DEFAULT_BUFFER_SIZE = 2048 ;
1614
1715class ByteBuffer
1816{
1917public:
18+ // 禁用拷贝构造函数和赋值运算符,防止浅拷贝导致的双重释放问题
19+ ByteBuffer (const ByteBuffer&) = delete ;
20+ ByteBuffer& operator =(const ByteBuffer&) = delete ;
21+
22+ // 允许移动语义
23+ ByteBuffer (ByteBuffer&& other) noexcept
24+ : mark_(other.mark_),
25+ limit_ (other.limit_),
26+ position_(other.position_),
27+ capacity_(other.capacity_),
28+ name_(std::move(other.name_)),
29+ p_buffer_(other.p_buffer_)
30+ {
31+ other.p_buffer_ = nullptr ;
32+ other.mark_ = -1 ;
33+ other.limit_ = 0 ;
34+ other.position_ = 0 ;
35+ other.capacity_ = 0 ;
36+ }
37+
38+ ByteBuffer& operator =(ByteBuffer&& other) noexcept
39+ {
40+ if (this != &other)
41+ {
42+ if (p_buffer_)
43+ {
44+ free (p_buffer_);
45+ }
46+ mark_ = other.mark_ ;
47+ limit_ = other.limit_ ;
48+ position_ = other.position_ ;
49+ capacity_ = other.capacity_ ;
50+ name_ = std::move (other.name_ );
51+ p_buffer_ = other.p_buffer_ ;
52+ other.p_buffer_ = nullptr ;
53+ other.mark_ = -1 ;
54+ other.limit_ = 0 ;
55+ other.position_ = 0 ;
56+ other.capacity_ = 0 ;
57+ }
58+ return *this ;
59+ }
60+
2061 ByteBuffer (uint32_t capacity = DEFAULT_BUFFER_SIZE , const char * name = " " )
2162 : mark_(-1 ),
22- limit_ (capacity),
23- position_(0 ),
24- capacity_(capacity),
25- name_(name)
63+ limit_ (capacity),
64+ position_(0 ),
65+ capacity_(capacity),
66+ name_(name)
2667 {
27- p_buffer_ = NULL ;
28- p_buffer_ = (uint8_t *)calloc (capacity_, sizeof (uint8_t ));
68+ p_buffer_ = static_cast <uint8_t *>(calloc (capacity_, sizeof (uint8_t )));
2969 }
3070
3171 ByteBuffer (uint8_t * arr, uint32_t length, const char * name = " " )
3272 : mark_(-1 ),
33- limit_(length),
34- position_(0 ),
35- capacity_(length),
36- name_(name)
73+ limit_(length),
74+ position_(0 ),
75+ capacity_(length),
76+ name_(name)
3777 {
38- p_buffer_ = NULL ;
39- p_buffer_ = (uint8_t *)calloc (capacity_, sizeof (uint8_t ));
78+ p_buffer_ = static_cast <uint8_t *>(calloc (capacity_, sizeof (uint8_t )));
4079
4180 putBytes (arr, capacity_);
4281 clear ();
4382 }
83+
4484 ~ByteBuffer ()
4585 {
4686 if (p_buffer_)
4787 {
4888 free (p_buffer_);
49- p_buffer_ = NULL ;
89+ p_buffer_ = nullptr ;
5090 }
5191 }
5292
@@ -307,7 +347,7 @@ class ByteBuffer
307347 }
308348
309349
310- bool hasRemaining ()
350+ bool hasRemaining () const
311351 {
312352 return limit_ > position_;
313353 }
@@ -384,7 +424,7 @@ class ByteBuffer
384424 uint32_t s = sizeof (data);
385425 checkSize (s);
386426
387- memcpy (&p_buffer_[position_], ( uint8_t *) &data, s);
427+ memcpy (&p_buffer_[position_], reinterpret_cast < uint8_t *>( &data) , s);
388428 position_ += s;
389429 }
390430
@@ -411,7 +451,7 @@ class ByteBuffer
411451
412452 uint32_t newSize = capacity_ + (increase + BUFFER_SIZE_INCREASE - 1 ) /
413453 BUFFER_SIZE_INCREASE * BUFFER_SIZE_INCREASE ;
414- uint8_t * pBuf = ( uint8_t *) realloc (p_buffer_, newSize);
454+ uint8_t * pBuf = static_cast < uint8_t *>( realloc (p_buffer_, newSize) );
415455 if (!pBuf)
416456 {
417457 std::cout << " relloc failed!" << std::endl;
0 commit comments