save_blocker.cpp

Go to the documentation of this file.
00001 /* $Id: save_blocker.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Daniel Franke.
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #include "save_blocker.hpp"
00017 
00018 play_controller* save_blocker::controller_ = NULL;
00019 void (play_controller::*save_blocker::callback_)() = NULL;
00020 SDL_sem* save_blocker::sem_ = SDL_CreateSemaphore(1);
00021 
00022 save_blocker::save_blocker() {
00023     block();
00024 }
00025 
00026 save_blocker::~save_blocker() {
00027     unblock();
00028     if(controller_ && callback_) {
00029         (controller_->*callback_)();
00030         controller_ = NULL;
00031         callback_ = NULL;
00032     }
00033 }
00034 
00035 void save_blocker::on_unblock(play_controller* controller, void (play_controller::*callback)()) {
00036     if(try_block()) {
00037         unblock();
00038         (controller->*callback)();
00039     } else {
00040         controller_ = controller;
00041         callback_ = callback;
00042     }
00043 }
00044 
00045 bool save_blocker::saves_are_blocked() {
00046     return SDL_SemValue(sem_) == 0;
00047 }
00048 
00049 void save_blocker::block() {
00050     SDL_SemWait(sem_);
00051 }
00052 
00053 bool save_blocker::try_block() {
00054     return SDL_SemTryWait(sem_) == 0;
00055 }
00056 
00057 void save_blocker::unblock() {
00058     assert(SDL_SemValue(sem_) == 0);
00059     SDL_SemPost(sem_);
00060 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:08 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs