OpenASIP  2.0
UnitImplementationLocation.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 UnitImplementationLocation.cc
26  *
27  * Implementation of UnitImplementationLocation class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <vector>
35 
37 #include "MachineImplementation.hh"
38 #include "FileSystem.hh"
39 #include "ObjectState.hh"
40 
41 using std::string;
42 using std::vector;
43 
44 namespace IDF {
45 
47  "unit_implementation";
48 const string UnitImplementationLocation::OSKEY_ID = "id";
49 const string UnitImplementationLocation::OSKEY_UNIT_NAME = "unit_name";
50 const string UnitImplementationLocation::OSKEY_HDB_FILE = "hdb_file";
51 
52 
53 /**
54  * The constructor.
55  *
56  * @param hdbFile The database that contains the implementation.
57  * @param id ID of the RF or FU entry in the database.
58  * @param unitName Name of the corresponding unit in ADF.
59  */
61  const std::string& hdbFile,
62  int id,
63  const std::string& unitName) :
64  hdbFile_(hdbFile), id_(id), unitName_(unitName), parent_(NULL) {
65 }
66 
67 
68 /**
69  * The constructor.
70  *
71  * Loads the state of the object from the given ObjectState instance.
72  *
73  * @param state The ObjectState instance.
74  * @exception ObjectStateLoadingException If the given ObjectState instance
75  * is invalid.
76  */
78  : hdbFile_(""), id_(0), unitName_(""), parent_(NULL) {
79  /// this is used by NullFUImplementationLocation
80  if (state == NULL)
81  return;
82  loadState(state);
83 }
84 
85 /**
86  * The destructor.
87  */
89 }
90 
91 
92 /**
93  * Returns the absolute path to the HDB file.
94  *
95  * @return Absolute path to the HDB file.
96  * @exception FileNotFound If the HDB file is not found in the search paths.
97  */
98 std::string
100  vector<string> paths = Environment::hdbPaths();
101  if (parent_) {
102  paths.insert(
103  paths.begin(), FileSystem::directoryOfPath(parent_->sourceIDF()));
104  }
105  TCEString expandedPath(hdbFile_);
106  expandedPath.replaceString("tce:", "");
107  expandedPath = FileSystem::expandTilde(expandedPath);
108  return FileSystem::findFileInSearchPaths(paths, expandedPath);
109 }
110 
111 /**
112  * Returns the path to the file, which was defined in IDF.
113  *
114  * @return Path to the HDB in IDF file.
115  */
116 std::string
118  return hdbFile_;
119 }
120 
121 /**
122  * Returns the entry ID in HDB.
123  *
124  * @return The entry ID.
125  */
126 int
128  return id_;
129 }
130 
131 
132 /**
133  * Returns the name of the unit in ADF.
134  *
135  * @return The name of the unit.
136  */
137 std::string
139  return unitName_;
140 }
141 
142 
143 /**
144  * Sets the parent of the object.
145  *
146  * @param parent The parent MachineImplementation instance.
147  * @exception InvalidData If the parent is already set.
148  */
149 void
151  if (parent_ != NULL) {
152  throw InvalidData(__FILE__, __LINE__, __func__);
153  }
154 
155  parent_ = &parent;
156 }
157 
158 /**
159  * Loads the state of the object from the given ObjectState instance.
160  *
161  * @exception ObjectStateLoadingException If the given ObjectState instance.
162  */
163 void
165  const string procName = "UnitImplementationLocation::loadState";
166 
167  if (state->name() != OSNAME_UNIT_IMPLEMENTATION) {
168  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
169  }
170 
171  try {
173  id_ = state->intAttribute(OSKEY_ID);
175  } catch (const Exception& exception) {
177  __FILE__, __LINE__, procName, exception.errorMessage());
178  }
179 }
180 
181 /**
182  * Saves the state of the object to an ObjectState instance.
183  *
184  * @return The newly created ObjectState instance.
185  */
190  state->setAttribute(OSKEY_ID, id());
192  return state;
193 }
194 
195 /**
196  * Sets the absolute path to the HDB file.
197  *
198  * @param file Absolute path to the HDB file.
199  */
200 void
202  hdbFile_ = file;
203 }
204 
205 
206 /**
207  * Sets the entry ID in HDB.
208  */
209 void
211  id_ = id;
212 }
213 
214 }
IDF::UnitImplementationLocation::id_
int id_
Entry ID in the HDB.
Definition: UnitImplementationLocation.hh:83
IDF::UnitImplementationLocation::unitName_
std::string unitName_
Name of the unit in ADF.
Definition: UnitImplementationLocation.hh:85
IDF::UnitImplementationLocation::setParent
virtual void setParent(MachineImplementation &parent)
Definition: UnitImplementationLocation.cc:150
FileSystem.hh
IDF::UnitImplementationLocation::OSKEY_HDB_FILE
static const std::string OSKEY_HDB_FILE
ObjectState attribute key for the name of the HDB file.
Definition: UnitImplementationLocation.hh:73
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
IDF::UnitImplementationLocation::setHDBFile
virtual void setHDBFile(std::string file)
Definition: UnitImplementationLocation.cc:201
ObjectStateLoadingException
Definition: Exception.hh:551
ObjectState
Definition: ObjectState.hh:59
IDF::UnitImplementationLocation::loadState
void loadState(const ObjectState *state)
Definition: UnitImplementationLocation.cc:164
IDF::UnitImplementationLocation::UnitImplementationLocation
UnitImplementationLocation(const std::string &hdbFile, int id, const std::string &unitName)
Definition: UnitImplementationLocation.cc:60
IDF::UnitImplementationLocation::setID
virtual void setID(int id)
Definition: UnitImplementationLocation.cc:210
IDF::UnitImplementationLocation::OSKEY_ID
static const std::string OSKEY_ID
ObjectState attribute key for the entry ID.
Definition: UnitImplementationLocation.hh:75
InvalidData
Definition: Exception.hh:149
Environment::hdbPaths
static std::vector< std::string > hdbPaths(bool libraryPathsOnly=false)
Definition: Environment.cc:683
__func__
#define __func__
Definition: Application.hh:67
FileSystem::directoryOfPath
static std::string directoryOfPath(const std::string fileName)
Definition: FileSystem.cc:79
ObjectState.hh
Exception
Definition: Exception.hh:54
IDF::MachineImplementation::sourceIDF
std::string sourceIDF() const
Definition: MachineImplementation.cc:121
IDF::UnitImplementationLocation::unitName
virtual std::string unitName() const
Definition: UnitImplementationLocation.cc:138
FileSystem::expandTilde
static std::string expandTilde(const std::string &stringWithTilde)
Definition: FileSystem.cc:217
ObjectState::name
std::string name() const
IDF::UnitImplementationLocation::OSKEY_UNIT_NAME
static const std::string OSKEY_UNIT_NAME
Objectstate attribute key for the name of the unit.
Definition: UnitImplementationLocation.hh:77
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
IDF::UnitImplementationLocation::saveState
ObjectState * saveState() const
Definition: UnitImplementationLocation.cc:187
TCEString::replaceString
TCEString & replaceString(const std::string &old, const std::string &newString)
Definition: TCEString.cc:94
IDF::UnitImplementationLocation::hdbFile_
std::string hdbFile_
Name of the HDB file.
Definition: UnitImplementationLocation.hh:81
IDF::UnitImplementationLocation::parent_
MachineImplementation * parent_
The parent MachineImplementation instance.
Definition: UnitImplementationLocation.hh:87
IDF::UnitImplementationLocation::id
virtual int id() const
Definition: UnitImplementationLocation.cc:127
TCEString
Definition: TCEString.hh:53
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
IDF::UnitImplementationLocation::hdbFile
virtual std::string hdbFile() const
Definition: UnitImplementationLocation.cc:99
IDF::UnitImplementationLocation::OSNAME_UNIT_IMPLEMENTATION
static const std::string OSNAME_UNIT_IMPLEMENTATION
ObjectState name for unit implementation.
Definition: UnitImplementationLocation.hh:71
FileSystem::findFileInSearchPaths
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
Definition: FileSystem.cc:562
IDF::UnitImplementationLocation::~UnitImplementationLocation
virtual ~UnitImplementationLocation()
Definition: UnitImplementationLocation.cc:88
IDF::MachineImplementation
Definition: MachineImplementation.hh:54
UnitImplementationLocation.hh
IDF::UnitImplementationLocation::hdbFileOriginal
std::string hdbFileOriginal() const
Definition: UnitImplementationLocation.cc:117
IDF
Definition: DSDBManager.hh:54
MachineImplementation.hh
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100