AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AWin32EventWait.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <Windows.h>
15#include <functional>
16#include <cassert>
17
21class API_AUI_CORE AWin32EventWait: public aui::noncopyable {
22private:
23 HANDLE mNewWaitObject = nullptr;
24 std::function<void()> mCallback;
25
26public:
27 AWin32EventWait() noexcept = default;
29 if (mNewWaitObject != nullptr) {
30 auto r = UnregisterWaitEx(mNewWaitObject, INVALID_HANDLE_VALUE);
31 AUI_ASSERT(r != 0);
32 }
33 }
34
35 void registerWaitForSingleObject(HANDLE baseHandle, std::function<void()> callback, DWORD timeout = INFINITE, DWORD flags = WT_EXECUTEDEFAULT) noexcept {
36 AUI_ASSERTX(mNewWaitObject == nullptr, "waitForExitCode object already registered");
37
38 mCallback = std::move(callback);
39
40 auto r = RegisterWaitForSingleObject(&mNewWaitObject, baseHandle, [](PVOID context, BOOLEAN){
41 auto self = reinterpret_cast<AWin32EventWait*>(context);
42 self->mCallback();
43 }, this, timeout, flags);
44 AUI_ASSERT(r != 0);
45 }
46};
47
48
Encapsulates calls to RegisterWaitForSingleObject/UnregisterWait.
Definition: AWin32EventWait.h:21
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition: Assert.h:55
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition: Assert.h:74
Forbids copy of your class.
Definition: values.h:40