OpenASIP  2.0
PasteComponentCmd.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2010 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 PasteComponentCmd.cc
26  *
27  * Implementation of PasteComponentCmd class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2004 (vjaaskel-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2010 (pjaaskel-no.spam-cs.tut.fi)
31  */
32 
33 #include <wx/docview.h>
34 #include <boost/format.hpp>
35 
36 #include "Application.hh"
37 #include "PasteComponentCmd.hh"
38 #include "MDFView.hh"
39 #include "MDFDocument.hh"
40 #include "ProDeConstants.hh"
41 #include "ProDeClipboard.hh"
42 #include "Machine.hh"
43 #include "Conversion.hh"
44 #include "WxConversion.hh"
45 #include "ProDeTextGenerator.hh"
46 #include "InformationDialog.hh"
47 #include "ObjectState.hh"
48 
49 #include "Socket.hh"
50 #include "FunctionUnit.hh"
51 #include "Bus.hh"
52 #include "RegisterFile.hh"
53 #include "ImmediateUnit.hh"
54 #include "ControlUnit.hh"
55 #include "Guard.hh"
56 
57 using std::string;
58 using boost::format;
59 using namespace TTAMachine;
60 
61 /**
62  * The Constructor.
63  */
65  EditorCommand(ProDeConstants::CMD_NAME_PASTE) {
66 }
67 
68 
69 
70 /**
71  * Executes the command.
72  *
73  * Copies descriptor of a component from the clipboard and adds
74  * corresponding component to the machine
75  *
76  * @return True, if the command was succesfully executed, false otherwise.
77  */
78 bool
80 
82  ObjectState* contents = clipboard->copyContents();
83 
84  MDFDocument* document =
85  dynamic_cast<MDFDocument*>(view()->GetDocument());
86  assert(document != NULL);
87  Machine* machine = document->getModel()->getMachine();
88  assert(machine != NULL);
89 
90  document->getModel()->pushToStack();
91 
92  if (contents->name() == Socket::OSNAME_SOCKET) {
93  // socket
95  Socket* socket = new Socket(contents);
96  paste(*machine, socket, navigator);
97  contents->setAttribute(Socket::OSKEY_NAME, socket->name());
98  // Socket can be pasted only if the target machine has busses with
99  // the same names as the busses where the copied socket was connected
100  // to.
101  try {
102  socket->loadState(contents);
103  } catch (ObjectStateLoadingException& e) {
104  wxString message =
105  _T("The socket cannot be pasted to this machine.\n\n");
106  message.Append(WxConversion::toWxString(e.errorMessage()));
107  InformationDialog dialog(parentWindow(), message);
108  dialog.ShowModal();
109  delete socket;
110  return false;
111  }
112 
113  } else if (contents->name() == FunctionUnit::OSNAME_FU) {
114  // function unit
117  TTAMachine::FunctionUnit* copiedFU = new FunctionUnit(contents);
118  std::string fuName = copiedFU->name();
119  paste(*machine, copiedFU, navigator);
120 
121  // cannot copy address space from one machien to another.
122  if (clipboard->sourceMachine() == machine) {
123  TTAMachine::FunctionUnit& original =
124  *navigator.item(fuName);
125  // need to do this after paste() as the FU must be registered to
126  // the machine before the address space can be set
127  if (original.hasAddressSpace()) {
128  copiedFU->setAddressSpace(original.addressSpace());
129  }
130  }
131  } else if (contents->name() == ImmediateUnit::OSNAME_IMMEDIATE_UNIT) {
132  // immediate unit
135  paste(*machine, new ImmediateUnit(contents), navigator);
136 
137  } else if (contents->name() == RegisterFile::OSNAME_REGISTER_FILE) {
138  // register file
141  paste(*machine, new RegisterFile(contents), navigator);
142 
143  } else if (contents->name() == Bus::OSNAME_BUS) {
144  // register file
145  Machine::BusNavigator navigator =
147  TTAMachine::Bus* copiedBus = new Bus(contents);
148 
149  // cannot copy guards from one machine into another
150  // plus additional check if oroginal bus has been deleted
151  if (clipboard->sourceMachine() == machine &&
152  navigator.hasItem(copiedBus->name())) {
153 
154  TTAMachine::Bus& original =
155  *navigator.item(copiedBus->name());
156 
157  // register the bus to the machine, possibly rename it to
158  // avoid name clashes
159  paste(*machine, copiedBus, navigator);
160 
161  // also copy the guards from the original Bus
162  assert(copiedBus->guardCount() == 0);
163  for (int i = 0; i < original.guardCount(); ++i) {
164  original.guard(i)->copyTo(*copiedBus);
165  }
166  } else {
167  // register the bus as above
168  paste(*machine, copiedBus, navigator);
169  assert(copiedBus->guardCount() == 0);
170  }
171  } else if (contents->name() == ControlUnit::OSNAME_CONTROL_UNIT) {
172  // control unit
173  if (machine->controlUnit() != NULL) {
174  // Target machine already contained a control unit.
175  // Display error message and pop the machine from undo stack.
177  format fmt =
179  string title = fmt.str();
180  wxString message = WxConversion::toWxString(title);
181  InformationDialog info(parentWindow(), message);
182  info.ShowModal();
183  document->getModel()->popFromStack();
184  delete contents;
185  return false;
186  } else {
187  // Paste control unit.
188  ControlUnit* gcu = new ControlUnit(contents);
189  gcu->setMachine(*machine);
190  }
191  } else {
192  // Unknown component type.
193  assert(false);
194  }
195 
196  delete contents;
197 
198  document->getModel()->notifyObservers();
199 
200  return false;
201 }
202 
203 
204 /**
205  * Returns command identifier of this command.
206  *
207  * @return ID for this command to be used in menus and toolbars.
208  */
209 int
212 }
213 
214 
215 /**
216  * Creates and returns a new instance of this command.
217  *
218  * @return Newly created instance of this command.
219  */
222  return new PasteComponentCmd();
223 }
224 
225 
226 
227 /**
228  * Returns path to the command's icon file.
229  *
230  * @return Full path to the command's icon file.
231  */
232 string
235 }
236 
237 
238 
239 /**
240  * Returns true when the command is executable, false when not.
241  *
242  * This command is executable when a document is open and the
243  * clipboard is not empty.
244  *
245  * @return True, if the command is executable.
246  */
247 bool
249 
250  // check that a document is open
251  wxDocManager* manager = wxGetApp().docManager();
252  MDFView* mdfView = dynamic_cast<MDFView*>(manager->GetCurrentView());
253  if (mdfView == NULL) {
254  return false;
255  }
256 
257  // check that the clipboard is not empty
259  if (clipboard->isEmpty()) {
260  return false;
261  }
262  return true;
263 }
MDFDocument::getModel
Model * getModel()
Definition: MDFDocument.cc:229
PasteComponentCmd.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
MDFDocument.hh
TTAMachine::FunctionUnit::hasAddressSpace
virtual bool hasAddressSpace() const
Definition: FunctionUnit.cc:608
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
ProDeConstants::CMD_ICON_PASTE
static const std::string CMD_ICON_PASTE
Icon location for the "Paste" command.
Definition: ProDeConstants.hh:314
ObjectStateLoadingException
Definition: Exception.hh:551
PasteComponentCmd::icon
virtual std::string icon() const
Definition: PasteComponentCmd.cc:233
TTAMachine::Bus
Definition: Bus.hh:53
TTAMachine::Socket::loadState
virtual void loadState(const ObjectState *state)
Definition: Socket.cc:502
ObjectState
Definition: ObjectState.hh:59
ImmediateUnit.hh
TTAMachine::FunctionUnit::addressSpace
virtual AddressSpace * addressSpace() const
Definition: FunctionUnit.cc:580
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
PasteComponentCmd::paste
void paste(TTAMachine::Machine &machine, TTAMachine::Component *component, ComponentNavigator &navigator)
Socket.hh
ProDeTextGenerator.hh
Model::pushToStack
void pushToStack()
Definition: Model.cc:167
ProDeTextGenerator
Definition: ProDeTextGenerator.hh:49
Model::notifyObservers
void notifyObservers(bool modified=true)
Definition: Model.cc:152
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAMachine::Guard::copyTo
virtual void copyTo(Bus &parentBus) const =0
TTAMachine::Machine::controlUnit
virtual ControlUnit * controlUnit() const
Definition: Machine.cc:345
TTAMachine::Machine::immediateUnitNavigator
virtual ImmediateUnitNavigator immediateUnitNavigator() const
Definition: Machine.cc:416
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
Conversion.hh
EditorCommand::view
wxView * view() const
Definition: EditorCommand.cc:76
TTAMachine::Machine::Navigator::hasItem
bool hasItem(const std::string &name) const
InformationDialog.hh
ProDeClipboard::sourceMachine
const TTAMachine::Machine * sourceMachine()
Definition: ProDeClipboard.hh:54
Application.hh
ObjectState.hh
TTAMachine::Machine::functionUnitNavigator
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition: Machine.cc:380
TTAMachine::Socket
Definition: Socket.hh:53
Guard.hh
PasteComponentCmd::isEnabled
virtual bool isEnabled()
Definition: PasteComponentCmd.cc:248
Machine.hh
TTAMachine::FunctionUnit::setAddressSpace
virtual void setAddressSpace(AddressSpace *as)
Definition: FunctionUnit.cc:594
Bus.hh
Model::popFromStack
void popFromStack(bool modified=false)
Definition: Model.cc:195
TTAMachine::Machine::socketNavigator
virtual SocketNavigator socketNavigator() const
Definition: Machine.cc:368
PasteComponentCmd::create
virtual PasteComponentCmd * create() const
Definition: PasteComponentCmd.cc:221
ProDeClipboard::isEmpty
bool isEmpty()
Definition: ProDeClipboard.cc:121
ObjectState::name
std::string name() const
TTAMachine::Bus::guardCount
int guardCount() const
Definition: Bus.cc:441
PasteComponentCmd
Definition: PasteComponentCmd.hh:49
TTAMachine::Bus::guard
Guard * guard(int index) const
Definition: Bus.cc:456
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
MDFDocument
Definition: MDFDocument.hh:51
ProDeConstants.hh
ProDeTextGenerator::instance
static ProDeTextGenerator * instance()
Definition: ProDeTextGenerator.cc:382
MDFView.hh
TTAMachine::Machine::registerFileNavigator
virtual RegisterFileNavigator registerFileNavigator() const
Definition: Machine.cc:450
TTAMachine::ControlUnit::setMachine
virtual void setMachine(Machine &mach)
Definition: ControlUnit.cc:119
ProDeClipboard
Definition: ProDeClipboard.hh:47
RegisterFile.hh
ControlUnit.hh
TTAMachine::Machine::busNavigator
virtual BusNavigator busNavigator() const
Definition: Machine.cc:356
ProDeTextGenerator::MSG_ERROR_ONE_GCU
@ MSG_ERROR_ONE_GCU
Error: Multiple control units.
Definition: ProDeTextGenerator.hh:224
PasteComponentCmd::PasteComponentCmd
PasteComponentCmd()
Definition: PasteComponentCmd.cc:64
PasteComponentCmd::Do
virtual bool Do()
Definition: PasteComponentCmd.cc:79
PasteComponentCmd::id
virtual int id() const
Definition: PasteComponentCmd.cc:210
EditorCommand
Definition: EditorCommand.hh:46
ProDeClipboard::copyContents
ObjectState * copyContents()
Definition: ProDeClipboard.cc:104
WxConversion.hh
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
InformationDialog
Definition: InformationDialog.hh:42
ProDeConstants
Definition: ProDeConstants.hh:43
TTAMachine
Definition: Assembler.hh:48
ProDeClipboard.hh
ProDeConstants::COMMAND_PASTE
@ COMMAND_PASTE
Definition: ProDeConstants.hh:437
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
GUICommand::parentWindow
wxWindow * parentWindow() const
Definition: GUICommand.cc:75
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
Model::getMachine
TTAMachine::Machine * getMachine()
Definition: Model.cc:88
TTAMachine::Machine
Definition: Machine.hh:73
FunctionUnit.hh
ProDeClipboard::instance
static ProDeClipboard * instance()
Definition: ProDeClipboard.cc:62
MDFView
Definition: MDFView.hh:59
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50