The Battle for Wesnoth  1.19.7+dev
undo_action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 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 "utils/optional_fwd.hpp"
19 #include "map/location.hpp"
20 #include "synced_context.hpp"
21 #include "config.hpp"
22 
23 namespace actions {
24 
25  struct undo_action;
26 
27  /*
28  * Contains all steps that are needed to undo a user action.
29  */
31  {
32  using t_step_ptr = std::unique_ptr<undo_action>;
33  using t_steps = std::vector<t_step_ptr>;
34 
37 
38  public:
39 
41 
42  bool empty() const { return steps_.empty(); }
43  t_steps& steps() { return steps_; }
44  bool undo(int side);
45  void write(config& cfg);
46  /**
47  * Creates the list of undo steps based on a config.
48  * Throws bad_lexical_cast or config::error if it cannot parse the config properly.
49  */
50  void read(const config& cfg);
51  void add(t_step_ptr&& action);
52  void set_unit_id_diff(int id_diff)
53  {
54  unit_id_diff_ = id_diff;
55  }
56 
57  using t_factory = std::function<t_step_ptr(const config&)>;
58  using t_factory_map = std::map<std::string, t_factory>;
59  static t_factory_map& get_factories();
60 
61  template<typename T>
63  {
65  {
66  std::string name = T::get_type_impl();
67  get_factories()[name] = [](const config& cfg) {
68  auto res = std::make_unique<T>(cfg);
69  return res;
70  };
71  }
72  };
73  };
74 
75  /**
76  * Records information to be able to undo an action.
77  * Each type of step gets its own derived type.
78  */
79  struct undo_action
80  {
82  // Virtual destructor to support derived classes.
83  virtual ~undo_action() {}
84 
85  /**
86  * Undoes this action.
87  * @return true on success; false on an error.
88  */
89  virtual bool undo(int side) = 0;
90  /** Writes this into the provided config. */
91  virtual void write(config& cfg) const
92  {
93  cfg["type"] = this->get_type();
94  }
95  virtual const char* get_type() const = 0;
96  };
97 
98  class undo_event : public undo_action
99  {
100  public:
101 
102  undo_event(int fcn_idx, const config& args, const game_events::queued_event& ctx);
103  undo_event(const config& cmds, const game_events::queued_event& ctx);
104 
105  undo_event(const config& cfg);
106 
107  virtual bool undo(int side);
108  virtual void write(config& cfg) const;
109 
110 
111  static const char* get_type_impl() { return "event"; }
112  virtual const char* get_type() const { return get_type_impl(); }
113 
114  private:
115  undo_event(const config& first, const config& second, const config& weapons, const config& cmds);
116 
117  utils::optional<int> lua_idx;
120  std::size_t uid1, uid2;
121  std::string id1, id2;
122  };
123 }
std::vector< t_step_ptr > t_steps
Definition: undo_action.hpp:33
void set_unit_id_diff(int id_diff)
Definition: undo_action.hpp:52
std::unique_ptr< undo_action > t_step_ptr
Definition: undo_action.hpp:32
void read(const config &cfg)
Creates the list of undo steps based on a config.
Definition: undo_action.cpp:65
std::map< std::string, t_factory > t_factory_map
Definition: undo_action.hpp:58
void add(t_step_ptr &&action)
Definition: undo_action.cpp:59
static t_factory_map & get_factories()
Definition: undo_action.cpp:79
std::function< t_step_ptr(const config &)> t_factory
Definition: undo_action.hpp:57
map_location filter_loc2
undo_event(int fcn_idx, const config &args, const game_events::queued_event &ctx)
Definition: undo_action.cpp:90
static const char * get_type_impl()
virtual bool undo(int side)
Undoes this action.
utils::optional< int > lua_idx
virtual const char * get_type() const
map_location filter_loc1
virtual void write(config &cfg) const
Writes this into the provided config.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Definitions for the interface to Wesnoth Markup Language (WML).
Records information to be able to undo an action.
Definition: undo_action.hpp:80
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:91
virtual const char * get_type() const =0
virtual bool undo(int side)=0
Undoes this action.
Encapsulates the map of the game.
Definition: location.hpp:45