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