diff --git a/include/of_util.h b/include/of_util.h index e284d84..3a80369 100644 --- a/include/of_util.h +++ b/include/of_util.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include namespace ofen { template class OfSingleton @@ -29,4 +31,19 @@ private: template std::shared_ptr OfSingleton::instance = nullptr; template std::once_flag OfSingleton::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 buffer_; + std::mutex mutex_; +}; + } // namespace ofen diff --git a/src/of_util.cpp b/src/of_util.cpp index e282d86..55a5def 100644 --- a/src/of_util.cpp +++ b/src/of_util.cpp @@ -1 +1,37 @@ -#include "of_util.h" \ No newline at end of file +#include "of_util.h" + +namespace ofen { +void CMutBuffer::push(const char* data, int len) +{ + std::lock_guard lock(mutex_); + buffer_.insert(buffer_.end(), data, data + len); +} +int CMutBuffer::index_of(const char* data, int len, int start_pos) +{ + std::lock_guard lock(mutex_); + if (start_pos < 0 || start_pos >= static_cast(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 lock(mutex_); + if (start_pos < 0 || start_pos >= static_cast(buffer_.size()) || len <= 0) { + return; + } + + auto end_pos = std::min(start_pos + len, static_cast(buffer_.size())); + buffer_.erase(buffer_.begin() + start_pos, buffer_.begin() + end_pos); +} +void CMutBuffer::clear() +{ + std::lock_guard lock(mutex_); + buffer_.clear(); +} +} // namespace ofen