From 9bf7f2bfeb3057ad8b49fc0361fee5036639dfe1 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 11 May 2015 13:32:30 +0300 Subject: libservice: add virtual device wrappers --- utils/libservice/include/libservice/device.hpp | 71 ++++++++++++++++++++ utils/libservice/include/libservice/handle.hpp | 80 +++++++++++++++++++++++ utils/libservice/include/libservice/interface.hpp | 2 + 3 files changed, 153 insertions(+) create mode 100644 utils/libservice/include/libservice/device.hpp create mode 100644 utils/libservice/include/libservice/handle.hpp (limited to 'utils/libservice/include') diff --git a/utils/libservice/include/libservice/device.hpp b/utils/libservice/include/libservice/device.hpp new file mode 100644 index 0000000..3a9540e --- /dev/null +++ b/utils/libservice/include/libservice/device.hpp @@ -0,0 +1,71 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "common.hpp" +#include "handle.hpp" + +#include + +#include +#include + +namespace libservice +{ + class Device + { + public: + static Device open(const std::string& path); + + Device(Device&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + Device& operator=(Device other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + void swap(Device& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(m_handle, other.m_handle); + } + + DWORD get_required_output_size(DWORD code, + const void* in_buf, + DWORD in_buf_size) const; + + DWORD send_control_code(DWORD code, + const void* in_buf, + DWORD in_buf_size, + void* out_buf, + DWORD out_buf_size) const; + + private: + explicit Device(Handle h) + : m_handle(std::move(h)) + { } + + Handle m_handle; + + Device(const Device&) = delete; + }; + + void swap(libservice::Device&, libservice::Device&) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::Device&, libservice::Device&) LIBSERVICE_NOEXCEPT; +} diff --git a/utils/libservice/include/libservice/handle.hpp b/utils/libservice/include/libservice/handle.hpp new file mode 100644 index 0000000..1292090 --- /dev/null +++ b/utils/libservice/include/libservice/handle.hpp @@ -0,0 +1,80 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "common.hpp" + +#include + +#include +#include +#include + +namespace libservice +{ + class Handle + { + public: + Handle() = default; + + explicit Handle(HANDLE raw) + : m_impl(raw) + { } + + Handle(Handle&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + explicit operator bool() const + { + return static_cast(m_impl); + } + + explicit operator HANDLE() const + { + return m_impl.get(); + } + + void swap(Handle& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(m_impl, other.m_impl); + } + + private: + struct Deleter + { + void operator()(HANDLE raw) + { + ::CloseHandle(raw); + } + }; + + std::unique_ptr::type, Deleter> m_impl; + + Handle(const Handle&) = delete; + }; + + void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::Handle& a, + libservice::Handle& b) LIBSERVICE_NOEXCEPT; +} diff --git a/utils/libservice/include/libservice/interface.hpp b/utils/libservice/include/libservice/interface.hpp index 53751b2..e68439b 100644 --- a/utils/libservice/include/libservice/interface.hpp +++ b/utils/libservice/include/libservice/interface.hpp @@ -9,6 +9,8 @@ #pragma once #include "common.hpp" +#include "device.hpp" +#include "handle.hpp" #include "service.hpp" #include "service_handle.hpp" #include "service_manager.hpp" -- cgit v1.2.3