summaryrefslogtreecommitdiff
path: root/i/marvin/src/utils
diff options
context:
space:
mode:
authorhaller2005-11-17 11:10:17 +0000
committerhaller2005-11-17 11:10:17 +0000
commitda4a77ff094a0dc74414ec1de2cac08e8b7b60f4 (patch)
tree402c39246c164af44648fd98d2ea85500d58df58 /i/marvin/src/utils
parent164d8870bc058450e6967a594dd9033d088316af (diff)
Importation brut du code récupérable de robert
Diffstat (limited to 'i/marvin/src/utils')
-rw-r--r--i/marvin/src/utils/Makefile.defs15
-rw-r--r--i/marvin/src/utils/any.hh117
-rw-r--r--i/marvin/src/utils/any.tcc169
-rw-r--r--i/marvin/src/utils/bind.hh91
-rw-r--r--i/marvin/src/utils/callback.hh95
-rw-r--r--i/marvin/src/utils/callback.tcc136
-rw-r--r--i/marvin/src/utils/errno_exception.hh48
-rw-r--r--i/marvin/src/utils/fd_set.cc53
-rw-r--r--i/marvin/src/utils/fd_set.hh54
-rw-r--r--i/marvin/src/utils/hexa.cc113
-rw-r--r--i/marvin/src/utils/hexa.hh49
-rw-r--r--i/marvin/src/utils/list_ostream_output.hh62
-rw-r--r--i/marvin/src/utils/mathutil.hh57
-rw-r--r--i/marvin/src/utils/meta/Makefile.defs5
-rw-r--r--i/marvin/src/utils/meta/is_string.hh45
-rw-r--r--i/marvin/src/utils/meta/test_is_string.cc35
-rw-r--r--i/marvin/src/utils/non_copyable.hh41
-rw-r--r--i/marvin/src/utils/test_any.cc50
-rw-r--r--i/marvin/src/utils/test_bind.cc57
-rw-r--r--i/marvin/src/utils/test_callback.cc84
20 files changed, 1376 insertions, 0 deletions
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: <contact@ni.fr.eu.org>
+// }}}
+
+#include <typeinfo>
+#include <exception>
+#include <iostream>
+
+/// This class can contain a data of any type.
+class any
+{
+ class AbstractHolder;
+ AbstractHolder *holder_;
+ public:
+ /// Default constructor.
+ any (void);
+ /// Constructor.
+ template<typename T>
+ 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<typename T>
+ 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<typename T>
+ 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<typename T>
+ 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<typename T>
+T *
+any_cast (any *rhs);
+
+/// Return a const pointer to the contained object or 0 on faillure.
+template<typename T>
+const T *
+any_cast (const any *rhs);
+
+/// Return a const reference to the contained object or throw a bad_any_cast
+/// on faillure.
+template<typename T>
+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: <contact@ni.fr.eu.org>
+// }}}
+#include "list_ostream_output.hh"
+
+#include <algorithm>
+
+/// Default constructor.
+inline
+any::any (void)
+ : holder_ (0)
+{
+}
+
+/// Constructor.
+template<typename T>
+any::any (const T &value)
+ : holder_ (new Holder<T> (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<typename T>
+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<typename T>
+any::Holder<T>::Holder (const T &value)
+ : value_ (value)
+{
+}
+
+template<typename T>
+const std::type_info &
+any::Holder<T>::type (void) const
+{
+ return typeid (T);
+}
+
+template<typename T>
+any::AbstractHolder *
+any::Holder<T>::clone (void) const
+{
+ return new Holder (value_);
+}
+
+template<typename T>
+std::ostream &
+any::Holder<T>::print (std::ostream &os) const
+{
+ return os << value_;
+}
+
+/// Return a pointer to the contained object or 0 on faillure.
+template<typename T>
+T *
+any_cast (any *rhs)
+{
+ return rhs && rhs->type () == typeid (T)
+ ? &static_cast<any::Holder<T> *> (rhs->holder_)->value_
+ : 0;
+}
+
+/// Return a const pointer to the contained object or 0 on faillure.
+template<typename T>
+const T *
+any_cast (const any *rhs)
+{
+ return any_cast<T> (const_cast<any *> (rhs));
+}
+
+/// Return a const reference to the contained object or throw a bad_any_cast
+/// on faillure.
+template<typename T>
+const T &
+any_cast (const any &rhs)
+{
+ const T *value = any_cast<T> (&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: <contact@ni.fr.eu.org>
+// }}}
+
+/// 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<typename R, typename F, typename A>
+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<typename R, typename F, typename A>
+ArgBinder<R, F, A>
+bind (F func, const A &arg)
+{
+ return ArgBinder<R, F, A> (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<typename R, typename C>
+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<typename R, typename C>
+ObjBinder<R, C>
+bind (R (C::*func) (void), C *obj)
+{
+ return ObjBinder<R, C> (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: <contact@ni.fr.eu.org>
+// }}}
+
+#include <stdexcept>
+
+/// Class to store a callback. This callback can be of any type (function or
+/// fonctor).
+template<typename R>
+class Callback
+{
+ class AbstractHolder;
+ AbstractHolder *holder_;
+ public:
+ /// Return value.
+ typedef R result_type;
+ public:
+ /// Default constructor. Make an empty callback.
+ Callback (void);
+ /// Constructor.
+ template<typename T>
+ 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<typename T>
+ 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<typename T>
+ 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: <contact@ni.fr.eu.org>
+// }}}
+
+#include <algorithm>
+
+/// Default constructor. Make an empty callback.
+template<typename R>
+Callback<R>::Callback (void)
+ : holder_ (0)
+{
+}
+
+/// Constructor.
+template<typename R>
+template<typename T>
+Callback<R>::Callback (T callback)
+ : holder_ (new Holder<T> (callback))
+{
+}
+
+/// Copy constructor.
+template<typename R>
+Callback<R>::Callback (const Callback<R> &other)
+ : holder_ (other.holder_ ? other.holder_->clone () : 0)
+{
+}
+
+/// Destructor.
+template<typename R>
+Callback<R>::~Callback (void)
+{
+ delete holder_;
+}
+
+/// Assignement operator.
+template<typename R>
+Callback<R> &
+Callback<R>::operator= (const Callback<R> &rhs)
+{
+ Callback<R> (rhs).swap (*this);
+ return *this;
+}
+
+/// Call the contained callback.
+template<typename R>
+R
+Callback<R>::operator () (void)
+{
+ if (!holder_)
+ throw bad_callback ();
+ else
+ return (*holder_) ();
+}
+
+/// Swap the callback content with another callback.
+template<typename R>
+void
+Callback<R>::swap (Callback<R> &rhs)
+{
+ std::swap (holder_, rhs.holder_);
+}
+
+/// Change the contained callback.
+template<typename R>
+template<typename T>
+Callback<R> &
+Callback<R>::operator= (T rhs)
+{
+ Callback<R> (rhs).swap (*this);
+ return *this;
+}
+
+/// Test if empty.
+template<typename R>
+bool
+Callback<R>::empty (void) const
+{
+ return !holder_;
+}
+
+/// Test if not empty.
+template<typename R>
+Callback<R>::operator bool (void) const
+{
+ return !empty ();
+}
+
+template<typename R>
+Callback<R>::AbstractHolder::~AbstractHolder (void)
+{
+}
+
+template<typename R>
+template<typename T>
+Callback<R>::Holder<T>::Holder (T callback)
+ : callback_ (callback)
+{
+}
+
+template<typename R>
+template<typename T>
+R
+Callback<R>::Holder<T>::operator () (void)
+{
+ return callback_ ();
+}
+
+template<typename R>
+template<typename T>
+typename Callback<R>::AbstractHolder *
+Callback<R>::Holder<T>::clone (void)
+{
+ return new Holder<T> (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 <string>
+#include <exception>
+#include <cstring>
+#include <errno.h>
+
+/// 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: <contact@ni.fr.eu.org>
+// }}}
+
+#include <sys/types.h>
+
+/// 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<int> (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 <iostream>
+#include <list>
+#include <vector>
+#include <algorithm>
+#include <iterator>
+
+/// Output any list.
+template<typename T>
+std::ostream &
+output_list (std::ostream &os, const T &list)
+{
+ os << "( ";
+ std::copy (list.begin (), list.end (),
+ std::ostream_iterator<typename T::value_type> (os, " "));
+ os << ')';
+ return os;
+}
+
+/// Output a list.
+template<typename T>
+std::ostream &
+operator<< (std::ostream &os, const std::list<T> &list)
+{
+ return output_list (os, list);
+}
+
+/// Output a vector.
+template<typename T>
+std::ostream &
+operator<< (std::ostream &os, const std::vector<T> &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 <cmath>
+
+/// 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: <contact@ni.fr.eu.org>
+// }}}
+
+#include <string>
+
+namespace meta {
+
+template<typename T>
+struct isString
+{
+ static const bool value = false;
+};
+
+template<>
+struct isString<std::string>
+{
+ 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: <contact@ni.fr.eu.org>
+// }}}
+#include "is_string.hh"
+
+#include <iostream>
+
+int
+main (void)
+{
+ std::cout << "std::string " << meta::isString<std::string>::value << std::endl;
+ std::cout << "int " << meta::isString<int>::value << std::endl;
+ std::cout << "char * " << meta::isString<char *>::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: <contact@ni.fr.eu.org>
+// }}}
+#include "any.hh"
+
+#include <exception>
+#include <iostream>
+#include <string>
+
+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<int> (a) << std::endl;
+ std::cout << any_cast<std::string> (b) << std::endl;
+ std::cout << any_cast<double> (c) << std::endl;
+ std::cout << any_cast<int> (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: <contact@ni.fr.eu.org>
+// }}}
+#include "bind.hh"
+
+#include <iostream>
+
+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<void> (&f, 42) ();
+ bind<void> (&g, "hello world !") ();
+ H h;
+ bind<void, H> (&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: <contact@ni.fr.eu.org>
+// }}}
+#include "callback.hh"
+
+#include <iostream>
+
+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<void> cf (f);
+ Callback<void> cg (g);
+ Callback<void> ch;
+ Callback<int> 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;
+}