OpenASIP  2.0
Reversible.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2020 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * @file Reversible.cc
27  * Implementation of Reversible class; Framework for operations that contain
28  * undo infromation and can be recursively reverted.
29  */
30 
31 #include <cassert>
32 #include "Reversible.hh"
33 
34 /** Delete the undo information. cannot revert after this */
38 }
39 
40 /** Delete children without reverting them.
41  They cannot be reverted after this */
42 void Reversible::deleteChildren(std::stack<Reversible*>& children) {
43  while (!children.empty()) {
44  Reversible* child = children.top();
45  assert(child != nullptr);
46  children.pop();
47  delete child;
48  }
49 }
50 
51 /**
52  * Undoes one stack of children.
53  */
54 void
55 Reversible::undoAndRemoveChildren(std::stack<Reversible*>& children) {
56  while (!children.empty()) {
57  Reversible* child = children.top();
58  assert(child != nullptr);
59  children.pop();
60  child->undo();
61  delete child;
62  }
63 }
64 
65 /**
66  * Undoes this and all children.
67  */
68 void
71  undoOnlyMe();
73 }
74 
75 /**
76  * Recursively calls undo for all children that were performed before this.
77  * So these are undoed after this is undoed
78 */
79 void
82 }
83 
84 /**
85  * Recursively calls undo for all children that were performed after this.
86  * So these are undoed before this is undoed
87 */
88 void
91 }
92 
93 /**
94  * Undoes the operations done by this class but not children.
95  * This method should be overloaded by most derived classes.
96  */
97 void
99 
100 /**
101  * Tries to run another Reversible and takes ownership of it.
102  *
103  * If the running succeeds, makes it a child.
104  * If the running fails, deletes it.
105  *
106  * @return true if running child succeeded, false if failed.
107  */
108 bool
109 Reversible::runChild(std::stack<Reversible*>& children, Reversible* child) {
110  if ((*child)()) {
111  children.push(child);
112  return true;
113  } else {
114  delete child;
115  return false;
116  }
117 }
118 
119 /**
120  * Tries to run another Reversible (before running this).
121  *
122  * If the running succeeds, makes it a pre-child.
123  * If the running fails, deletes it.
124  *
125  * @return true if running child succeeded, false if failed.
126  */
128  return runChild(preChildren_, preChild);
129 }
130 
131 /**
132  * Tries to run another Reversible (after running this).
133  *
134  * If the running succeeds, makes it a post-child.
135  * If the running fails, deletes it.
136  *
137  * @return true if running child succeeded, false if failed.
138  */
140  return runChild(postChildren_, postChild);
141 }
142 
143 /**
144  * Tries to run another Reversible and takes ownership of it.
145  *
146  * @param if pre is true, make it a pre-child,
147  * if pre is false, make it a post-child.
148  *
149  * @return true if running child succeeded, false if failed.
150  */
151 bool Reversible::runChild(Reversible* child, bool pre) {
152  if (pre) {
153  return runPreChild(child);
154  } else {
155  return runPostChild(child);
156  }
157 }
158 
159 int Reversible::idCounter_ = 0;
Reversible::postChildren_
std::stack< Reversible * > postChildren_
Definition: Reversible.hh:58
Reversible::idCounter_
static int idCounter_
Definition: Reversible.hh:62
Reversible::undoAndRemovePreChildren
void undoAndRemovePreChildren()
Definition: Reversible.cc:80
Reversible::undoAndRemoveChildren
void undoAndRemoveChildren(std::stack< Reversible * > &children)
Definition: Reversible.cc:55
Reversible
Definition: Reversible.hh:36
Reversible::preChildren_
std::stack< Reversible * > preChildren_
Definition: Reversible.hh:57
assert
#define assert(condition)
Definition: Application.hh:86
Reversible::undo
virtual void undo()
Definition: Reversible.cc:69
Reversible::undoOnlyMe
virtual void undoOnlyMe()
Definition: Reversible.cc:98
Reversible.hh
Reversible::deleteChildren
void deleteChildren(std::stack< Reversible * > &children)
Definition: Reversible.cc:42
Reversible::~Reversible
virtual ~Reversible()
Definition: Reversible.cc:35
Reversible::runChild
bool runChild(std::stack< Reversible * > &children, Reversible *child)
Definition: Reversible.cc:109
Reversible::runPreChild
bool runPreChild(Reversible *preChild)
Definition: Reversible.cc:127
Reversible::undoAndRemovePostChildren
void undoAndRemovePostChildren()
Definition: Reversible.cc:89
Reversible::runPostChild
bool runPostChild(Reversible *preChild)
Definition: Reversible.cc:139