Subprocess.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include <Poco/Process.h>
  6. #include <Poco/Pipe.h>
  7. #include <Poco/PipeStream.h>
  8. #include <Atomic/Container/Str.h>
  9. #include <Atomic/Core/Mutex.h>
  10. #include <Atomic/Core/Thread.h>
  11. #include <Atomic/Core/Object.h>
  12. using namespace Atomic;
  13. namespace ToolCore
  14. {
  15. class SubprocessSystem;
  16. class Subprocess : public Object, public Thread
  17. {
  18. friend class SubprocessSystem;
  19. OBJECT(Subprocess);
  20. public:
  21. /// Construct.
  22. Subprocess(Context* context);
  23. /// Destruct.
  24. ~Subprocess();
  25. void GetOutput(String& outputText, String& errorText)
  26. {
  27. mutex_.Acquire();
  28. outputText = output_;
  29. output_.Clear();
  30. errorText = errorOutput_;
  31. errorOutput_.Clear();
  32. mutex_.Release(); }
  33. private:
  34. void ThreadFunction();
  35. mutable Mutex mutex_;
  36. void ProcessOutput(SubprocessSystem* system);
  37. bool Update(SubprocessSystem* system);
  38. bool Launch(const String& command, const Vector<String>& args, const String& initialDirectory = "");
  39. bool Launch(const String& command, const Vector<String>& args, const String& initialDirectory, const Poco::Process::Env& env);
  40. String output_;
  41. String errorOutput_;
  42. int returnCode_;
  43. Poco::ProcessHandle* handle_;
  44. Poco::Pipe pipeIn_;
  45. Poco::Pipe pipeOut_;
  46. Poco::Pipe pipeError_;
  47. Poco::PipeInputStream inStream_;
  48. Poco::PipeInputStream errorStream_;
  49. };
  50. }