wesnoth/src/synced_commands.hpp
gfgtdf fc8c15e46a add synced_context class
the intention  is to fix #20871 and implement #21697, to sync
prestart/start events, and to implement
http://forums.wesnoth.org/viewtopic.php?f=6&t=39611 in the next patches,

the intention of synced_checkup is to replace random .. set/get_random_results.
Because set/get_random_results didn't have much to do with random, since it was only used to compare unit checksums and attacker damage to replays.

the intention of synced_commands is to move code out of do_replay_handle and make it callable from other places too so that we can just call the same function taht was called from replay in the simple cases (with synced_context::run_in_synced_command).

the object set_scontext_synced can be used to enter the synced context which enables synced_checkup and make the random calls synced.
Or we can use run_in_synced_context which is normaly easier than set_scontext_synced.
we can check wether we are in a synced context with get_syced_state to make addidional checks to detect oos (the other intention of that is to implement #21697 ).

this commit is part of pf 121.
2014-04-02 21:44:52 +02:00

55 lines
2.0 KiB
C++

/*
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 as published by
the Free Software Foundation; either version 2 of the License, 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 SYNCED_COMMANDS_H_INCLUDED
#define SYNCED_COMMANDS_H_INCLUDED
#include <map>
#include <exception>
#include "config.hpp"
#include <boost/function.hpp>
class synced_command {
public:
/*
the parameters or error handlers are
1) the message of the error
2) a boolean that indicates whether the error is heavy enough to make proceeding impossible.
TODO: remove the second argument because it isn't used.
*/
typedef boost::function2<void, const std::string&, bool> error_handler_function;
/*
returns: true if the action succeeded correclty,
*/
typedef bool (*handler)(const config &, bool use_undo, bool show, error_handler_function error_handler);
typedef std::map<std::string, handler> map;
synced_command(const std::string & tag, handler function);
/// using static function variable instead of static member variable to prevent static initialization fiasco when used in other files.
static map& registry();
};
/*
this is currently only used in "synced_commands.cpp" and there is no reason to use it anywhere else.
but if you have a good reason feel free to do so.
*/
#define SYNCED_COMMAND_HANDLER_FUNCTION(pname, pcfg, use_undo, show, error_handler) \
static bool synced_command_func_##pname(const config & pcfg, bool use_undo, bool show, synced_command::error_handler_function error_handler ); \
static synced_command synced_command_action_##pname(#pname, &synced_command_func_##pname); \
static bool synced_command_func_##pname(const config & pcfg, bool use_undo, bool show, synced_command::error_handler_function error_handler)
#endif