// data_circular_buffer.cc // marvin - programme du robot 2006. {{{ // // Copyright (C) 2006 Dufour Jérémy // // Robot APB Team/Efrei 2004. // Web: http://assos.efrei.fr/robot/ // Email: robot AT efrei DOT fr // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // }}} #include "data_circular_buffer.hh" #include // std::memcpy #include // std::runtime_error #include // std::swap, std::min #include // XXX /// Default constructor. /// Size is the allocated size in memory. DataCircularBuffer::DataCircularBuffer (unsigned size) : size_ (size + 1), startPos_ (0), endPos_ (0) { // The + 1 for size is because, in some cases, we will have an empty case. buffer_ = new uint8_t [size_]; } /// Destructor. DataCircularBuffer::~DataCircularBuffer (void) { delete [] buffer_; } /// Read data of maximum size in the data pointer (data must be allocated of /// size). unsigned DataCircularBuffer::read (uint8_t *data, unsigned size) { if (!data || !size) throw std::invalid_argument ("Invalid parameter"); return internalRead (data, size); } /// Internal read, do the real job. unsigned DataCircularBuffer::internalRead (uint8_t *data, unsigned size) { // Size of data to read unsigned sizeToRead = std::min (maxReadableData (), size); // Size of data to read for the end of the buffer unsigned sizeToReadEnd = std::min (size_ - startPos_, sizeToRead); // Copy end part of buffer if (data) std::memcpy (&data[0], &buffer_[startPos_], sizeToReadEnd); // Need to do the begin part of buffer ? int sizeToReadStart = sizeToRead - sizeToReadEnd; if (sizeToReadStart > 0) { if (data) std::memcpy (&data[sizeToReadEnd], &buffer_[0], sizeToReadStart); // Move start of data startPos_ = 0 + sizeToReadStart; } else // Move start of data startPos_ = (startPos_ + sizeToRead) % size_; return sizeToRead; } /// Write data of size length. void DataCircularBuffer::write (const uint8_t *data, unsigned size) { if (!data || !size) throw std::invalid_argument ("Invalid parameter"); // Max size we can write unsigned sizeToWrite = std::min (size, size_ - 1); // Read data before overwrite it if needed int sizeToRead = sizeToWrite - (size_ - maxReadableData ()) + 1; if (sizeToRead > 0) if (internalRead (0, sizeToRead) != (unsigned) sizeToRead) throw std::runtime_error ("Unexpected error"); // Max size we can write until the end of the buffer unsigned sizeToWriteEnd = std::min (size_ - endPos_, sizeToWrite); // Write first part. std::memcpy (&buffer_[endPos_], &data[size - sizeToWrite + 0], sizeToWriteEnd); // More to write ? unsigned sizeToWriteStart = sizeToWrite - sizeToWriteEnd; if (sizeToWriteStart > 0) { std::memcpy (&buffer_[0], &data[size - sizeToWrite + sizeToWriteEnd], sizeToWriteStart); endPos_ = 0 + sizeToWriteStart; } else endPos_ = (endPos_ + sizeToWrite) % size_; } /// Swap data between tow DataCircularBuffer. void DataCircularBuffer::swap (DataCircularBuffer &d) { std::swap (buffer_, d.buffer_); std::swap (size_, d.size_); std::swap (startPos_, d.startPos_); std::swap (endPos_, d.endPos_); }