undolib.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined (INCLUDED_UNDOLIB_H)
00023 #define INCLUDED_UNDOLIB_H
00024
00025 #include "iundo.h"
00026 #include "mapfile.h"
00027 #include "generic/callback.h"
00028
00029 template<typename Copyable>
00030 class BasicUndoMemento: public UndoMemento
00031 {
00032 Copyable m_data;
00033 public:
00034 BasicUndoMemento (const Copyable& data) :
00035 m_data(data)
00036 {
00037 }
00038
00039 const Copyable& get () const
00040 {
00041 return m_data;
00042 }
00043 };
00044
00045 template<typename Copyable>
00046 class ObservedUndoableObject: public Undoable
00047 {
00048 typedef Callback1<const Copyable&> ImportCallback;
00049
00050 Copyable& m_object;
00051 ImportCallback m_importCallback;
00052 UndoObserver* m_undoQueue;
00053 MapFile* m_map;
00054 public:
00055
00056 ObservedUndoableObject<Copyable> (Copyable& object, const ImportCallback& importCallback) :
00057 m_object(object), m_importCallback(importCallback), m_undoQueue(0), m_map(0)
00058 {
00059 }
00060 ~ObservedUndoableObject ()
00061 {
00062 }
00063
00064 MapFile* map ()
00065 {
00066 return m_map;
00067 }
00068
00069 void instanceAttach (MapFile* map)
00070 {
00071 m_map = map;
00072 m_undoQueue = GlobalUndoSystem().observer(this);
00073 }
00074 void instanceDetach (MapFile* map)
00075 {
00076 m_map = 0;
00077 m_undoQueue = 0;
00078 GlobalUndoSystem().release(this);
00079 }
00080
00081 void save ()
00082 {
00083 if (m_map != 0) {
00084 m_map->changed();
00085 }
00086 if (m_undoQueue != 0) {
00087 m_undoQueue->save(this);
00088 }
00089 }
00090
00091 UndoMemento* exportState () const
00092 {
00093 return new BasicUndoMemento<Copyable> (m_object);
00094 }
00095 void importState (const UndoMemento* state)
00096 {
00097 save();
00098 m_importCallback((static_cast<const BasicUndoMemento<Copyable>*> (state))->get());
00099 }
00100 };
00101
00102 template<typename Copyable>
00103 class UndoableObject: public Undoable
00104 {
00105 Copyable& m_object;
00106 UndoObserver* m_undoQueue;
00107 MapFile* m_map;
00108
00109 public:
00110 UndoableObject (Copyable& object) :
00111 m_object(object), m_undoQueue(0), m_map(0)
00112 {
00113 }
00114 ~UndoableObject ()
00115 {
00116 }
00117
00118 void instanceAttach (MapFile* map)
00119 {
00120 m_map = map;
00121 m_undoQueue = GlobalUndoSystem().observer(this);
00122 }
00123 void instanceDetach (MapFile* map)
00124 {
00125 m_map = 0;
00126 m_undoQueue = 0;
00127 GlobalUndoSystem().release(this);
00128 }
00129
00130 void save ()
00131 {
00132 if (m_map != 0) {
00133 m_map->changed();
00134 }
00135 if (m_undoQueue != 0) {
00136 m_undoQueue->save(this);
00137 }
00138 }
00139
00140 UndoMemento* exportState () const
00141 {
00142 return new BasicUndoMemento<Copyable> (m_object);
00143 }
00144 void importState (const UndoMemento* state)
00145 {
00146 save();
00147 m_object = (static_cast<const BasicUndoMemento<Copyable>*> (state))->get();
00148 }
00149 };
00150
00151 #endif