The Battle for Wesnoth  1.15.0+dev
save_blocker.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Daniel Franke.
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <SDL2/SDL_mutex.h>
18 
19 #include <cassert>
20 
21 class play_controller;
22 
23 /** While any instance of this class exists, attempts to save the game via
24  * any call to play_controller will be temporarily postponed: the call will
25  * return immediately without performing the save, but the save method will
26  * then be reinvoked from this class's destructor. If multiple save attempts
27  * are performed, only the last will be carried out.
28  */
29 
30 /**
31  * NOTE: This class is broken and you probably shouldn't use it. Calling a save
32  * game dialog from the destructor of a class is a bad idea, because those
33  * functions throw exceptions. For example if the user decides to quit the game,
34  * or there is a filesystem error. If the destructor throws exceptions, it will
35  * cause memory leaks and crashes.
36  * https://wiki.wesnoth.org/CodingStandards#Destructors_must_not_throw_exceptions
37  *
38  * As a temporary fix the destructor has been changed to swallow all exceptions.
39  * However this means that if the user attempts to quit the game from the savegame
40  * dialog, or any filesystem error occurs, it will be suppressed instead of being
41  * handled normally. So you should avoid using this class and it may be removed.
42  */
43 
44 class save_blocker {
45 public:
46  save_blocker();
47  ~save_blocker();
48  static bool saves_are_blocked();
49  static void on_unblock(play_controller* controller, void (play_controller::*callback)());
50 
51 protected:
52  friend class play_controller;
53  static void block();
54  static bool try_block();
55  static void unblock();
56 
57  /** An exception-safe means of making sure that unblock() gets called
58  * after try_block().
59  */
61  public:
64  };
65 
66 private:
68  static void (play_controller::*callback_)();
69  static SDL_sem* sem_;
70 };
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()