Medical Imaging Interaction Toolkit  2026.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 
33  class ExternalProcessOutputEvent : public itk::AnyEvent
34  {
35  public:
37  typedef itk::AnyEvent Superclass;
38 
43  explicit ExternalProcessOutputEvent(const std::string &output = "") : m_Output(output) {}
45 
46  const char *GetEventName() const override { return "ExternalProcessOutputEvent"; }
47  bool CheckEvent(const ::itk::EventObject *e) const override { return dynamic_cast<const Self *>(e); }
48  itk::EventObject *MakeObject() const override { return new Self(m_Output); }
49 
51  std::string GetOutput() const { return m_Output; }
52 
53  private:
54  std::string m_Output;
55  };
56 
57 #define mitkProcessExecutorEventMacro(classname) \
58  class classname : public ExternalProcessOutputEvent \
59  { \
60  public: \
61  typedef classname Self; \
62  typedef ExternalProcessOutputEvent Superclass; \
63  \
64  explicit classname(const std::string &output) : Superclass(output) {} \
65  ~classname() override {} \
66  \
67  virtual const char *GetEventName() const override { return #classname; } \
68  virtual bool CheckEvent(const ::itk::EventObject *e) const override { return dynamic_cast<const Self *>(e); } \
69  virtual ::itk::EventObject *MakeObject() const override { return new Self(this->GetOutput()); } \
70  };
71 
74 
81  class MITKSEGMENTATION_EXPORT ProcessExecutor : public itk::Object
82  {
83  public:
85  using Superclass = ::itk::Object;
88 
89  itkTypeMacro(ProcessExecutor, ::itk::Object);
90  itkFactorylessNewMacro(Self);
91 
92  itkSetMacro(SharedOutputPipes, bool);
93  itkGetConstMacro(SharedOutputPipes, bool);
94 
96  using ArgumentListType = std::vector<std::string>;
97 
105  bool Execute(const std::string &executionPath, const std::string &executableName, ArgumentListType &argumentList);
106 
117  virtual bool Execute(const std::string &executionPath, const ArgumentListType &argumentList);
118 
124 
130  static std::string EnsureCorrectOSPathSeparator(const std::string & path);
131 
137  static std::string GetOSDependendExecutableName(const std::string &name);
138 
142  void KillProcess();
143 
144  protected:
146  ~ProcessExecutor() override;
147 
149 
156 
157  private:
158  itksysProcess *m_ProcessID = nullptr;
159  };
160 
161 } // namespace mitk
162 #endif
#define MITKSEGMENTATION_EXPORT
Event carrying output text from an external process.
const char * GetEventName() const override
bool CheckEvent(const ::itk::EventObject *e) const override
itk::EventObject * MakeObject() const override
std::string GetOutput() const
Returns the output text from the external process.
ExternalProcessOutputEvent(const std::string &output="")
Constructor.
ExternalProcessOutputEvent Self
You may register an observer for an ExternalProcessOutputEvent, ExternalProcessStdOutEvent or Externa...
~ProcessExecutor() override
static std::string GetOSDependendExecutableName(const std::string &name)
Returns the OS-dependent executable name (e.g. adds ".exe" on Windows).
bool m_SharedOutputPipes
Specifies if the child process should share the output pipes (true) or not (false)....
bool Execute(const std::string &executionPath, const std::string &executableName, ArgumentListType &argumentList)
Executes an external process.
int GetExitValue()
Returns the exit value of the last executed process.
std::vector< std::string > ArgumentListType
Type for the list of command-line arguments.
virtual bool Execute(const std::string &executionPath, const ArgumentListType &argumentList)
Executes an external process.
static std::string EnsureCorrectOSPathSeparator(const std::string &path)
Converts path separators to the OS-correct form.
void KillProcess()
Kills the currently running process.
#define mitkProcessExecutorEventMacro(classname)
Find image slices visible on a given plane.