From bd3116f43658fdd4ac145693e8ff4576061f22fa Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 20 Oct 2016 03:20:49 +0300 Subject: refactoring --- src/error.cpp | 35 ----------------------------------- src/error.hpp | 23 +++++++++++++++++++---- src/os.cpp | 24 ------------------------ src/os.hpp | 12 +++++++++++- src/process.hpp | 9 +++++++++ src/resource.cpp | 37 ------------------------------------- src/resource.hpp | 21 +++++++++++++++++++-- 7 files changed, 58 insertions(+), 103 deletions(-) delete mode 100644 src/error.cpp delete mode 100644 src/os.cpp delete mode 100644 src/resource.cpp diff --git a/src/error.cpp b/src/error.cpp deleted file mode 100644 index edce89f..0000000 --- a/src/error.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2016 Egor Tensin -// This file is part of the "Privilege check" project. -// For details, see https://github.com/egor-tensin/privilege-check. -// Distributed under the MIT License. - -#include "error.hpp" - -#include - -#include -#include - -namespace error -{ - Error make(const char* function_name) - { - const auto ec = GetLastError(); - return {static_cast(ec), std::system_category(), function_name}; - } - - void raise(const char* function_name) - { - throw make(function_name); - } - - void report(const std::exception& e) - { - MessageBoxA(NULL, e.what(), NULL, MB_OK); - } - - int get_code(const Error& e) - { - return e.code().value(); - } -} diff --git a/src/error.hpp b/src/error.hpp index b8ba468..40522bb 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -5,6 +5,8 @@ #pragma once +#include + #include #include @@ -12,11 +14,24 @@ typedef std::system_error Error; namespace error { - Error make(const char* function_name); + inline Error make(const char* function_name) + { + const auto ec = GetLastError(); + return {static_cast(ec), std::system_category(), function_name}; + } - void raise(const char* function_name); + inline void raise(const char* function_name) + { + throw make(function_name); + } - void report(const std::exception&); + inline void report(const std::exception& e) + { + MessageBoxA(NULL, e.what(), NULL, MB_OK); + } - int get_code(const Error&); + inline int get_code(const Error& e) + { + return e.code().value(); + } } diff --git a/src/os.cpp b/src/os.cpp deleted file mode 100644 index 13fd766..0000000 --- a/src/os.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2016 Egor Tensin -// This file is part of the "Privilege check" project. -// For details, see https://github.com/egor-tensin/privilege-check. -// Distributed under the MIT License. - -#include "error.hpp" -#include "os.hpp" - -#include - -namespace os -{ - OSVERSIONINFOW get_version_info() - { - OSVERSIONINFOW info; - ZeroMemory(&info, sizeof(info)); - info.dwOSVersionInfoSize = sizeof(info); - - if (!GetVersionExW(&info)) - error::raise("GetVersionExW"); - - return info; - } -} diff --git a/src/os.hpp b/src/os.hpp index c9a89a1..6734fb4 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -11,7 +11,17 @@ namespace os { - OSVERSIONINFOW get_version_info(); + inline OSVERSIONINFOW get_version_info() + { + OSVERSIONINFOW info; + ZeroMemory(&info, sizeof(info)); + info.dwOSVersionInfoSize = sizeof(info); + + if (!GetVersionExW(&info)) + error::raise("GetVersionExW"); + + return info; + } inline bool is_vista_or_later(const OSVERSIONINFOW& info) { diff --git a/src/process.hpp b/src/process.hpp index c422aac..53fcdf8 100644 --- a/src/process.hpp +++ b/src/process.hpp @@ -5,6 +5,7 @@ #pragma once +#include "error.hpp" #include "cmd_line.hpp" #include @@ -13,6 +14,14 @@ namespace process { + inline HMODULE load_exe_module() + { + const auto module = GetModuleHandle(NULL); + if (module == NULL) + error::raise("GetModuleHandle"); + return module; + } + std::wstring get_executable_path(); inline std::wstring get_command_line() diff --git a/src/resource.cpp b/src/resource.cpp deleted file mode 100644 index e8ac300..0000000 --- a/src/resource.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2016 Egor Tensin -// This file is part of the "Privilege check" project. -// For details, see https://github.com/egor-tensin/privilege-check. -// Distributed under the MIT License. - -#include "error.hpp" -#include "resource.hpp" - -#include - -#include - -#include - -namespace resource -{ - HMODULE load_exe_module() - { - const auto module = GetModuleHandle(NULL); - if (module == NULL) - error::raise("GetModuleHandle"); - return module; - } - - std::wstring load_string(unsigned int id) - { - wchar_t* s = nullptr; - - const auto ret = LoadStringW( - load_exe_module(), id, reinterpret_cast(&s), 0); - - if (ret <= 0) - error::raise("LoadStringW"); - - return {s, static_cast(ret)}; - } -} diff --git a/src/resource.hpp b/src/resource.hpp index 0e3186c..a0f1cc3 100644 --- a/src/resource.hpp +++ b/src/resource.hpp @@ -5,13 +5,30 @@ #pragma once +#include "error.hpp" +#include "process.hpp" + #include +#include + #include namespace resource { - HMODULE load_exe_module(); + inline std::wstring load_string(unsigned int id) + { + wchar_t* s = nullptr; + + const auto ret = LoadStringW( + process::load_exe_module(), + id, + reinterpret_cast(&s), + 0); + + if (ret <= 0) + error::raise("LoadStringW"); - std::wstring load_string(unsigned int id); + return {s, static_cast(ret)}; + } } -- cgit v1.2.3