file_out.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_FILE_OUT_HPP
  10. #define BOOST_PROCESS_DETAIL_WINDOWS_FILE_OUT_HPP
  11. #include <boost/winapi/process.hpp>
  12. #include <boost/winapi/handles.hpp>
  13. #include <boost/winapi/handle_info.hpp>
  14. #include <boost/process/detail/handler_base.hpp>
  15. #include <boost/process/detail/windows/file_descriptor.hpp>
  16. namespace boost { namespace process { namespace detail { namespace windows {
  17. template<int p1, int p2>
  18. struct file_out : public ::boost::process::detail::handler_base
  19. {
  20. file_descriptor file;
  21. ::boost::winapi::HANDLE_ handle = file.handle();
  22. template<typename T>
  23. file_out(T&& t) : file(std::forward<T>(t), file_descriptor::write) {}
  24. file_out(FILE * f) : handle(reinterpret_cast<void*>(_get_osfhandle(_fileno(f)))) {}
  25. template <typename WindowsExecutor>
  26. inline void on_setup(WindowsExecutor &e) const;
  27. };
  28. template<>
  29. template<typename WindowsExecutor>
  30. void file_out<1,-1>::on_setup(WindowsExecutor &e) const
  31. {
  32. boost::winapi::SetHandleInformation(handle,
  33. boost::winapi::HANDLE_FLAG_INHERIT_,
  34. boost::winapi::HANDLE_FLAG_INHERIT_);
  35. e.startup_info.hStdOutput = handle;
  36. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  37. e.inherit_handles = true;
  38. }
  39. template<>
  40. template<typename WindowsExecutor>
  41. void file_out<2,-1>::on_setup(WindowsExecutor &e) const
  42. {
  43. boost::winapi::SetHandleInformation(handle,
  44. boost::winapi::HANDLE_FLAG_INHERIT_,
  45. boost::winapi::HANDLE_FLAG_INHERIT_);
  46. e.startup_info.hStdError = handle;
  47. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  48. e.inherit_handles = true;
  49. }
  50. template<>
  51. template<typename WindowsExecutor>
  52. void file_out<1,2>::on_setup(WindowsExecutor &e) const
  53. {
  54. boost::winapi::SetHandleInformation(handle,
  55. boost::winapi::HANDLE_FLAG_INHERIT_,
  56. boost::winapi::HANDLE_FLAG_INHERIT_);
  57. e.startup_info.hStdOutput = handle;
  58. e.startup_info.hStdError = handle;
  59. e.startup_info.dwFlags |= ::boost::winapi::STARTF_USESTDHANDLES_;
  60. e.inherit_handles = true;
  61. }
  62. }}}}
  63. #endif