]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/undolib.h
portability fixes
[xonotic/netradiant.git] / libs / undolib.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_UNDOLIB_H)
23 #define INCLUDED_UNDOLIB_H
24
25 #include "iundo.h"
26 #include "mapfile.h"
27 #include "warnings.h"
28 #include "generic/callback.h"
29
30 template<typename Copyable>
31 class BasicUndoMemento : public UndoMemento
32 {
33   Copyable m_data;
34 public:
35   BasicUndoMemento(const Copyable& data)
36     : m_data(data)
37   {
38   }
39
40   void release()
41   {
42     delete this;
43   }
44
45   const Copyable& get() const
46   {
47     return m_data;
48   }
49 };
50
51
52 template<typename Copyable>
53 class ObservedUndoableObject : public Undoable
54 {
55   typedef Callback1<const Copyable&> ImportCallback;
56
57   Copyable& m_object;
58   ImportCallback m_importCallback;
59   UndoObserver* m_undoQueue;
60   MapFile* m_map;
61 public:
62
63   ObservedUndoableObject<Copyable>(Copyable& object, const ImportCallback& importCallback)
64     : m_object(object), m_importCallback(importCallback), m_undoQueue(0), m_map(0)
65   {
66   }
67   ~ObservedUndoableObject()
68   {
69   }
70
71   MapFile* map()
72   {
73     return m_map;
74   }
75
76   void instanceAttach(MapFile* map)
77   {
78     m_map = map;
79     m_undoQueue = GlobalUndoSystem().observer(this);
80   }
81   void instanceDetach(MapFile* map)
82   {
83     m_map = 0;
84     m_undoQueue = 0;
85     GlobalUndoSystem().release(this);
86   }
87
88   void save()
89   {
90     if(m_map != 0)
91     {
92       m_map->changed();
93     }
94     if(m_undoQueue != 0)
95     {
96       m_undoQueue->save(this);
97     }
98   }
99
100   UndoMemento* exportState() const
101   {
102     return new BasicUndoMemento<Copyable>(m_object);
103   }
104   void importState(const UndoMemento* state)
105   {
106     save();
107     m_importCallback((static_cast<const BasicUndoMemento<Copyable>*>(state))->get());
108   }
109 };
110
111 template<typename Copyable>
112 class UndoableObject : public Undoable
113 {
114   Copyable& m_object;
115   UndoObserver* m_undoQueue;
116   MapFile* m_map;
117
118 public:
119   UndoableObject(Copyable& object)
120     : m_object(object), m_undoQueue(0), m_map(0)
121   {}
122   ~UndoableObject()
123   {
124   }
125
126   void instanceAttach(MapFile* map)
127   {
128     m_map = map;
129     m_undoQueue = GlobalUndoSystem().observer(this);
130   }
131   void instanceDetach(MapFile* map)
132   {
133     m_map = 0;
134     m_undoQueue = 0;
135     GlobalUndoSystem().release(this);
136   }
137
138   void save()
139   {
140     if(m_map != 0)
141     {
142       m_map->changed();
143     }
144     if(m_undoQueue != 0)
145     {
146       m_undoQueue->save(this);
147     }
148   }
149
150   UndoMemento* exportState() const
151   {
152     return new BasicUndoMemento<Copyable>(m_object);
153   }
154   void importState(const UndoMemento* state)
155   {
156     save();
157     m_object = (static_cast<const BasicUndoMemento<Copyable>*>(state))->get();
158   }
159 };
160
161 #endif