Subprocess.h 1.6 KB

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