int main (int argc, const char * argv[]) { //Create a new circular buffer for unsigned integers CircularBuffer< UINT > buffer; //Resize the buffer buffer.resize( 10 ); //Add some values to the buffer so we fill it for(UINT i=0; i<buffer.getSize(); i++){ cout << "Adding " << i << " to buffer\n"; buffer.push_back( i ); //Print the values in the buffer cout << "Values: \t\t"; for(UINT j=0; j<buffer.getSize(); j++){ cout << buffer[j] << "\t"; }cout << endl; //Print the raw values in the buffer cout << "RawValues: \t\t"; for(UINT j=0; j<buffer.getSize(); j++){ cout << buffer(j) << "\t"; }cout << endl; } //Get all the data in the buffer as a vector vector<UINT> data = buffer.getDataAsVector(); cout << "Data: \t\t\t"; for(UINT j=0; j<data.size(); j++){ cout << data[j] << "\t"; } cout << endl; return EXIT_SUCCESS; }
int main ( int argc, char ** argv ) { size_t buffsize = DEFAULT_CIRBUFFER_SIZE; size_t maxsize = MAX_CIRBUFFER_SIZE; if ( argc == 2 ) buffsize = StringUtils::FromString<size_t>(argv[1]); CircularBuffer * buff = new CircularBuffer(buffsize); std::string bstr = "0123456789"; int count = buffsize / bstr.length(); std::cout << " buffer capacity = " << buff->size() << ", max is " << maxsize << ", string '" << bstr << std::endl << "', count is " << count << std::endl << std::endl; while ( buff->writeAvailable() >= bstr.length() ) buff->write(bstr.c_str(), bstr.length()); char * out = (char*) calloc(bstr.length(), sizeof(char)); std::cout << " dataAvail in buffer = " << buff->readAvailable() << std::endl; for ( int i = 0; i < (count / 2); ++i ) { buff->read(out, bstr.length()); } std::cout << " read: '" << out << "'" << std::endl; buffsize = buffsize - (count / 2); std::cout << " resizing to " << buffsize << std::endl; if ( ! buff->resize(buffsize) ) std::cout << "RESIZE FAILED" << std::endl; std::cout << " buffer capacity = " << buff->size() << std::endl << " fullDataAvail = " << buff->readAvailable() << " dataAvail = " << buff->readPtrAvailable() << std::endl << " fullSpaceAvail = " << buff->writeAvailable() << " spaceAvail = " << buff->writePtrAvailable() << std::endl << std::endl; int c = 0; while ( buff->writePtrAvailable() >= bstr.length() ) { buff->write(bstr.c_str(), bstr.length()); c++; } std::cout << " write count = " << c << std::endl << std::endl; std::cout << " buffer capacity = " << buff->size() << std::endl << " fullDataAvail = " << buff->readAvailable() << " dataAvail = " << buff->readPtrAvailable() << std::endl << " fullSpaceAvail = " << buff->writeAvailable() << " spaceAvail = " << buff->writePtrAvailable() << std::endl << std::endl; buffsize = buff->size() + bstr.length(); std::cout << " resizing to " << buffsize << std::endl; if ( ! buff->resize(buffsize) ) std::cout << " RESIZE FAILED " << std::endl; std::cout << " buffer capacity = " << buff->size() << std::endl << " fullDataAvail = " << buff->readAvailable() << " dataAvail = " << buff->readPtrAvailable() << std::endl << " fullSpaceAvail = " << buff->writeAvailable() << " spaceAvail = " << buff->writePtrAvailable() << std::endl << std::endl; ::free(out); delete buff; return 0; }