ofen/include/of_util.h

53 lines
1.2 KiB
C
Raw Normal View History

2024-12-09 23:29:42 +08:00
#pragma once
2024-12-07 22:31:27 +08:00
#include "of_def.hpp"
#include <cassert>
#include <iostream>
#include <memory>
#include <mutex>
2024-12-11 22:49:46 +08:00
#include <vector>
2024-12-11 10:21:57 +08:00
#include <algorithm>
#include <cstring>
2024-12-07 22:31:27 +08:00
namespace ofen {
template <typename T> class OfSingleton
{
public:
OfSingleton(const OfSingleton&) = delete;
OfSingleton& operator=(const OfSingleton&) = delete;
static std::shared_ptr<T> getInstance()
{
std::call_once(init_flag, []() { instance.reset(new T()); });
return instance;
}
protected:
OfSingleton() = default;
virtual ~OfSingleton() = default;
private:
static std::shared_ptr<T> instance;
static std::once_flag init_flag;
};
template <typename T> std::shared_ptr<T> OfSingleton<T>::instance = nullptr;
template <typename T> std::once_flag OfSingleton<T>::init_flag;
2024-12-11 10:21:57 +08:00
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);
2024-12-11 17:00:42 +08:00
const char* get_data() const;
2024-12-11 22:49:46 +08:00
int get_len() const;
2024-12-11 10:21:57 +08:00
void remove_of(int start_pos, int len);
void clear();
private:
std::vector<char> buffer_;
std::mutex mutex_;
};
2024-12-07 22:31:27 +08:00
} // namespace ofen