From ef61f3d827d8aaa8ca987aa1de8ff8dbdeb8a55d Mon Sep 17 00:00:00 2001 From: klemens-morgenstern Date: Sun, 10 Apr 2016 14:27:21 +0200 Subject: [PATCH] added asnyc_close to async_pipe --- include/boost/process/detail/env.hpp | 20 ++++----- include/boost/process/io.hpp | 1 - include/boost/process/pipe.hpp | 48 +++++++++++---------- include/boost/process/windows/async_out.hpp | 4 +- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/include/boost/process/detail/env.hpp b/include/boost/process/detail/env.hpp index b0bdee5a..24ddc961 100644 --- a/include/boost/process/detail/env.hpp +++ b/include/boost/process/detail/env.hpp @@ -13,6 +13,9 @@ namespace boost { namespace process { namespace detail { +struct env_tag {}; + + template struct env_set { @@ -147,19 +150,16 @@ auto build_env(boost::hana::tuple<> && e) } template -inline constexpr std:: true_type is_env_setter(const env_set&) {return {};} +inline constexpr env_tag initializer_tag(const env_set&) {return {};} template -inline constexpr std:: true_type is_env_setter(const env_append&) {return {};} -inline constexpr std:: true_type is_env_setter(const env_reset&) {return {};} +inline constexpr env_tag initializer_tag(const env_append&) {return {};} +inline constexpr env_tag initializer_tag(const env_reset&) {return {};} -inline constexpr std:: true_type is_env_setter(const environment&) {return {};} -inline constexpr std:: true_type is_env_setter(const wenvironment&) {return {};} +inline constexpr env_tag initializer_tag(const environment&) {return {};} +inline constexpr env_tag initializer_tag(const wenvironment&) {return {};} -inline constexpr std:: true_type is_env_setter(const empty_env&) {return {};} -inline constexpr std:: true_type is_env_setter(const wempty_env&) {return {};} - -template -inline constexpr std::false_type is_env_setter(const T &) {return {};} +inline constexpr env_tag initializer_tag(const empty_env&) {return {};} +inline constexpr env_tag initializer_tag(const wempty_env&) {return {};} template struct is_env_t : decltype(is_env_setter(T())) {}; diff --git a/include/boost/process/io.hpp b/include/boost/process/io.hpp index 72cd98ed..4fb51175 100644 --- a/include/boost/process/io.hpp +++ b/include/boost/process/io.hpp @@ -125,7 +125,6 @@ struct std_out_ api::async_out_buffer operator>(asio::mutable_buffer & buf) const {return buf;} api::async_out_buffer operator>(asio::streambuf & os) const {return os ;} - #if defined (BOOST_PROCESS_USE_FUTURE) api::async_out_future operator=(std::future & fut) const; api::async_out_future operator>(std::future & fut) const; diff --git a/include/boost/process/pipe.hpp b/include/boost/process/pipe.hpp index 53610bdd..7cee5a02 100644 --- a/include/boost/process/pipe.hpp +++ b/include/boost/process/pipe.hpp @@ -46,33 +46,14 @@ struct pipe pipe() : pipe(boost::process::detail::api::pipe::create()) {} + pipe(const std::string & name) : pipe(native_pipe::create_named(name)) {} + pipe( pipe&&) = default; pipe(const pipe& ) = delete; pipe& operator=( pipe&&) = default; pipe& operator=(const pipe& ) = delete; - static pipe create() { return pipe(native_pipe::create()); } - static pipe create(std::error_code &ec) { return pipe(native_pipe::create(ec));} - - static pipe create(const std::string & name) { return create_named(name); } - static pipe create(const std::string & name, std::error_code &ec) { return create_named(name, ec);} - - - inline static std::string make_pipe_name() {return native_pipe::make_pipe_name(); } - - inline static pipe create_named(const std::string & name = make_pipe_name()) {return native_pipe::create_named(name);} - inline static pipe create_named(const std::string & name, std::error_code & ec) {return native_pipe::create_named(name,ec);} - - - inline static pipe create_named(std::error_code & ec) {return create_named(make_pipe_name(), ec);} - - inline static pipe create_async() {return native_pipe::create_async(); } - inline static pipe create_async(std::error_code & ec) {return native_pipe::create_async(ec); } - - - - std::streamsize read(char_type* s, std::streamsize n) { return _source.read(s, n); @@ -127,7 +108,13 @@ struct async_pipe : pipe { return _sink.is_open() || _source.is_open(); } - + void async_close() + { + if (_sink.is_open()) + _sink.get_io_service(). post([this]{_sink.close();}); + if (_source.is_open()) + _source.get_io_service().post([this]{_source.close();}); + } template @@ -165,17 +152,32 @@ struct async_pipe : pipe } boost::asio::io_service& get_io_service() {return *_ios;} - async_pipe(boost::asio::io_service & ios) : _ios(&ios), + async_pipe(boost::asio::io_service & ios) : + pipe(native_pipe::create_async()), + _ios(&ios), _sink (ios, pipe::sink(). handle()), _source(ios, pipe::source().handle()) {} + async_pipe(boost::asio::io_service & ios, const std::string & name) : + pipe(name), + _ios(&ios), + _sink (ios, pipe::sink(). handle()), + _source(ios, pipe::source().handle()) + {} async_pipe(const async_pipe &) = default; async_pipe(async_pipe &&) = default; + ~async_pipe() {if (_destruction_notifier) _destruction_notifier->store(false);} async_pipe& operator=(const async_pipe &) = default; async_pipe& operator=(async_pipe &&) = default; + std::shared_ptr> make_destruction_notifier() + { + return _destruction_notifier = std::make_shared>(true); + } + private: + std::shared_ptr> _destruction_notifier; boost::asio::io_service * _ios; native_async_handle _sink; native_async_handle _source; diff --git a/include/boost/process/windows/async_out.hpp b/include/boost/process/windows/async_out.hpp index 331d0754..3ed59f4d 100644 --- a/include/boost/process/windows/async_out.hpp +++ b/include/boost/process/windows/async_out.hpp @@ -65,8 +65,6 @@ void apply_out_handles(Executor &e, void* handle, std::integral_constant e.inherit_handles = true; } - - template struct async_out_buffer : ::boost::process::detail::windows::async_handler { @@ -116,6 +114,8 @@ struct async_out_buffer : ::boost::process::detail::windows::async_handler } }; + + #if defined (BOOST_PROCESS_USE_FUTURE) template