/////// File Cirbuf.C /////// #include #include "Cirbuf.H" Cirbuf::Cirbuf (int s /* = D_SIZE */) { _head = _tail = _length = 0; _size = s; _cb = new char[s]; // allocate buffer space } Cirbuf::Cirbuf (const Cirbuf& buf) { _head = buf._head; _tail = buf._tail; _length = buf._length; _size = buf._size; _cb = new char [_size]; // allocate buffer for (int i = 0; i < _size; ++i) { _cb[i] = buf._cb[i]; // copy elements } } Cirbuf::~Cirbuf() { delete [] _cb; // deallocate buffer } Cirbuf& Cirbuf::operator= (const Cirbuf& buf) { if (this == &buf) { // self-assignment return *this; } if (_size != buf._size) { _size = buf._size; delete [] _cb; _cb = new char [_size]; // allocate buffer } for (int i = 0; i < _size; ++i) { _cb[i] = buf._cb[i]; // copy elements } _head = buf._head; _tail = buf._tail; _length = buf._length; return *this; } int Cirbuf::Produce (char c) { if ( IsFull() ) { cerr << "produce: buffer full\n"; return (-1); } _cb [_tail++] = c; _length++; _tail = mod (_tail); return (0); } int Cirbuf::Consume() { char c; if ( IsEmpty() ) { cerr << "consume: buffer empty\n"; return (-1); } c = _cb [_head++]; _length--; _head = mod (_head); return (c); }