Browse Source

Add a current() method to History, so that consumers don't need to worry about bounds checking.

Marcus Huderle 6 years ago
parent
commit
dfc990f0da
2 changed files with 14 additions and 7 deletions
  1. 2
    2
      map.cpp
  2. 12
    5
      map.h

+ 2
- 2
map.cpp View File

618
 
618
 
619
 void Map::undo() {
619
 void Map::undo() {
620
     if (blockdata) {
620
     if (blockdata) {
621
-        Blockdata *commit = history.pop();
621
+        Blockdata *commit = history.back();
622
         if (commit != NULL) {
622
         if (commit != NULL) {
623
             blockdata->copyFrom(commit);
623
             blockdata->copyFrom(commit);
624
             emit mapChanged(this);
624
             emit mapChanged(this);
638
 
638
 
639
 void Map::commit() {
639
 void Map::commit() {
640
     if (blockdata) {
640
     if (blockdata) {
641
-        if (!blockdata->equals(history.history.at(history.head))) {
641
+        if (!blockdata->equals(history.current())) {
642
             Blockdata* commit = blockdata->copy();
642
             Blockdata* commit = blockdata->copy();
643
             history.push(commit);
643
             history.push(commit);
644
             emit mapChanged(this);
644
             emit mapChanged(this);

+ 12
- 5
map.h View File

13
 template <typename T>
13
 template <typename T>
14
 class History {
14
 class History {
15
 public:
15
 public:
16
-    QList<T> history;
17
-    int head = -1;
18
-    int saved = -1;
19
-
20
     History() {
16
     History() {
21
 
17
 
22
     }
18
     }
23
-    T pop() {
19
+    T back() {
24
         if (head > 0) {
20
         if (head > 0) {
25
             return history.at(--head);
21
             return history.at(--head);
26
         }
22
         }
42
         history.append(commit);
38
         history.append(commit);
43
         head++;
39
         head++;
44
     }
40
     }
41
+    T current() {
42
+        if (head < 0 || history.length() == 0) {
43
+            return NULL;
44
+        }
45
+        return history.at(head);
46
+    }
45
     void save() {
47
     void save() {
46
         saved = head;
48
         saved = head;
47
     }
49
     }
48
     bool isSaved() {
50
     bool isSaved() {
49
         return saved == head;
51
         return saved == head;
50
     }
52
     }
53
+
54
+private:
55
+    QList<T> history;
56
+    int head = -1;
57
+    int saved = -1;
51
 };
58
 };
52
 
59
 
53
 class Connection {
60
 class Connection {