From 1cd43840d6359864352734a505d21599ad62a473 Mon Sep 17 00:00:00 2001 From: beaufour Date: Sat, 29 Apr 2006 23:20:51 +0000 Subject: Classe permettant d'associer une action a un signal --- i/marvin/src/utils/Makefile.defs | 4 +- i/marvin/src/utils/signalhandler.cc | 66 ++++++++++++++++++++++++++++++++ i/marvin/src/utils/signalhandler.hh | 51 ++++++++++++++++++++++++ i/marvin/src/utils/test_signalHandler.cc | 15 ++++++++ 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 i/marvin/src/utils/signalhandler.cc create mode 100644 i/marvin/src/utils/signalhandler.hh create mode 100644 i/marvin/src/utils/test_signalHandler.cc (limited to 'i/marvin') diff --git a/i/marvin/src/utils/Makefile.defs b/i/marvin/src/utils/Makefile.defs index ebd316c..f9ed92a 100644 --- a/i/marvin/src/utils/Makefile.defs +++ b/i/marvin/src/utils/Makefile.defs @@ -1,4 +1,4 @@ -PROGRAMS += test_any test_callback test_bind +PROGRAMS += test_any test_callback test_bind test_signalhandler utils_OBJECTS = fd_set.o hexa.o @@ -7,3 +7,5 @@ test_any_OBJECTS = test_any.o test_callback_OBJECTS = test_callback.o test_bind_OBJECTS = test_bind.o + +test_signalhandler_OBJECT = testsignalhandler.o diff --git a/i/marvin/src/utils/signalhandler.cc b/i/marvin/src/utils/signalhandler.cc new file mode 100644 index 0000000..04a1660 --- /dev/null +++ b/i/marvin/src/utils/signalhandler.cc @@ -0,0 +1,66 @@ +// signalhandler.cc +// // // marvin - programme du robot 2006. {{{ +// // // +// // // Copyright (C) 2003-2006 Sebastien Beaufour +// // // +// // // Robot APB Team/Efrei 2006. +// // // Web: http://assos.efrei.fr/robot/ +// // // Email: robot AT efrei DOT fr +// // // +// // // This program is free software; you can redistribute it and/or modify +// // // it under the terms of the GNU General Public License as published by +// // // the Free Software Foundation; either version 2 of the License, or +// // // (at your option) any later version. +// // // +// // // This program is distributed in the hope that it will be useful, +// // // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // // GNU General Public License for more details. +// // // +// // // You should have received a copy of the GNU General Public License +// // // along with this program; if not, write to the Free Software +// // // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// // // +// // // }}} +// // +// // +// + +#include "signalhandler.hh" + +/// reset NEED a default reset, so here is a default reset with nothing inside +template void SignalHandler::DefaultReset (void) +{ + // Nothing +} + +// Now we can set the default value to reset +template Callback SignalHandler::reset=SignalHandler::DefaultReset; + +///Static member which will be use by sigaction +template void SignalHandler::trapsigint(int a) +{ + std::cout << "Trap du crtl+c "<< std::endl; + reset(); +} + +///Function to bind to the function to be called when signal il caught +template void SignalHandler::bind(Callback r) +{ + reset=r; + action.sa_handler = trapsigint; + action.sa_flags = SA_ONESHOT; + sigaction(sig,&action,NULL); +} + +/// Constructor, nothing to do +template SignalHandler::SignalHandler() +{ + //nada +} + +///Destructor, put the old sig handler back +template SignalHandler::~SignalHandler() +{ + sigaction(sig,&old,NULL); +} diff --git a/i/marvin/src/utils/signalhandler.hh b/i/marvin/src/utils/signalhandler.hh new file mode 100644 index 0000000..07f6970 --- /dev/null +++ b/i/marvin/src/utils/signalhandler.hh @@ -0,0 +1,51 @@ +#ifndef signalhandler_hh +#define signalhandler_hh +// signalhandler.hh +// // marvin - programme du robot 2006. {{{ +// // +// // Copyright (C) 2003-2006 Sebastien beaufour +// // +// // Robot APB Team/Efrei 2006. +// // Web: http://assos.efrei.fr/robot/ +// // Email: robot AT efrei DOT fr +// // +// // This program is free software; you can redistribute it and/or modify +// // it under the terms of the GNU General Public License as published by +// // the Free Software Foundation; either version 2 of the License, or +// // (at your option) any later version. +// // +// // This program is distributed in the hope that it will be useful, +// // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // GNU General Public License for more details. +// // +// // You should have received a copy of the GNU General Public License +// // along with this program; if not, write to the Free Software +// // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// // +// // }}} +// +// + +#include +#include +#include +#include "callback.hh" + +template +class SignalHandler +{ + private: + struct sigaction action; + struct sigaction old; + static Callback reset; + static void DefaultReset(); + public: + static void trapsigint(int a); + void bind(Callback r); + SignalHandler(); + ~SignalHandler(); +}; + +#endif // end of signalhandler_hh + diff --git a/i/marvin/src/utils/test_signalHandler.cc b/i/marvin/src/utils/test_signalHandler.cc new file mode 100644 index 0000000..e25b1d2 --- /dev/null +++ b/i/marvin/src/utils/test_signalHandler.cc @@ -0,0 +1,15 @@ +#include +#include "signalhandler.hh" + +void res() +{ + std::cout << "RESET" << std::endl; +} + +int main() +{ + SignalHandler test; + test.bind(res); + while(1); + return 0; +} -- cgit v1.2.3