The Battle for Wesnoth  1.15.2+dev
synced_commands.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by David White <dave@whitevine.net>
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 <map>
18 #include <exception>
19 #include <string>
20 
21 #include "utils/functional.hpp"
22 
23 class config;
24 
26  public:
27  /*
28  the parameters or error handlers are
29  1) the message of the error
30  2) a boolean that indicates whether the error is heavy enough to make proceeding impossible.
31  TODO: remove the second argument because it isn't used.
32 
33  */
34  typedef std::function<void(const std::string&, bool)> error_handler_function;
35  /*
36  returns: true if the action succeeded correctly,
37 
38  */
39  typedef bool (*handler)(const config &, bool use_undo, bool show, error_handler_function error_handler);
40  typedef std::map<std::string, handler> map;
41 
42 
43  synced_command(const std::string & tag, handler function);
44 
45  /// using static function variable instead of static member variable to prevent static initialization fiasco when used in other files.
46  static map& registry();
47  };
48 
49 /*
50  this is currently only used in "synced_commands.cpp" and there is no reason to use it anywhere else.
51  but if you have a good reason feel free to do so.
52 */
53 
54 #define SYNCED_COMMAND_HANDLER_FUNCTION(pname, pcfg, use_undo, show, error_handler) \
55  static bool synced_command_func_##pname(const config & pcfg, bool use_undo, bool show, synced_command::error_handler_function error_handler ); \
56  static synced_command synced_command_action_##pname(#pname, &synced_command_func_##pname); \
57  static bool synced_command_func_##pname(const config & pcfg, bool use_undo, bool show, synced_command::error_handler_function error_handler)
static map & registry()
using static function variable instead of static member variable to prevent static initialization fia...
synced_command(const std::string &tag, handler function)
std::map< std::string, handler > map
bool(* handler)(const config &, bool use_undo, bool show, error_handler_function error_handler)
std::function< void(const std::string &, bool)> error_handler_function
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
void show(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Shows a tip.
Definition: tooltip.cpp:154