OpenASIP  2.0
CostDBEntryKey.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 CostDBEntryKey.cc
26  *
27  * Implementation of CostDBEntryKey class.
28  *
29  * @author Tommi Rantanen 2003 (tommi.rantanen-no.spam-tut.fi)
30  * @author Jari Mäntyneva 2005 (jari.mantyneva-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include "CostDBEntryKey.hh"
35 #include "Application.hh"
36 #include <iostream>
37 
38 /**
39  * Constructor.
40  *
41  * @param entryType Type of the entry key.
42  */
44  type_(entryType) {
45 }
46 
47 /**
48  * Destructor.
49  *
50  * Deallocates memory reserved for the fields of the entry key.
51  */
53  for (FieldTable::iterator i = fields_.begin(); i != fields_.end(); i++) {
54  assert(*i != NULL);
55  delete *i;
56  *i = NULL;
57  }
58 }
59 
60 /**
61  * Copies the entry key.
62  *
63  * Client is responsible of deallocating the memory reserved for the
64  * returned object.
65  *
66  * @return A copy of the entry key.
67  */
70 
72 
73  for (FieldTable::const_iterator i = fields_.begin();
74  i != fields_.end(); i++) {
75 
76  copy->addField(new EntryKeyField(*(*i)));
77  }
78  return copy;
79 }
80 
81 /**
82  * Adds certain field to the entry key.
83  *
84  * @param field A field to be added.
85  * @exception ObjectAlreadyExists Equal field already exists.
86  */
87 void
89  for (FieldTable::iterator i = fields_.begin(); i != fields_.end(); i++) {
90  if ((*i)->type() == field->type()) {
91  throw ObjectAlreadyExists(
92  __FILE__, __LINE__, "CostDBEntryKey::addField");
93  }
94  }
95  fields_.push_back(field);
96 }
97 
98 /**
99  * Replaces certain field of the entry key.
100  *
101  * @param newField A field.
102  * @exception KeyNotFound Equal field was not found.
103  */
104 void
106  for (FieldTable::iterator i = fields_.begin(); i != fields_.end(); i++) {
107  if ((*i)->type() == newField->type()) {
108  delete *i;
109  *i = newField;
110  return;
111  }
112  }
113  throw KeyNotFound(__FILE__, __LINE__, "CostDBEntry::replaceField");
114 }
115 
116 /**
117  * Returns demanded field of the entry key.
118  *
119  * @param fieldType Type of the field.
120  * @exception KeyNotFound Requested field type was not found.
121  */
124  for (FieldTable::const_iterator i = fields_.begin();
125  i != fields_.end(); i++) {
126  if ((*i)->type() == &fieldType) {
127  return *(*i);
128  }
129  }
130  throw KeyNotFound(__FILE__, __LINE__, "CostDBEntry::keyFieldOfType");
131  return *fields_[0];// stupid return statement to make compiler quiet
132 }
133 
134 /**
135  * Returns demanded field of the entry key.
136  *
137  * @return type Type of the field.
138  */
140 CostDBEntryKey::keyFieldOfType(std::string fieldType) const {
141 
142  return keyFieldOfType(*type_->fieldProperty(fieldType));
143 }
144 
145 /**
146  * Compares if two entry keys are equal.
147  *
148  * @param entryKey An entry key.
149  * @return True if two entry keys are equal, otherwise false.
150  */
151 bool
152 CostDBEntryKey::isEqual(const CostDBEntryKey& entryKey) const {
153 
154  if (type() == entryKey.type()) {
155  bool isMatch = true;
156  for (FieldTable::const_iterator f = fields_.begin();
157  f != fields_.end(); f++) {
158 
159  if (!(*f)->isEqual(entryKey.keyFieldOfType(*(*f)->type()))) {
160  isMatch = false;
161  break;
162  }
163  }
164  if (isMatch) {
165  return true;
166  }
167  }
168  return false;
169 }
170 
171 /**
172  * Returns the field found on the given index.
173  *
174  * The index must be between 0 and the number of fields - 1.
175  *
176  * @param index Index.
177  * @return The field found on the given index.
178  * @exception OutOfRange Index was out of range.
179  */
180 const EntryKeyField&
181 CostDBEntryKey::field(int index) const {
182  if (index >= fieldCount() || index < 0) {
183  throw OutOfRange(__FILE__, __LINE__, "CostDBEntryKey::field");
184  }
185  return *fields_[index];
186 }
EntryKeyFieldProperty
Definition: EntryKeyFieldProperty.hh:47
CostDBEntryKey::type
const EntryKeyProperty * type() const
CostDBEntryKey::CostDBEntryKey
CostDBEntryKey(const EntryKeyProperty *entryType)
Definition: CostDBEntryKey.cc:43
OutOfRange
Definition: Exception.hh:320
CostDBEntryKey::fieldCount
int fieldCount() const
EntryKeyField
Definition: EntryKeyField.hh:45
CostDBEntryKey::fields_
FieldTable fields_
Fields of the entry key.
Definition: CostDBEntryKey.hh:75
CostDBEntryKey::type_
const EntryKeyProperty * type_
Type of the entry key.
Definition: CostDBEntryKey.hh:73
assert
#define assert(condition)
Definition: Application.hh:86
CostDBEntryKey::~CostDBEntryKey
virtual ~CostDBEntryKey()
Definition: CostDBEntryKey.cc:52
CostDBEntryKey::replaceField
void replaceField(EntryKeyField *newField)
Definition: CostDBEntryKey.cc:105
CostDBEntryKey::addField
void addField(EntryKeyField *field)
Definition: CostDBEntryKey.cc:88
Application.hh
EntryKeyField::type
const EntryKeyFieldProperty * type() const
CostDBEntryKey::isEqual
bool isEqual(const CostDBEntryKey &entryKey) const
Definition: CostDBEntryKey.cc:152
CostDBEntryKey::field
const EntryKeyField & field(int index) const
Definition: CostDBEntryKey.cc:181
EntryKeyProperty
Definition: EntryKeyProperty.hh:57
CostDBEntryKey::copy
CostDBEntryKey * copy() const
Definition: CostDBEntryKey.cc:69
ObjectAlreadyExists
Definition: Exception.hh:1002
CostDBEntryKey.hh
CostDBEntryKey::keyFieldOfType
EntryKeyField keyFieldOfType(const EntryKeyFieldProperty &fieldType) const
Definition: CostDBEntryKey.cc:123
CostDBEntryKey
Definition: CostDBEntryKey.hh:52
KeyNotFound
Definition: Exception.hh:285
EntryKeyProperty::fieldProperty
EntryKeyFieldProperty * fieldProperty(std::string field) const
Definition: EntryKeyProperty.cc:104