SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProvenanceManager.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9  License for the specific language governing rights and limitations under
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 /// @todo Documentation Dataflow/Engine/Controller/ProvenanceManager.h
29 
30 #ifndef ENGINE_NETWORK_PROVENANCEMANAGER_H
31 #define ENGINE_NETWORK_PROVENANCEMANAGER_H
32 
33 #include <stack>
34 #include <boost/noncopyable.hpp>
38 
39 namespace SCIRun {
40 namespace Dataflow {
41 namespace Engine {
42 
43  template <class Memento>
44  class ProvenanceManager : boost::noncopyable
45  {
46  public:
48  typedef typename Item::Handle ItemHandle;
49  typedef std::stack<ItemHandle> Stack;
50  typedef typename Stack::container_type List;
52 
54  void setInitialState(const Memento& initialState);
55  void addItem(ItemHandle item);
56  ItemHandle undo();
57  ItemHandle redo();
58 
59  List undoAll();
60  List redoAll();
61 
62  void clearAll();
63 
64  size_t undoSize() const;
65  size_t redoSize() const;
66 
67  const IOType* networkIO() const;
68 
69  private:
70  ItemHandle undo(bool restore);
71  ItemHandle redo(bool restore);
72  IOType* networkIO_;
73  Stack undo_, redo_;
74  boost::optional<Memento> initialState_;
75  };
76 
77 
78 
79  template <class Memento>
80  ProvenanceManager<Memento>::ProvenanceManager(IOType* networkIO) : networkIO_(networkIO) {}
81 
82  template <class Memento>
83  void ProvenanceManager<Memento>::setInitialState(const Memento& initialState)
84  {
85  initialState_ = initialState;
86  }
87 
88  template <class Memento>
90  {
91  return undo_.size();
92  }
93 
94  template <class Memento>
96  {
97  return redo_.size();
98  }
99 
100  template <class Memento>
102  {
103  undo_.push(item);
104  Stack().swap(redo_);
105  }
106 
107  template <class Memento>
109  {
110  Stack().swap(undo_);
111  Stack().swap(redo_);
112  }
113 
114  template <class Memento>
116  {
117  return undo(true);
118  }
119 
120  template <class Memento>
122  {
123  if (!undo_.empty())
124  {
125  auto undone = undo_.top();
126  undo_.pop();
127  redo_.push(undone);
128 
129  //clear and load previous memento
130  if (restore)
131  {
132  networkIO_->clear();
133  if (!undo_.empty())
134  networkIO_->loadNetwork(undo_.top()->memento());
135  else if (initialState_)
136  networkIO_->loadNetwork(initialState_.get());
137  }
138 
139  return undone;
140  }
141  return ItemHandle();
142  }
143 
144  template <class Memento>
146  {
147  return redo(true);
148  }
149 
150  template <class Memento>
152  {
153  if (!redo_.empty())
154  {
155  auto redone = redo_.top();
156  redo_.pop();
157  undo_.push(redone);
158 
159  //clear and load redone memento
160  if (restore)
161  {
162  networkIO_->clear();
163  networkIO_->loadNetwork(redone->memento());
164  }
165 
166  return redone;
167  }
168  return ItemHandle();
169  }
170 
171  template <class Memento>
173  {
174  List undone;
175  while (0 != undoSize())
176  undone.push_back(undo(false));
177  networkIO_->clear();
178  if (initialState_)
179  networkIO_->loadNetwork(initialState_.get());
180  return undone;
181  }
182 
183  template <class Memento>
185  {
186  List redone;
187  while (0 != redoSize())
188  redone.push_back(redo(false));
189  networkIO_->clear();
190  networkIO_->loadNetwork(undo_.top()->memento());
191  return redone;
192  }
193 
194  template <class Memento>
196  {
197  return networkIO_;
198  }
199 
200 
201 }
202 }
203 }
204 
205 #endif
void clearAll()
Definition: ProvenanceManager.h:108
ProvenanceManager(IOType *networkIO)
Definition: ProvenanceManager.h:80
Item::Handle ItemHandle
Definition: ProvenanceManager.h:48
Engine::NetworkIOInterface< Memento > IOType
Definition: ProvenanceManager.h:51
size_t undoSize() const
Definition: ProvenanceManager.h:89
Stack::container_type List
Definition: ProvenanceManager.h:50
List undoAll()
Definition: ProvenanceManager.h:172
void addItem(ItemHandle item)
Definition: ProvenanceManager.h:101
Definition: ControllerInterfaces.h:54
std::stack< ItemHandle > Stack
Definition: ProvenanceManager.h:49
const IOType * networkIO() const
Definition: ProvenanceManager.h:195
void setInitialState(const Memento &initialState)
Definition: ProvenanceManager.h:83
List redoAll()
Definition: ProvenanceManager.h:184
ProvenanceItem< Memento > Item
Definition: ProvenanceManager.h:47
ItemHandle undo()
Definition: ProvenanceManager.h:115
size_t redoSize() const
Definition: ProvenanceManager.h:95
ItemHandle redo()
Definition: ProvenanceManager.h:145
Definition: ControllerInterfaces.h:42
boost::shared_ptr< ProvenanceItem< Memento > > Handle
Definition: ProvenanceItem.h:45