From 8f486613be58ced269db1d437e560c16558604e8 Mon Sep 17 00:00:00 2001 From: becquet Date: Thu, 10 May 2007 18:49:20 +0000 Subject: Création de chuck, le programme du robot 2007. --- i/chuck/src/utils/Makefile.defs | 11 ++ i/chuck/src/utils/any.hh | 125 +++++++++++++++++++ i/chuck/src/utils/any.tcc | 185 +++++++++++++++++++++++++++++ i/chuck/src/utils/bind.hh | 91 ++++++++++++++ i/chuck/src/utils/callback.hh | 96 +++++++++++++++ i/chuck/src/utils/callback.tcc | 140 ++++++++++++++++++++++ i/chuck/src/utils/errno_exception.hh | 48 ++++++++ i/chuck/src/utils/fd_set.cc | 53 +++++++++ i/chuck/src/utils/fd_set.hh | 54 +++++++++ i/chuck/src/utils/hexa.cc | 113 ++++++++++++++++++ i/chuck/src/utils/hexa.hh | 49 ++++++++ i/chuck/src/utils/list_ostream_output.hh | 62 ++++++++++ i/chuck/src/utils/mathutil.hh | 57 +++++++++ i/chuck/src/utils/meta/Makefile.defs | 3 + i/chuck/src/utils/meta/is_equal.hh | 44 +++++++ i/chuck/src/utils/meta/is_string.hh | 45 +++++++ i/chuck/src/utils/meta/remove_reference.hh | 44 +++++++ i/chuck/src/utils/meta/test_meta.cc | 57 +++++++++ i/chuck/src/utils/non_copyable.hh | 41 +++++++ i/chuck/src/utils/signalhandler.hh | 52 ++++++++ i/chuck/src/utils/signalhandler.tcc | 66 ++++++++++ i/chuck/src/utils/test_any.cc | 50 ++++++++ i/chuck/src/utils/test_bind.cc | 57 +++++++++ i/chuck/src/utils/test_callback.cc | 84 +++++++++++++ i/chuck/src/utils/test_signalhandler.cc | 15 +++ 25 files changed, 1642 insertions(+) create mode 100644 i/chuck/src/utils/Makefile.defs create mode 100644 i/chuck/src/utils/any.hh create mode 100644 i/chuck/src/utils/any.tcc create mode 100644 i/chuck/src/utils/bind.hh create mode 100644 i/chuck/src/utils/callback.hh create mode 100644 i/chuck/src/utils/callback.tcc create mode 100644 i/chuck/src/utils/errno_exception.hh create mode 100644 i/chuck/src/utils/fd_set.cc create mode 100644 i/chuck/src/utils/fd_set.hh create mode 100644 i/chuck/src/utils/hexa.cc create mode 100644 i/chuck/src/utils/hexa.hh create mode 100644 i/chuck/src/utils/list_ostream_output.hh create mode 100644 i/chuck/src/utils/mathutil.hh create mode 100644 i/chuck/src/utils/meta/Makefile.defs create mode 100644 i/chuck/src/utils/meta/is_equal.hh create mode 100644 i/chuck/src/utils/meta/is_string.hh create mode 100644 i/chuck/src/utils/meta/remove_reference.hh create mode 100644 i/chuck/src/utils/meta/test_meta.cc create mode 100644 i/chuck/src/utils/non_copyable.hh create mode 100644 i/chuck/src/utils/signalhandler.hh create mode 100644 i/chuck/src/utils/signalhandler.tcc create mode 100644 i/chuck/src/utils/test_any.cc create mode 100644 i/chuck/src/utils/test_bind.cc create mode 100644 i/chuck/src/utils/test_callback.cc create mode 100644 i/chuck/src/utils/test_signalhandler.cc (limited to 'i/chuck/src/utils') diff --git a/i/chuck/src/utils/Makefile.defs b/i/chuck/src/utils/Makefile.defs new file mode 100644 index 0000000..8eb0711 --- /dev/null +++ b/i/chuck/src/utils/Makefile.defs @@ -0,0 +1,11 @@ +PROGRAMS += test_any test_callback test_bind test_signalhandler + +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_signalhandler_OBJECTS = test_signalhandler.o diff --git a/i/chuck/src/utils/any.hh b/i/chuck/src/utils/any.hh new file mode 100644 index 0000000..dc6204e --- /dev/null +++ b/i/chuck/src/utils/any.hh @@ -0,0 +1,125 @@ +#ifndef any_hh +#define any_hh +// any.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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 + +/// This class can contain a data of almost any type. This very type must be +/// CopyConstructible and OutputStreamable. The OutputStreamable restriction +/// could be removed, but it is so convenient to output an any... +class any +{ + class AbstractHolder; + AbstractHolder *holder_; + public: + /// Default constructor. + any (void); + /// Constructor. + template + any (const T &value); + /// Copy constructor. + 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; + }; +}; + +/// 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); + +/// Object throw if a any_cast returning a reference fail. +class bad_any_cast : public std::bad_cast +{ + std::string what_; + public: + /// Constructor. + bad_any_cast (const std::type_info &to, const std::type_info &from); + /// Destructor which should throw nothing. + virtual ~bad_any_cast() throw() + { } + /// Get the error text. + virtual const char *what() const throw() + { return what_.c_str (); } +}; + +/// Print the contained object. +std::ostream & +operator<< (std::ostream &os, const any &rhs); + +#include "any.tcc" + +#endif // any_hh diff --git a/i/chuck/src/utils/any.tcc b/i/chuck/src/utils/any.tcc new file mode 100644 index 0000000..70d1084 --- /dev/null +++ b/i/chuck/src/utils/any.tcc @@ -0,0 +1,185 @@ +#ifndef any_tcc +#define any_tcc +// any.tcc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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 "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 (typeid (T), rhs.type ()); + return *value; +} + +/// Constructor. +inline +bad_any_cast::bad_any_cast (const std::type_info &to, + const std::type_info &from) +{ + what_ = "illegal conversion from \'"; + what_ += from.name (); + what_ += "\' to \'"; + what_ += to.name (); + what_ += "\'"; +} + +/// Print the contained object. +inline +std::ostream & +operator<< (std::ostream &os, const any &rhs) +{ + return rhs.holder_->print (os); +} + +#endif // any_tcc diff --git a/i/chuck/src/utils/bind.hh b/i/chuck/src/utils/bind.hh new file mode 100644 index 0000000..a5630fe --- /dev/null +++ b/i/chuck/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/chuck/src/utils/callback.hh b/i/chuck/src/utils/callback.hh new file mode 100644 index 0000000..ab1311c --- /dev/null +++ b/i/chuck/src/utils/callback.hh @@ -0,0 +1,96 @@ +#ifndef callback_hh +#define callback_hh +// callback.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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 + +/// 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/chuck/src/utils/callback.tcc b/i/chuck/src/utils/callback.tcc new file mode 100644 index 0000000..4e8bdd4 --- /dev/null +++ b/i/chuck/src/utils/callback.tcc @@ -0,0 +1,140 @@ +#ifndef callback_tcc +#define callback_tcc +// callback.tcc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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 + +/// 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_); +} + +#endif // callback_tcc diff --git a/i/chuck/src/utils/errno_exception.hh b/i/chuck/src/utils/errno_exception.hh new file mode 100644 index 0000000..58a636e --- /dev/null +++ b/i/chuck/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/chuck/src/utils/fd_set.cc b/i/chuck/src/utils/fd_set.cc new file mode 100644 index 0000000..998f3f5 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/fd_set.hh b/i/chuck/src/utils/fd_set.hh new file mode 100644 index 0000000..3d6d44c --- /dev/null +++ b/i/chuck/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/chuck/src/utils/hexa.cc b/i/chuck/src/utils/hexa.cc new file mode 100644 index 0000000..102458e --- /dev/null +++ b/i/chuck/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/chuck/src/utils/hexa.hh b/i/chuck/src/utils/hexa.hh new file mode 100644 index 0000000..f7d3399 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/list_ostream_output.hh b/i/chuck/src/utils/list_ostream_output.hh new file mode 100644 index 0000000..3863a39 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/mathutil.hh b/i/chuck/src/utils/mathutil.hh new file mode 100644 index 0000000..d58cec3 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/meta/Makefile.defs b/i/chuck/src/utils/meta/Makefile.defs new file mode 100644 index 0000000..8fa3a10 --- /dev/null +++ b/i/chuck/src/utils/meta/Makefile.defs @@ -0,0 +1,3 @@ +PROGRAMS += test_meta + +test_meta_OBJECTS = test_meta.o diff --git a/i/chuck/src/utils/meta/is_equal.hh b/i/chuck/src/utils/meta/is_equal.hh new file mode 100644 index 0000000..a67e8a6 --- /dev/null +++ b/i/chuck/src/utils/meta/is_equal.hh @@ -0,0 +1,44 @@ +#ifndef is_equal_hh +#define is_equal_hh +// is_equal.hh - test if two types equals. +// {{{ +// +// Copyright (C) 2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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. +// +// }}} + +namespace meta { + +template +struct isEqual +{ + static const bool value = false; +}; + +template +struct isEqual +{ + static const bool value = true; +}; + +} // namespace meta + +#endif // is_equal_hh diff --git a/i/chuck/src/utils/meta/is_string.hh b/i/chuck/src/utils/meta/is_string.hh new file mode 100644 index 0000000..99782d2 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/meta/remove_reference.hh b/i/chuck/src/utils/meta/remove_reference.hh new file mode 100644 index 0000000..f12422d --- /dev/null +++ b/i/chuck/src/utils/meta/remove_reference.hh @@ -0,0 +1,44 @@ +#ifndef remove_reference_hh +#define remove_reference_hh +// remove_reference.hh - Remove reference from a type. +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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. +// +// }}} + +namespace meta { + +template +struct removeReference +{ + typedef T type; +}; + +template +struct removeReference +{ + typedef T type; +}; + +} // namespace meta + +#endif // remove_reference_hh diff --git a/i/chuck/src/utils/meta/test_meta.cc b/i/chuck/src/utils/meta/test_meta.cc new file mode 100644 index 0000000..7c973d2 --- /dev/null +++ b/i/chuck/src/utils/meta/test_meta.cc @@ -0,0 +1,57 @@ +// test_meta.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2003-2006 Nicolas Schodet +// +// Robot APB Team/Efrei 2006. +// 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 "is_string.hh" +#include "is_equal.hh" +#include "remove_reference.hh" + +#include + +int +main (void) +{ + // Test isString. + std::cout << "isString" << std::endl; + 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; + // Test isEqual. + std::cout << "isEqual" << std::endl; + std::cout << " int int " << meta::isEqual::value << std::endl; + std::cout << " int& int " << meta::isEqual::value << std::endl; + std::cout << " int& int& " << meta::isEqual::value << std::endl; + std::cout << " int double " << meta::isEqual::value << std::endl; + // Test removeReference. + std::cout << "removeReference" << std::endl; + std::cout << " int " + << meta::isEqual::type>::value + << std::endl; + std::cout << " int& " + << meta::isEqual::type>::value + << std::endl; + std::cout << " const int& " + << meta::isEqual::type>::value + << std::endl; + return 0; +} diff --git a/i/chuck/src/utils/non_copyable.hh b/i/chuck/src/utils/non_copyable.hh new file mode 100644 index 0000000..e788365 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/signalhandler.hh b/i/chuck/src/utils/signalhandler.hh new file mode 100644 index 0000000..43e0f89 --- /dev/null +++ b/i/chuck/src/utils/signalhandler.hh @@ -0,0 +1,52 @@ +#ifndef signalhandler_hh +#define signalhandler_hh +// signalhandler.hh +// // marvin - programme du robot 2006. {{{ +// // +// // Copyright (C) 2003-2006 Sebastien beaufour +// // +// // Robot APB Team/Efrei 2006. +// // 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 "callback.hh" + +template +class SignalHandler +{ + private: + struct sigaction action; + struct sigaction old; + static Callback reset; + static void DefaultReset(); + public: + static void trapsigint(int a); + void bind(Callback r); + SignalHandler(); + ~SignalHandler(); +}; +#include "signalhandler.tcc" + +#endif // end of signalhandler_hh + diff --git a/i/chuck/src/utils/signalhandler.tcc b/i/chuck/src/utils/signalhandler.tcc new file mode 100644 index 0000000..04a1660 --- /dev/null +++ b/i/chuck/src/utils/signalhandler.tcc @@ -0,0 +1,66 @@ +// signalhandler.cc +// // // marvin - programme du robot 2006. {{{ +// // // +// // // Copyright (C) 2003-2006 Sebastien Beaufour +// // // +// // // Robot APB Team/Efrei 2006. +// // // 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 "signalhandler.hh" + +/// reset NEED a default reset, so here is a default reset with nothing inside +template void SignalHandler::DefaultReset (void) +{ + // Nothing +} + +// Now we can set the default value to reset +template Callback SignalHandler::reset=SignalHandler::DefaultReset; + +///Static member which will be use by sigaction +template void SignalHandler::trapsigint(int a) +{ + std::cout << "Trap du crtl+c "<< std::endl; + reset(); +} + +///Function to bind to the function to be called when signal il caught +template void SignalHandler::bind(Callback r) +{ + reset=r; + action.sa_handler = trapsigint; + action.sa_flags = SA_ONESHOT; + sigaction(sig,&action,NULL); +} + +/// Constructor, nothing to do +template SignalHandler::SignalHandler() +{ + //nada +} + +///Destructor, put the old sig handler back +template SignalHandler::~SignalHandler() +{ + sigaction(sig,&old,NULL); +} diff --git a/i/chuck/src/utils/test_any.cc b/i/chuck/src/utils/test_any.cc new file mode 100644 index 0000000..85d2319 --- /dev/null +++ b/i/chuck/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/chuck/src/utils/test_bind.cc b/i/chuck/src/utils/test_bind.cc new file mode 100644 index 0000000..b883cbe --- /dev/null +++ b/i/chuck/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/chuck/src/utils/test_callback.cc b/i/chuck/src/utils/test_callback.cc new file mode 100644 index 0000000..2a5b6f0 --- /dev/null +++ b/i/chuck/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; +} diff --git a/i/chuck/src/utils/test_signalhandler.cc b/i/chuck/src/utils/test_signalhandler.cc new file mode 100644 index 0000000..e25b1d2 --- /dev/null +++ b/i/chuck/src/utils/test_signalhandler.cc @@ -0,0 +1,15 @@ +#include +#include "signalhandler.hh" + +void res() +{ + std::cout << "RESET" << std::endl; +} + +int main() +{ + SignalHandler test; + test.bind(res); + while(1); + return 0; +} -- cgit v1.2.3