OpenASIP 2.2
Loading...
Searching...
No Matches
MachinePart.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 MachinePart.cc
26 *
27 * Implementation of MachinePart class and derived Component and SubComponent
28 * classes.
29 *
30 * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi)
31 * @note reviewed 17 Jun 2003 by jn, pj, jm, ll
32 * @note rating: red
33 */
34
35#include <string>
36
37#include "MachinePart.hh"
38#include "MachineTester.hh"
39#include "MOMTextGenerator.hh"
40#include "Application.hh"
41#include "ObjectState.hh"
42
43using std::string;
44using boost::format;
45
46namespace TTAMachine {
47
48/////////////////////////////////////////////////////////////////////////////
49// MachinePart
50/////////////////////////////////////////////////////////////////////////////
51
52/**
53 * Constructor.
54 */
55MachinePart::MachinePart() : id_(idCounter_++){
56}
57
58
59/**
60 * Destructor.
61 */
64
66
67
68/////////////////////////////////////////////////////////////////////////////
69// Component
70/////////////////////////////////////////////////////////////////////////////
71
72const string Component::OSNAME_COMPONENT = "component";
73const string Component::OSKEY_NAME = "name";
74
75/**
76 * Constructor.
77 *
78 * @param name Name of the component.
79 * @exception InvalidName If the given name is not a valid name for a
80 * component.
81 */
82Component::Component(const std::string& name)
83 : MachinePart(), name_(name), machine_(NULL) {
85 const string procName = "Component::Component";
86 MOMTextGenerator textGen;
87 format errorMsg = textGen.text(MOMTextGenerator::TXT_INVALID_NAME);
88 errorMsg % name;
89 throw InvalidName(__FILE__, __LINE__, procName, errorMsg.str());
90 }
91}
92
93/**
94 * Constructor.
95 *
96 * Loads the name from the given ObjectState object.
97 *
98 * @param state The ObjectState instance from which the name is loaded.
99 * @exception ObjectStateLoadingException If the given ObjectState instance
100 * is invalid.
101 */
102Component::Component(const ObjectState* state) : MachinePart(), machine_(NULL) {
103 try {
105 } catch (const Exception& e) {
106 string procName = "Component::Component";
108 __FILE__, __LINE__, procName, e.errorMessage());
109 }
110}
111
112/**
113 * Destructor.
114 */
117
118
119/**
120 * Returns the name of the component.
121 *
122 * @return Name of the component.
123 */
126 return name_;
127}
128
129
130/**
131 * Sets the name of the component.
132 *
133 * @param name The new name.
134 * @exception ComponentAlreadExists Sub class implementations may throw the
135 * exception if there exists another
136 * component by the same name in the machine
137 * already.
138 * @exception InvalidName If the given name is not a valid name for
139 * a component.
140 */
141void
142Component::setName(const std::string& name) {
144 const string procName = "Component::setName";
145 MOMTextGenerator textGen;
146 format errorMsg = textGen.text(MOMTextGenerator::TXT_INVALID_NAME);
147 errorMsg % name;
148 throw InvalidName(__FILE__, __LINE__, procName, errorMsg.str());
149 }
150
151 name_ = name;
152}
153
154/**
155 * Ensures that the component is registered to the same machine as the
156 * given component.
157 *
158 * @param component The component.
159 * @exception IllegalRegistration If the components are not registered to the
160 * same machine.
161 */
162void
164 if (machine() == NULL || machine() != component.machine()) {
165 const string procName = "Component::ensureRegistration";
166 throw IllegalRegistration(__FILE__, __LINE__, procName);
167 }
168}
169
170/**
171 * Returns true if the component is registered to a machine, otherwise false.
172 *
173 * @return True if the component is registered to a machine, otherwise
174 * false.
175 */
176bool
178 return (machine_ != NULL);
179}
180
181
182/**
183 * Creates a new ObjectState instance and saves the name of the component
184 * into it.
185 *
186 * @return The newly created ObjectState instance.
187 */
191 state->setAttribute(OSKEY_NAME, name());
192 return state;
193}
194
195
196/**
197 * Loads the name of the component from the given ObjectState instance.
198 *
199 * @param state The ObjectState instance.
200 * @exception ObjectStateLoadingException If the machine already contains
201 * same type of component with the
202 * same name.
203 */
204void
206 const string procName = "Component::loadState";
207
208 try {
209 string name = state->stringAttribute(OSKEY_NAME);
210 setName(name);
211 } catch (const KeyNotFound& e) {
212 throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
213 } catch (const ComponentAlreadyExists& e) {
214 MOMTextGenerator textGenerator;
215 format text = textGenerator.text(
217 text % state->stringAttribute(OSKEY_NAME);
219 __FILE__, __LINE__, procName, text.str());
220 } catch (const InvalidName& e) {
221 MOMTextGenerator textGenerator;
222 format text = textGenerator.text(
224 text % state->stringAttribute(OSKEY_NAME);
226 __FILE__, __LINE__, procName, text.str());
227 }
228}
229
230/////////////////////////////////////////////////////////////////////////////
231// SubComponent
232/////////////////////////////////////////////////////////////////////////////
233
234/**
235 * Constructor.
236 */
239
240
241/**
242 * Destructor.
243 */
246
247}
std::string errorMessage() const
Definition Exception.cc:123
static bool isValidComponentName(const std::string &name)
void setAttribute(const std::string &name, const std::string &value)
std::string stringAttribute(const std::string &name) const
virtual void setName(const std::string &name)
virtual Machine * machine() const
virtual void loadState(const ObjectState *state)
static const std::string OSKEY_NAME
ObjectState attribute key for the name of the component.
static const std::string OSNAME_COMPONENT
ObjectState name for component.
Component(const std::string &name)
std::string name_
Name of the component.
virtual bool isRegistered() const
Machine * machine_
Machine to which the component is registered.
virtual void ensureRegistration(const Component &component) const
virtual TCEString name() const
virtual ObjectState * saveState() const
static int idCounter_
Id just for comparison for sets and maps. More deterministic than pointer to the object.
virtual boost::format text(int textId)