winapi_common
error.hpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
2 // This file is part of the "winapi-common" project.
3 // For details, see https://github.com/egor-tensin/winapi-common.
4 // Distributed under the MIT License.
5 
11 #pragma once
12 
13 #include <windows.h>
14 
15 #include <cstdint>
16 #include <sstream>
17 #include <stdexcept>
18 #include <string>
19 #include <system_error>
20 
21 namespace winapi {
22 namespace error {
23 
24 class CategoryWindows : public std::error_category {
25 public:
26  CategoryWindows() = default;
27 
28  const char* name() const noexcept { return "Windows"; }
29 
30  std::string message(int32_t) const;
31 };
32 
33 inline const CategoryWindows& category_windows() {
34  static const CategoryWindows instance;
35  return instance;
36 }
37 
43 std::system_error windows(DWORD code, const char* function);
44 
45 template <typename Ret>
46 std::runtime_error custom(Ret ret, const char* function) {
47  std::ostringstream oss;
48  oss << "Function " << function << " failed with error code " << ret;
49  return std::runtime_error{oss.str()};
50 }
51 
52 } // namespace error
53 } // namespace winapi