PCL
ExternalProcess.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/ExternalProcess.h - Released 2024-01-13T15:47:58Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_ExternalProcess_h
53 #define __PCL_ExternalProcess_h
54 
56 
57 #ifndef __PCL_BUILDING_PIXINSIGHT_APPLICATION
58 
59 #include <pcl/Defs.h>
60 
61 #include <pcl/AutoPointer.h>
62 #include <pcl/Control.h>
63 #include <pcl/Flags.h>
64 #include <pcl/UIObject.h>
65 
66 namespace pcl
67 {
68 
69 // ----------------------------------------------------------------------------
70 
84 namespace ExternalProcessError
85 {
86  enum value_type
87  {
88  FailedToStart,
89  Crashed,
90  TimedOut,
91  ReadError,
92  WriteError,
93  UnknownError
94  };
95 }
96 
97 // ----------------------------------------------------------------------------
98 
109 class PCL_CLASS ExternalProcess : public UIObject
110 {
111 public:
112 
113 #ifndef __PCL_WINDOWS
114 
123  using pid_type = int64;
124 #else
125  using pid_type = void*;
126 #endif
127 
131  using error_code = ExternalProcessError::value_type;
132 
136  ExternalProcess();
137 
144  ~ExternalProcess() override
145  {
146  }
147 
152  ExternalProcess( const ExternalProcess& ) = delete;
153 
158  ExternalProcess& operator =( const ExternalProcess& ) = delete;
159 
164  ExternalProcess( ExternalProcess&& ) = delete;
165 
170  ExternalProcess& operator =( ExternalProcess&& ) = delete;
171 
179  void EnsureUnique() override
180  {
181  }
182 
188  static ExternalProcess& Null();
189 
211  void Start( const String& program, const StringList& arguments = StringList() );
212 
232  bool WaitForStarted( int ms = 6000 );
233 
253  bool WaitForFinished( int ms = 6000 );
254 
277  bool WaitForDataAvailable( int ms = 6000 );
278 
301  bool WaitForDataWritten( int ms = 6000 );
302 
319  void Terminate();
320 
335  void Kill();
336 
353  void CloseStandardInput();
354 
367  void CloseStandardOutput();
368 
381  void CloseStandardError();
382 
411  void RedirectStandardOutput( const String& filePath, bool append = false );
412 
438  void RedirectStandardOutput( ExternalProcess& process );
439 
456  void RedirectStandardError( const String& filePath, bool append = false );
457 
482  void RedirectStandardInput( const String& filePath );
483 
487  String WorkingDirectory() const;
488 
498  void SetWorkingDirectory( const String& dirPath );
499 
506  bool IsRunning() const;
507 
513  bool IsStarting() const;
514 
518  bool HasCrashed() const;
519 
527  pid_type PID() const;
528 
533  int ExitCode() const;
534 
542  size_type BytesAvailable() const;
543 
552  size_type BytesToWrite() const;
553 
563  ByteArray StandardOutput();
564 
574  ByteArray StandardError();
575 
591  ByteArray Read();
592 
597  void Write( const ByteArray& data );
598 
605  void Write( const String& text );
606 
612  void Write( const IsoString& text );
613 
619  void Write( const char* text );
620 
625  void Write( const void* data, size_type count );
626 
635  StringList Environment() const;
636 
650  void SetEnvironment( const StringList& environment );
651 
666  using process_event_handler = void (Control::*)( ExternalProcess& sender );
667 
684  using process_exit_event_handler = void (Control::*)( ExternalProcess& sender, int exitCode, bool exitOk );
685 
699  using process_error_event_handler = void (Control::*)( ExternalProcess& sender, error_code error );
700 
714  void OnStarted( process_event_handler handler, Control& receiver );
715 
729  void OnFinished( process_exit_event_handler handler, Control& receiver );
730 
745  void OnStandardOutputDataAvailable( process_event_handler handler, Control& receiver );
746 
761  void OnStandardErrorDataAvailable( process_event_handler handler, Control& receiver );
762 
776  void OnError( process_error_event_handler handler, Control& receiver );
777 
791  static int ExecuteProgram( const String& program, const StringList& arguments = StringList() );
792 
815  static pid_type StartProgram( const String& program,
816  const StringList& arguments = StringList(),
817  const String& workingDirectory = String() );
818 
819 private:
820 
821  struct EventHandlers
822  {
823  process_event_handler onStarted = nullptr;
824  process_exit_event_handler onFinished = nullptr;
825  process_event_handler onStandardOutputDataAvailable = nullptr;
826  process_event_handler onStandardErrorDataAvailable = nullptr;
827  process_error_event_handler onError = nullptr;
828 
829  EventHandlers() = default;
830  EventHandlers( const EventHandlers& ) = default;
831  EventHandlers& operator =( const EventHandlers& ) = default;
832  };
833 
834  AutoPointer<EventHandlers> m_handlers;
835 
836  ExternalProcess( void* );
837  void* CloneHandle() const override;
838 
839  friend class ExternalProcessPrivate;
840  friend class ExternalProcessEventDispatcher;
841 };
842 
843 // ----------------------------------------------------------------------------
844 
845 } // pcl
846 
847 #endif // __PCL_BUILDING_PIXINSIGHT_APPLICATION
848 
849 #endif // __PCL_ExternalProcess_h
850 
851 // ----------------------------------------------------------------------------
852 // EOF pcl/ExternalProcess.h - Released 2024-01-13T15:47:58Z
pcl
PCL root namespace.
Definition: AbstractImage.h:76
pcl::ExternalProcess::process_exit_event_handler
void(Control::*)(ExternalProcess &sender, int exitCode, bool exitOk) process_exit_event_handler
Definition: ExternalProcess.h:684
pcl::String
Unicode (UTF-16) string.
Definition: String.h:8112
pcl::ExternalProcess
Execution of external programs.
Definition: ExternalProcess.h:109
pcl::ExternalProcess::EnsureUnique
void EnsureUnique() override
Definition: ExternalProcess.h:179
pcl::ExternalProcess::process_error_event_handler
void(Control::*)(ExternalProcess &sender, error_code error) process_error_event_handler
Definition: ExternalProcess.h:699
pcl::IsoString
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5424
pcl::UIObject
Root base class for all user interface objects.
Definition: UIObject.h:94
pcl::AutoPointer< EventHandlers >
pcl::size_type
size_t size_type
Definition: Defs.h:612
pcl::Array< String >
pcl::ExternalProcess::process_event_handler
void(Control::*)(ExternalProcess &sender) process_event_handler
Definition: ExternalProcess.h:666
pcl::ExternalProcess::pid_type
int64 pid_type
Definition: ExternalProcess.h:123
AutoPointer.h
UIObject.h
pcl::ExternalProcess::~ExternalProcess
~ExternalProcess() override
Definition: ExternalProcess.h:144
pcl::Control
Client-side interface to a PixInsight Control object.
Definition: Control.h:125
Flags.h
pcl::int64
signed long long int64
Definition: Defs.h:679
Defs.h
Control.h