@@ -51,24 +51,11 @@ PBufWrapper::PBufWrapper(DataSize_t length)
51
51
}
52
52
}
53
53
54
- // TODO: Uses copy assignment. Improvement possible
55
- PBufWrapper::PBufWrapper (const PBufWrapper &other) { *this = other; }
56
-
57
54
// TODO: Uses move assignment. Improvement possible
58
55
PBufWrapper::PBufWrapper (PBufWrapper &&other) noexcept {
59
56
*this = std::move (other);
60
57
}
61
58
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
-
72
59
PBufWrapper &PBufWrapper::operator =(PBufWrapper &&other) noexcept {
73
60
copySimpleMembersAndResetBuffer (other);
74
61
@@ -88,26 +75,17 @@ void PBufWrapper::copySimpleMembersAndResetBuffer(const PBufWrapper &other) {
88
75
}
89
76
}
90
77
91
- PBufWrapper::~PBufWrapper () {
78
+ void PBufWrapper::destroy ()
79
+ {
92
80
if (firstElement != nullptr ) {
93
81
pbuf_free (firstElement);
82
+ firstElement = nullptr ;
94
83
}
84
+ m_freeSpace = 0 ;
95
85
}
96
86
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 ();
111
89
}
112
90
113
91
bool PBufWrapper::isValid () const { return firstElement != nullptr ; }
@@ -131,29 +109,24 @@ bool PBufWrapper::append(const uint8_t *data, DataSize_t length) {
131
109
if (err != ERR_OK) {
132
110
return false ;
133
111
}
134
-
135
112
m_freeSpace -= length;
136
113
return true ;
137
114
}
138
115
139
- void PBufWrapper::append (PBufWrapper &&other) {
140
- if (this == &other) {
141
- return ;
142
- }
116
+ void PBufWrapper::append (const PBufWrapper &other) {
143
117
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 );
145
121
return ;
146
122
}
147
123
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 );
153
126
}
154
127
155
128
bool PBufWrapper::reserve (DataSize_t length) {
156
- auto additionalAllocation = length - m_freeSpace;
129
+ int16_t additionalAllocation = length - m_freeSpace;
157
130
if (additionalAllocation <= 0 ) {
158
131
return true ;
159
132
}
0 commit comments