summaryrefslogtreecommitdiff
path: root/i/marvin/src/data/data_circular_buffer.cc
diff options
context:
space:
mode:
authordufourj2006-04-09 15:53:14 +0000
committerdufourj2006-04-09 15:53:14 +0000
commitfe8085484038b83d431459ac7fc3980cbbac9137 (patch)
tree5f4bd11fbbb60ee311e969630bbe9d294849ca75 /i/marvin/src/data/data_circular_buffer.cc
parent1cb15048a3da2076ea3d41b53b2791cbb8925eec (diff)
Data:
- ajout du buffer circulaire (enfin !)
Diffstat (limited to 'i/marvin/src/data/data_circular_buffer.cc')
-rw-r--r--i/marvin/src/data/data_circular_buffer.cc123
1 files changed, 123 insertions, 0 deletions
diff --git a/i/marvin/src/data/data_circular_buffer.cc b/i/marvin/src/data/data_circular_buffer.cc
new file mode 100644
index 0000000..be17526
--- /dev/null
+++ b/i/marvin/src/data/data_circular_buffer.cc
@@ -0,0 +1,123 @@
+// 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 <cstring> // std::memcpy
+#include <stdexcept> // std::runtime_error
+#include <algorithm> // std::swap, std::min
+
+#include <iostream> // 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_);
+}