]> Creatis software - gdcm.git/blob - gdcmPython/gdcmCommandPy.h
Fix mistypings
[gdcm.git] / gdcmPython / gdcmCommandPy.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmCommandPy.h,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:07 $
7   Version:   $Revision: 1.3 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #ifndef GDCMCOMMANDPY_H
20 #define GDCMCOMMANDPY_H
21
22 #include "gdcmDebug.h"
23 #include "gdcmCommand.h"
24 #include "Python.h"
25
26 namespace GDCM_NAME_SPACE 
27 {
28 //-----------------------------------------------------------------------------
29 /**
30  * \brief CommandPy base class to react on a gdcm event
31  *
32  * \remarks The execution parameter depends on the
33  */
34 class CommandPy : public Command
35 {
36    gdcmTypeMacro(CommandPy);
37
38 public:
39    static CommandPy *New() {return new CommandPy(); }
40
41    void SetCallback(PyObject *callback)
42    {
43       Callback = callback;
44       Py_INCREF(Callback);
45    }
46
47    virtual void Execute()
48    {
49       PyObject *arglist = Py_BuildValue("()");
50       PyObject *result = PyEval_CallObject(Callback, arglist);
51       Py_DECREF(arglist);
52
53       if (result)
54       {
55          Py_XDECREF(result);
56       }
57       else
58       {
59          if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
60          {
61             std::cerr << "Caught a Ctrl-C within python, exiting program.\n";
62             Py_Exit(1);
63          }
64          PyErr_Print();
65       }
66    }
67
68 protected:
69    CommandPy()
70    {
71       Callback = NULL;
72    }
73    virtual ~CommandPy() 
74    {
75       if (Callback)
76          Py_DECREF(Callback);
77    }
78
79 private:
80    /// pointer to the initialisation method for any progress bar   
81    PyObject *Callback;
82 };
83 } // end namespace gdcm
84
85 //-----------------------------------------------------------------------------
86 #endif