diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-16 10:09:55 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-16 10:12:47 +0300 |
commit | a632bb8e796be52929f1541d305910d704e55076 (patch) | |
tree | 6a03b50fcb16ef1713f6cbbe5f2377b2fdc10990 /include/winapi/handle.hpp | |
parent | echo: make it really UTF-16 (diff) | |
download | winapi-common-a632bb8e796be52929f1541d305910d704e55076.tar.gz winapi-common-a632bb8e796be52929f1541d305910d704e55076.zip |
Process: support pipe redirection
Diffstat (limited to '')
-rw-r--r-- | include/winapi/handle.hpp | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/include/winapi/handle.hpp b/include/winapi/handle.hpp index 9714db3..af37424 100644 --- a/include/winapi/handle.hpp +++ b/include/winapi/handle.hpp @@ -5,50 +5,57 @@ #pragma once -#include "workarounds.hpp" - #include <boost/config.hpp> #include <windows.h> -#include <cassert> +#include <cstddef> #include <memory> #include <utility> +#include <vector> namespace winapi { class Handle { public: Handle() = default; + explicit Handle(HANDLE); + + Handle(Handle&& other) BOOST_NOEXCEPT_OR_NOTHROW; + Handle& operator=(Handle other) BOOST_NOEXCEPT_OR_NOTHROW; + + void swap(Handle& other) BOOST_NOEXCEPT_OR_NOTHROW; + + explicit operator HANDLE() const { return m_impl.get(); } + + bool is_invalid() const; + + void close(); + + bool is_std() const; + static Handle std_in(); + static Handle std_out(); + static Handle std_err(); - explicit Handle(HANDLE raw) : impl{raw} {} + typedef std::vector<unsigned char> Buffer; - Handle(Handle&& other) BOOST_NOEXCEPT_OR_NOTHROW { swap(other); } + Buffer read() const; - Handle& operator=(Handle other) BOOST_NOEXCEPT_OR_NOTHROW { - swap(other); - return *this; - } + BOOST_STATIC_CONSTEXPR std::size_t max_chunk_size = 16 * 1024; + bool read_chunk(Buffer& read_chunk) const; - void swap(Handle& other) BOOST_NOEXCEPT_OR_NOTHROW { - using std::swap; - swap(impl, other.impl); - } + void write(const void*, std::size_t nb) const; + void write(const Buffer& buffer) const; - explicit operator HANDLE() const { return impl.get(); } + void inherit(bool yes = true) const; + void dont_inherit() const { inherit(false); } private: struct Close { - void operator()(HANDLE raw) const { - if (raw == NULL || raw == INVALID_HANDLE_VALUE) - return; - const auto ret = ::CloseHandle(raw); - assert(ret); - WINAPI_UNUSED_PARAMETER(ret); - } + void operator()(HANDLE) const; }; - std::unique_ptr<void, Close> impl; + std::unique_ptr<void, Close> m_impl; Handle(const Handle&) = delete; }; |