Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

child.hpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /// \file child.hpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. /*
  23. \brief The child module provides classes to spawn and communicate
  24. with child processes.
  25. */
  26. #ifndef CHILD_HPP
  27. #define CHILD_HPP
  28. #include <windows.h>
  29. #include <cstdint>
  30. #include <istream>
  31. #include <ostream>
  32. #include <string>
  33. #include <vector>
  34. namespace CodeDweller {
  35. /**
  36. \namespace CodeDweller
  37. The CodeDweller namespace contains components providing high-level
  38. functionality for applications.
  39. */
  40. /** Class that abstracts the creation of child processes.
  41. This class provides functionality to create a child process,
  42. communicate with the child process via streams and signals, and
  43. obtain the exit code of the child process.
  44. */
  45. class Child {
  46. public:
  47. /** Constructor for spawning with command-line parameters.
  48. The constructor configures the object, but doesn't spawn the
  49. child process.
  50. \param[in] args contains the child executable file name and
  51. command-line parameters. args[0] contains the full path of the
  52. executable, and args[1] thru args[n] are the command-line
  53. parameters.
  54. */
  55. Child(std::vector<std::string> args);
  56. /** Constructor for spawning without command-line parameters.
  57. The constructor configures the object, but doesn't spawn the
  58. child process.
  59. \param[in] childpath contains the child executable file name.
  60. */
  61. Child(std::string childpath);
  62. /** Destructor terminates the child process. */
  63. ~Child();
  64. /// Return a stream that is seen by the child as standard output.
  65. std::istream *reader();
  66. /// Return a stream that is seen by the child as standard input.
  67. std::ostream *writer();
  68. /** Spawn the child process.
  69. If an error occurs, an exception is thrown.
  70. */
  71. void run();
  72. /** Terminite the child process.
  73. If an error occurs, an exception is thrown.
  74. */
  75. void terminate();
  76. /** Get the exit value of the child process.
  77. \returns The exit value of the child process if the child
  78. process has exited. If the child process has not exited, an
  79. exception is thrown.
  80. */
  81. int32_t result();
  82. private:
  83. /// Exit code to use when terminating the child process.
  84. static const uint32_t terminateExitCode = 0;
  85. /// Initialize data members.
  86. void init();
  87. /// Child executable path and command-line parameters.
  88. std::string cmdline;
  89. /// Parent's read handle for child process standard output.
  90. HANDLE childStdOutAtParent;
  91. /// Parent's write handle for child process standard input.
  92. HANDLE childStdInAtParent;
  93. /// Child's process handle.
  94. HANDLE childProcess;
  95. /// Child's thread handle.
  96. HANDLE childThread;
  97. /// Exit value of the process.
  98. int32_t exitCode;
  99. /// True if the exit code has been obtained.
  100. bool exitCodeObtainedFlag;
  101. /// Return text for the most recent error.
  102. //
  103. // \returns Human-readable description of the most recent error.
  104. //
  105. std::string getErrorText();
  106. };
  107. }
  108. #endif // CHILD_HPP