Scid  4.6.5
undoredo.h
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2014 Fulvio Benini
3 
4 * This file is part of Scid (Shane's Chess Information Database).
5 *
6 * Scid is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 *
10 * Scid is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Scid. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef UNDOREDO_H
20 #define UNDOREDO_H
21 
22 #include <vector>
23 
24 template <class T, unsigned int UNDOMAX>
25 class UndoRedo {
26  typedef std::vector<T*> contT;
27  contT undo_, redo_;
28 
29  void doUndoRedo(contT& cont1, contT& cont2, T*& current) {
30  if (! cont1.empty()) {
31  if (cont2.empty() || cont2.back() != current) {
32  cont2.push_back(current);
33  }
34  current = cont1.back();
35  cont1.pop_back();
36  }
37  }
38  void clear(contT& cont) {
39  typename contT::iterator it = cont.begin();
40  for (; it != cont.end(); it++) delete *it;
41  cont.clear();
42  }
43 
44 public:
45  ~UndoRedo() { clear(); }
46 
47  void clear() { clear(undo_); clear(redo_); }
48  bool undoAll(T*& current) {
49  if (undo_.size() >= UNDOMAX) return false;
50  while (! undo_.empty()) undo(current);
51  return true;
52  }
53  void undo(T*& current) { doUndoRedo(undo_, redo_, current); }
54  void redo(T*& current) { doUndoRedo(redo_, undo_, current); }
55  size_t undoSize() { return undo_.size(); }
56  size_t redoSize() { return redo_.size(); }
57  void store (T* current) {
58  undo_.push_back(current->clone());
59  clear(redo_);
60  if (undo_.size() > UNDOMAX) {
61  delete undo_.front();
62  undo_.erase(undo_.begin());
63  }
64  }
65 };
66 
67 #endif
void undo(T *&current)
Definition: undoredo.h:53
~UndoRedo()
Definition: undoredo.h:45
size_t undoSize()
Definition: undoredo.h:55
void redo(T *&current)
Definition: undoredo.h:54
bool undoAll(T *&current)
Definition: undoredo.h:48
void store(T *current)
Definition: undoredo.h:57
size_t redoSize()
Definition: undoredo.h:56
void clear()
Definition: undoredo.h:47