ofen/include/of_util.h

51 lines
1.1 KiB
C++

#pragma once
#include "of_def.hpp"
#include <cassert>
#include <iostream>
#include <memory>
#include <mutex>
#include <algorithm>
#include <cstring>
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;
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);
const char* get_data() const;
void remove_of(int start_pos, int len);
void clear();
private:
std::vector<char> buffer_;
std::mutex mutex_;
};
} // namespace ofen