Medical Imaging Interaction Toolkit  2022.04.99-194dd5d8
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 // Class is adapted from MatchPoint ProcessExecutor
14 
15 #ifndef __MITK_PROCESS_EXECUTOR_H
16 #define __MITK_PROCESS_EXECUTOR_H
17 
19 #include <itkObject.h>
20 #include <vector>
21 #include <itksys/Process.h>
22 
23 namespace mitk
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 { return #classname; } \
54  virtual bool CheckEvent(const ::itk::EventObject *e) const { return dynamic_cast<const Self *>(e); } \
55  virtual ::itk::EventObject *MakeObject() const { 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  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
bool CheckEvent(const ::itk::EventObject *e) const override
You may register an observer for an ExternalProcessOutputEvent, ExternalProcessStdOutEvent or Externa...
#define mitkProcessExecutorEventMacro(classname)
#define MITKSEGMENTATION_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
bool m_SharedOutputPipes
Specifies if the child process should share the output pipes (true) or not (false). If pipes are not shared the output will be passed by invoking ExternalProcessOutputEvents.
const char * GetEventName() const override
ExternalProcessOutputEvent(const std::string &output="")
std::vector< std::string > ArgumentListType
ExternalProcessOutputEvent Self
itk::EventObject * MakeObject() const override