From 86c65de3ae99420ca7a2af406a97ade6aa989774 Mon Sep 17 00:00:00 2001 From: schodet Date: Mon, 17 Jan 2005 15:04:29 +0000 Subject: Initial revision --- 2005/i/robert/build/Makefile | 2 + 2005/i/robert/src/Makefile.defs | 29 ++++ 2005/i/robert/src/README | 1 + 2005/i/robert/src/geometry/vector.hh | 67 +++++++++ 2005/i/robert/src/geometry/vector.tcc | 163 +++++++++++++++++++++ 2005/i/robert/src/log/Makefile.defs | 6 + 2005/i/robert/src/log/log.cc | 120 +++++++++++++++ 2005/i/robert/src/log/log.hh | 70 +++++++++ 2005/i/robert/src/log/log_message.cc | 74 ++++++++++ 2005/i/robert/src/log/log_message.hh | 52 +++++++ 2005/i/robert/src/log/log_message.tcc | 36 +++++ 2005/i/robert/src/log/test_log.cc | 34 +++++ 2005/i/robert/src/scheduler/Makefile.defs | 6 + 2005/i/robert/src/scheduler/schedulable.hh | 49 +++++++ 2005/i/robert/src/scheduler/schedulable_alarm.cc | 56 +++++++ 2005/i/robert/src/scheduler/schedulable_alarm.hh | 53 +++++++ 2005/i/robert/src/scheduler/schedulable_read_fd.cc | 51 +++++++ 2005/i/robert/src/scheduler/schedulable_read_fd.hh | 52 +++++++ 2005/i/robert/src/scheduler/scheduler.cc | 96 ++++++++++++ 2005/i/robert/src/scheduler/scheduler.hh | 53 +++++++ 2005/i/robert/src/scheduler/test_scheduler.cc | 68 +++++++++ 2005/i/robert/src/utils/Makefile.defs | 13 ++ 2005/i/robert/src/utils/any.hh | 106 ++++++++++++++ 2005/i/robert/src/utils/any.tcc | 158 ++++++++++++++++++++ 2005/i/robert/src/utils/bind.hh | 55 +++++++ 2005/i/robert/src/utils/callback.hh | 79 ++++++++++ 2005/i/robert/src/utils/callback.tcc | 126 ++++++++++++++++ 2005/i/robert/src/utils/fd_set.hh | 51 +++++++ 2005/i/robert/src/utils/meta/Makefile.defs | 5 + 2005/i/robert/src/utils/meta/is_string.hh | 45 ++++++ 2005/i/robert/src/utils/meta/test_is_string.cc | 35 +++++ 2005/i/robert/src/utils/test_any.cc | 48 ++++++ 2005/i/robert/src/utils/test_bind.cc | 45 ++++++ 2005/i/robert/src/utils/test_callback.cc | 84 +++++++++++ 34 files changed, 1988 insertions(+) create mode 100644 2005/i/robert/build/Makefile create mode 100644 2005/i/robert/src/Makefile.defs create mode 100644 2005/i/robert/src/README create mode 100644 2005/i/robert/src/geometry/vector.hh create mode 100644 2005/i/robert/src/geometry/vector.tcc create mode 100644 2005/i/robert/src/log/Makefile.defs create mode 100644 2005/i/robert/src/log/log.cc create mode 100644 2005/i/robert/src/log/log.hh create mode 100644 2005/i/robert/src/log/log_message.cc create mode 100644 2005/i/robert/src/log/log_message.hh create mode 100644 2005/i/robert/src/log/log_message.tcc create mode 100644 2005/i/robert/src/log/test_log.cc create mode 100644 2005/i/robert/src/scheduler/Makefile.defs create mode 100644 2005/i/robert/src/scheduler/schedulable.hh create mode 100644 2005/i/robert/src/scheduler/schedulable_alarm.cc create mode 100644 2005/i/robert/src/scheduler/schedulable_alarm.hh create mode 100644 2005/i/robert/src/scheduler/schedulable_read_fd.cc create mode 100644 2005/i/robert/src/scheduler/schedulable_read_fd.hh create mode 100644 2005/i/robert/src/scheduler/scheduler.cc create mode 100644 2005/i/robert/src/scheduler/scheduler.hh create mode 100644 2005/i/robert/src/scheduler/test_scheduler.cc create mode 100644 2005/i/robert/src/utils/Makefile.defs create mode 100644 2005/i/robert/src/utils/any.hh create mode 100644 2005/i/robert/src/utils/any.tcc create mode 100644 2005/i/robert/src/utils/bind.hh create mode 100644 2005/i/robert/src/utils/callback.hh create mode 100644 2005/i/robert/src/utils/callback.tcc create mode 100644 2005/i/robert/src/utils/fd_set.hh create mode 100644 2005/i/robert/src/utils/meta/Makefile.defs create mode 100644 2005/i/robert/src/utils/meta/is_string.hh create mode 100644 2005/i/robert/src/utils/meta/test_is_string.cc create mode 100644 2005/i/robert/src/utils/test_any.cc create mode 100644 2005/i/robert/src/utils/test_bind.cc create mode 100644 2005/i/robert/src/utils/test_callback.cc (limited to '2005') diff --git a/2005/i/robert/build/Makefile b/2005/i/robert/build/Makefile new file mode 100644 index 0000000..0ab6550 --- /dev/null +++ b/2005/i/robert/build/Makefile @@ -0,0 +1,2 @@ +SRCDIR = ../src +include $(SRCDIR)/Makefile.defs diff --git a/2005/i/robert/src/Makefile.defs b/2005/i/robert/src/Makefile.defs new file mode 100644 index 0000000..6b69819 --- /dev/null +++ b/2005/i/robert/src/Makefile.defs @@ -0,0 +1,29 @@ +CXXFLAGS = -O2 -Wall -g +CPPFLAGS = -MMD $(INCLUDES) +INCLUDES = -I$(SRCDIR) + +SUBDIRS = log scheduler utils utils/meta + +LINK.o = $(CXX) $(LDFLAGS) $(TARGET_ARCH) +VPATH = $(SUBDIRS:%=$(SRCDIR)/%) + +PROGRAMS = +LIBS = + +all: build-all + +# Inclus les autres répertoires. +include $(SUBDIRS:%=$(SRCDIR)/%/Makefile.defs) + +OBJECTS = $(filter %.o,$(foreach PROGRAM,$(PROGRAMS),$($(PROGRAM)_OBJECTS))) + +build-all: $(PROGRAMS) + +# Dépendances. +-include $(OBJECTS:%.o=%.d) + +# Ménage. +clean: + rm -f *.o *.d $(PROGRAMS) + +.PHONY: all build-all clean diff --git a/2005/i/robert/src/README b/2005/i/robert/src/README new file mode 100644 index 0000000..dce641a --- /dev/null +++ b/2005/i/robert/src/README @@ -0,0 +1 @@ +robert - programme du robot 2005. diff --git a/2005/i/robert/src/geometry/vector.hh b/2005/i/robert/src/geometry/vector.hh new file mode 100644 index 0000000..a54de2a --- /dev/null +++ b/2005/i/robert/src/geometry/vector.hh @@ -0,0 +1,67 @@ +#ifndef vector_hh +#define vector_hh +// vector.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 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 geometry { + +/// Vecteur 2d en coordonées carthésiennes. +template +struct VectorT +{ + T x, y; + VectorT (void) { } + VectorT (T x_, T y_) { x = x_; y = y_; } + VectorT (T v) { x = v; y = v; } + VectorT (const VectorT &v) { x = v.x; y = v.y; } + VectorT &operator= (const VectorT &v); + VectorT &operator+= (const VectorT &v); + VectorT &operator-= (const VectorT &v); + VectorT &operator*= (T scalar); + VectorT &operator/= (T scalar); + VectorT operator+ (const VectorT &v) const; + VectorT operator- (const VectorT &v) const; + VectorT operator* (T scalar) const; + T operator* (const VectorT &v) const; + VectorT operator/ (T scalar) const; + VectorT &normalize (void); + VectorT &rotate (void); + VectorT &rotate (T angle); +}; + +template +VectorT +operator* (T scalar, const VectorT &v); + +template +std::ostream &operator<< (std::ostream &os, const VectorT &v); + +typedef VectorT Vector; + +} // namespace geometry + +#include "vector.tcc" + +#endif // vector_hh diff --git a/2005/i/robert/src/geometry/vector.tcc b/2005/i/robert/src/geometry/vector.tcc new file mode 100644 index 0000000..ddcafd2 --- /dev/null +++ b/2005/i/robert/src/geometry/vector.tcc @@ -0,0 +1,163 @@ +// vector.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 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: +// }}} + +namespace geometry { + +template +VectorT & +VectorT::operator= (const VectorT &v) +{ + x = v.x; + y = v.y; + return *this; +} + +template +VectorT & +VectorT::operator+= (const VectorT &v) +{ + x += v.x; + y += v.y; + return *this; +} + +template +VectorT & +VectorT::operator-= (const VectorT &v) +{ + x -= v.x; + y -= v.y; + return *this; +} + +template +VectorT & +VectorT::operator*= (T scalar) +{ + x *= scalar; + y *= scalar; + return *this; +} + +template +VectorT & +VectorT::operator/= (T scalar) +{ + x /= scalar; + y /= scalar; + return *this; +} + +template +VectorT +VectorT::operator+ (const VectorT &v) const +{ + VectorT r (*this); + r += v; + return r; +} + +template +VectorT +VectorT::operator- (const VectorT &v) const +{ + VectorT r (*this); + r -= v; + return r; +} + +template +VectorT +VectorT::operator/ (T scalar) const +{ + VectorT r (*this); + r /= scalar; + return r; +} + +template +VectorT +VectorT::operator* (T scalar) const +{ + VectorT r (*this); + r *= scalar; + return r; +} + +template +VectorT +operator* (T scalar, const VectorT &v) +{ + VectorT r (v); + r *= scalar; + return r; +} + +template +T +VectorT::operator* (const VectorT &v) const +{ + return x * v.x + y * v.y; +} + +template +VectorT & +VectorT::normalize (void) +{ + T norm = sqrt (x * x + y * y); + x /= norm; + y /= norm; + return *this; +} + +/// Rotate by PI/2. +template +VectorT & +VectorT::rotate (void) +{ + T tx = x; + x = -y; + y = tx; + return *this; +} + +template +VectorT & +VectorT::rotate (T angle) +{ + T tx = x; + T c, s; + sincos (angle, *s, *c); + x = tx * c - y * s; + y = tx * s + y * c; +} + +template +std::ostream &operator<< (std::ostream &os, const VectorT &v) +{ + os << v.x << ' ' << v.y; + return os; +} + +} // namespace geometry diff --git a/2005/i/robert/src/log/Makefile.defs b/2005/i/robert/src/log/Makefile.defs new file mode 100644 index 0000000..b895d5c --- /dev/null +++ b/2005/i/robert/src/log/Makefile.defs @@ -0,0 +1,6 @@ +PROGRAMS += test_log + +test_log_OBJECTS = \ + test_log.o log.o log_message.o + +test_log: $(test_log_OBJECTS) diff --git a/2005/i/robert/src/log/log.cc b/2005/i/robert/src/log/log.cc new file mode 100644 index 0000000..7f65efa --- /dev/null +++ b/2005/i/robert/src/log/log.cc @@ -0,0 +1,120 @@ +// log.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 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 "log.hh" + +/// Constructeur. +Log::Log (const char *module) + : module_ (module), instance_ (0) +{ +} + +/// Constructeur. +Log::Log (const char *module, const char *instance) + : module_ (module), instance_ (instance) +{ +} + +/// Crée un nouveau LogMessage. +LogMessage +Log::operator() (const char *msg, Level level/*info*/) const +{ + return LogMessage (*this, msg, level); +} + +/// Traduit le niveau de log. +void +Log::toString (Level level, std::string &s) +{ + switch (level) + { + case none: + s = "none"; break; + case fatal: + s = "fatal"; break; + case error: + s = "error"; break; + case warning: + s = "warning"; break; + case info: + s = "info"; break; + case debug: + s = "debug"; break; + case verydebug: + s = "verydebug"; break; + default: + s = "logunknown"; break; + } +} + +/// Traduit le niveau de log. +Log::Level +Log::toLevel (const std::string &level) +{ + switch (level[0]) + { + case 'n': + return none; + case 'f': + return fatal; + case 'e': + return error; + case 'w': + return warning; + case 'i': + return info; + case 'd': + return debug; + case 'v': + return verydebug; + default: + return levelReserved; + } +} + +/// Traduit le masque de niveau de log. +Log::Level +Log::toLevelMask (const std::string &level) +{ + switch (level[0]) + { + case 'n': + return static_cast (none); + case 'f': + return static_cast (fatal); + case 'e': + return static_cast (error | fatal); + case 'w': + return static_cast (warning | error | fatal); + case 'i': + return static_cast (info | warning | error | fatal); + case 'd': + return static_cast (debug | info | warning | error | fatal); + case 'v': + return static_cast (verydebug | debug | info | warning | error | fatal); + default: + return toLevel (level); + } +} + diff --git a/2005/i/robert/src/log/log.hh b/2005/i/robert/src/log/log.hh new file mode 100644 index 0000000..026b1f0 --- /dev/null +++ b/2005/i/robert/src/log/log.hh @@ -0,0 +1,70 @@ +#ifndef log_hh +#define log_hh +// log.h +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 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 + +class LogMessage; + +/// Classe de log. Permet de construire des LogMessage. +class Log +{ + public: + enum Level + { + none = 0x00, + fatal = 0x01, + error = 0x02, + warning = 0x04, + info = 0x08, + debug = 0x10, + verydebug = 0x20, + levelReserved = 0xffff + }; + private: + const char *module_; + const char *instance_; + public: + /// Constructeur. + Log (const char *module); + /// Constructeur. + Log (const char *module, const char *instance); + /// Crée un nouveau LogMessage. + LogMessage operator() (const char *msg, Level level = info) const; + /// Récupère le module. + const char *getModule (void) const { return module_; } + /// Récupère l'instance. + const char *getInstance (void) const { return instance_; } + /// Traduit le niveau de log. + static void toString (Level level, std::string &s); + /// Traduit le niveau de log. + static Level toLevel (const std::string &level); + /// Traduit le masque de niveau de log. + static Level toLevelMask (const std::string &level); +}; + +#include "log_message.hh" + +#endif // log_hh diff --git a/2005/i/robert/src/log/log_message.cc b/2005/i/robert/src/log/log_message.cc new file mode 100644 index 0000000..68568c1 --- /dev/null +++ b/2005/i/robert/src/log/log_message.cc @@ -0,0 +1,74 @@ +// log_message.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 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 "log_message.hh" + +#include + +/// Constructeur. +LogMessage::LogMessage (const Log &log, const char *msg, Log::Level level) +{ + std::cout << log.getModule () << ':'; + if (log.getInstance ()) + std::cout << ' ' << log.getInstance () << ':'; +} + +/// Destructeur. +LogMessage::~LogMessage (void) +{ + std::cout << std::endl; +} + +/// Output a string or a variable name. +LogMessage & +LogMessage::operator<< (const char *s) +{ + std::cout << ' ' << s; + return *this; +} + +/// Output a string or a variable name. +LogMessage & +LogMessage::operator<< (const std::string &s) +{ + std::cout << ' ' << s; + return *this; +} + +/// Output a integer. +LogMessage & +LogMessage::operator<< (int i) +{ + std::cout << ' ' << i; + return *this; +} + +/// Output a double. +LogMessage & +LogMessage::operator<< (double d) +{ + std::cout << ' ' << d; + return *this; +} + diff --git a/2005/i/robert/src/log/log_message.hh b/2005/i/robert/src/log/log_message.hh new file mode 100644 index 0000000..e5a9e20 --- /dev/null +++ b/2005/i/robert/src/log/log_message.hh @@ -0,0 +1,52 @@ +#ifndef log_message_hh +#define log_message_hh +// log_message.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 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 "log.hh" + +class LogMessage +{ + public: + /// Constructeur. + LogMessage (const Log &log, const char *msg, Log::Level level); + /// Destructeur. + ~LogMessage (void); + /// Output a string or a variable name. + LogMessage &operator<< (const char *s); + /// Output a string or a variable name. + LogMessage &operator<< (const std::string &s); + /// Output a integer. + LogMessage &operator<< (int i); + /// Output a double. + LogMessage &operator<< (double d); + /// Output a OutputStreamable as a string. + template + LogMessage &operator<< (const OutputStreamable &o); +}; + +#include "log_message.tcc" + +#endif // log_message_hh diff --git a/2005/i/robert/src/log/log_message.tcc b/2005/i/robert/src/log/log_message.tcc new file mode 100644 index 0000000..ecf4970 --- /dev/null +++ b/2005/i/robert/src/log/log_message.tcc @@ -0,0 +1,36 @@ +// log_message.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 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 + +/// Output a OutputStreamable as a string. +template +LogMessage & +LogMessage::operator<< (const OutputStreamable &o) +{ + std::cout << o; + return *this; +} + diff --git a/2005/i/robert/src/log/test_log.cc b/2005/i/robert/src/log/test_log.cc new file mode 100644 index 0000000..0cd7d42 --- /dev/null +++ b/2005/i/robert/src/log/test_log.cc @@ -0,0 +1,34 @@ +// test_log.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 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 "log.hh" + +int +main (void) +{ + Log log ("main"); + log ("foo") << "bar" << 4 << "foobar" << 5.6; + log ("bar", Log::error) << "foo" << 5; + return 0; +} diff --git a/2005/i/robert/src/scheduler/Makefile.defs b/2005/i/robert/src/scheduler/Makefile.defs new file mode 100644 index 0000000..73b5da9 --- /dev/null +++ b/2005/i/robert/src/scheduler/Makefile.defs @@ -0,0 +1,6 @@ +PROGRAMS += test_scheduler + +test_scheduler_OBJECTS = \ + test_scheduler.o scheduler.o schedulable_read_fd.o schedulable_alarm.o + +test_scheduler: $(test_scheduler_OBJECTS) diff --git a/2005/i/robert/src/scheduler/schedulable.hh b/2005/i/robert/src/scheduler/schedulable.hh new file mode 100644 index 0000000..bfe6de3 --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable.hh @@ -0,0 +1,49 @@ +#ifndef schedulable_hh +#define schedulable_hh +// schedulable.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: +// }}} + +class FdSet; + +namespace scheduler { + +class Scheduler; + +/// Classe abstraite pour un objet utilisable par le Scheduler. +class Schedulable +{ + public: + /// Destructeur. + virtual ~Schedulable (void) {} + /// Paramètre le Scheduler. + virtual void setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout) = + 0; + /// Appelé aprés l'attente du Scheduler, renvois true si un événement a + /// été traité. + virtual bool run (Scheduler &scheduler, int time, const FdSet &readSet) = 0; +}; + +} // namespace scheduler + +#endif // schedulable_hh diff --git a/2005/i/robert/src/scheduler/schedulable_alarm.cc b/2005/i/robert/src/scheduler/schedulable_alarm.cc new file mode 100644 index 0000000..c55832e --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_alarm.cc @@ -0,0 +1,56 @@ +// schedulable_alarm.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable_alarm.hh" + +namespace scheduler { + +/// Paramètre le Scheduler. +void +SchedulableAlarm::setup (Scheduler &scheduler, int time, FdSet &readSet, int + &timeout) +{ + if (start_ != -1) + { + int to = start_ + step_ - time; + timeout = to > 0 ? to : 0; + } +} + +/// Appelé aprés l'attente du Scheduler, renvois true si un événement a +/// été traité. +bool +SchedulableAlarm::run (Scheduler &scheduler, int time, const FdSet &readSet) +{ + if (time > start_ + step_) + { + start_ = periodic_ ? start_ + step_ : -1; + callback_ (); + return true; + } + else + return false; +} + +} // namespace scheduler + diff --git a/2005/i/robert/src/scheduler/schedulable_alarm.hh b/2005/i/robert/src/scheduler/schedulable_alarm.hh new file mode 100644 index 0000000..f416d12 --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_alarm.hh @@ -0,0 +1,53 @@ +#ifndef schedulable_alarm_hh +#define schedulable_alarm_hh +// schedulable_alarm.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 "schedulable.hh" +#include "utils/callback.hh" + +namespace scheduler { + +/// Execute un callback à un instant donné. +class SchedulableAlarm : public Schedulable +{ + Callback callback_; + int start_, step_; + bool periodic_; + public: + /// Constructeur. + template + SchedulableAlarm (T callback, int start, int step, bool periodic = true) + : callback_ (callback), + start_ (start), step_ (step), periodic_ (periodic) + { } + /// Paramètre le Scheduler. + void setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout); + /// Appelé aprés l'attente du Scheduler, renvois true si un événement a + /// été traité. + bool run (Scheduler &scheduler, int time, const FdSet &readSet); +}; + +} // namespace scheduler + +#endif // schedulable_alarm_hh diff --git a/2005/i/robert/src/scheduler/schedulable_read_fd.cc b/2005/i/robert/src/scheduler/schedulable_read_fd.cc new file mode 100644 index 0000000..3c84c9b --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_read_fd.cc @@ -0,0 +1,51 @@ +// schedulable_read_fd.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "schedulable_read_fd.hh" +#include "utils/fd_set.hh" + +namespace scheduler { + +/// Paramètre le Scheduler. +void +SchedulableReadFd::setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout) +{ + readSet.set (fd_); +} + +/// Appelé aprés l'attente du Scheduler, renvois true si un événement a +/// été traité. +bool +SchedulableReadFd::run (Scheduler &scheduler, int time, const FdSet &readSet) +{ + if (readSet.isSet (fd_)) + { + callback_ (); + return true; + } + else + return false; +} + +} // namespace scheduler + diff --git a/2005/i/robert/src/scheduler/schedulable_read_fd.hh b/2005/i/robert/src/scheduler/schedulable_read_fd.hh new file mode 100644 index 0000000..01d7b26 --- /dev/null +++ b/2005/i/robert/src/scheduler/schedulable_read_fd.hh @@ -0,0 +1,52 @@ +#ifndef schedulable_read_fd_hh +#define schedulable_read_fd_hh +// schedulable_read_fd.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 "schedulable.hh" +#include "utils/callback.hh" + +namespace scheduler { + +/// Execute un callback sur une lecture possible sur un descripteur de +/// fichier. +class SchedulableReadFd : public Schedulable +{ + Callback callback_; + int fd_; + public: + /// Constructeur. + template + SchedulableReadFd (T callback, int fd) + : callback_ (callback), fd_ (fd) + { } + /// Paramètre le Scheduler. + void setup (Scheduler &scheduler, int time, FdSet &readSet, int &timeout); + /// Appelé aprés l'attente du Scheduler, renvois true si un événement a + /// été traité. + bool run (Scheduler &scheduler, int time, const FdSet &readSet); +}; + +} // namespace scheduler + +#endif // schedulable_read_fd_hh diff --git a/2005/i/robert/src/scheduler/scheduler.cc b/2005/i/robert/src/scheduler/scheduler.cc new file mode 100644 index 0000000..e37504f --- /dev/null +++ b/2005/i/robert/src/scheduler/scheduler.cc @@ -0,0 +1,96 @@ +// scheduler.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "scheduler.hh" +#include "utils/fd_set.hh" + +#include //XXX +#include +#include + +namespace scheduler { + +/// Destructeur. +Scheduler::~Scheduler (void) +{ + // TODO: Test for schedulables_ empty ? +} + +/// Ajoute un élément schedulable. +void +Scheduler::insert (Schedulable &schedulable) +{ + schedulables_.insert (&schedulable); +} + +/// Enlève un élément schedulable. +void +Scheduler::erase (Schedulable &schedulable) +{ + schedulables_.erase (&schedulable); +} + +/// Lance le scheduler. Si timeout est différent de -1, c'est le temps +/// maximal que prend le scheduler avant de rendre la main. Si +/// returnOnEvent est true, le premier évenement arrète le scheduler. +/// Renvois true si au moins un événement a été traité. +bool +Scheduler::schedule (int timeout/*-1*/, bool returnOnEvent/*false*/) +{ + bool event = false; + int t = time (0) * 1000; //XXX + int start = t; + do + { + // Prépare le select. + int to = timeout == -1 ? -1 : start + timeout - t; + FdSet readFds; + for (Schedulables::const_iterator i = schedulables_.begin (); + i != schedulables_.end (); + ++i) + { + int top = -1; + (*i)->setup (*this, t, readFds, top); + if (to == -1 || top != -1 && top < to) + { + to = top; + } + } + // Select. + timeval tv; + tv.tv_sec = to / 1000; + tv.tv_usec = to % 1000 * 1000; + select (FD_SETSIZE, readFds.get (), 0, 0, &tv); + // Run. + t = time (0) * 1000; //XXX + for (Schedulables::const_iterator i = schedulables_.begin (); + i != schedulables_.end (); + ++i) + event |= (*i)->run (*this, t, readFds); + t = time (0) * 1000; //XXX + } while (!(returnOnEvent && event) && start + timeout > t); //XXX + return event; +} + +} // namespace scheduler + diff --git a/2005/i/robert/src/scheduler/scheduler.hh b/2005/i/robert/src/scheduler/scheduler.hh new file mode 100644 index 0000000..3bd885b --- /dev/null +++ b/2005/i/robert/src/scheduler/scheduler.hh @@ -0,0 +1,53 @@ +#ifndef scheduler_hh +#define scheduler_hh +// scheduler.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 "schedulable.hh" + +#include + +namespace scheduler { + +/// Scheduler, répartie le travail en fonction des événements. +class Scheduler +{ + typedef std::set Schedulables; + Schedulables schedulables_; + public: + /// Destructeur. + ~Scheduler (void); + /// Ajoute un élément schedulable. + void insert (Schedulable &schedulable); + /// Enlève un élément schedulable. + void erase (Schedulable &schedulable); + /// Lance le scheduler. Si timeout est différent de -1, c'est le temps + /// maximal que prend le scheduler avant de rendre la main. Si + /// returnOnEvent est true, le premier évenement arrète le scheduler. + /// Renvois true si au moins un événement a été traité. + bool schedule (int timeout = -1, bool returnOnEvent = false); +}; + +} // namespace scheduler + +#endif // scheduler_hh diff --git a/2005/i/robert/src/scheduler/test_scheduler.cc b/2005/i/robert/src/scheduler/test_scheduler.cc new file mode 100644 index 0000000..e16ecb1 --- /dev/null +++ b/2005/i/robert/src/scheduler/test_scheduler.cc @@ -0,0 +1,68 @@ +// test_scheduler.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "scheduler.hh" +#include "schedulable_read_fd.hh" +#include "schedulable_alarm.hh" +#include "utils/bind.hh" + +#include +#include +#include + +void +ev (void) +{ + std::cout << "event received" << std::endl; + char buf[1024]; + read (0, buf, 1024); +} + +void +to (const char *msg) +{ + std::cout << "timeout received " << msg << " at " << time (0) * 1000 << + std::endl; +} + +int +main (void) +{ + using namespace scheduler; + try + { + Scheduler s; + SchedulableReadFd srf (ev, 0); + s.insert (srf); + std::cout << "start at " << time (0) * 1000 << std::endl; + SchedulableAlarm sa1 (utils::bind (to, "3s"), time (0) * 1000, 1000); + s.insert (sa1); + s.schedule (10000); + } + catch (const std::exception &e) + { + std::cerr << e.what () << std::endl; + return 1; + } + return 0; +} diff --git a/2005/i/robert/src/utils/Makefile.defs b/2005/i/robert/src/utils/Makefile.defs new file mode 100644 index 0000000..a06d61c --- /dev/null +++ b/2005/i/robert/src/utils/Makefile.defs @@ -0,0 +1,13 @@ +PROGRAMS += test_any test_callback test_bind + +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/2005/i/robert/src/utils/any.hh b/2005/i/robert/src/utils/any.hh new file mode 100644 index 0000000..1c36875 --- /dev/null +++ b/2005/i/robert/src/utils/any.hh @@ -0,0 +1,106 @@ +#ifndef any_hh +#define any_hh +// any.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include +#include +#include + +/// Class containing anything. +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); + any &swap (any &other); + any &operator= (const any &other); + template + any &operator= (const T &value); + bool empty (void) const; + 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; + }; +}; + +class bad_any_cast : public std::bad_cast +{ + public: + virtual const char * what() const throw() + { + return "bad_any_cast: illegal conversion"; + } +}; + +template +T * +any_cast (any *rhs); + +template +const T * +any_cast (const any *rhs); + +template +T +any_cast (const any &rhs); + +std::ostream & +operator<< (std::ostream &os, const any &rhs); + +#include "any.tcc" + +#endif // any_hh diff --git a/2005/i/robert/src/utils/any.tcc b/2005/i/robert/src/utils/any.tcc new file mode 100644 index 0000000..c7fd4a0 --- /dev/null +++ b/2005/i/robert/src/utils/any.tcc @@ -0,0 +1,158 @@ +// any.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +/// 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_; +} + +inline +any & +any::swap (any &other) +{ + std::swap (holder_, other.holder_); + return *this; +} + +inline +any & +any::operator= (const any &other) +{ + any (other).swap (*this); + return *this; +} + +template +any & +any::operator= (const T &value) +{ + any (value).swap (*this); + return *this; +} + +inline +bool +any::empty (void) const +{ + return !holder_; +} + +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_; +} + +template +T * +any_cast (any *rhs) +{ + return rhs && rhs->type () == typeid (T) + ? &static_cast *> (rhs->holder_)->value_ + : 0; +} + +template +const T * +any_cast (const any *rhs) +{ + return any_cast (const_cast (rhs)); +} + +template +T +any_cast (const any &rhs) +{ + const T *value = any_cast (&rhs); + if (!value) + throw bad_any_cast (); + return *value; +} + +inline +std::ostream & +operator<< (std::ostream &os, const any &rhs) +{ + return rhs.holder_->print (os); +} + diff --git a/2005/i/robert/src/utils/bind.hh b/2005/i/robert/src/utils/bind.hh new file mode 100644 index 0000000..2a92c78 --- /dev/null +++ b/2005/i/robert/src/utils/bind.hh @@ -0,0 +1,55 @@ +#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: +// }}} + +namespace utils { + +template +class binder +{ + F func_; + const A &arg_; + public: + typedef R result_type; + public: + binder (F func, const A &arg) + : func_ (func), arg_ (arg) + { } + result_type operator () (void) + { + return func_ (arg_); + } +}; + +template +binder +bind (F func, const A &arg) +{ + return binder (func, arg); +} + +} // namespace utils + +#endif // bind_hh diff --git a/2005/i/robert/src/utils/callback.hh b/2005/i/robert/src/utils/callback.hh new file mode 100644 index 0000000..f9eff8f --- /dev/null +++ b/2005/i/robert/src/utils/callback.hh @@ -0,0 +1,79 @@ +#ifndef callback_hh +#define callback_hh +// callback.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +template +class Callback +{ + class AbstractHolder; + AbstractHolder *holder_; + public: + typedef R result_type; + public: + Callback (void); + template + Callback (T callback); + Callback (const Callback &other); + ~Callback (void); + R operator () (void); + void swap (Callback &rhs); + Callback &operator= (const Callback &rhs); + template + Callback &operator= (T rhs); + bool empty (void) const; + operator bool (void) const; + private: + class AbstractHolder + { + public: + virtual ~AbstractHolder (void); + virtual R operator () (void) = 0; + virtual AbstractHolder *clone (void) = 0; + }; + template + class Holder : public AbstractHolder + { + private: + T callback_; + public: + Holder (T callback); + virtual R operator () (void); + virtual AbstractHolder *clone (void); + }; +}; + +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/2005/i/robert/src/utils/callback.tcc b/2005/i/robert/src/utils/callback.tcc new file mode 100644 index 0000000..36d97a7 --- /dev/null +++ b/2005/i/robert/src/utils/callback.tcc @@ -0,0 +1,126 @@ +// callback.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} + +#include + +template +Callback::Callback (void) + : holder_ (0) +{ +} + +template +template +Callback::Callback (T callback) + : holder_ (new Holder (callback)) +{ +} + +template +Callback::Callback (const Callback &other) + : holder_ (other.holder_ ? other.holder_->clone () : 0) +{ +} + +template +Callback::~Callback (void) +{ + delete holder_; +} + +template +R +Callback::operator () (void) +{ + if (!holder_) + throw bad_callback (); + else + return (*holder_) (); +} + +template +void +Callback::swap (Callback &rhs) +{ + std::swap (holder_, rhs.holder_); +} + +template +Callback & +Callback::operator= (const Callback &rhs) +{ + Callback (rhs).swap (*this); + return *this; +} + +template +template +Callback & +Callback::operator= (T rhs) +{ + Callback (rhs).swap (*this); + return *this; +} + +template +bool +Callback::empty (void) const +{ + return !holder_; +} + +template +Callback::operator bool (void) const +{ + return !empty (); +} + +template +Callback::AbstractHolder::~AbstractHolder (void) +{ +} + +template +template +Callback::Holder::Holder (T callback) + : callback_ (callback) +{ +} + +template +template +R +Callback::Holder::operator () (void) +{ + return callback_ (); +} + +template +template +typename Callback::AbstractHolder * +Callback::Holder::clone (void) +{ + return new Holder (callback_); +} + diff --git a/2005/i/robert/src/utils/fd_set.hh b/2005/i/robert/src/utils/fd_set.hh new file mode 100644 index 0000000..7cc53e5 --- /dev/null +++ b/2005/i/robert/src/utils/fd_set.hh @@ -0,0 +1,51 @@ +#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_; } +}; + +#endif // fd_set_hh diff --git a/2005/i/robert/src/utils/meta/Makefile.defs b/2005/i/robert/src/utils/meta/Makefile.defs new file mode 100644 index 0000000..5650f40 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/utils/meta/is_string.hh b/2005/i/robert/src/utils/meta/is_string.hh new file mode 100644 index 0000000..99782d2 --- /dev/null +++ b/2005/i/robert/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/2005/i/robert/src/utils/meta/test_is_string.cc b/2005/i/robert/src/utils/meta/test_is_string.cc new file mode 100644 index 0000000..c61483a --- /dev/null +++ b/2005/i/robert/src/utils/meta/test_is_string.cc @@ -0,0 +1,35 @@ +// test_is_string.cc +// {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "is_string.hh" + +#include + +int +main (void) +{ + std::cout << "std::string " << meta::isString::value << std::endl; + std::cout << "int " << meta::isString::value << std::endl; + std::cout << "char * " << meta::isString::value << std::endl; + return 0; +} diff --git a/2005/i/robert/src/utils/test_any.cc b/2005/i/robert/src/utils/test_any.cc new file mode 100644 index 0000000..e8af644 --- /dev/null +++ b/2005/i/robert/src/utils/test_any.cc @@ -0,0 +1,48 @@ +// 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 + { + std::cout << a << ' ' << b << ' ' << c << ' ' << d << std::endl; + 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/2005/i/robert/src/utils/test_bind.cc b/2005/i/robert/src/utils/test_bind.cc new file mode 100644 index 0000000..9ebdc4e --- /dev/null +++ b/2005/i/robert/src/utils/test_bind.cc @@ -0,0 +1,45 @@ +// 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; +} + +int +main (void) +{ + utils::bind (&f, 42) (); + utils::bind (&g, "hello world !") (); +} diff --git a/2005/i/robert/src/utils/test_callback.cc b/2005/i/robert/src/utils/test_callback.cc new file mode 100644 index 0000000..2a5b6f0 --- /dev/null +++ b/2005/i/robert/src/utils/test_callback.cc @@ -0,0 +1,84 @@ +// test_callback.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: http://perso.efrei.fr/~schodet/ +// Email: +// }}} +#include "callback.hh" + +#include + +void +f (void) +{ + std::cout << " f ()" << std::endl; +} + +int +i (void) +{ + static int v = 42; + std::cout << " i () = " << v << std::endl; + return v++; +} + +struct G +{ + void operator () (void) + { + std::cout << " g ()" << std::endl; + } +}; + +int +main (void) +{ + G g; + Callback cf (f); + Callback cg (g); + Callback ch; + Callback ci (i); + try + { + std::cout << "cf" << std::endl; + cf (); + std::cout << "cg" << std::endl; + cg (); + cf = cg; + cg = f; + std::cout << "cf" << std::endl; + cf (); + std::cout << "cg" << std::endl; + cg (); + std::cout << "cf " << (cf ? "not " : "") << "empty" << std::endl; + std::cout << "ch " << (ch ? "not " : "") << "empty" << std::endl; + std::cout << "ci" << std::endl; + int r = ci (); + std::cout << "ci = " << r << std::endl; + std::cout << "ch" << std::endl; + ch (); + } + catch (const std::exception &e) + { + std::cout << e.what () << std::endl; + return 1; + } + return 0; +} -- cgit v1.2.3