c++缓冲区 vbuffer.hpp

//vov

#ifndef VBUFFER_HPP
#define VBUFFER_HPP

#include <iostream>
#include <deque>

template <class T>
class vBuffer
{
    std::deque<T> dbuf_;
    int len_;
    std::mutex mtx_;
public:
    vBuffer();
    vBuffer(int len);
    ~vBuffer();
    void push_back(T data);
    void pop_front();
    T top();
    T back();
    int size();
    void clear();
private:
    //拷贝构造函数
    vBuffer(const vBuffer& t) {}
    //赋值构造函数
    vBuffer& operator=(const vBuffer& t) {}
};

template <class T>
vBuffer<T>::vBuffer() {
    len_=1024;
}

template <class T>
vBuffer<T>::vBuffer(int len) {
    len_=(len>1024||len<1)?1024:len;
}

template <class T>
vBuffer<T>::~vBuffer() {
}

template <class T>
void vBuffer<T>::push_back(T data) {
    if(dbuf_.size()==(unsigned int)len_) {
        dbuf_.pop_front();
        std::cout<<"[vBuffer]buffer is full:loss one data"<<std::endl;
    }
    dbuf_.push_back(data);
}

template <class T>
void vBuffer<T>::pop_front() {
    if(!dbuf_.empty())
        dbuf_.pop_front();
}

template <class T>
T vBuffer<T>::top() {
    T data;
    if(!dbuf_.empty()) {
        data=dbuf_.front();
    }

    return data;
}

template <class T>
T vBuffer<T>::back() {
    if(!dbuf_.empty())
        return dbuf_.back();
}

template <class T>
int vBuffer<T>::size() {
    return dbuf_.size();
}

template <class T>
void vBuffer<T>::clear() {
    if(!dbuf_.empty())
        dbuf_.clear();
}
#endif
posted @ 2019-04-23 19:46  JohnRed  阅读(231)  评论(0编辑  收藏  举报