From da4a77ff094a0dc74414ec1de2cac08e8b7b60f4 Mon Sep 17 00:00:00 2001 From: haller Date: Thu, 17 Nov 2005 11:10:17 +0000 Subject: Importation brut du code récupérable de robert --- i/marvin/src/utils/Makefile.defs | 15 +++ i/marvin/src/utils/any.hh | 117 +++++++++++++++++++++ i/marvin/src/utils/any.tcc | 169 ++++++++++++++++++++++++++++++ i/marvin/src/utils/bind.hh | 91 ++++++++++++++++ i/marvin/src/utils/callback.hh | 95 +++++++++++++++++ i/marvin/src/utils/callback.tcc | 136 ++++++++++++++++++++++++ i/marvin/src/utils/errno_exception.hh | 48 +++++++++ i/marvin/src/utils/fd_set.cc | 53 ++++++++++ i/marvin/src/utils/fd_set.hh | 54 ++++++++++ i/marvin/src/utils/hexa.cc | 113 ++++++++++++++++++++ i/marvin/src/utils/hexa.hh | 49 +++++++++ i/marvin/src/utils/list_ostream_output.hh | 62 +++++++++++ i/marvin/src/utils/mathutil.hh | 57 ++++++++++ i/marvin/src/utils/meta/Makefile.defs | 5 + i/marvin/src/utils/meta/is_string.hh | 45 ++++++++ i/marvin/src/utils/meta/test_is_string.cc | 35 +++++++ i/marvin/src/utils/non_copyable.hh | 41 ++++++++ i/marvin/src/utils/test_any.cc | 50 +++++++++ i/marvin/src/utils/test_bind.cc | 57 ++++++++++ i/marvin/src/utils/test_callback.cc | 84 +++++++++++++++ 20 files changed, 1376 insertions(+) create mode 100644 i/marvin/src/utils/Makefile.defs create mode 100644 i/marvin/src/utils/any.hh create mode 100644 i/marvin/src/utils/any.tcc create mode 100644 i/marvin/src/utils/bind.hh create mode 100644 i/marvin/src/utils/callback.hh create mode 100644 i/marvin/src/utils/callback.tcc create mode 100644 i/marvin/src/utils/errno_exception.hh create mode 100644 i/marvin/src/utils/fd_set.cc create mode 100644 i/marvin/src/utils/fd_set.hh create mode 100644 i/marvin/src/utils/hexa.cc create mode 100644 i/marvin/src/utils/hexa.hh create mode 100644 i/marvin/src/utils/list_ostream_output.hh create mode 100644 i/marvin/src/utils/mathutil.hh create mode 100644 i/marvin/src/utils/meta/Makefile.defs create mode 100644 i/marvin/src/utils/meta/is_string.hh create mode 100644 i/marvin/src/utils/meta/test_is_string.cc create mode 100644 i/marvin/src/utils/non_copyable.hh create mode 100644 i/marvin/src/utils/test_any.cc create mode 100644 i/marvin/src/utils/test_bind.cc create mode 100644 i/marvin/src/utils/test_callback.cc (limited to 'i/marvin/src/utils') diff --git a/i/marvin/src/utils/Makefile.defs b/i/marvin/src/utils/Makefile.defs new file mode 100644 index 0000000..43711a8 --- /dev/null +++ b/i/marvin/src/utils/Makefile.defs @@ -0,0 +1,15 @@ +PROGRAMS += test_any test_callback test_bind + +utils_OBJECTS = fd_set.o hexa.o + +test_any_OBJECTS = test_any.o + +test_callback_OBJECTS = test_callback.o + +test_bind_OBJECTS = test_bind.o + +test_any: $(test_any_OBJECTS) + +test_callback: $(test_callback_OBJECTS) + +test_bind: $(test_bind_OBJECTS) diff --git a/i/marvin/src/utils/any.hh b/i/marvin/src/utils/any.hh new file mode 100644 index 0000000..eabee96 --- /dev/null +++ b/i/marvin/src/utils/any.hh @@ -0,0 +1,117 @@ +#ifndef any_hh +#define any_hh +// any.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include +#include +#include + +/// This class can contain a data of any type. +class any +{ + class AbstractHolder; + AbstractHolder *holder_; + public: + /// Default constructor. + any (void); + /// Constructor. + template + explicit any (const T &value); + /// Copy constructor. + explicit any (const any &other); + /// Destructor. + ~any (void); + /// Swap content between two any objects. + any &swap (any &other); + /// Copy another any object. + any &operator= (const any &other); + /// Copy another object into this any. + template + any &operator= (const T &value); + /// Test if the any object is empty. + bool empty (void) const; + /// Return the std::type_info of the contained object. + const std::type_info &type (void) const; + private: + template + friend T *any_cast (any *rhs); + friend std::ostream &operator<< (std::ostream &os, const any &rhs); + /// Abstract holder class. + class AbstractHolder + { + public: + /// Destructor. + virtual ~AbstractHolder (void); + virtual const std::type_info &type (void) const = 0; + virtual AbstractHolder *clone (void) const = 0; + virtual std::ostream &print (std::ostream &os) const = 0; + }; + /// Holder class, templated. + template + class Holder : public AbstractHolder + { + public: + T value_; + public: + Holder (const T &value); + virtual const std::type_info &type (void) const; + virtual AbstractHolder *clone (void) const; + virtual std::ostream &print (std::ostream &os) const; + }; +}; + +/// Object throw if a any_cast returning a reference fail. +class bad_any_cast : public std::bad_cast +{ + public: + virtual const char * what() const throw() + { + return "bad_any_cast: illegal conversion"; + } +}; + +/// Return a pointer to the contained object or 0 on faillure. +template +T * +any_cast (any *rhs); + +/// Return a const pointer to the contained object or 0 on faillure. +template +const T * +any_cast (const any *rhs); + +/// Return a const reference to the contained object or throw a bad_any_cast +/// on faillure. +template +const T & +any_cast (const any &rhs); + +/// Print the contained object. +std::ostream & +operator<< (std::ostream &os, const any &rhs); + +#include "any.tcc" + +#endif // any_hh diff --git a/i/marvin/src/utils/any.tcc b/i/marvin/src/utils/any.tcc new file mode 100644 index 0000000..fd3dca6 --- /dev/null +++ b/i/marvin/src/utils/any.tcc @@ -0,0 +1,169 @@ +// any.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "list_ostream_output.hh" + +#include + +/// Default constructor. +inline +any::any (void) + : holder_ (0) +{ +} + +/// Constructor. +template +any::any (const T &value) + : holder_ (new Holder (value)) +{ +} + +/// Copy constructor. +inline +any::any (const any &other) + : holder_ (other.holder_ ? other.holder_->clone () : 0) +{ +} + +/// Destructor. +inline +any::~any (void) +{ + delete holder_; +} + +/// Swap content between two any objects. +inline +any & +any::swap (any &other) +{ + std::swap (holder_, other.holder_); + return *this; +} + +/// Copy another any object. +inline +any & +any::operator= (const any &other) +{ + any (other).swap (*this); + return *this; +} + +/// Copy another object into this any. +template +any & +any::operator= (const T &value) +{ + any (value).swap (*this); + return *this; +} + +/// Test if the any object is empty. +inline +bool +any::empty (void) const +{ + return !holder_; +} + +/// Return the std::type_info of the contained object. +inline +const std::type_info & +any::type (void) const +{ + return holder_ ? holder_->type () : typeid (void); +} + +/// Destructor. +inline +any::AbstractHolder::~AbstractHolder (void) +{ +} + +template +any::Holder::Holder (const T &value) + : value_ (value) +{ +} + +template +const std::type_info & +any::Holder::type (void) const +{ + return typeid (T); +} + +template +any::AbstractHolder * +any::Holder::clone (void) const +{ + return new Holder (value_); +} + +template +std::ostream & +any::Holder::print (std::ostream &os) const +{ + return os << value_; +} + +/// Return a pointer to the contained object or 0 on faillure. +template +T * +any_cast (any *rhs) +{ + return rhs && rhs->type () == typeid (T) + ? &static_cast *> (rhs->holder_)->value_ + : 0; +} + +/// Return a const pointer to the contained object or 0 on faillure. +template +const T * +any_cast (const any *rhs) +{ + return any_cast (const_cast (rhs)); +} + +/// Return a const reference to the contained object or throw a bad_any_cast +/// on faillure. +template +const T & +any_cast (const any &rhs) +{ + const T *value = any_cast (&rhs); + if (!value) + throw bad_any_cast (); + return *value; +} + +/// Print the contained object. +inline +std::ostream & +operator<< (std::ostream &os, const any &rhs) +{ + return rhs.holder_->print (os); +} + diff --git a/i/marvin/src/utils/bind.hh b/i/marvin/src/utils/bind.hh new file mode 100644 index 0000000..a5630fe --- /dev/null +++ b/i/marvin/src/utils/bind.hh @@ -0,0 +1,91 @@ +#ifndef bind_hh +#define bind_hh +// bind.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +/// Class used to bind a function taking an argument to a functor taking no +/// argument. This is not supposed to be used directly, but using the bind +/// template function. +template +class ArgBinder +{ + /// Original function. + F func_; + /// Stored argument reference. + const A &arg_; + public: + /// Return value. + typedef R result_type; + public: + ArgBinder (F func, const A &arg) + : func_ (func), arg_ (arg) + { } + result_type operator () (void) + { + return func_ (arg_); + } +}; + +/// Bind a function taking an argument to a functor taking no argument. +/// Return value must be specified, other template arguments can be implied. +template +ArgBinder +bind (F func, const A &arg) +{ + return ArgBinder (func, arg); +} + +/// Class used to bind a member function to a functor taking no argument. +/// This is not supposed to be used directly, but using the bind template +/// function. +template +class ObjBinder +{ + /// Original member function. + R (C::*func_) (void); + /// Object pointer stored. + C *obj_; + public: + /// Return value. + typedef R result_type; + public: + ObjBinder (R (C::*func) (void), C *obj) + : func_ (func), obj_ (obj) + { } + result_type operator () (void) + { + (obj_->*func_) (); + } +}; + +/// Bind a member function to a functor taking no argument. All template +/// parameters must be specified. +template +ObjBinder +bind (R (C::*func) (void), C *obj) +{ + return ObjBinder (func, obj); +} + +#endif // bind_hh diff --git a/i/marvin/src/utils/callback.hh b/i/marvin/src/utils/callback.hh new file mode 100644 index 0000000..c5dc229 --- /dev/null +++ b/i/marvin/src/utils/callback.hh @@ -0,0 +1,95 @@ +#ifndef callback_hh +#define callback_hh +// callback.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +/// Class to store a callback. This callback can be of any type (function or +/// fonctor). +template +class Callback +{ + class AbstractHolder; + AbstractHolder *holder_; + public: + /// Return value. + typedef R result_type; + public: + /// Default constructor. Make an empty callback. + Callback (void); + /// Constructor. + template + Callback (T callback); + /// Copy constructor. + Callback (const Callback &other); + /// Destructor. + ~Callback (void); + /// Assignement operator. + Callback &operator= (const Callback &rhs); + /// Call the contained callback. + R operator () (void); + /// Swap the callback content with another callback. + void swap (Callback &rhs); + /// Change the contained callback. + template + Callback &operator= (T rhs); + /// Test if empty. + bool empty (void) const; + /// Test if not empty. + operator bool (void) const; + private: + /// Abstract container. + class AbstractHolder + { + public: + virtual ~AbstractHolder (void); + virtual R operator () (void) = 0; + virtual AbstractHolder *clone (void) = 0; + }; + /// Templated container. + template + class Holder : public AbstractHolder + { + private: + T callback_; + public: + Holder (T callback); + virtual R operator () (void); + virtual AbstractHolder *clone (void); + }; +}; + +/// Exception thrown on call of an empty callback. +class bad_callback : public std::runtime_error +{ + public: + bad_callback (void) : std::runtime_error ("bad_callback: empty callback call") + { + } +}; + +#include "callback.tcc" + +#endif // callback_hh diff --git a/i/marvin/src/utils/callback.tcc b/i/marvin/src/utils/callback.tcc new file mode 100644 index 0000000..d10df75 --- /dev/null +++ b/i/marvin/src/utils/callback.tcc @@ -0,0 +1,136 @@ +// callback.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +/// Default constructor. Make an empty callback. +template +Callback::Callback (void) + : holder_ (0) +{ +} + +/// Constructor. +template +template +Callback::Callback (T callback) + : holder_ (new Holder (callback)) +{ +} + +/// Copy constructor. +template +Callback::Callback (const Callback &other) + : holder_ (other.holder_ ? other.holder_->clone () : 0) +{ +} + +/// Destructor. +template +Callback::~Callback (void) +{ + delete holder_; +} + +/// Assignement operator. +template +Callback & +Callback::operator= (const Callback &rhs) +{ + Callback (rhs).swap (*this); + return *this; +} + +/// Call the contained callback. +template +R +Callback::operator () (void) +{ + if (!holder_) + throw bad_callback (); + else + return (*holder_) (); +} + +/// Swap the callback content with another callback. +template +void +Callback::swap (Callback &rhs) +{ + std::swap (holder_, rhs.holder_); +} + +/// Change the contained callback. +template +template +Callback & +Callback::operator= (T rhs) +{ + Callback (rhs).swap (*this); + return *this; +} + +/// Test if empty. +template +bool +Callback::empty (void) const +{ + return !holder_; +} + +/// Test if not empty. +template +Callback::operator bool (void) const +{ + return !empty (); +} + +template +Callback::AbstractHolder::~AbstractHolder (void) +{ +} + +template +template +Callback::Holder::Holder (T callback) + : callback_ (callback) +{ +} + +template +template +R +Callback::Holder::operator () (void) +{ + return callback_ (); +} + +template +template +typename Callback::AbstractHolder * +Callback::Holder::clone (void) +{ + return new Holder (callback_); +} + diff --git a/i/marvin/src/utils/errno_exception.hh b/i/marvin/src/utils/errno_exception.hh new file mode 100644 index 0000000..58a636e --- /dev/null +++ b/i/marvin/src/utils/errno_exception.hh @@ -0,0 +1,48 @@ +#ifndef errno_exception_hh +#define errno_exception_hh +// errno_exception.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 +#include +#include +#include + +/// Exception lancé lorsqu'une erreur est retournée par la libc. +class errno_exception : public std::exception +{ + int errno_; + std::string what_; + public: + errno_exception (const std::string &desc, int errno__) + : errno_ (errno__), what_ (desc + ": " + strerror (errno__)) { } + errno_exception (int errno__) + : errno_ (errno__), what_ (strerror (errno__)) { } + ~errno_exception (void) throw () { } + virtual const char* what () const throw () { return what_.c_str (); } + int getErrno (void) const { return errno_; } +}; + +#endif // errno_exception_hh diff --git a/i/marvin/src/utils/fd_set.cc b/i/marvin/src/utils/fd_set.cc new file mode 100644 index 0000000..998f3f5 --- /dev/null +++ b/i/marvin/src/utils/fd_set.cc @@ -0,0 +1,53 @@ +// fd_set.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 "fd_set.hh" +#include "utils/errno_exception.hh" + +/// Wait for an read event or timeout in milliseconds, return true on read +/// event. +bool +FdSet::wait (int timeout/*-1*/) +{ + int r; + if (timeout == -1) + { + // Without timeout. + r = select (FD_SETSIZE, get (), 0, 0, 0); + } + else + { + // With timeout. + struct timeval tv; + tv.tv_sec = timeout / 1000; + tv.tv_usec = (timeout % 1000) * 1000; + r = select (FD_SETSIZE, get (), 0, 0, &tv); + } + if (r == -1) + // Error. + throw errno_exception (errno); + else + return r != 0; +} + diff --git a/i/marvin/src/utils/fd_set.hh b/i/marvin/src/utils/fd_set.hh new file mode 100644 index 0000000..3d6d44c --- /dev/null +++ b/i/marvin/src/utils/fd_set.hh @@ -0,0 +1,54 @@ +#ifndef fd_set_hh +#define fd_set_hh +// fd_set.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +/// Wrapper over libc fd_set. +class FdSet +{ + fd_set set_; + public: + /// Default constructor. + FdSet (void) + { FD_ZERO (&set_); } + /// Set a fd. + void set (int fd) + { FD_SET (fd, &set_); } + /// Clear a fd. + void clear (int fd) + { FD_CLR (fd, &set_); } + /// Test if a fd is set. + bool isSet (int fd) const + { return FD_ISSET (fd, &set_); } + /// Get pointer to internal fd_set. + fd_set *get (void) + { return &set_; } + /// Wait for an read event or timeout in milliseconds, return true on read + /// event. + bool wait (int timeout = -1); +}; + +#endif // fd_set_hh diff --git a/i/marvin/src/utils/hexa.cc b/i/marvin/src/utils/hexa.cc new file mode 100644 index 0000000..102458e --- /dev/null +++ b/i/marvin/src/utils/hexa.cc @@ -0,0 +1,113 @@ +// hexa.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 "hexa.hh" + +// Table de conversion en hexa. +static const char *digit2hexTbl_ = "0123456789abcdef"; + +// Table de conversion depuis l'hexa. +#define hI hexInvalid +static const int hex2digitTbl_[] = +{ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 0-15 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 16-31 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 32-47 */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,hI,hI,hI,hI,hI,hI, /* 48-63 */ + hI,10,11,12,13,14,15,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 64-79 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 80-95 */ + hI,10,11,12,13,14,15,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 96-111 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 112-127 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 128-143 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 144-159 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 160-175 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 176-191 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 192-207 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 208-223 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 224-239 */ + hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI, /* 240-255 */ +}; + +/// Converti un caractère hexa (0-9a-f) en chiffre (0-15). +int +hex2digit (char c) +{ + return hex2digitTbl_[static_cast (c)]; +} + +/// Converti un chiffre (0-15) en hexa (0-9a-f). +char +digit2hex (int d) +{ + return digit2hexTbl_[d]; +} + +/// Décode un mot non-signé (1 octets). +unsigned int +hexUnsignedChar2int (const char *s) +{ + int h1 = hex2digit (s[0]); + int h0 = hex2digit (s[1]); + if (h1 == hexInvalid || h0 == hexInvalid) + return hexInvalid; + return (unsigned char) (h1 << 4 | h0); +} + +/// Décode un mot signé (1 octets). +int +hexSignedChar2int (const char *s) +{ + int h1 = hex2digit (s[0]); + int h0 = hex2digit (s[1]); + if (h1 == hexInvalid || h0 == hexInvalid) + return hexInvalid; + return (signed char) (h1 << 4 | h0); +} + +/// Décode un mot non-signé (2 octets). +int +hexUnsignedShort2int (const char *s) +{ + int h3 = hex2digit (s[0]); + int h2 = hex2digit (s[1]); + int h1 = hex2digit (s[2]); + int h0 = hex2digit (s[3]); + if (h3 == hexInvalid || h2 == hexInvalid || h1 == hexInvalid || h0 == hexInvalid) + return hexInvalid; + return (unsigned short) (h3 << 12 | h2 << 8 | h1 << 4 | h0); +} + +/// Décode un mot signé (2 octets). +int +hexSignedShort2int (const char *s) +{ + int h3 = hex2digit (s[0]); + int h2 = hex2digit (s[1]); + int h1 = hex2digit (s[2]); + int h0 = hex2digit (s[3]); + if (h3 == hexInvalid || h2 == hexInvalid || h1 == hexInvalid || h0 == hexInvalid) + return hexInvalid; + return (short) (h3 << 12 | h2 << 8 | h1 << 4 | h0); +} + diff --git a/i/marvin/src/utils/hexa.hh b/i/marvin/src/utils/hexa.hh new file mode 100644 index 0000000..f7d3399 --- /dev/null +++ b/i/marvin/src/utils/hexa.hh @@ -0,0 +1,49 @@ +#ifndef hexa_hh +#define hexa_hh +// hexa.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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. +// +// }}} + +/// Constante retournée pour une valeur invalide. +const int hexInvalid = 1 << 24; + +/// Converti un caractère hexa (0-9a-f) en chiffre (0-15). +int hex2digit (char c); + +/// Converti un chiffre (0-15) en hexa (0-9a-f). +char digit2hex (int d); + +/// Décode un mot non-signé (1 octets). +unsigned int hexUnsignedChar2int (const char *s); + +/// Décode un mot signé (1 octets). +int hexSignedChar2int (const char *s); + +/// Décode un mot non-signé (2 octets). +int hexUnsignedShort2int (const char *s); + +/// Décode un mot signé (2 octets). +int hexSignedShort2int (const char *s); + +#endif // hexa_hh diff --git a/i/marvin/src/utils/list_ostream_output.hh b/i/marvin/src/utils/list_ostream_output.hh new file mode 100644 index 0000000..3863a39 --- /dev/null +++ b/i/marvin/src/utils/list_ostream_output.hh @@ -0,0 +1,62 @@ +#ifndef list_ostream_output_hh +#define list_ostream_output_hh +// list_ostream_output.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 +#include +#include +#include +#include + +/// Output any list. +template +std::ostream & +output_list (std::ostream &os, const T &list) +{ + os << "( "; + std::copy (list.begin (), list.end (), + std::ostream_iterator (os, " ")); + os << ')'; + return os; +} + +/// Output a list. +template +std::ostream & +operator<< (std::ostream &os, const std::list &list) +{ + return output_list (os, list); +} + +/// Output a vector. +template +std::ostream & +operator<< (std::ostream &os, const std::vector &list) +{ + return output_list (os, list); +} + +#endif // list_ostream_output_hh diff --git a/i/marvin/src/utils/mathutil.hh b/i/marvin/src/utils/mathutil.hh new file mode 100644 index 0000000..d58cec3 --- /dev/null +++ b/i/marvin/src/utils/mathutil.hh @@ -0,0 +1,57 @@ +#ifndef mathutil_hh +#define mathutil_hh +// mathutil.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 + +/// Normalise l'angle entre -pi et +pi. +double +angleNorm (double a); + +/// Converti en coordonnées polaires. +void +toPolar (double x, double y, double &r, double &a); + +/// Normalise l'angle en [-pi..+pi). +inline double +angleNorm (double a) +{ + while (a >= M_PI) + a -= 2.0 * M_PI; + while (a < -M_PI) + a += 2.0 * M_PI; + return a; +} + +/// Converti en coordonnées polaires. +inline void +toPolar (double x, double y, double &r, double &a) +{ + // Rayon. + r = sqrt (x * x + y * y); + a = y > 0 ? acos (x / r) : -acos (x / r); +} + +#endif // mathutil_hh diff --git a/i/marvin/src/utils/meta/Makefile.defs b/i/marvin/src/utils/meta/Makefile.defs new file mode 100644 index 0000000..5650f40 --- /dev/null +++ b/i/marvin/src/utils/meta/Makefile.defs @@ -0,0 +1,5 @@ +PROGRAMS += test_is_string + +test_is_string_OBJECTS = test_is_string.o + +test_is_string: $(test_is_string_OBJECTS) diff --git a/i/marvin/src/utils/meta/is_string.hh b/i/marvin/src/utils/meta/is_string.hh new file mode 100644 index 0000000..99782d2 --- /dev/null +++ b/i/marvin/src/utils/meta/is_string.hh @@ -0,0 +1,45 @@ +#ifndef is_string_hh +#define is_string_hh +// is_string.hh +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +namespace meta { + +template +struct isString +{ + static const bool value = false; +}; + +template<> +struct isString +{ + static const bool value = true; +}; + +} // namespace meta + +#endif // is_string_hh diff --git a/i/marvin/src/utils/meta/test_is_string.cc b/i/marvin/src/utils/meta/test_is_string.cc new file mode 100644 index 0000000..c61483a --- /dev/null +++ b/i/marvin/src/utils/meta/test_is_string.cc @@ -0,0 +1,35 @@ +// test_is_string.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "is_string.hh" + +#include + +int +main (void) +{ + std::cout << "std::string " << meta::isString::value << std::endl; + std::cout << "int " << meta::isString::value << std::endl; + std::cout << "char * " << meta::isString::value << std::endl; + return 0; +} diff --git a/i/marvin/src/utils/non_copyable.hh b/i/marvin/src/utils/non_copyable.hh new file mode 100644 index 0000000..e788365 --- /dev/null +++ b/i/marvin/src/utils/non_copyable.hh @@ -0,0 +1,41 @@ +#ifndef non_copyable_hh +#define non_copyable_hh +// non_copyable.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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. +// +// }}} + +/// Toute classe qui hérite de NonCopyable ne peut pas être copiée. Cela est +/// assuré par le fait que l'operator= et le constructeur de recopie sont +/// privé. +/// Idée originale : Boost.org. +class NonCopyable +{ + public: + NonCopyable (void) { } + private: + NonCopyable (const NonCopyable &); + const NonCopyable &operator= (const NonCopyable &); +}; + +#endif // non_copyable_hh diff --git a/i/marvin/src/utils/test_any.cc b/i/marvin/src/utils/test_any.cc new file mode 100644 index 0000000..85d2319 --- /dev/null +++ b/i/marvin/src/utils/test_any.cc @@ -0,0 +1,50 @@ +// test_any.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "any.hh" + +#include +#include +#include + +int +main (int argc, char **argv) +{ + any a (45), b (std::string ("toto")), c (5.4), d (std::string ("5")); + try + { + // Direct call to output operator. + std::cout << a << ' ' << b << ' ' << c << ' ' << d << std::endl; + // Here, it tries to extract the contained data. + std::cout << any_cast (a) << std::endl; + std::cout << any_cast (b) << std::endl; + std::cout << any_cast (c) << std::endl; + std::cout << any_cast (d) << std::endl; + } + catch (const std::exception &e) + { + std::cout << e.what () << std::endl; + return 1; + } + return 0; +} diff --git a/i/marvin/src/utils/test_bind.cc b/i/marvin/src/utils/test_bind.cc new file mode 100644 index 0000000..b883cbe --- /dev/null +++ b/i/marvin/src/utils/test_bind.cc @@ -0,0 +1,57 @@ +// test_bind.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "bind.hh" + +#include + +void +f (int i) +{ + std::cout << "f " << i << std::endl; +} + +void +g (const char *s) +{ + std::cout << "g " << s << std::endl; +} + +class H +{ + public: + void f (void) + { + std::cout << "h" << std::endl; + } +}; + +int +main (void) +{ + // Create the callback and execute it. + bind (&f, 42) (); + bind (&g, "hello world !") (); + H h; + bind (&H::f, &h) (); +} diff --git a/i/marvin/src/utils/test_callback.cc b/i/marvin/src/utils/test_callback.cc new file mode 100644 index 0000000..2a5b6f0 --- /dev/null +++ b/i/marvin/src/utils/test_callback.cc @@ -0,0 +1,84 @@ +// test_callback.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// 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: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "callback.hh" + +#include + +void +f (void) +{ + std::cout << " f ()" << std::endl; +} + +int +i (void) +{ + static int v = 42; + std::cout << " i () = " << v << std::endl; + return v++; +} + +struct G +{ + void operator () (void) + { + std::cout << " g ()" << std::endl; + } +}; + +int +main (void) +{ + G g; + Callback cf (f); + Callback cg (g); + Callback ch; + Callback ci (i); + try + { + std::cout << "cf" << std::endl; + cf (); + std::cout << "cg" << std::endl; + cg (); + cf = cg; + cg = f; + std::cout << "cf" << std::endl; + cf (); + std::cout << "cg" << std::endl; + cg (); + std::cout << "cf " << (cf ? "not " : "") << "empty" << std::endl; + std::cout << "ch " << (ch ? "not " : "") << "empty" << std::endl; + std::cout << "ci" << std::endl; + int r = ci (); + std::cout << "ci = " << r << std::endl; + std::cout << "ch" << std::endl; + ch (); + } + catch (const std::exception &e) + { + std::cout << e.what () << std::endl; + return 1; + } + return 0; +} -- cgit v1.2.3