undolib.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant 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. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined (INCLUDED_UNDOLIB_H)
  18. #define INCLUDED_UNDOLIB_H
  19. #include "iundo.h"
  20. #include "mapfile.h"
  21. #include "warnings.h"
  22. #include "generic/callback.h"
  23. template<typename Copyable>
  24. class BasicUndoMemento : public UndoMemento
  25. {
  26. Copyable m_data;
  27. public:
  28. BasicUndoMemento(const Copyable& data)
  29. : m_data(data)
  30. {
  31. }
  32. void release()
  33. {
  34. delete this;
  35. }
  36. const Copyable& get() const
  37. {
  38. return m_data;
  39. }
  40. };
  41. template<typename Copyable>
  42. class ObservedUndoableObject : public Undoable
  43. {
  44. typedef Callback1<const Copyable&> ImportCallback;
  45. Copyable& m_object;
  46. ImportCallback m_importCallback;
  47. UndoObserver* m_undoQueue;
  48. MapFile* m_map;
  49. public:
  50. ObservedUndoableObject<Copyable>(Copyable& object, const ImportCallback& importCallback)
  51. : m_object(object), m_importCallback(importCallback), m_undoQueue(0), m_map(0)
  52. {
  53. }
  54. ~ObservedUndoableObject()
  55. {
  56. }
  57. MapFile* map()
  58. {
  59. return m_map;
  60. }
  61. void instanceAttach(MapFile* map)
  62. {
  63. m_map = map;
  64. m_undoQueue = GlobalUndoSystem().observer(this);
  65. }
  66. void instanceDetach(MapFile* map)
  67. {
  68. m_map = 0;
  69. m_undoQueue = 0;
  70. GlobalUndoSystem().release(this);
  71. }
  72. void save()
  73. {
  74. if(m_map != 0)
  75. {
  76. m_map->changed();
  77. }
  78. if(m_undoQueue != 0)
  79. {
  80. m_undoQueue->save(this);
  81. }
  82. }
  83. UndoMemento* exportState() const
  84. {
  85. return new BasicUndoMemento<Copyable>(m_object);
  86. }
  87. void importState(const UndoMemento* state)
  88. {
  89. save();
  90. m_importCallback((static_cast<const BasicUndoMemento<Copyable>*>(state))->get());
  91. }
  92. };
  93. template<typename Copyable>
  94. class UndoableObject : public Undoable
  95. {
  96. Copyable& m_object;
  97. UndoObserver* m_undoQueue;
  98. MapFile* m_map;
  99. public:
  100. UndoableObject(Copyable& object)
  101. : m_object(object), m_undoQueue(0), m_map(0)
  102. {}
  103. ~UndoableObject()
  104. {
  105. }
  106. void instanceAttach(MapFile* map)
  107. {
  108. m_map = map;
  109. m_undoQueue = GlobalUndoSystem().observer(this);
  110. }
  111. void instanceDetach(MapFile* map)
  112. {
  113. m_map = 0;
  114. m_undoQueue = 0;
  115. GlobalUndoSystem().release(this);
  116. }
  117. void save()
  118. {
  119. if(m_map != 0)
  120. {
  121. m_map->changed();
  122. }
  123. if(m_undoQueue != 0)
  124. {
  125. m_undoQueue->save(this);
  126. }
  127. }
  128. UndoMemento* exportState() const
  129. {
  130. return new BasicUndoMemento<Copyable>(m_object);
  131. }
  132. void importState(const UndoMemento* state)
  133. {
  134. save();
  135. m_object = (static_cast<const BasicUndoMemento<Copyable>*>(state))->get();
  136. }
  137. };
  138. #endif