The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
save_blocker.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 - 2025
3 by Daniel Franke
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#include "log.hpp"
17#include "save_blocker.hpp"
18
19#include <exception>
20
22void (play_controller::*save_blocker::callback_)() = nullptr;
23SDL_Semaphore* save_blocker::sem_ = SDL_CreateSemaphore(1);
24
28
30 try {
31 unblock();
32 if(controller_ && callback_) {
34 controller_ = nullptr;
35 callback_ = nullptr;
36 }
37 } catch (const std::exception & e) {
38 PLAIN_LOG << "Save blocker dtor swallowing an exception: " << e.what();
39 }
40}
41
42void save_blocker::on_unblock(play_controller* controller, void (play_controller::*callback)()) {
43 if(try_block()) {
44 unblock();
45 (controller->*callback)();
46 } else {
47 controller_ = controller;
48 callback_ = callback;
49 }
50}
51
53 return SDL_GetSemaphoreValue(sem_) == 0;
54}
55
57 SDL_WaitSemaphore(sem_);
58}
59
61 return SDL_TryWaitSemaphore(sem_) == 0;
62}
63
65 assert(SDL_GetSemaphoreValue(sem_) == 0);
66 SDL_SignalSemaphore(sem_);
67}
While any instance of this class exists, attempts to save the game via any call to play_controller wi...
static bool saves_are_blocked()
static void on_unblock(play_controller *controller, void(play_controller::*callback)())
static void(play_controller::* callback_)()
static play_controller * controller_
static void unblock()
static bool try_block()
static SDL_Semaphore * sem_
static void block()
Standard logging facilities (interface).
#define PLAIN_LOG
Definition log.hpp:296
#define e