The Battle for Wesnoth  1.19.5+dev
synced_commands.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <map>
19 #include <string>
20 
21 #include <functional>
22 
23 class config;
24 
26 {
27 public:
28  typedef std::function<void(const std::string&)> error_handler_function;
29  action_spectator() = default;
31  : eh_(eh)
32  {
33  }
34  virtual ~action_spectator() = default;
35  /**
36  * Called when synced_context::run received nonsensial data based on the current gamestate.
37  */
38  virtual void error(const std::string& message)
39  {
40  eh_(message);
41  }
43 };
44 
45 // TODO: it would be nice if we would use some consistent terminology along the codebase
46 // The things that the user invokes are sometimes called "commands" (in particular
47 // in the replay), and sometimes called "actions" (in particular inn the actions/ folder)
48 
50  public:
51  /*
52  the parameters or error handlers are
53  1) the message of the error
54  */
55  typedef std::function<void(const std::string&)> error_handler_function;
56  /*
57  returns: true if the action succeeded correctly,
58  */
59  typedef bool (*handler)(const config&, action_spectator& spectator);
60  typedef std::map<std::string, handler> map;
61 
62 
63  synced_command(const std::string & tag, handler function);
64 
65  /** using static function variable instead of static member variable to prevent static initialization fiasco when used in other files. */
66  static map& registry();
67  };
68 
69 /*
70  this is currently only used in "synced_commands.cpp" and there is no reason to use it anywhere else.
71  but if you have a good reason feel free to do so.
72 */
73 
74 #define SYNCED_COMMAND_HANDLER_FUNCTION(pname, pcfg, spectator) \
75  static bool synced_command_func_##pname(const config& pcfg, action_spectator& spectator); \
76  static synced_command synced_command_action_##pname(#pname, &synced_command_func_##pname); \
77  static bool synced_command_func_##pname(const config& pcfg, action_spectator& spectator)
action_spectator()=default
virtual ~action_spectator()=default
error_handler_function eh_
virtual void error(const std::string &message)
Called when synced_context::run received nonsensial data based on the current gamestate.
std::function< void(const std::string &)> error_handler_function
action_spectator(error_handler_function eh)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
bool(* handler)(const config &, action_spectator &spectator)
synced_command(const std::string &tag, handler function)
static map & registry()
using static function variable instead of static member variable to prevent static initialization fia...
std::function< void(const std::string &)> error_handler_function
std::map< std::string, handler > map
std::string tag(const std::string &tag_name, Args &&... contents)
Definition: markup.hpp:45