Move save_blocker out of savegame.cpp/hpp...

...and into its own module, per request by jhinrichs
This commit is contained in:
Daniel Franke 2009-04-08 07:00:29 +00:00
parent 95a7c0a1a3
commit 55483b8f63
9 changed files with 119 additions and 73 deletions

View file

@ -301,6 +301,7 @@ SET(wesnoth-main_SRC
portrait.cpp
replay.cpp
replay_controller.cpp
save_blocker.cpp
savegame.cpp
scripting/lua.cpp
sha1.cpp

View file

@ -138,6 +138,7 @@ wesnoth_source = \
portrait.cpp \
replay.cpp \
replay_controller.cpp \
save_blocker.cpp \
savegame.cpp \
scripting/lua.cpp \
sha1.cpp \

View file

@ -193,6 +193,7 @@ wesnoth_sources = Split("""
portrait.cpp
replay.cpp
replay_controller.cpp
save_blocker.cpp
savegame.cpp
scripting/lua.cpp
sha1.cpp

View file

@ -28,7 +28,7 @@
#include "sound.hpp"
#include "unit_id.hpp"
#include "terrain_filter.hpp"
#include "savegame.hpp"
#include "save_blocker.hpp"
#define LOG_NG LOG_STREAM(info, engine)

View file

@ -34,7 +34,7 @@
#include "upload_log.hpp"
#include "formula_string_utils.hpp"
#include "events.hpp"
#include "savegame.hpp"
#include "save_blocker.hpp"
#define ERR_NG LOG_STREAM(err, engine)
#define LOG_NG LOG_STREAM(info, engine)

59
src/save_blocker.cpp Normal file
View file

@ -0,0 +1,59 @@
/* $Id$ */
/*
Copyright (C) 2009 by Daniel Franke.
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "save_blocker.hpp"
play_controller* save_blocker::controller_ = NULL;
void (play_controller::*save_blocker::callback_)() = NULL;
SDL_sem* save_blocker::sem_ = SDL_CreateSemaphore(1);
save_blocker::save_blocker() {
block();
}
save_blocker::~save_blocker() {
unblock();
if(controller_ && callback_) {
(controller_->*callback_)();
controller_ = NULL;
callback_ = NULL;
}
}
void save_blocker::on_unblock(play_controller* controller, void (play_controller::*callback)()) {
if(try_block()) {
unblock();
(controller->*callback)();
} else {
controller_ = controller;
callback_ = callback;
}
}
bool save_blocker::saves_are_blocked() {
return SDL_SemValue(sem_) == 0;
}
void save_blocker::block() {
SDL_SemWait(sem_);
}
bool save_blocker::try_block() {
return SDL_SemTryWait(sem_) == 0;
}
void save_blocker::unblock() {
assert(SDL_SemValue(sem_) == 0);
SDL_SemPost(sem_);
}

55
src/save_blocker.hpp Normal file
View file

@ -0,0 +1,55 @@
/* $Id$ */
/*
Copyright (C) 2009 by Daniel Franke.
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef SAVE_BLOCKER_H_INCLUDED
#define SAVE_BLOCKER_H_INCLUDED
#include "play_controller.hpp"
#include "SDL_mutex.h"
/** While any instance of this class exists, attempts to save the game via
* any call to play_controller will be temporarily postponed: the call will
* return immediately without performing the save, but the save method will
* then be reinvoked from this class's destructor. If multiple save attempts
* are performed, only the last will be carried out.
*/
class save_blocker {
public:
save_blocker();
~save_blocker();
static bool saves_are_blocked();
static void on_unblock(play_controller* controller, void (play_controller::*callback)());
protected:
friend class play_controller;
static void block();
static bool try_block();
static void unblock();
/** An exception-safe means of making sure that unblock() gets called
* after try_block().
*/
class save_unblocker {
public:
save_unblocker() {}
~save_unblocker() { save_blocker::unblock(); }
};
private:
static play_controller *controller_;
static void (play_controller::*callback_)();
static SDL_sem* sem_;
};
#endif

View file

@ -39,50 +39,6 @@ savegame::savegame(game_state& gamestate, const std::string title)
, interactive_(false)
{}
play_controller* save_blocker::controller_ = NULL;
void (play_controller::*save_blocker::callback_)() = NULL;
SDL_sem* save_blocker::sem_ = SDL_CreateSemaphore(1);
save_blocker::save_blocker() {
block();
}
save_blocker::~save_blocker() {
unblock();
if(controller_ && callback_) {
(controller_->*callback_)();
controller_ = NULL;
callback_ = NULL;
}
}
void save_blocker::on_unblock(play_controller* controller, void (play_controller::*callback)()) {
if(try_block()) {
unblock();
(controller->*callback)();
} else {
controller_ = controller;
callback_ = callback;
}
}
bool save_blocker::saves_are_blocked() {
return SDL_SemValue(sem_) == 0;
}
void save_blocker::block() {
SDL_SemWait(sem_);
}
bool save_blocker::try_block() {
return SDL_SemTryWait(sem_) == 0;
}
void save_blocker::unblock() {
assert(SDL_SemValue(sem_) == 0);
SDL_SemPost(sem_);
}
void savegame::save_game_interactive(display& gui, const std::string& message,
gui::DIALOG_TYPE dialog_type, const bool has_exit_button,
const bool ask_for_filename)

View file

@ -19,36 +19,9 @@
#include "global.hpp"
#include "show_dialog.hpp"
#include "gamestatus.hpp"
#include "play_controller.hpp"
#include <string>
#include "SDL_mutex.h"
class save_blocker {
public:
save_blocker();
~save_blocker();
static bool saves_are_blocked();
static void on_unblock(play_controller* controller, void (play_controller::*callback)());
protected:
friend class play_controller;
static void block();
static bool try_block();
static void unblock();
class save_unblocker {
public:
save_unblocker() {}
~save_unblocker() { save_blocker::unblock(); }
};
private:
static play_controller *controller_;
static void (play_controller::*callback_)();
static SDL_sem* sem_;
};
/** The base class for all savegame stuff */
class savegame