add:添加get_data接口。

This commit is contained in:
taynpg 2024-12-11 17:00:42 +08:00
parent 76c702cea8
commit 79e086bae5
2 changed files with 7 additions and 2 deletions

View File

@ -39,6 +39,7 @@ public:
public: public:
void push(const char* data, int len); void push(const char* data, int len);
int index_of(const char* data, int len, int start_pos = 0); int index_of(const char* data, int len, int start_pos = 0);
const char* get_data() const;
void remove_of(int start_pos, int len); void remove_of(int start_pos, int len);
void clear(); void clear();
private: private:

View File

@ -12,7 +12,6 @@ int CMutBuffer::index_of(const char* data, int len, int start_pos)
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) { if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
return -1; return -1;
} }
auto it = std::search(buffer_.begin() + start_pos, buffer_.end(), data, data + len); auto it = std::search(buffer_.begin() + start_pos, buffer_.end(), data, data + len);
if (it != buffer_.end()) { if (it != buffer_.end()) {
return std::distance(buffer_.begin(), it); return std::distance(buffer_.begin(), it);
@ -25,10 +24,15 @@ void CMutBuffer::remove_of(int start_pos, int len)
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) { if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
return; return;
} }
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size())); auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos); buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos);
} }
const char* CMutBuffer::get_data() const
{
return buffer_.data();
}
void CMutBuffer::clear() void CMutBuffer::clear()
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);