The Battle for Wesnoth
1.17.4+dev
save_blocker.cpp
Go to the documentation of this file.
1
/*
2
Copyright (C) 2009 - 2022
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 "
save_blocker.hpp
"
17
#include <exception>
18
#include <iostream>
19
20
play_controller
*
save_blocker::controller_
=
nullptr
;
21
void (
play_controller
::*
save_blocker::callback_
)() =
nullptr
;
22
SDL_sem*
save_blocker::sem_
= SDL_CreateSemaphore(1);
23
24
save_blocker::save_blocker
() {
25
block
();
26
}
27
28
save_blocker::~save_blocker
() {
29
try
{
30
unblock
();
31
if
(
controller_
&&
callback_
) {
32
(
controller_
->*
callback_
)();
33
controller_
=
nullptr
;
34
callback_
=
nullptr
;
35
}
36
}
catch
(
const
std::exception &
e
) {
37
std::cerr <<
"Save blocker dtor swallowing an exception: "
<< e.what() <<
"\n"
;
38
}
39
}
40
41
void
save_blocker::on_unblock
(
play_controller
*
controller
,
void
(
play_controller
::*callback)()) {
42
if
(
try_block
()) {
43
unblock
();
44
(controller->*callback)();
45
}
else
{
46
controller_
=
controller
;
47
callback_
= callback;
48
}
49
}
50
51
bool
save_blocker::saves_are_blocked
() {
52
return
SDL_SemValue(
sem_
) == 0;
53
}
54
55
void
save_blocker::block
() {
56
SDL_SemWait(
sem_
);
57
}
58
59
bool
save_blocker::try_block
() {
60
return
SDL_SemTryWait(
sem_
) == 0;
61
}
62
63
void
save_blocker::unblock
() {
64
assert(SDL_SemValue(
sem_
) == 0);
65
SDL_SemPost(
sem_
);
66
}
play_controller
Definition:
play_controller.hpp:80
save_blocker::unblock
static void unblock()
Definition:
save_blocker.cpp:63
save_blocker::on_unblock
static void on_unblock(play_controller *controller, void(play_controller::*callback)())
Definition:
save_blocker.cpp:41
save_blocker::save_blocker
save_blocker()
Definition:
save_blocker.cpp:24
save_blocker::callback_
static void(play_controller::* callback_)()
Definition:
save_blocker.hpp:69
save_blocker::saves_are_blocked
static bool saves_are_blocked()
Definition:
save_blocker.cpp:51
save_blocker.hpp
save_blocker::~save_blocker
~save_blocker()
Definition:
save_blocker.cpp:28
save_blocker::block
static void block()
Definition:
save_blocker.cpp:55
save_blocker::controller_
static play_controller * controller_
Definition:
save_blocker.hpp:68
save_blocker::sem_
static SDL_sem * sem_
Definition:
save_blocker.hpp:70
save_blocker::try_block
static bool try_block()
Definition:
save_blocker.cpp:59
ng::controller
controller
Definition:
connect_engine.hpp:30
e
#define e
Generated by
1.8.13