]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/iundo.h
[q3map2] Unwind script stack in case of script loading error.
[xonotic/netradiant.git] / include / iundo.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_IUNDO_H )
23 #define INCLUDED_IUNDO_H
24
25 /// \file
26 /// \brief The undo-system interface. Uses the 'memento' pattern.
27
28 #include <cstddef>
29 #include "generic/constant.h"
30 #include "generic/callback.h"
31
32 class UndoMemento
33 {
34 public:
35 virtual void release() = 0;
36 virtual ~UndoMemento() {
37 }
38 };
39
40 class Undoable
41 {
42 public:
43 virtual UndoMemento* exportState() const = 0;
44 virtual void importState( const UndoMemento* state ) = 0;
45 virtual ~Undoable() {
46 }
47 };
48
49 class UndoObserver
50 {
51 public:
52 virtual void save( Undoable* undoable ) = 0;
53 virtual ~UndoObserver() {
54 }
55 };
56
57 class UndoTracker
58 {
59 public:
60 virtual void clear() = 0;
61 virtual void begin() = 0;
62 virtual void undo() = 0;
63 virtual void redo() = 0;
64 virtual ~UndoTracker() {
65 }
66 };
67
68 class UndoSystem
69 {
70 public:
71 INTEGER_CONSTANT( Version, 1 );
72 STRING_CONSTANT( Name, "undo" );
73
74 virtual UndoObserver* observer( Undoable* undoable ) = 0;
75 virtual void release( Undoable* undoable ) = 0;
76
77 virtual std::size_t size() const = 0;
78 virtual void start() = 0;
79 virtual void finish( const char* command ) = 0;
80 virtual void undo() = 0;
81 virtual void redo() = 0;
82 virtual void clear() = 0;
83
84 virtual void trackerAttach( UndoTracker& tracker ) = 0;
85 virtual void trackerDetach( UndoTracker& tracker ) = 0;
86
87 virtual ~UndoSystem() {
88 }
89 };
90
91 #include "modulesystem.h"
92
93 template<typename Type>
94 class GlobalModule;
95 typedef GlobalModule<UndoSystem> GlobalUndoModule;
96
97 template<typename Type>
98 class GlobalModuleRef;
99 typedef GlobalModuleRef<UndoSystem> GlobalUndoModuleRef;
100
101 inline UndoSystem& GlobalUndoSystem(){
102         return GlobalUndoModule::getTable();
103 }
104
105 class UndoableCommand
106 {
107 const char* m_command;
108 public:
109 UndoableCommand( const char* command ) : m_command( command ){
110         GlobalUndoSystem().start();
111 }
112 ~UndoableCommand(){
113         GlobalUndoSystem().finish( m_command );
114 }
115 };
116
117
118 #endif