aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/um/service
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-05-01 16:21:37 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-05-01 16:21:37 +0300
commit669a246c175a002e368404fc0037379900bcca04 (patch)
tree2d50a6775f2ede118fdd6ab3f1ad05a726a891b4 /um/service
parentum: strip the 'lib' prefix from namespace names (diff)
downloadwindows7-drivers-669a246c175a002e368404fc0037379900bcca04.tar.gz
windows7-drivers-669a246c175a002e368404fc0037379900bcca04.zip
service: refactoring
* No more compiler-specific macros.
Diffstat (limited to '')
-rw-r--r--um/service/include/service/common.hpp9
-rw-r--r--um/service/include/service/windows_error.hpp52
-rw-r--r--um/service/src/device.cpp10
-rw-r--r--um/service/src/service.cpp22
-rw-r--r--um/service/src/service_manager.cpp5
-rw-r--r--um/service/src/windows_error.cpp41
-rw-r--r--um/service/test/windows_error.cpp6
7 files changed, 63 insertions, 82 deletions
diff --git a/um/service/include/service/common.hpp b/um/service/include/service/common.hpp
index a1c46fb..89aa600 100644
--- a/um/service/include/service/common.hpp
+++ b/um/service/include/service/common.hpp
@@ -5,13 +5,4 @@
#pragma once
-#define LIBSERVICE_FILE_PATH __FILE__
-#define LIBSERVICE_LINE_NUMBER __LINE__
-#define LIBSERVICE_FUNCTION_NAME __FUNCTION__
-
-#define LIBSERVICE_TO_STRING(s) LIBSERVICE_TO_STRING_(s)
-#define LIBSERVICE_TO_STRING_(s) #s
-
-#define LIBSERVICE_LINE_NUMBER_STRING LIBSERVICE_TO_STRING(LIBSERVICE_LINE_NUMBER)
-
#define LIBSERVICE_NOEXCEPT throw()
diff --git a/um/service/include/service/windows_error.hpp b/um/service/include/service/windows_error.hpp
index c96dcfc..ab46cfd 100644
--- a/um/service/include/service/windows_error.hpp
+++ b/um/service/include/service/windows_error.hpp
@@ -8,31 +8,33 @@
#include "common.hpp"
#include "singleton.hpp"
+#include <Windows.h>
+
#include <sstream>
#include <string>
#include <system_error>
namespace service
{
- class WindowsErrorCategory
- : public std::error_category
- , public Singleton<WindowsErrorCategory>
+ namespace windows_error
{
- public:
- const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; }
+ class Category
+ : public std::error_category
+ , public Singleton<Category>
+ {
+ public:
+ const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; }
- std::string message(int) const;
+ std::string message(int) const;
- private:
- friend class Singleton<WindowsErrorCategory>;
- };
+ private:
+ friend class Singleton<Category>;
+ };
- namespace error
- {
- inline std::string build_what(
- const char* function,
+ inline std::string build_message_prefix(
const char* file,
- int line)
+ int line,
+ const char* function)
{
std::ostringstream oss;
oss << "Error in function '" << function
@@ -40,15 +42,17 @@ namespace service
<< "', line " << line;
return oss.str();
}
+
+ inline std::system_error make(
+ DWORD code,
+ const char* file,
+ int line,
+ const char* function)
+ {
+ return std::system_error(
+ code,
+ Category::get(),
+ build_message_prefix(file, line, function));
+ }
}
}
-
-#if defined(_MSC_VER)
-#define LIBSERVICE_ERROR_PREFIX \
- "Error in function '" LIBSERVICE_FUNCTION_NAME "' at file '" LIBSERVICE_FILE_PATH "', line " LIBSERVICE_LINE_NUMBER_STRING
-#elif defined(__GNUC__)
-#define LIBSERVICE_ERROR_PREFIX \
- service::error::build_what(LIBSERVICE_FUNCTION_NAME, LIBSERVICE_FILE_PATH, LIBSERVICE_LINE_NUMBER)
-#else
-#define LIBSERVICE_ERROR_PREFIX "Error"
-#endif
diff --git a/um/service/src/device.cpp b/um/service/src/device.cpp
index 003c19d..870930f 100644
--- a/um/service/src/device.cpp
+++ b/um/service/src/device.cpp
@@ -12,7 +12,6 @@
#include <limits>
#include <stdexcept>
#include <string>
-#include <system_error>
namespace service
{
@@ -32,8 +31,7 @@ namespace service
if (INVALID_HANDLE_VALUE == raw)
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
return Handle(raw);
@@ -75,8 +73,7 @@ namespace service
return nbreq;
default:
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
}
@@ -110,8 +107,7 @@ namespace service
if (0 == nbwritten)
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
return nbwritten;
diff --git a/um/service/src/service.cpp b/um/service/src/service.cpp
index 26cf35d..b7384ce 100644
--- a/um/service/src/service.cpp
+++ b/um/service/src/service.cpp
@@ -8,7 +8,6 @@
#include <Windows.h>
#include <string>
-#include <system_error>
namespace service
{
@@ -26,8 +25,7 @@ namespace service
if (NULL == raw)
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
return raw;
@@ -56,8 +54,7 @@ namespace service
if (NULL == raw)
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
return raw;
@@ -68,8 +65,7 @@ namespace service
if (!StartService(handle, 0, NULL))
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
}
@@ -80,8 +76,7 @@ namespace service
if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status))
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
}
@@ -90,8 +85,7 @@ namespace service
if (!DeleteService(handle))
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
}
@@ -118,8 +112,7 @@ namespace service
return false;
default:
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
}
@@ -135,8 +128,7 @@ namespace service
handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq))
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
return status;
diff --git a/um/service/src/service_manager.cpp b/um/service/src/service_manager.cpp
index 58377ed..48c6bfe 100644
--- a/um/service/src/service_manager.cpp
+++ b/um/service/src/service_manager.cpp
@@ -7,8 +7,6 @@
#include <Windows.h>
-#include <system_error>
-
namespace service
{
ServiceManager ServiceManager::open()
@@ -18,8 +16,7 @@ namespace service
if (NULL == raw)
{
const auto ec = GetLastError();
- throw std::system_error(
- ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__);
}
return ServiceHandle(raw);
diff --git a/um/service/src/windows_error.cpp b/um/service/src/windows_error.cpp
index 5b358ca..c228536 100644
--- a/um/service/src/windows_error.cpp
+++ b/um/service/src/windows_error.cpp
@@ -11,29 +11,32 @@
namespace service
{
- std::string WindowsErrorCategory::message(int code) const
+ namespace windows_error
{
- char* buf_ptr;
+ std::string Category::message(int code) const
+ {
+ char* buf_ptr;
- const auto nbwritten = FormatMessageA(
- FORMAT_MESSAGE_ALLOCATE_BUFFER
- | FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- reinterpret_cast<char*>(&buf_ptr),
- 0,
- NULL);
+ const auto nbwritten = FormatMessageA(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER
+ | FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ code,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ reinterpret_cast<char*>(&buf_ptr),
+ 0,
+ NULL);
- if (0 == nbwritten)
- {
+ if (0 == nbwritten)
+ {
+ LocalFree(buf_ptr);
+ return "Couldn't format the error message";
+ }
+
+ std::string str(buf_ptr, nbwritten - 2);
LocalFree(buf_ptr);
- return "Couldn't format error message";
+ return str;
}
-
- std::string str(buf_ptr, nbwritten - 2);
- LocalFree(buf_ptr);
- return str;
}
}
diff --git a/um/service/test/windows_error.cpp b/um/service/test/windows_error.cpp
index 5785e99..1334620 100644
--- a/um/service/test/windows_error.cpp
+++ b/um/service/test/windows_error.cpp
@@ -9,16 +9,14 @@
#include <exception>
#include <iostream>
-#include <system_error>
int main()
{
try
{
- throw std::system_error(
+ throw service::windows_error::make(
ERROR_FILE_NOT_FOUND,
- service::WindowsErrorCategory::get(),
- LIBSERVICE_ERROR_PREFIX);
+ __FILE__, __LINE__, __FUNCTION__);
}
catch (const std::exception& e)
{