aboutsummaryrefslogtreecommitdiff
path: root/src/base/platform/win/Base.cpp
blob: 0aae64665419a8eeb0772770cdb82ef7ac965df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "cru/base/platform/win/Base.h"

#include <format>

namespace cru::platform::win {

HResultError::HResultError(HRESULT h_result,
                           std::string_view additional_message)
    : Exception(std::format("HRESULT is {}.", h_result)), h_result_(h_result) {
  AppendMessage(additional_message);
}

Win32Error::Win32Error(std::string_view message)
    : Win32Error(::GetLastError(), message) {}

Win32Error::Win32Error(DWORD error_code, std::string_view message)
    : Exception(std::format("Last Windows error code is {}.", error_code)),
      error_code_(error_code) {
  AppendMessage(message);
}

UniDirectionalWin32PipeResult OpenUniDirectionalPipe(Win32PipeFlag flag) {
  HANDLE read, write;
  CheckWinReturn(::CreatePipe(&read, &write, nullptr, 0));

  if (flag.Has(Win32PipeFlags::ReadInheritable)) {
    CheckWinReturn(
        ::SetHandleInformation(read, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT));
  }

  if (flag.Has(Win32PipeFlags::WriteInheritable)) {
    CheckWinReturn(::SetHandleInformation(write, HANDLE_FLAG_INHERIT,
                                          HANDLE_FLAG_INHERIT));
  }

  return {Win32Handle(read, true), Win32Handle(write, true)};
}
}  // namespace cru::platform::win