|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +// XG_CancellationManager.hpp --- Cancellation Manager |
| 3 | +// Copyright (C) 2012-2020 Katayama Hirofumi MZ. All Rights Reserved. |
| 4 | +// (Japanese, UTF-8) |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include "XWordGiver.hpp" |
| 9 | + |
| 10 | +////////////////////////////////////////////////////////////////////////////// |
| 11 | +// キャンセル管理クラス(軽量設計、実行速度への影響を最小化) |
| 12 | +// Lightweight cancellation manager to ensure stable state during cancellation |
| 13 | +// without impacting execution speed. |
| 14 | + |
| 15 | +class XG_CancellationManager |
| 16 | +{ |
| 17 | +private: |
| 18 | + HANDLE m_hCompletionEvent; // 完了通知用イベント(completion event for thread waiting only) |
| 19 | + XG_Board m_initial_board; // 初期状態(復元用)(initial state for restoration) |
| 20 | + |
| 21 | +public: |
| 22 | + // コンストラクタ |
| 23 | + XG_CancellationManager() noexcept |
| 24 | + : m_hCompletionEvent(nullptr) |
| 25 | + { |
| 26 | + // 手動リセット、初期状態は非シグナル状態のイベントを作成 |
| 27 | + // Create manual-reset event, initially non-signaled |
| 28 | + m_hCompletionEvent = ::CreateEventW(nullptr, TRUE, FALSE, nullptr); |
| 29 | + } |
| 30 | + |
| 31 | + // デストラクタ |
| 32 | + ~XG_CancellationManager() noexcept |
| 33 | + { |
| 34 | + if (m_hCompletionEvent != nullptr) { |
| 35 | + ::CloseHandle(m_hCompletionEvent); |
| 36 | + m_hCompletionEvent = nullptr; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // 初期状態を保存 |
| 41 | + // Save initial state for potential restoration |
| 42 | + void SaveInitialState() noexcept |
| 43 | + { |
| 44 | + m_initial_board = xg_xword; |
| 45 | + } |
| 46 | + |
| 47 | + // 完了を通知(処理成功時) |
| 48 | + // Notify completion (on successful processing) |
| 49 | + void SetCompleted() noexcept |
| 50 | + { |
| 51 | + if (m_hCompletionEvent != nullptr) { |
| 52 | + ::SetEvent(m_hCompletionEvent); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // 完了またはキャンセルを待機(タイムアウト付き) |
| 57 | + // Wait for completion or cancellation with timeout |
| 58 | + // Returns true if completed normally, false if timeout or cancelled |
| 59 | + bool WaitForCompletion(DWORD dwMilliseconds = 5000) const noexcept |
| 60 | + { |
| 61 | + if (m_hCompletionEvent == nullptr) |
| 62 | + return false; |
| 63 | + |
| 64 | + DWORD dwResult = ::WaitForSingleObject(m_hCompletionEvent, dwMilliseconds); |
| 65 | + return (dwResult == WAIT_OBJECT_0); |
| 66 | + } |
| 67 | + |
| 68 | + // キャンセル時に初期状態に復元 |
| 69 | + // Restore initial state on cancellation |
| 70 | + void RestoreOnCancel() noexcept |
| 71 | + { |
| 72 | + // キャンセルされた場合のみ復元 |
| 73 | + // Only restore if cancelled |
| 74 | + if (xg_bCancelled && !xg_bSolved) { |
| 75 | + xg_xword = m_initial_board; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // リセット(再利用時) |
| 80 | + // Reset for reuse |
| 81 | + void Reset() noexcept |
| 82 | + { |
| 83 | + if (m_hCompletionEvent != nullptr) { |
| 84 | + ::ResetEvent(m_hCompletionEvent); |
| 85 | + } |
| 86 | + m_initial_board.clear(); |
| 87 | + } |
| 88 | + |
| 89 | + // コピー禁止 |
| 90 | + XG_CancellationManager(const XG_CancellationManager&) = delete; |
| 91 | + XG_CancellationManager& operator=(const XG_CancellationManager&) = delete; |
| 92 | +}; |
| 93 | + |
| 94 | +////////////////////////////////////////////////////////////////////////////// |
0 commit comments