Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkProcessExecutor.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkProcessExecutor_h
14 #define mitkProcessExecutor_h
15 
17 #include <itkObject.h>
18 #include <vector>
19 #include <itksys/Process.h>
20 
21 namespace mitk
22 {
23  // Class is adapted from MatchPoint ProcessExecutor
24 
25  class ExternalProcessOutputEvent : public itk::AnyEvent
26  {
27  public:
29  typedef itk::AnyEvent Superclass;
30 
31  explicit ExternalProcessOutputEvent(const std::string &output = "") : m_Output(output) {}
33 
34  const char *GetEventName() const override { return "ExternalProcessOutputEvent"; }
35  bool CheckEvent(const ::itk::EventObject *e) const override { return dynamic_cast<const Self *>(e); }
36  itk::EventObject *MakeObject() const override { return new Self(m_Output); }
37  std::string GetOutput() const { return m_Output; }
38 
39  private:
40  std::string m_Output;
41  };
42 
43 #define mitkProcessExecutorEventMacro(classname) \
44  class classname : public ExternalProcessOutputEvent \
45  { \
46  public: \
47  typedef classname Self; \
48  typedef ExternalProcessOutputEvent Superclass; \
49  \
50  explicit classname(const std::string &output) : Superclass(output) {} \
51  ~classname() override {} \
52  \
53  virtual const char *GetEventName() const override { return #classname; } \
54  virtual bool CheckEvent(const ::itk::EventObject *e) const override { return dynamic_cast<const Self *>(e); } \
55  virtual ::itk::EventObject *MakeObject() const override { return new Self(this->GetOutput()); } \
56  };
57 
60 
67  class MITKSEGMENTATION_EXPORT ProcessExecutor : public itk::Object
68  {
69  public:
71  using Superclass = ::itk::Object;
74 
75  itkTypeMacro(ProcessExecutor, ::itk::Object);
76  itkFactorylessNewMacro(Self);
77 
78  itkSetMacro(SharedOutputPipes, bool);
79  itkGetConstMacro(SharedOutputPipes, bool);
80 
81  using ArgumentListType = std::vector<std::string>;
82 
83  bool Execute(const std::string &executionPath, const std::string &executableName, ArgumentListType &argumentList);
84 
89  virtual bool Execute(const std::string &executionPath, const ArgumentListType &argumentList);
90 
91  int GetExitValue();
92  static std::string EnsureCorrectOSPathSeparator(const std::string &);
93 
94  static std::string GetOSDependendExecutableName(const std::string &name);
95 
96  void KillProcess();
97 
98  protected:
100  ~ProcessExecutor() override;
101 
103 
110 
111  private:
112  itksysProcess *m_ProcessID = nullptr;
113  };
114 
115 } // namespace mitk
116 #endif
mitk::ExternalProcessOutputEvent::Self
ExternalProcessOutputEvent Self
Definition: mitkProcessExecutor.h:28
mitk::ProcessExecutor::m_ExitValue
int m_ExitValue
Definition: mitkProcessExecutor.h:102
mitk::ProcessExecutor::ArgumentListType
std::vector< std::string > ArgumentListType
Definition: mitkProcessExecutor.h:81
itk::SmartPointer< Self >
mitkProcessExecutorEventMacro
#define mitkProcessExecutorEventMacro(classname)
Definition: mitkProcessExecutor.h:43
mitk::ExternalProcessOutputEvent::GetOutput
std::string GetOutput() const
Definition: mitkProcessExecutor.h:37
mitk::ExternalProcessOutputEvent::MakeObject
itk::EventObject * MakeObject() const override
Definition: mitkProcessExecutor.h:36
mitk::ExternalProcessStdOutEvent
Definition: mitkProcessExecutor.h:58
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::ExternalProcessOutputEvent
Definition: mitkProcessExecutor.h:25
mitk::ExternalProcessOutputEvent::Superclass
itk::AnyEvent Superclass
Definition: mitkProcessExecutor.h:29
MITKSEGMENTATION_EXPORT
#define MITKSEGMENTATION_EXPORT
Definition: MitkSegmentationExports.h:15
mitk::ExternalProcessStdErrEvent
Definition: mitkProcessExecutor.h:59
mitk::ExternalProcessOutputEvent::~ExternalProcessOutputEvent
~ExternalProcessOutputEvent() override
Definition: mitkProcessExecutor.h:32
mitk::ExternalProcessOutputEvent::CheckEvent
bool CheckEvent(const ::itk::EventObject *e) const override
Definition: mitkProcessExecutor.h:35
mitk::ExternalProcessOutputEvent::GetEventName
const char * GetEventName() const override
Definition: mitkProcessExecutor.h:34
mitk::ExternalProcessOutputEvent::ExternalProcessOutputEvent
ExternalProcessOutputEvent(const std::string &output="")
Definition: mitkProcessExecutor.h:31
MitkSegmentationExports.h
mitk::ProcessExecutor
You may register an observer for an ExternalProcessOutputEvent, ExternalProcessStdOutEvent or Externa...
Definition: mitkProcessExecutor.h:67
mitk::ProcessExecutor::m_SharedOutputPipes
bool m_SharedOutputPipes
Specifies if the child process should share the output pipes (true) or not (false)....
Definition: mitkProcessExecutor.h:109
mitk::ProcessExecutor::Superclass
::itk::Object Superclass
Definition: mitkProcessExecutor.h:71