The Battle for Wesnoth  1.19.0-dev
action_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Tomasz Sniatowski <kailoran@gmail.com>
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 /**
17  * @file
18  * Base class for editor actions
19  *
20  * An action is constructed in response to a user command, then executed on
21  * the map. An undo action is returned by default via pointer, caller-owned).
22  * It is possible to call an action without creating the undo action.
23  * Actions report failure via exceptions.
24  * Code that only deals with actions polymorphically should only need to
25  * include this header file.
26  */
27 
28 #pragma once
29 
30 #include "editor/editor_common.hpp"
31 
32 namespace editor
33 {
34 /**
35  * Base class for all editor actions. An editor_action object
36  * encapsulates the action to be performed on a map, in separation
37  * from the user interface or display, and provides a way of reversing
38  * its affects by creating an "undo" action. All the actions can be
39  * processed via this base class' virtual functions.
40  */
42 {
43 public:
44  editor_action();
45  virtual ~editor_action();
46 
47  /**
48  * Action cloning
49  */
50  virtual std::unique_ptr<editor_action> clone() const = 0;
51 
52  /**
53  * Perform the action, returning an undo action that,
54  * when performed, shall reverse any effects of this
55  * action. The undo action object is owned by the
56  * caller. Default behavior is to create a whole-map
57  * undo, call the perform_without_undo function and
58  * return the undo object.
59  */
60  virtual std::unique_ptr<editor_action> perform(map_context&) const;
61 
62  /**
63  * Perform the action without creating an undo action.
64  */
65  virtual void perform_without_undo(map_context&) const = 0;
66 
67  /**
68  * @return the actual number of actions contained within
69  */
70  virtual int action_count() const;
71 
72  /**
73  * @return a short name of this action type, defaults to unknown
74  */
75  virtual const std::string& get_name() const
76  {
77  static const std::string name("unknown");
78  return name;
79  }
80 
81  /**
82  * A textual description of the action. For use
83  * e.g. in the undo menu, to have a "Undo: Fill with
84  * Grassland" item rather than just "Undo". Should be
85  * overridden by derived Actions, defaults to a debug
86  * message.
87  */
88  virtual std::string get_description() const;
89 
90  /**
91  * Debugging aid. Return an unique identifier of this Action.
92  */
93  int get_id() const
94  {
95  return id_;
96  }
97 
98  /**
99  * Debugging aid. Return number of existing instances of Actions.
100  */
101  static int get_instance_count()
102  {
103  return instance_count_;
104  }
105 
106 private:
107  static int next_id_;
108  static int instance_count_;
109 
110  const int id_;
111 };
112 
113 // TODO: add messages etc
115 {
116  editor_action_exception(const std::string& msg)
118  {
119  }
120 };
121 
122 /**
123  * Helper macro to implement common action methods.
124  */
125 #define IMPLEMENT_ACTION(id) \
126  \
127  const std::string& editor_action_##id::get_name() const \
128  { \
129  static const std::string name(#id); \
130  return name; \
131  } \
132  \
133  std::unique_ptr<editor_action> editor_action_##id::clone() const \
134  { \
135  return std::make_unique<editor_action_##id>(*this); \
136  } \
137 
138 } // end namespace editor
Base class for all editor actions.
Definition: action_base.hpp:42
static int instance_count_
virtual void perform_without_undo(map_context &) const =0
Perform the action without creating an undo action.
virtual std::unique_ptr< editor_action > clone() const =0
Action cloning.
int get_id() const
Debugging aid.
Definition: action_base.hpp:93
virtual const std::string & get_name() const
Definition: action_base.hpp:75
virtual std::unique_ptr< editor_action > perform(map_context &) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action.cpp:64
virtual ~editor_action()
Definition: action.cpp:45
virtual std::string get_description() const
A textual description of the action.
Definition: action.cpp:59
static int get_instance_count()
Debugging aid.
virtual int action_count() const
Definition: action.cpp:54
This class wraps around a map to provide a concise interface for the editor to work with.
Definition: map_context.hpp:63
Main (common) editor header.
Manage the empty-palette in the editor.
Definition: action.cpp:31
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
editor_action_exception(const std::string &msg)