The Battle for Wesnoth  1.17.0-dev
save_blocker.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
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 #pragma once
17 
18 #include <SDL2/SDL_mutex.h>
19 
20 #include <cassert>
21 
22 class play_controller;
23 
24 /** While any instance of this class exists, attempts to save the game via
25  * any call to play_controller will be temporarily postponed: the call will
26  * return immediately without performing the save, but the save method will
27  * then be reinvoked from this class's destructor. If multiple save attempts
28  * are performed, only the last will be carried out.
29  */
30 
31 /**
32  * NOTE: This class is broken and you probably shouldn't use it. Calling a save
33  * game dialog from the destructor of a class is a bad idea, because those
34  * functions throw exceptions. For example if the user decides to quit the game,
35  * or there is a filesystem error. If the destructor throws exceptions, it will
36  * cause memory leaks and crashes.
37  * https://wiki.wesnoth.org/CodingStandards#Destructors_must_not_throw_exceptions
38  *
39  * As a temporary fix the destructor has been changed to swallow all exceptions.
40  * However this means that if the user attempts to quit the game from the savegame
41  * dialog, or any filesystem error occurs, it will be suppressed instead of being
42  * handled normally. So you should avoid using this class and it may be removed.
43  */
44 
45 class save_blocker {
46 public:
47  save_blocker();
48  ~save_blocker();
49  static bool saves_are_blocked();
50  static void on_unblock(play_controller* controller, void (play_controller::*callback)());
51 
52 protected:
53  friend class play_controller;
54  static void block();
55  static bool try_block();
56  static void unblock();
57 
58  /** An exception-safe means of making sure that unblock() gets called
59  * after try_block().
60  */
62  public:
65  };
66 
67 private:
69  static void (play_controller::*callback_)();
70  static SDL_sem* sem_;
71 };
While any instance of this class exists, attempts to save the game via any call to play_controller wi...
static void unblock()
static void on_unblock(play_controller *controller, void(play_controller::*callback)())
An exception-safe means of making sure that unblock() gets called after try_block().
static void(play_controller::* callback_)()
static bool saves_are_blocked()
static void block()
static play_controller * controller_
static SDL_sem * sem_
static bool try_block()