add:添加一个mut buffer。
This commit is contained in:
parent
428aae1b10
commit
76c702cea8
@ -4,6 +4,8 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace ofen {
|
||||
template <typename T> class OfSingleton
|
||||
@ -29,4 +31,19 @@ private:
|
||||
|
||||
template <typename T> std::shared_ptr<T> OfSingleton<T>::instance = nullptr;
|
||||
template <typename T> std::once_flag OfSingleton<T>::init_flag;
|
||||
|
||||
class CMutBuffer
|
||||
{
|
||||
public:
|
||||
CMutBuffer() = default;
|
||||
public:
|
||||
void push(const char* data, int len);
|
||||
int index_of(const char* data, int len, int start_pos = 0);
|
||||
void remove_of(int start_pos, int len);
|
||||
void clear();
|
||||
private:
|
||||
std::vector<char> buffer_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace ofen
|
||||
|
@ -1 +1,37 @@
|
||||
#include "of_util.h"
|
||||
#include "of_util.h"
|
||||
|
||||
namespace ofen {
|
||||
void CMutBuffer::push(const char* data, int len)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
buffer_.insert(buffer_.end(), data, data + len);
|
||||
}
|
||||
int CMutBuffer::index_of(const char* data, int len, int start_pos)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto it = std::search(buffer_.begin() + start_pos, buffer_.end(), data, data + len);
|
||||
if (it != buffer_.end()) {
|
||||
return std::distance(buffer_.begin(), it);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
void CMutBuffer::remove_of(int start_pos, int len)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (start_pos < 0 || start_pos >= static_cast<int>(buffer_.size()) || len <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto end_pos = std::min(start_pos + len, static_cast<int>(buffer_.size()));
|
||||
buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos);
|
||||
}
|
||||
void CMutBuffer::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
buffer_.clear();
|
||||
}
|
||||
} // namespace ofen
|
||||
|
Loading…
x
Reference in New Issue
Block a user