Cafu Engine
CommandPattern.hpp
Go to the documentation of this file.
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_COMMAND_PATTERN_HPP_INCLUDED
8 #define CAFU_COMMAND_PATTERN_HPP_INCLUDED
9 
10 /// \file
11 /// This file provides the classes for the Command pattern as described in the book by the GoF.
12 
13 #include "wx/wx.h"
14 #include "Templates/Array.hpp"
15 
16 
17 /// This class represents a general command for implementing modifications to the applications document.
18 /// Commands are usually kept in an undo/redo history in order to provide the user with undo and redo.
19 ///
20 /// The use of commands - especially with an undo/redo history - implies several inherent "rules and considerations"
21 /// that must be followed and taken into account for a successful implementation.
22 /// - It is obvious that when undo/redo is desired, documents can ONLY be modified by a closed series of commands
23 /// that can be played forth and back in its respective, proper order only.
24 /// - VERY IMPORTANT: The time between the constructor and calling Do() is problematic, and should be null:
25 /// there should be NO code between the construction of a command and the related call to Do().
26 /// This is especially problematic when commands are (improperly) queued, e.g. while macro commands are being constructed.
27 /// As keeping the ctor empty is usually quasi impossible (we normally want to use it to "configure" the command), this
28 /// implies that we must make sure that the command is run and submitted to the undo/redo history IMMEDIATELY after construction.
29 /// As a help, both macro commands and the undo/redo history are able to accept commands whose Do() has already been run by the caller.
30 class CommandT
31 {
32  public:
33 
34  /// The constructor.
35  /// @param ShowInHistory If the command should be visible in the history.
36  /// @param SuggestsSave If the command should suggest that the document has to be saved
37  /// if it is closed and the command has been executed without a
38  /// following save.
39  CommandT(bool ShowInHistory=true, bool SuggestsSave=true);
40 
41  /// The virtual destructor.
42  virtual ~CommandT() {}
43 
44  /// This method executes the command.
45  /// @returns true if the command succeeded, false if it failed.
46  virtual bool Do()=0;
47 
48  /// This method un-does the command.
49  virtual void Undo()=0;
50 
51  /// Returns the name (a description) of the command.
52  virtual wxString GetName() const=0;
53 
54  /// Whether the command should be shown in the undo/redo history.
55  /// Commands not shown are still part of the history but are not undoable/redoable
56  /// by the user. Instead they are silently undone/redone when the preceding command
57  /// is undone/redone.
58  bool ShowInHistory() const { return m_ShowInHistory; }
59 
60  /// Whether the command suggests to save the document when its closed and hasn't
61  /// been saved between the command execution and the closing (selection changes
62  /// for example don't suggest to save the document since no important changes
63  /// have been made).
64  bool SuggestsSave() const { return m_SuggestsSave; }
65 
66  /// Whether the command has been executed.
67  bool IsDone() const { return m_Done; }
68 
69  /// Returns the commands unique ID.
70  unsigned long GetID() const { return m_ID; }
71 
72 
73  protected:
74 
75  bool m_Done; ///< Whether the command has been executed.
76  const bool m_ShowInHistory; ///< Whether the command should have an entry in the undo/redo history.
77  const bool m_SuggestsSave; ///< Whether the command suggests saving of the document on close.
78  const unsigned long m_ID; ///< The commands unique ID.
79 
80 
81  private:
82 
83  /// Use of the Copy Constructor is not allowed. See "svn log -r 174" and the top of "svn cat -r 174 DocumentCommands.hpp" for a discussion.
84  CommandT(const CommandT&);
85 
86  /// Use of the Assignment Operator is not allowed. See "svn log -r 174" and the top of "svn cat -r 174 DocumentCommands.hpp" for a discussion.
87  void operator = (const CommandT&);
88 };
89 
90 
91 class CommandMacroT : public CommandT
92 {
93  public:
94 
95  CommandMacroT(const ArrayT<CommandT*>& SubCommands, const wxString& Name="Macro Command", bool ShowInHistory=true, bool SuggestsSave=true);
96  ~CommandMacroT();
97 
98  // Implement the CommandT interface.
99  virtual bool Do();
100  virtual void Undo();
101  virtual wxString GetName() const;
102 
103 
104  private:
105 
106  const ArrayT<CommandT*> m_SubCommands; ///< The sub-commands that this macro is composed of.
107  const wxString m_Name;
108 };
109 
110 #endif
virtual wxString GetName() const =0
Returns the name (a description) of the command.
virtual ~CommandT()
The virtual destructor.
Definition: CommandPattern.hpp:42
const unsigned long m_ID
The commands unique ID.
Definition: CommandPattern.hpp:78
CommandT(bool ShowInHistory=true, bool SuggestsSave=true)
The constructor.
Definition: CommandPattern.cpp:18
bool m_Done
Whether the command has been executed.
Definition: CommandPattern.hpp:75
Definition: CommandPattern.hpp:91
const bool m_SuggestsSave
Whether the command suggests saving of the document on close.
Definition: CommandPattern.hpp:77
bool IsDone() const
Whether the command has been executed.
Definition: CommandPattern.hpp:67
virtual bool Do()
This method executes the command.
Definition: CommandPattern.cpp:49
virtual void Undo()
This method un-does the command.
Definition: CommandPattern.cpp:72
virtual void Undo()=0
This method un-does the command.
virtual bool Do()=0
This method executes the command.
unsigned long GetID() const
Returns the commands unique ID.
Definition: CommandPattern.hpp:70
bool SuggestsSave() const
Whether the command suggests to save the document when its closed and hasn't been saved between the c...
Definition: CommandPattern.hpp:64
virtual wxString GetName() const
Returns the name (a description) of the command.
Definition: CommandPattern.cpp:85
const bool m_ShowInHistory
Whether the command should have an entry in the undo/redo history.
Definition: CommandPattern.hpp:76
This class represents a general command for implementing modifications to the applications document...
Definition: CommandPattern.hpp:30
bool ShowInHistory() const
Whether the command should be shown in the undo/redo history.
Definition: CommandPattern.hpp:58