diff options
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; }; |