OpenASIP  2.0
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 
43 using std::string;
44 using boost::format;
45 
46 namespace TTAMachine {
47 
48 /////////////////////////////////////////////////////////////////////////////
49 // MachinePart
50 /////////////////////////////////////////////////////////////////////////////
51 
52 /**
53  * Constructor.
54  */
55 MachinePart::MachinePart() : id_(idCounter_++){
56 }
57 
58 
59 /**
60  * Destructor.
61  */
63 }
64 
66 
67 
68 /////////////////////////////////////////////////////////////////////////////
69 // Component
70 /////////////////////////////////////////////////////////////////////////////
71 
72 const string Component::OSNAME_COMPONENT = "component";
73 const 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  */
82 Component::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  */
102 Component::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  */
116 }
117 
118 
119 /**
120  * Returns the name of the component.
121  *
122  * @return Name of the component.
123  */
124 TCEString
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  */
141 void
142 Component::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  */
162 void
163 Component::ensureRegistration(const Component& component) const {
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  */
176 bool
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  */
204 void
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  */
238 }
239 
240 
241 /**
242  * Destructor.
243  */
245 }
246 
247 }
TTAMachine::Component::setName
virtual void setName(const std::string &name)
Definition: MachinePart.cc:142
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
TTAMachine::Component::isRegistered
virtual bool isRegistered() const
Definition: MachinePart.cc:177
ObjectStateLoadingException
Definition: Exception.hh:551
TTAMachine::Component::ensureRegistration
virtual void ensureRegistration(const Component &component) const
Definition: MachinePart.cc:163
TTAMachine::SubComponent::SubComponent
SubComponent()
Definition: MachinePart.cc:237
TTAMachine::Component::saveState
virtual ObjectState * saveState() const
Definition: MachinePart.cc:189
ObjectState
Definition: ObjectState.hh:59
MOMTextGenerator::TXT_INVALID_NAME
@ TXT_INVALID_NAME
Definition: MOMTextGenerator.hh:84
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
TTAMachine::MachinePart::MachinePart
MachinePart()
Definition: MachinePart.cc:55
MachinePart.hh
TTAMachine::Component::machine_
Machine * machine_
Machine to which the component is registered.
Definition: MachinePart.hh:155
TTAMachine::MachinePart::~MachinePart
virtual ~MachinePart()
Definition: MachinePart.cc:62
TTAMachine::Component::Component
Component(const std::string &name)
Definition: MachinePart.cc:82
TTAMachine::MachinePart::idCounter_
static int idCounter_
Id just for comparison for sets and maps. More deterministic than pointer to the object.
Definition: MachinePart.hh:74
InvalidName
Definition: Exception.hh:827
TTAMachine::Component::loadState
virtual void loadState(const ObjectState *state)
Definition: MachinePart.cc:205
Application.hh
ObjectState.hh
TTAMachine::Component
Definition: MachinePart.hh:90
MOMTextGenerator::TXT_SAME_NAME
@ TXT_SAME_NAME
Definition: MOMTextGenerator.hh:83
TTAMachine::MachinePart
Definition: MachinePart.hh:57
TTAMachine::Component::OSKEY_NAME
static const std::string OSKEY_NAME
ObjectState attribute key for the name of the component.
Definition: MachinePart.hh:137
Exception
Definition: Exception.hh:54
MachineTester::isValidComponentName
static bool isValidComponentName(const std::string &name)
Definition: MachineTester.cc:312
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
IllegalRegistration
Definition: Exception.hh:532
MachineTester.hh
TTAMachine::Component::OSNAME_COMPONENT
static const std::string OSNAME_COMPONENT
ObjectState name for component.
Definition: MachinePart.hh:135
TTAMachine::Component::machine
virtual Machine * machine() const
MOMTextGenerator
Definition: MOMTextGenerator.hh:40
TTAMachine::SubComponent::~SubComponent
virtual ~SubComponent()
Definition: MachinePart.cc:244
TCEString
Definition: TCEString.hh:53
ComponentAlreadyExists
Definition: Exception.hh:510
TTAMachine::Component::~Component
virtual ~Component()
Definition: MachinePart.cc:115
MOMTextGenerator.hh
KeyNotFound
Definition: Exception.hh:285
TTAMachine
Definition: Assembler.hh:48
TTAMachine::Component::name_
std::string name_
Name of the component.
Definition: MachinePart.hh:153
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100