]> Creatis software - gdcm.git/blobdiff - gdcmPython/gdcmCommandPy.h
* Add the CommandPy and CommandManager to the python wrapping
[gdcm.git] / gdcmPython / gdcmCommandPy.h
diff --git a/gdcmPython/gdcmCommandPy.h b/gdcmPython/gdcmCommandPy.h
new file mode 100644 (file)
index 0000000..fb00b8a
--- /dev/null
@@ -0,0 +1,86 @@
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: gdcmCommandPy.h,v $
+  Language:  C++
+  Date:      $Date: 2005/11/28 17:47:38 $
+  Version:   $Revision: 1.1 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
+
+#ifndef GDCMCOMMANDPY_H
+#define GDCMCOMMANDPY_H
+
+#include "gdcmDebug.h"
+#include "gdcmCommand.h"
+#include "python.h"
+
+namespace gdcm 
+{
+//-----------------------------------------------------------------------------
+/**
+ * \brief CommandPy base class to react on a gdcm event
+ *
+ * \remarks The execution parameter depends on the
+ */
+class CommandPy : public Command
+{
+   gdcmTypeMacro(CommandPy);
+
+public:
+   static CommandPy *New() {return new CommandPy(); }
+
+   void SetCallback(PyObject *callback)
+   {
+      Callback = callback;
+      Py_INCREF(Callback);
+   }
+
+   virtual void Execute()
+   {
+      PyObject *arglist = Py_BuildValue("()");
+      PyObject *result = PyEval_CallObject(Callback, arglist);
+      Py_DECREF(arglist);
+
+      if (result)
+      {
+         Py_XDECREF(result);
+      }
+      else
+      {
+         if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
+         {
+            std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
+            Py_Exit(1);
+         }
+         PyErr_Print();
+      }
+   }
+
+protected:
+   CommandPy()
+   {
+      Callback = NULL;
+   }
+   virtual ~CommandPy() 
+   {
+      if (Callback)
+         Py_DECREF(Callback);
+   }
+
+private:
+   /// pointer to the initialisation method for any progress bar   
+   PyObject *Callback;
+};
+} // end namespace gdcm
+
+//-----------------------------------------------------------------------------
+#endif