From 01c1146276f7319843c357480c4374fe555f07c5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 15 Jul 2015 22:08:33 +0300 Subject: utils: code style --- utils/libservice/include/libservice/device.hpp | 35 ++++--- utils/libservice/include/libservice/handle.hpp | 18 ++-- utils/libservice/include/libservice/service.hpp | 26 +++-- .../include/libservice/service_handle.hpp | 18 ++-- .../include/libservice/service_manager.hpp | 12 +-- utils/libservice/include/libservice/singleton.hpp | 16 +-- .../include/libservice/windows_error.hpp | 17 ++-- utils/libservice/src/device.cpp | 61 +++++------ utils/libservice/src/handle.cpp | 5 +- utils/libservice/src/service.cpp | 113 ++++++++++++--------- utils/libservice/src/service_handle.cpp | 3 +- utils/libservice/src/service_manager.cpp | 10 +- utils/libservice/src/windows_error.cpp | 14 +-- utils/libservice/test/windows_error.cpp | 8 +- 14 files changed, 191 insertions(+), 165 deletions(-) (limited to 'utils') diff --git a/utils/libservice/include/libservice/device.hpp b/utils/libservice/include/libservice/device.hpp index 3a9540e..9a6f278 100644 --- a/utils/libservice/include/libservice/device.hpp +++ b/utils/libservice/include/libservice/device.hpp @@ -13,6 +13,8 @@ #include +#include + #include #include @@ -21,6 +23,8 @@ namespace libservice class Device { public: + typedef DWORD Code; + static Device open(const std::string& path); Device(Device&& other) LIBSERVICE_NOEXCEPT @@ -37,35 +41,38 @@ namespace libservice void swap(Device& other) LIBSERVICE_NOEXCEPT { using std::swap; - swap(m_handle, other.m_handle); + swap(handle, other.handle); } - DWORD get_required_output_size(DWORD code, - const void* in_buf, - DWORD in_buf_size) const; + std::size_t get_required_output_size( + Code code, + const void* in_buf, + std::size_t 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; + std::size_t send_control_code( + Code code, + const void* in_buf, + std::size_t in_buf_size, + void* out_buf, + std::size_t out_buf_size) const; private: - explicit Device(Handle h) - : m_handle(std::move(h)) + Device(Handle handle) + : handle(std::move(handle)) { } - Handle m_handle; + Handle handle; Device(const Device&) = delete; }; - void swap(libservice::Device&, libservice::Device&) LIBSERVICE_NOEXCEPT; + void swap(Device&, Device&) LIBSERVICE_NOEXCEPT; } namespace std { template <> void swap( - libservice::Device&, libservice::Device&) LIBSERVICE_NOEXCEPT; + libservice::Device&, + libservice::Device&) LIBSERVICE_NOEXCEPT; } diff --git a/utils/libservice/include/libservice/handle.hpp b/utils/libservice/include/libservice/handle.hpp index 1292090..26aba77 100644 --- a/utils/libservice/include/libservice/handle.hpp +++ b/utils/libservice/include/libservice/handle.hpp @@ -23,8 +23,8 @@ namespace libservice public: Handle() = default; - explicit Handle(HANDLE raw) - : m_impl(raw) + Handle(HANDLE raw) + : impl(raw) { } Handle(Handle&& other) LIBSERVICE_NOEXCEPT @@ -38,20 +38,20 @@ namespace libservice return *this; } - explicit operator bool() const + operator bool() const { - return static_cast(m_impl); + return static_cast(impl); } - explicit operator HANDLE() const + operator HANDLE() const { - return m_impl.get(); + return impl.get(); } void swap(Handle& other) LIBSERVICE_NOEXCEPT { using std::swap; - swap(m_impl, other.m_impl); + swap(impl, other.impl); } private: @@ -59,11 +59,11 @@ namespace libservice { void operator()(HANDLE raw) { - ::CloseHandle(raw); + CloseHandle(raw); } }; - std::unique_ptr::type, Deleter> m_impl; + std::unique_ptr::type, Deleter> impl; Handle(const Handle&) = delete; }; diff --git a/utils/libservice/include/libservice/service.hpp b/utils/libservice/include/libservice/service.hpp index d350c91..6e4d268 100644 --- a/utils/libservice/include/libservice/service.hpp +++ b/utils/libservice/include/libservice/service.hpp @@ -20,14 +20,18 @@ namespace libservice class Service { public: - static bool does_exist(const ServiceManager&, - const std::string& name); + static bool exists( + const ServiceManager&, + const std::string& name); - static Service open(const ServiceManager&, - const std::string& name); - static Service install(const ServiceManager&, - const std::string& name, - const std::string& bin_path); + static Service open( + const ServiceManager&, + const std::string& name); + + static Service install( + const ServiceManager&, + const std::string& name, + const std::string& bin_path); void start() const; void stop() const; @@ -47,15 +51,15 @@ namespace libservice void swap(Service& other) LIBSERVICE_NOEXCEPT { using std::swap; - swap(m_handle, other.m_handle); + swap(handle, other.handle); } private: - explicit Service(ServiceHandle h) - : m_handle(std::move(h)) + Service(ServiceHandle handle) + : handle(std::move(handle)) { } - ServiceHandle m_handle; + ServiceHandle handle; Service(const Service&) = delete; }; diff --git a/utils/libservice/include/libservice/service_handle.hpp b/utils/libservice/include/libservice/service_handle.hpp index 37e69ce..0f2cf59 100644 --- a/utils/libservice/include/libservice/service_handle.hpp +++ b/utils/libservice/include/libservice/service_handle.hpp @@ -22,8 +22,8 @@ namespace libservice public: ServiceHandle() = default; - explicit ServiceHandle(SC_HANDLE raw) - : m_impl(raw) + ServiceHandle(SC_HANDLE raw) + : impl(raw) { } ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT @@ -37,20 +37,20 @@ namespace libservice return *this; } - explicit operator bool() const + operator bool() const { - return static_cast(m_impl); + return static_cast(impl); } - explicit operator SC_HANDLE() const + operator SC_HANDLE() const { - return m_impl.get(); + return impl.get(); } void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT { using std::swap; - swap(m_impl, other.m_impl); + swap(impl, other.impl); } private: @@ -58,11 +58,11 @@ namespace libservice { void operator()(SC_HANDLE raw) { - ::CloseServiceHandle(raw); + CloseServiceHandle(raw); } }; - std::unique_ptr m_impl; + std::unique_ptr impl; ServiceHandle(const ServiceHandle&) = delete; }; diff --git a/utils/libservice/include/libservice/service_manager.hpp b/utils/libservice/include/libservice/service_manager.hpp index c39b99e..8d8b862 100644 --- a/utils/libservice/include/libservice/service_manager.hpp +++ b/utils/libservice/include/libservice/service_manager.hpp @@ -36,20 +36,20 @@ namespace libservice void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT { using std::swap; - swap(m_handle, other.m_handle); + swap(handle, other.handle); } - explicit operator SC_HANDLE() const + operator SC_HANDLE() const { - return static_cast(m_handle); + return handle; } private: - explicit ServiceManager(ServiceHandle h) - : m_handle(std::move(h)) + ServiceManager(ServiceHandle handle) + : handle(std::move(handle)) { } - ServiceHandle m_handle; + ServiceHandle handle; ServiceManager(const ServiceManager&) = delete; }; diff --git a/utils/libservice/include/libservice/singleton.hpp b/utils/libservice/include/libservice/singleton.hpp index a9d55ad..81e6445 100644 --- a/utils/libservice/include/libservice/singleton.hpp +++ b/utils/libservice/include/libservice/singleton.hpp @@ -12,13 +12,13 @@ namespace libservice { - template + template class Singleton { public: - static DerivedType& get() + static DerivedT& get() { - std::call_once(is_initialized, initialize); + std::call_once(initialized, initialize); return get_unsafe(); } @@ -32,15 +32,15 @@ namespace libservice get_unsafe(); } - static DerivedType& get_unsafe() + static DerivedT& get_unsafe() { - static DerivedType instance; + static DerivedT instance; return instance; } - static std::once_flag is_initialized; + static std::once_flag initialized; }; - template - std::once_flag Singleton::is_initialized; + template + std::once_flag Singleton::initialized; } diff --git a/utils/libservice/include/libservice/windows_error.hpp b/utils/libservice/include/libservice/windows_error.hpp index 907c7e7..430705a 100644 --- a/utils/libservice/include/libservice/windows_error.hpp +++ b/utils/libservice/include/libservice/windows_error.hpp @@ -16,22 +16,19 @@ namespace libservice { - class WinErrorCategory : public std::error_category - , public Singleton + class WindowsErrorCategory + : public std::error_category + , public Singleton { public: - const char* name() const LIBSERVICE_NOEXCEPT { return "windows"; } + const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; } std::string message(int) const; private: - friend class Singleton; + friend class Singleton; }; } -#define LIBSERVICE_ERROR_PREFIX "Error in function '" \ - LIBSERVICE_FUNCTION_NAME \ - "' at file '" \ - LIBSERVICE_FILE_PATH \ - "', line " \ - LIBSERVICE_LINE_NUMBER_STRING +#define LIBSERVICE_ERROR_PREFIX \ + "Error in function '" LIBSERVICE_FUNCTION_NAME "' at file '" LIBSERVICE_FILE_PATH "', line " LIBSERVICE_LINE_NUMBER_STRING diff --git a/utils/libservice/src/device.cpp b/utils/libservice/src/device.cpp index 3cfb760..4ae25a5 100644 --- a/utils/libservice/src/device.cpp +++ b/utils/libservice/src/device.cpp @@ -6,13 +6,12 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/device.hpp" -#include "libservice/handle.hpp" -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include +#include + #include #include #include @@ -23,18 +22,20 @@ namespace libservice { Handle open_device(const std::string& path) { - const auto raw = CreateFileA(path.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); + const auto raw = CreateFileA( + path.c_str(), + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); if (INVALID_HANDLE_VALUE == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } return Handle(raw); @@ -46,15 +47,15 @@ namespace libservice return Device(open_device(path)); } - DWORD Device::get_required_output_size( - DWORD code, + std::size_t Device::get_required_output_size( + Code code, const void* in_buf, - DWORD in_buf_size) const + std::size_t in_buf_size) const { DWORD nbreq; - DWORD ret = DeviceIoControl( - static_cast(m_handle), + std::size_t nbwritten = DeviceIoControl( + handle, code, const_cast(in_buf), in_buf_size, @@ -63,7 +64,7 @@ namespace libservice &nbreq, NULL); - if (0 == ret) + if (0 == nbwritten) { const auto ec = GetLastError(); @@ -73,24 +74,25 @@ namespace libservice return nbreq; default: - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } - return ret; + return nbwritten; } - DWORD Device::send_control_code( - DWORD code, + std::size_t Device::send_control_code( + Code code, const void* in_buf, - DWORD in_buf_size, + std::size_t in_buf_size, void* out_buf, - DWORD out_buf_size) const + std::size_t out_buf_size) const { DWORD nbreq; - DWORD ret = DeviceIoControl( - static_cast(m_handle), + std::size_t nbwritten = DeviceIoControl( + handle, code, const_cast(in_buf), in_buf_size, @@ -99,13 +101,14 @@ namespace libservice &nbreq, NULL); - if (0 == ret) + if (0 == nbwritten) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ret; + return nbwritten; } void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT diff --git a/utils/libservice/src/handle.cpp b/utils/libservice/src/handle.cpp index 2f9a65f..bc3dc35 100644 --- a/utils/libservice/src/handle.cpp +++ b/utils/libservice/src/handle.cpp @@ -6,8 +6,9 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/handle.hpp" +#include "libservice/all.hpp" + +#include namespace libservice { diff --git a/utils/libservice/src/service.cpp b/utils/libservice/src/service.cpp index efe645f..57bb568 100644 --- a/utils/libservice/src/service.cpp +++ b/utils/libservice/src/service.cpp @@ -6,15 +6,12 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/service.hpp" -#include "libservice/service_handle.hpp" -#include "libservice/service_manager.hpp" -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include #include +#include #include #include @@ -22,20 +19,23 @@ namespace libservice { namespace { - ServiceHandle open_service(const ServiceManager& mgr, const std::string& name) + ServiceHandle open_service( + const ServiceManager& mgr, + const std::string& name) { const auto raw = OpenServiceA( - static_cast(mgr), + mgr, name.c_str(), SERVICE_ALL_ACCESS); if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ServiceHandle(raw); + return raw; } ServiceHandle install_service( @@ -44,7 +44,7 @@ namespace libservice const std::string& bin_path) { const auto raw = CreateServiceA( - static_cast(mgr), + mgr, name.c_str(), name.c_str(), SERVICE_ALL_ACCESS, @@ -61,18 +61,20 @@ namespace libservice if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ServiceHandle(raw); + return raw; } void start_service(const ServiceHandle& handle) { - if (!StartService(static_cast(handle), 0, NULL)) + if (!StartService(handle, 0, NULL)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } @@ -80,28 +82,32 @@ namespace libservice { SERVICE_STATUS service_status; - if (!ControlService(static_cast(handle), SERVICE_CONTROL_STOP, &service_status)) + if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } void uninstall_service(const ServiceHandle& handle) { - if (!DeleteService(static_cast(handle))) + if (!DeleteService(handle)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } - bool does_service_exist(const ServiceManager& mgr, - const std::string& name) + bool service_exists( + const ServiceManager& mgr, + const std::string& name) { - const auto raw = OpenService(static_cast(mgr), - name.c_str(), - SERVICE_QUERY_STATUS); + const auto raw = OpenServiceA( + mgr, + name.c_str(), + SERVICE_QUERY_STATUS); if (NULL != raw) { @@ -117,23 +123,25 @@ namespace libservice return false; default: - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } - + SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) { SERVICE_STATUS_PROCESS status; DWORD nbreq; - if (!QueryServiceStatusEx(static_cast(handle), - SC_STATUS_PROCESS_INFO, - reinterpret_cast(&status), - sizeof(status), - &nbreq)) + const auto buf_ptr = reinterpret_cast(&status); + const auto buf_size = sizeof(status); + + if (!QueryServiceStatusEx( + handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } return status; @@ -143,7 +151,7 @@ namespace libservice { return query_service_status(handle).dwCurrentState; } - + SERVICE_STATUS_PROCESS wait_for_service_state( const ServiceHandle& handle, const DWORD desired_state) @@ -183,31 +191,36 @@ namespace libservice return status; } } + return status; } } - Service Service::open(const ServiceManager& mgr, const std::string& name) + Service Service::open( + const ServiceManager& mgr, + const std::string& name) { - return Service(open_service(mgr, name)); + return open_service(mgr, name); } - Service Service::install(const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) + Service Service::install( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path) { - return Service(install_service(mgr, name, bin_path)); + return install_service(mgr, name, bin_path); } - bool Service::does_exist(const ServiceManager& mgr, - const std::string& name) + bool Service::exists( + const ServiceManager& mgr, + const std::string& name) { - return does_service_exist(mgr, name); + return service_exists(mgr, name); } void Service::start() const { - const auto state = query_service_state(m_handle); + const auto state = query_service_state(handle); switch (state) { @@ -215,37 +228,37 @@ namespace libservice break; case SERVICE_STOP_PENDING: - wait_for_service_state(m_handle, SERVICE_STOPPED); + wait_for_service_state(handle, SERVICE_STOPPED); break; default: return; } - start_service(m_handle); - wait_for_service_state(m_handle, SERVICE_RUNNING); + start_service(handle); + wait_for_service_state(handle, SERVICE_RUNNING); } void Service::stop() const { - switch (query_service_state(m_handle)) + switch (query_service_state(handle)) { case SERVICE_STOPPED: return; case SERVICE_STOP_PENDING: - wait_for_service_state(m_handle, SERVICE_STOPPED); + wait_for_service_state(handle, SERVICE_STOPPED); return; } - stop_service(m_handle); - wait_for_service_state(m_handle, SERVICE_STOPPED); + stop_service(handle); + wait_for_service_state(handle, SERVICE_STOPPED); } void Service::uninstall() const { stop(); - uninstall_service(m_handle); + uninstall_service(handle); } void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT diff --git a/utils/libservice/src/service_handle.cpp b/utils/libservice/src/service_handle.cpp index 21fb78c..ab85bc8 100644 --- a/utils/libservice/src/service_handle.cpp +++ b/utils/libservice/src/service_handle.cpp @@ -6,8 +6,7 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/service_handle.hpp" +#include "libservice/all.hpp" #include diff --git a/utils/libservice/src/service_manager.cpp b/utils/libservice/src/service_manager.cpp index b3c0b96..d815089 100644 --- a/utils/libservice/src/service_manager.cpp +++ b/utils/libservice/src/service_manager.cpp @@ -6,10 +6,7 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/service_handle.hpp" -#include "libservice/service_manager.hpp" -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include @@ -25,10 +22,11 @@ namespace libservice if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get()); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ServiceManager(ServiceHandle(raw)); + return ServiceHandle(raw); } void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT diff --git a/utils/libservice/src/windows_error.cpp b/utils/libservice/src/windows_error.cpp index 1e28345..2a54366 100644 --- a/utils/libservice/src/windows_error.cpp +++ b/utils/libservice/src/windows_error.cpp @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include @@ -14,14 +14,14 @@ namespace libservice { - std::string WinErrorCategory::message(int code) const + std::string WindowsErrorCategory::message(int code) const { char* buf_ptr; - DWORD written = FormatMessageA( + const auto nbwritten = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, + | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), @@ -29,13 +29,13 @@ namespace libservice 0, NULL); - if (0 == written) + if (0 == nbwritten) { LocalFree(buf_ptr); return "Couldn't format error message"; } - std::string str(buf_ptr, written - 2); + std::string str(buf_ptr, nbwritten - 2); LocalFree(buf_ptr); return str; } diff --git a/utils/libservice/test/windows_error.cpp b/utils/libservice/test/windows_error.cpp index 1f0b1ee..464fd90 100644 --- a/utils/libservice/test/windows_error.cpp +++ b/utils/libservice/test/windows_error.cpp @@ -12,17 +12,21 @@ #include #include +#include int main() { try { - throw std::system_error(ERROR_FILE_NOT_FOUND, libservice::WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ERROR_FILE_NOT_FOUND, + libservice::WindowsErrorCategory::get(), + LIBSERVICE_ERROR_PREFIX); } catch (const std::exception& e) { std::cerr << e.what() << "\n"; - return -1; + return 1; } return 0; } -- cgit v1.2.3