OpenASIP  2.0
Model.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 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  * @file Model.cc
26  *
27  * Definition of Model class.
28  *
29  * @author Tommi Rantanen 2003 (tommi.rantanen-no.spam-tut.fi)
30  */
31 
32 #include "Application.hh"
33 #include "Model.hh"
34 #include "ModelConstants.hh"
35 #include "Machine.hh"
36 #include "ADFSerializer.hh"
37 #include "ModelObserver.hh"
38 #include "ImmediateUnit.hh"
39 #include "Socket.hh"
40 #include "Bus.hh"
41 #include "FUPort.hh"
42 #include "Port.hh"
43 #include "Segment.hh"
44 #include "Guard.hh"
45 #include "ProDe.hh"
46 #include "ProDeOptions.hh"
47 
48 using namespace TTAMachine;
49 
50 /**
51  * The Constructor.
52  */
53 Model::Model() : machine_(new Machine()), undone_(NULL), modified_(false) {
54 }
55 
56 
57 /**
58  * The Destructor.
59  */
61  // delete machine object states in the undo stack
62  while (undoStack_.size() != 0) {
63  Machine* machine = undoStack_.front();
64  undoStack_.pop_front();
65  delete machine;
66  }
67 }
68 
69 
70 /**
71  * The Constructor.
72  *
73  * Creates model from a specified file.
74  *
75  * @param fileName The file name of the machine description to open.
76  */
77 Model::Model(const std::string& fileName) : undone_(NULL), modified_(false) {
78  // read Machine from fileName and set it to machine_
79  ADFSerializer reader;
80  reader.setSourceFile(fileName);
81  machine_ = reader.readMachine();
82 }
83 
84 /**
85  * Returns the Machine.
86  */
87 Machine*
89  return machine_;
90 }
91 
92 
93 /**
94  * Restores Machine to the state before the last modification.
95  */
96 void
98 
99  // delete redo cache
100  if (undone_ != NULL) {
101  delete undone_;
102  undone_ = NULL;
103  }
104 
105  // add machine to the redo cache before undo
106  undone_ = new Machine(*machine_);
107 
108  // pop last machine from the undo stack and set is as the machine
109  // and set machine as modified.
110  popFromStack(true);
111 }
112 
113 
114 /**
115  * Redoes last undoed modification.
116  */
117 void
119 
120  assert (undone_ != NULL);
121 
122  Machine* undone = undone_;
123  undone_ = NULL;
124 
125  // push machine to the undo stack before redoing
126  pushToStack();
127 
128  // replace machine from the redo cache
129  if (machine_ != NULL) {
130  delete machine_;
131  }
132 
133  machine_ = undone;
134  notifyObservers();
135 }
136 
137 /**
138  * Adds an observer for this Model.
139  *
140  * @param observer The observer to add.
141  */
142 void
144  observers_.push_back(observer);
145 }
146 
147 
148 /**
149  * Notifies all observers of this Model to update themselves.
150  */
151 void
152 Model::notifyObservers(bool modified) {
153  if (modified) {
154  modified_ = true;
155  }
156  for (ObserverTable::iterator i = observers_.begin();
157  i != observers_.end(); i++) {
158  (*i)->update();
159  }
160 }
161 
162 
163 /**
164  * Pushes the current machine to the undo stack.
165  */
166 void
168 
169  // Redo is not available after modifications, clear redo cache
170  if (undone_ != NULL) {
171  delete undone_;
172  undone_ = NULL;
173  }
174 
175  // if undo stack size is at maximum, delete oldest machine from the stack
176  while (undoStack_.size() > 0 &&
177  undoStack_.size() >=
178  unsigned(wxGetApp().options()->undoStackSize())) {
179 
180  Machine* last = undoStack_.back();
181  undoStack_.pop_back();
182  delete last;
183  }
184 
185  // push copy of the current machine to the undo stack
186  undoStack_.push_front(new Machine(*machine_));
187 }
188 
189 
190 /**
191  * Replaces the current machine with the one that is in the top of the
192  * undo stack.
193  */
194 void
195 Model::popFromStack(bool modified) {
196 
197  if (machine_ != NULL) {
198  delete machine_;
199  }
200 
201  machine_ = undoStack_.front();
202  undoStack_.pop_front();
203  notifyObservers(modified);
204 }
205 
206 
207 /**
208  * Returns true if the undo stack is not empty.
209  *
210  * @return True, if the undo stack is not empty.
211  */
212 bool
214  if (undoStack_.size() > 0) {
215  return true;
216  }
217  return false;
218 }
219 
220 
221 
222 /**
223  * Returns true if the last undone command can be redone.
224  *
225  * @return True, if the last undone command can be redone.
226  */
227 bool
229  if (undone_ != NULL) {
230  return true;
231  }
232  return false;
233 }
Model::undo
void undo()
Definition: Model.cc:97
ProDe.hh
Model::undone_
TTAMachine::Machine * undone_
Undone modification cache for the redo funciton.
Definition: Model.hh:92
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
Model::canUndo
bool canUndo()
Definition: Model.cc:213
ImmediateUnit.hh
Socket.hh
Model::observers_
ObserverTable observers_
Model observers.
Definition: Model.hh:79
Model::pushToStack
void pushToStack()
Definition: Model.cc:167
Model::Model
Model()
Definition: Model.cc:53
Model::notifyObservers
void notifyObservers(bool modified=true)
Definition: Model.cc:152
assert
#define assert(condition)
Definition: Application.hh:86
Port.hh
Segment.hh
Model::~Model
~Model()
Definition: Model.cc:60
Model::addObserver
void addObserver(ModelObserver *observer)
Definition: Model.cc:143
Model::modified_
bool modified_
Definition: Model.hh:94
ADFSerializer
Definition: ADFSerializer.hh:49
Application.hh
Model.hh
Model::canRedo
bool canRedo()
Definition: Model.cc:228
Guard.hh
ModelConstants.hh
Machine.hh
Bus.hh
Model::popFromStack
void popFromStack(bool modified=false)
Definition: Model.cc:195
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
Model::machine_
TTAMachine::Machine * machine_
Machine assigned for this Model. Singleton within a Model.
Definition: Model.hh:73
Model::undoStack_
UndoStack undoStack_
Definition: Model.hh:90
ProDeOptions.hh
ADFSerializer.hh
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
FUPort.hh
ModelObserver.hh
ModelObserver
Definition: ModelObserver.hh:38
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
TTAMachine
Definition: Assembler.hh:48
Model::redo
void redo()
Definition: Model.cc:118
Model::getMachine
TTAMachine::Machine * getMachine()
Definition: Model.cc:88
TTAMachine::Machine
Definition: Machine.hh:73