@@ -51,24 +51,11 @@ PBufWrapper::PBufWrapper(DataSize_t length)
5151 }
5252}
5353
54- // TODO: Uses copy assignment. Improvement possible
55- PBufWrapper::PBufWrapper (const PBufWrapper &other) { *this = other; }
56-
5754// TODO: Uses move assignment. Improvement possible
5855PBufWrapper::PBufWrapper (PBufWrapper &&other) noexcept {
5956 *this = std::move (other);
6057}
6158
62- PBufWrapper &PBufWrapper::operator =(const PBufWrapper &other) {
63- copySimpleMembersAndResetBuffer (other);
64-
65- if (other.firstElement != nullptr ) {
66- pbuf_ref (other.firstElement );
67- }
68- firstElement = other.firstElement ;
69- return *this ;
70- }
71-
7259PBufWrapper &PBufWrapper::operator =(PBufWrapper &&other) noexcept {
7360 copySimpleMembersAndResetBuffer (other);
7461
@@ -88,26 +75,17 @@ void PBufWrapper::copySimpleMembersAndResetBuffer(const PBufWrapper &other) {
8875 }
8976}
9077
91- PBufWrapper::~PBufWrapper () {
78+ void PBufWrapper::destroy ()
79+ {
9280 if (firstElement != nullptr ) {
9381 pbuf_free (firstElement);
82+ firstElement = nullptr ;
9483 }
84+ m_freeSpace = 0 ;
9585}
9686
97- PBufWrapper PBufWrapper::deepCopy () const {
98- PBufWrapper clone;
99- clone.copySimpleMembersAndResetBuffer (*this );
100-
101- // Decided not to use pbuf_clone because it prevents const
102- clone.firstElement = pbuf_alloc (m_layer, this ->firstElement ->tot_len , m_type);
103- if (clone.firstElement != nullptr ) {
104- if (pbuf_copy (clone.firstElement , this ->firstElement ) != ERR_OK) {
105- PBUF_WRAP_LOG (" PBufWrapper::deepCopy: Copy of pbuf failed" );
106- }
107- } else {
108- clone.m_freeSpace = 0 ;
109- }
110- return clone;
87+ PBufWrapper::~PBufWrapper () {
88+ destroy ();
11189}
11290
11391bool PBufWrapper::isValid () const { return firstElement != nullptr ; }
@@ -131,29 +109,24 @@ bool PBufWrapper::append(const uint8_t *data, DataSize_t length) {
131109 if (err != ERR_OK) {
132110 return false ;
133111 }
134-
135112 m_freeSpace -= length;
136113 return true ;
137114}
138115
139- void PBufWrapper::append (PBufWrapper &&other) {
140- if (this == &other) {
141- return ;
142- }
116+ void PBufWrapper::append (const PBufWrapper &other) {
143117 if (this ->firstElement == nullptr ) {
144- *this = std::move (other);
118+ m_freeSpace = other.m_freeSpace ;
119+ this ->firstElement = other.firstElement ;
120+ pbuf_ref (this ->firstElement );
145121 return ;
146122 }
147123
148- m_freeSpace = other.m_freeSpace ;
149- pbuf *const newElement = other.firstElement ;
150- pbuf_cat (this ->firstElement , newElement);
151-
152- other.firstElement = nullptr ;
124+ m_freeSpace += other.m_freeSpace ;
125+ pbuf_chain (this ->firstElement , other.firstElement );
153126}
154127
155128bool PBufWrapper::reserve (DataSize_t length) {
156- auto additionalAllocation = length - m_freeSpace;
129+ int16_t additionalAllocation = length - m_freeSpace;
157130 if (additionalAllocation <= 0 ) {
158131 return true ;
159132 }
0 commit comments