summaryrefslogtreecommitdiff
path: root/i/marvin
diff options
context:
space:
mode:
authorbeaufour2006-04-29 23:20:51 +0000
committerbeaufour2006-04-29 23:20:51 +0000
commit1cd43840d6359864352734a505d21599ad62a473 (patch)
treea6533dab65e2292914ae98c71c34ca74ff22a835 /i/marvin
parentc4d106054587e45e370c237124773f0e07a81407 (diff)
Classe permettant d'associer une action a un signal
Diffstat (limited to 'i/marvin')
-rw-r--r--i/marvin/src/utils/Makefile.defs4
-rw-r--r--i/marvin/src/utils/signalhandler.cc66
-rw-r--r--i/marvin/src/utils/signalhandler.hh51
-rw-r--r--i/marvin/src/utils/test_signalHandler.cc15
4 files changed, 135 insertions, 1 deletions
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<int sig> void SignalHandler<sig>::DefaultReset (void)
+{
+ // Nothing
+}
+
+// Now we can set the default value to reset
+template<int sig> Callback<void> SignalHandler<sig>::reset=SignalHandler::DefaultReset;
+
+///Static member which will be use by sigaction
+template<int sig> void SignalHandler<sig>::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<int sig> void SignalHandler<sig>::bind(Callback<void> r)
+{
+ reset=r;
+ action.sa_handler = trapsigint;
+ action.sa_flags = SA_ONESHOT;
+ sigaction(sig,&action,NULL);
+}
+
+/// Constructor, nothing to do
+template<int sig> SignalHandler<sig>::SignalHandler()
+{
+ //nada
+}
+
+///Destructor, put the old sig handler back
+template<int sig> SignalHandler<sig>::~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 <stdio.h>
+#include <signal.h>
+#include <iostream>
+#include "callback.hh"
+
+template<int sig>
+class SignalHandler
+{
+ private:
+ struct sigaction action;
+ struct sigaction old;
+ static Callback<void> reset;
+ static void DefaultReset();
+ public:
+ static void trapsigint(int a);
+ void bind(Callback<void> 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 <iostream>
+#include "signalhandler.hh"
+
+void res()
+{
+ std::cout << "RESET" << std::endl;
+}
+
+int main()
+{
+ SignalHandler<SIGINT> test;
+ test.bind(res);
+ while(1);
+ return 0;
+}