LCOV - code coverage report
Current view: top level - src - undoredo.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 8 8 100.0 %
Date: 2017-06-21 14:32:49 Functions: 2 2 100.0 %

          Line data    Source code
       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          24 : 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          16 :         void clear(contT& cont) {
      39          16 :                 typename contT::iterator it = cont.begin();
      40          32 :                 for (; it != cont.end(); it++) delete *it;
      41          16 :                 cont.clear();
      42          16 :         }
      43             : 
      44             : public:
      45          32 :         ~UndoRedo() { clear(); }
      46             : 
      47           8 :         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

Generated by: LCOV version 1.12