From f5b479fbabdceda48e3fd20d60bef1af393799cc Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 18 Oct 2020 13:39:44 +0300 Subject: stream.hpp -> process_io.hpp, move IO there --- src/process.cpp | 13 ++---- src/process_io.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/stream.cpp | 48 ---------------------- 3 files changed, 119 insertions(+), 57 deletions(-) create mode 100644 src/process_io.cpp delete mode 100644 src/stream.cpp (limited to 'src') diff --git a/src/process.cpp b/src/process.cpp index 1e0a970..9fe19e5 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -26,7 +27,7 @@ namespace { typedef std::vector EscapedCommandLine; -Handle create_process(EscapedCommandLine cmd_line, Process::IO& io) { +Handle create_process(EscapedCommandLine cmd_line, process::IO& io) { BOOST_STATIC_CONSTEXPR DWORD flags = /*CREATE_NO_WINDOW | */ CREATE_UNICODE_ENVIRONMENT; STARTUPINFOW startup_info; @@ -64,23 +65,17 @@ EscapedCommandLine escape_command_line(const CommandLine& cmd_line) { return buffer; } -Handle create_process(const CommandLine& cmd_line, Process::IO& io) { +Handle create_process(const CommandLine& cmd_line, process::IO& io) { return create_process(escape_command_line(cmd_line), io); } } // namespace -void Process::IO::close() { - std_in.handle.close(); - std_out.handle.close(); - std_err.handle.close(); -} - Process Process::create(const CommandLine& cmd_line) { return create(cmd_line, {}); } -Process Process::create(const CommandLine& cmd_line, IO io) { +Process Process::create(const CommandLine& cmd_line, process::IO io) { return Process{create_process(cmd_line, io)}; } diff --git a/src/process_io.cpp b/src/process_io.cpp new file mode 100644 index 0000000..ec7dbc9 --- /dev/null +++ b/src/process_io.cpp @@ -0,0 +1,115 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#include +#include +#include +#include + +#include + +#include +#include + +namespace winapi { +namespace process { + +Stdin::Stdin() : Stream{Handle::std_in()} {} + +Stdout::Stdout() : Stream{Handle::std_out()} {} + +Stderr::Stderr() : Stream{Handle::std_err()} {} + +Stdin::Stdin(const std::string& path) : Stream{File::open_r(path)} {} + +Stdin::Stdin(const CanonicalPath& path) : Stream{File::open_r(path)} {} + +Stdout::Stdout(const std::string& path) : Stream{File::open_w(path)} {} + +Stdout::Stdout(const CanonicalPath& path) : Stream{File::open_w(path)} {} + +Stderr::Stderr(const std::string& path) : Stream{File::open_w(path)} {} + +Stderr::Stderr(const CanonicalPath& path) : Stream{File::open_w(path)} {} + +Stdin::Stdin(Pipe& pipe) : Stream{std::move(pipe.read_end())} { + pipe.write_end().dont_inherit(); +} + +Stdout::Stdout(Pipe& pipe) : Stream{std::move(pipe.write_end())} { + pipe.read_end().dont_inherit(); +} + +Stderr::Stderr(Pipe& pipe) : Stream{std::move(pipe.write_end())} { + pipe.read_end().dont_inherit(); +} + +Stream::Stream(Stream&& other) BOOST_NOEXCEPT_OR_NOTHROW { + swap(other); +} + +Stream& Stream::operator=(Stream other) BOOST_NOEXCEPT_OR_NOTHROW { + swap(other); + return *this; +} + +void Stream::swap(Stream& other) BOOST_NOEXCEPT_OR_NOTHROW { + using std::swap; + swap(handle, other.handle); +} + +void swap(Stream& a, Stream& b) BOOST_NOEXCEPT_OR_NOTHROW { + a.swap(b); +} + +Stdin::Stdin(Stdin&& other) BOOST_NOEXCEPT_OR_NOTHROW : Stream{std::move(other)} {} + +Stdin& Stdin::operator=(Stdin other) BOOST_NOEXCEPT_OR_NOTHROW { + Stream::operator=(std::move(other)); + return *this; +} + +Stdout::Stdout(Stdout&& other) BOOST_NOEXCEPT_OR_NOTHROW : Stream{std::move(other)} {} + +Stdout& Stdout::operator=(Stdout other) BOOST_NOEXCEPT_OR_NOTHROW { + Stream::operator=(std::move(other)); + return *this; +} + +Stderr::Stderr(Stderr&& other) BOOST_NOEXCEPT_OR_NOTHROW : Stream{std::move(other)} {} + +Stderr& Stderr::operator=(Stderr other) BOOST_NOEXCEPT_OR_NOTHROW { + Stream::operator=(std::move(other)); + return *this; +} + +void IO::close() { + std_in.handle.close(); + std_out.handle.close(); + std_err.handle.close(); +} + +IO::IO(IO&& other) BOOST_NOEXCEPT_OR_NOTHROW { + swap(other); +} + +IO& IO::operator=(IO other) BOOST_NOEXCEPT_OR_NOTHROW { + swap(other); + return *this; +} + +void IO::swap(IO& other) BOOST_NOEXCEPT_OR_NOTHROW { + using std::swap; + swap(std_in, other.std_in); + swap(std_out, other.std_out); + swap(std_err, other.std_err); +} + +void swap(IO& a, IO& b) BOOST_NOEXCEPT_OR_NOTHROW { + a.swap(b); +} + +} // namespace process +} // namespace winapi diff --git a/src/stream.cpp b/src/stream.cpp deleted file mode 100644 index 76dd5a5..0000000 --- a/src/stream.cpp +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2020 Egor Tensin -// This file is part of the "winapi-common" project. -// For details, see https://github.com/egor-tensin/winapi-common. -// Distributed under the MIT License. - -#include -#include -#include -#include - -#include -#include - -namespace winapi { -namespace process { - -Stdin::Stdin() : Stream{Handle::std_in()} {} - -Stdout::Stdout() : Stream{Handle::std_out()} {} - -Stderr::Stderr() : Stream{Handle::std_err()} {} - -Stdin::Stdin(const std::string& path) : Stream{File::open_r(path)} {} - -Stdin::Stdin(const CanonicalPath& path) : Stream{File::open_r(path)} {} - -Stdout::Stdout(const std::string& path) : Stream{File::open_w(path)} {} - -Stdout::Stdout(const CanonicalPath& path) : Stream{File::open_w(path)} {} - -Stderr::Stderr(const std::string& path) : Stream{File::open_w(path)} {} - -Stderr::Stderr(const CanonicalPath& path) : Stream{File::open_w(path)} {} - -Stdin::Stdin(Pipe& pipe) : Stream{std::move(pipe.read_end())} { - pipe.write_end().dont_inherit(); -} - -Stdout::Stdout(Pipe& pipe) : Stream{std::move(pipe.write_end())} { - pipe.read_end().dont_inherit(); -} - -Stderr::Stderr(Pipe& pipe) : Stream{std::move(pipe.write_end())} { - pipe.read_end().dont_inherit(); -} - -} // namespace process -} // namespace winapi -- cgit v1.2.3