The Battle for Wesnoth  1.19.0-dev
save_blocker.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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 
22 void (play_controller::*save_blocker::callback_)() = nullptr;
23 SDL_sem* save_blocker::sem_ = SDL_CreateSemaphore(1);
24 
26  block();
27 }
28 
30  try {
31  unblock();
32  if(controller_ && callback_) {
33  (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 
43  if(try_block()) {
44  unblock();
45  (controller->*callback)();
46  } else {
48  callback_ = callback;
49  }
50 }
51 
53  return SDL_SemValue(sem_) == 0;
54 }
55 
57  SDL_SemWait(sem_);
58 }
59 
61  return SDL_SemTryWait(sem_) == 0;
62 }
63 
65  assert(SDL_SemValue(sem_) == 0);
66  SDL_SemPost(sem_);
67 }
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_sem * sem_
static void block()
Standard logging facilities (interface).
#define PLAIN_LOG
Definition: log.hpp:295
#define e