Process.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_Process_hxx
00024 #define __ccxx_Process_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/Stream.h++>
00028 #include <commonc++/String.h++>
00029 #include <commonc++/SystemException.h++>
00030 
00031 #include <vector>
00032 #include <memory>
00033 
00034 #ifdef CCXX_OS_POSIX
00035 #include <sys/types.h>
00036 #include <fcntl.h>
00037 #endif
00038 
00039 namespace ccxx {
00040 
00046 class COMMONCPP_API ExitStatus
00047 {
00048   friend class Process;
00049 
00050   public:
00051 
00053   ExitStatus() throw() { }
00054 
00056   ~ExitStatus() throw() { }
00057 
00059   inline int getExitCode() const throw()
00060   { return(_exitCode); }
00061 
00067   inline int getExitSignal() const throw()
00068   { return(_exitSignal); }
00069 
00071   inline bool isNormal() const throw()
00072   { return(_exitedNormally); }
00073 
00077   inline uint64_t getUserTime() const throw()
00078   { return(_userTime); }
00079 
00083   inline uint64_t getKernelTime() const throw()
00084   { return(_kernelTime); }
00085 
00090   inline uint_t getMaxRSS() const throw()
00091   { return(_maxSize); }
00092 
00094   static const int SigAbort;
00095 
00099   static const int SigFloatingPointException;
00103   static const int SigIllegalInstruction;
00107   static const int SigKeyboardInterrupt;
00110   static const int SigSegmentationFault;
00112   static const int SigTerminationRequest;
00113 
00115   static const int SUCCESS;
00116 
00118   static const int FAILURE;
00119 
00120   private:
00121 
00122   int _exitCode;
00123   int _exitSignal;
00124   bool _exitedNormally;
00125   uint64_t _userTime;
00126   uint64_t _kernelTime;
00127   uint_t _maxSize;
00128 };
00129 
00148 class COMMONCPP_API Process
00149 {
00150   public:
00151 
00159   Process(const String &path, const StringVec& args,
00160           const StringVec& env = StringVec::emptyVec,
00161           const String &workingDir = ".");
00162 
00164   ~Process() throw();
00165 
00171   void start() throw(SystemException);
00172 
00178   void stop() throw();
00179 
00186   bool kill() throw();
00187 
00189   void setPriority(Priority priority) throw();
00190 
00192   Priority getPriority() const throw();
00193 
00203   bool waitFor(ExitStatus& status) throw();
00204 
00206   inline bool isStarted() const throw()
00207   { return(_started); }
00208 
00212   inline Stream& getInputStream() throw()
00213   { return(*_ins); }
00214 
00218   inline Stream& getOutputStream() throw()
00219   { return(*_outs); }
00220 
00224   inline Stream& getErrorStream() throw()
00225   { return(*_errs); }
00226 
00228   inline time_ms_t getStartTime() const throw()
00229   { return(_startTime); }
00230 
00235   static void exit(int status) throw();
00236 
00238   inline static const char *getProgramName() throw()
00239   { return(_progname.c_str()); }
00240 
00242   static String getExecutablePath() throw(SystemException);
00243 
00247   static String getExecutableDir() throw(SystemException);
00248 
00250   inline static void setProgramName(const char *name) throw()
00251   { _progname = name; }
00252 
00254   static String getWorkingDir();
00255 
00261   static bool setWorkingDir(const String &path) throw();
00262 
00268   static bool hasConsole() throw();
00269 
00282   static bool getConsoleSize(uint_t *columns, uint_t *rows) throw();
00283 
00288   static void setConsoleTitle(const String &title) throw();
00289 
00291   static ProcessID currentProcessID() throw();
00292 
00293   private:
00294 
00295   String _path;
00296   String _workingDir;
00297   bool _started;
00298   time_ms_t _startTime;
00299   ProcessHandle _handle;
00300   StringVec _args;
00301   StringVec _env;
00302   Stream* _ins;
00303   Stream* _outs;
00304   Stream* _errs;
00305 
00306   static String _progname;
00307 
00308   Process(); // not supported
00309   CCXX_COPY_DECLS(Process);
00310 };
00311 
00312 }; // namespace ccxx
00313 
00314 #endif // __ccxx_Process_hxx
00315 
00316 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3