From 62c6ae5cd97f7fa85709eba90a01a7cddd6685a5 Mon Sep 17 00:00:00 2001 From: dufourj Date: Tue, 26 Apr 2005 19:02:47 +0000 Subject: Ajout de la classe DataBuffer : cette classe permet de gérer un buffer de données accessible en lecture comme en écriture. --- 2005/i/robert/src/data/Makefile.defs | 8 +- 2005/i/robert/src/data/data_buffer.cc | 126 +++++++++++++++++++++++++++++ 2005/i/robert/src/data/data_buffer.hh | 67 +++++++++++++++ 2005/i/robert/src/data/test_data_buffer.cc | 77 ++++++++++++++++++ 4 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 2005/i/robert/src/data/data_buffer.cc create mode 100644 2005/i/robert/src/data/data_buffer.hh create mode 100644 2005/i/robert/src/data/test_data_buffer.cc (limited to '2005') diff --git a/2005/i/robert/src/data/Makefile.defs b/2005/i/robert/src/data/Makefile.defs index 270db4d..31698f1 100644 --- a/2005/i/robert/src/data/Makefile.defs +++ b/2005/i/robert/src/data/Makefile.defs @@ -1,8 +1,12 @@ -PROGRAMS += test_data +PROGRAMS += test_data test_data_buffer data_OBJECTS = data_input.o data_input_file.o data_input_zlib.o \ - data_output.o -lz + data_buffer.o data_output.o -lz test_data_OBJECTS = test_data.o $(data_OBJECTS) +test_data_buffer_OBJECTS = test_data_buffer.o data_buffer.o + test_data: $(test_data_OBJECTS) + +test_data_buffer: $(test_data_buffer_OBJECTS) diff --git a/2005/i/robert/src/data/data_buffer.cc b/2005/i/robert/src/data/data_buffer.cc new file mode 100644 index 0000000..4fc7c56 --- /dev/null +++ b/2005/i/robert/src/data/data_buffer.cc @@ -0,0 +1,126 @@ +// data_buffer.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 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_buffer.hh" + +#include // std::memcpy +#include // std::runtime_error +#include // std::swap + +static const unsigned min_size = 10; + +/// Constructeur par defaut +DataBuffer::DataBuffer (void) + : size_ (min_size), headPos_ (0), tailPos_ (0) +{ + buffer_ = new uint8_t [size_]; +} + +/// Constructeur par recopie. +DataBuffer::DataBuffer (const DataBuffer &d) + : headPos_ (0) +{ + unsigned sizeUse = d.tailPos_ - d.headPos_; + size_ = tailPos_ = sizeUse; + buffer_ = new uint8_t [size_]; + std::memcpy (buffer_, &d.buffer_[headPos_], sizeUse); +} + +/// Constructeur avec données. +DataBuffer::DataBuffer (uint8_t *data, unsigned size, unsigned sizeAllocated) + : size_ (sizeAllocated), headPos_ (0), tailPos_ (size) +{ + if (!data || size > sizeAllocated) + throw std::invalid_argument ("Paramêtres invalides"); + buffer_ = new uint8_t [size_]; + std::memcpy (buffer_, data, tailPos_); +} + +/// Surcharge de l'opérateur d'affectation +DataBuffer & +DataBuffer::operator= (const DataBuffer &d) +{ + DataBuffer b (d); + b.swap (*this); + return *this; +} + +/// Lit des données. Même convention que le read de la libc, mais lance +/// une exception en cas d'erreur. +unsigned +DataBuffer::read (uint8_t *buf, unsigned size) +{ + if (!buf) + throw std::invalid_argument ("Paramêtres invalides"); + unsigned sizeTmp = tailPos_ - headPos_; + // On regarde combien de données peuvent être lues + if (size < sizeTmp) + sizeTmp = size; + std::memcpy (buf, &buffer_[headPos_], sizeTmp); + headPos_ += sizeTmp; + // Si il n'y a plus de données dans le buffer, on recommence de puis 0 + if (headPos_ == tailPos_) + headPos_ = tailPos_ = 0; + return sizeTmp; +} + +/// Écrit des données. Même convention que le write de la libc, mais lance +/// une exception en cas d'erreur. +void +DataBuffer::write (const uint8_t *buf, unsigned size) +{ + // Vérification des paramêtres + if (!buf) + throw std::invalid_argument ("Parametres invalides"); + // Agrandissement du buffer + grow (size); + // Ecriture des données + std::memcpy (&buffer_[tailPos_], buf, size); + tailPos_ += size; +} + +/// Augmente la taille du buffer pour correspondre à size. +void +DataBuffer::grow (const unsigned size) +{ + // Taille restant non utilisé dans le buffer + unsigned tailleLibre = size_ - tailPos_; + // Si la taille du buffer n'est pas suffisante + if (tailleLibre < size) + { + // On augmente de deux fois la taille + DataBuffer d (&buffer_[headPos_], tailPos_ - headPos_, 2 * size + size_); + d.swap (*this); + } +} + +/// Echange les buffers. +void +DataBuffer::swap (DataBuffer &d) +{ + std::swap (buffer_, d.buffer_); + std::swap (size_, d.size_); + std::swap (headPos_, d.headPos_); + std::swap (tailPos_, d.tailPos_); +} diff --git a/2005/i/robert/src/data/data_buffer.hh b/2005/i/robert/src/data/data_buffer.hh new file mode 100644 index 0000000..11724d8 --- /dev/null +++ b/2005/i/robert/src/data/data_buffer.hh @@ -0,0 +1,67 @@ +#ifndef data_buffer_hh +#define data_buffer_hh +// data_buffer.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "data_input.hh" +#include "data_output.hh" + +#include // uint8_t + +/// Classe contenant un buffer de données accessible en lecture et en +/// écriture. +class DataBuffer : public DataInput, public DataOutput +{ + /// Buffer de données. + uint8_t *buffer_; + /// Taille alloué en mémoire pour le buffer. + unsigned size_; + /// Position du début et de la fin des données dans le buffer. + unsigned headPos_, tailPos_; + public: + /// Constructeur par défaut. + DataBuffer (void); + /// Constructeur par recopie. + DataBuffer (const DataBuffer &d); + /// Constructeur avec données. + DataBuffer (uint8_t *data, unsigned size, unsigned sizeAllocated); + /// Destructeur par défaut. + ~DataBuffer (void) { delete [] buffer_; } + /// Surcharge de l'opérateur d'affectation + DataBuffer &operator= (const DataBuffer &d); + /// Lit des données. Même convention que le read de la libc, mais lance + /// une exception en cas d'erreur. + unsigned read (uint8_t *buf, unsigned size); + /// Écrit des données. Même convention que le write de la libc, mais lance + /// une exception en cas d'erreur. + void write (const uint8_t *buf, unsigned size); + /// Taille utile du buffer. + unsigned size (void) const { return tailPos_ - headPos_; } + private: + /// Augmente la taille du buffer pour correspondre à size. + void grow (const unsigned size); + /// Echange les buffers. + void swap (DataBuffer &d); +}; +#endif // data_buffer_hh diff --git a/2005/i/robert/src/data/test_data_buffer.cc b/2005/i/robert/src/data/test_data_buffer.cc new file mode 100644 index 0000000..36095ab --- /dev/null +++ b/2005/i/robert/src/data/test_data_buffer.cc @@ -0,0 +1,77 @@ +// test_data_buffer.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "data_buffer.hh" + +#include +#include +#include + +void +check_data (uint8_t *b1, uint8_t *b2, unsigned size) +{ + for (unsigned i = 0; i < size; i ++) + if (b1[i] != b2[i]) + throw std::runtime_error ("Erreur, buffers invalides"); +} + +int +main (int argc, char **argv) +{ + try + { + DataBuffer d; + uint8_t bufW[100]; + uint8_t bufR[150]; + // Remplissage des buffers de test + memset (bufW, 42, 100); + memset (bufR, 1, 150); + // On en écrit 50 + d.write (bufW, 50); + // On en lit 42 + if (d.read (bufR, 42) == 42) + check_data (bufW, bufR, 42); + else + throw std::runtime_error ("Erreur de taille de données"); + // On en re écrit 50 + d.write (&bufW[50], 50); + unsigned pos = 42; + unsigned status; + // On lit jusqu'à la fin + do + { + status = d.read (&bufR[pos], 10); + if (status) + check_data (&bufW[pos], &bufR[pos], status); + pos += status; + } while (status); + // On vérifie que tout a été bien lue + } + catch (const std::runtime_error &r) + { + std::cerr << r.what () << std::endl; + return 1; + } + return 0; +} -- cgit v1.2.3