OpenASIP 2.2
Loading...
Searching...
No Matches
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
57using std::string;
58using boost::format;
59using namespace TTAMachine;
60
61/**
62 * The Constructor.
63 */
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 */
78bool
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 */
209int
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 */
232string
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 */
247bool
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}
#define assert(condition)
TTAMachine::Machine * machine
the architecture definition of the estimated processor
wxView * view() const
std::string errorMessage() const
Definition Exception.cc:123
wxWindow * parentWindow() const
Definition GUICommand.cc:75
Model * getModel()
void pushToStack()
Definition Model.cc:167
void notifyObservers(bool modified=true)
Definition Model.cc:152
void popFromStack(bool modified=false)
Definition Model.cc:195
TTAMachine::Machine * getMachine()
Definition Model.cc:88
void setAttribute(const std::string &name, const std::string &value)
std::string name() const
void paste(TTAMachine::Machine &machine, TTAMachine::Component *component, ComponentNavigator &navigator)
virtual PasteComponentCmd * create() const
virtual int id() const
virtual std::string icon() const
virtual bool isEnabled()
ObjectState * copyContents()
const TTAMachine::Machine * sourceMachine()
static ProDeClipboard * instance()
static const std::string CMD_ICON_PASTE
Icon location for the "Paste" command.
static ProDeTextGenerator * instance()
@ MSG_ERROR_ONE_GCU
Error: Multiple control units.
Guard * guard(int index) const
Definition Bus.cc:456
static const std::string OSNAME_BUS
ObjectState name for Bus ObjectState.
Definition Bus.hh:116
int guardCount() const
Definition Bus.cc:441
virtual TCEString name() const
static const std::string OSNAME_CONTROL_UNIT
ObjectState name for ControlUnit.
virtual void setMachine(Machine &mach)
virtual AddressSpace * addressSpace() const
virtual void setAddressSpace(AddressSpace *as)
static const std::string OSNAME_FU
ObjectState name for function unit.
virtual bool hasAddressSpace() const
virtual void copyTo(Bus &parentBus) const =0
static const std::string OSNAME_IMMEDIATE_UNIT
ObjectState name for ImmediateUnit.
ComponentType * item(int index) const
bool hasItem(const std::string &name) const
virtual RegisterFileNavigator registerFileNavigator() const
Definition Machine.cc:450
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition Machine.cc:380
virtual SocketNavigator socketNavigator() const
Definition Machine.cc:368
virtual ImmediateUnitNavigator immediateUnitNavigator() const
Definition Machine.cc:416
virtual BusNavigator busNavigator() const
Definition Machine.cc:356
virtual ControlUnit * controlUnit() const
Definition Machine.cc:345
static const std::string OSNAME_REGISTER_FILE
ObjectState name for RegisterFile.
static const std::string OSNAME_SOCKET
ObjectState name for socket.
Definition Socket.hh:100
virtual void loadState(const ObjectState *state)
Definition Socket.cc:502
virtual boost::format text(int textId)
static wxString toWxString(const std::string &source)