#pragma once
#include <memory.h>

namespace render{
namespace utils{

template<typename T>
class buffer
{
public:

    buffer()
    {
        _size = 0;
        data = _data = nullptr;
        count = 0;
    }

    buffer(uint size)
    {
        data = _data = new T[size];
        count = 0;
        _size = size;
    }

    buffer(const buffer<T> &b)
    {
        data = _data = new T[b.count];
        _size = count = b.count;
        memcpy(_data, b._data, count*sizeof(T));
    }

    ~buffer()
    {
        if(!_data)
            return;
        delete [] _data;
    }

    bool add(const T *d, uint c)
    {
        if(count+c >= _size)
            return false;
        memcpy(&_data[count], d, c*sizeof(T));
        count+=c;
        return true;
    }

    buffer<T> &operator=(const buffer<T> &b)
    {
        if(_data)
            delete [] _data;
        data = _data = new T[b.count];
        _size = count = b.count;
        memcpy(_data, b._data, count*sizeof(T));
        return *this;
    }

    const T *data;
    uint count;
private:
    T *_data;
    uint _size;
};

}}
