The Battle for Wesnoth  1.13.4+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 - 2016 by Tomasz Sniatowski <[email protected]>
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 #ifndef EDITOR_ACTION_BASE_HPP_INCLUDED
28 #define EDITOR_ACTION_BASE_HPP_INCLUDED
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 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 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 char* get_name() const { return "unknown"; }
76 
77  /**
78  * A textual description of the action. For use
79  * e.g. in the undo menu, to have a "Undo: Fill with
80  * Grassland" item rather than just "Undo". Should be
81  * overridden by derived Actions, defaults to a debug
82  * message.
83  */
84  virtual std::string get_description() const;
85 
86  /**
87  * Debugging aid. Return an unique identifier of this Action.
88  */
89  int get_id() const { return id_; }
90 
91  /**
92  * Debugging aid. Return number of existing instances of Actions.
93  */
94  static int get_instance_count() { return instance_count_; }
95 
96  private:
97  static int next_id_;
98  static int instance_count_;
99  const int id_;
100 };
101 
102 
103 //TODO: add messages etc
105 {
107  : editor_exception(msg)
108  {
109  }
110 };
111 
112 } //end namespace editor
113 
114 #endif
virtual std::string get_description() const
A textual description of the action.
Definition: action.cpp:55
static int get_instance_count()
Debugging aid.
Definition: action_base.hpp:94
editor_action_exception(const std::string &msg)
virtual int action_count() const
Definition: action.cpp:50
virtual const char * get_name() const
Definition: action_base.hpp:75
virtual editor_action * clone() const =0
Action cloning.
Manage the empty-palette in the editor.
Definition: action.cpp:28
virtual ~editor_action()
Definition: action.cpp:42
Main (common) editor header.
static int instance_count_
Definition: action_base.hpp:98
Base class for all editor actions.
Definition: action_base.hpp:41
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:41
int get_id() const
Debugging aid.
Definition: action_base.hpp:89
virtual void perform_without_undo(map_context &) const =0
Perform the action without creating an undo action.
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:60
GLsizei const GLcharARB ** string
Definition: glew.h:4503