The Battle for Wesnoth  1.19.25+dev
undo_action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2025
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  /**
53  * Merges consecutive undo::move_action steps that form one continuous path into a
54  * single step. See the .cpp file for why this is safe and useful.
55  */
56  void combine_moves();
57  void set_unit_id_diff(int id_diff)
58  {
59  unit_id_diff_ = id_diff;
60  }
61 
62  using t_factory = std::function<t_step_ptr(const config&)>;
63  using t_factory_map = std::map<std::string, t_factory>;
64  static t_factory_map& get_factories();
65 
66  template<typename T>
68  {
70  {
71  std::string name = T::get_type_impl();
72  get_factories()[name] = [](const config& cfg) {
73  auto res = std::make_unique<T>(cfg);
74  return res;
75  };
76  }
77  };
78  };
79 
80  /**
81  * Records information to be able to undo an action.
82  * Each type of step gets its own derived type.
83  */
84  struct undo_action
85  {
87  // Virtual destructor to support derived classes.
88  virtual ~undo_action() {}
89 
90  /**
91  * Undoes this action.
92  * @return true on success; false on an error.
93  */
94  virtual bool undo(int side) = 0;
95  /** Writes this into the provided config. */
96  virtual void write(config& cfg) const
97  {
98  cfg["type"] = this->get_type();
99  }
100  virtual const char* get_type() const = 0;
101  };
102 
103  class undo_event : public undo_action
104  {
105  public:
106 
107  undo_event(int fcn_idx, const config& args, const game_events::queued_event& ctx);
108  undo_event(const config& cmds, const game_events::queued_event& ctx);
109 
110  undo_event(const config& cfg);
111 
112  virtual bool undo(int side);
113  virtual void write(config& cfg) const;
114 
115 
116  static const char* get_type_impl() { return "event"; }
117  virtual const char* get_type() const { return get_type_impl(); }
118 
119  private:
120  undo_event(const config& first, const config& second, const config& weapons, const config& cmds);
121 
122  utils::optional<int> lua_idx;
125  std::size_t uid1, uid2;
126  std::string id1, id2;
127  };
128 }
std::vector< t_step_ptr > t_steps
Definition: undo_action.hpp:33
void set_unit_id_diff(int id_diff)
Definition: undo_action.hpp:57
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:99
std::map< std::string, t_factory > t_factory_map
Definition: undo_action.hpp:63
void combine_moves()
Merges consecutive undo::move_action steps that form one continuous path into a single step.
Definition: undo_action.cpp:78
void add(t_step_ptr &&action)
Definition: undo_action.cpp:61
static t_factory_map & get_factories()
std::function< t_step_ptr(const config &)> t_factory
Definition: undo_action.hpp:62
map_location filter_loc2
undo_event(int fcn_idx, const config &args, const game_events::queued_event &ctx)
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:157
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
Records information to be able to undo an action.
Definition: undo_action.hpp:85
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:96
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:46