torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Permutable.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
17 #include "torc/generic/Port.hpp"
18 
19 namespace torc {
20 namespace generic {
21 
22 /**
23  * Create a permutable.
24  *
25  * @param[in] inPorts Vector of ports to this permutable.
26  * @param[in] inViewPtr Pointer to parented(View) object.
27  * @param[in] inParentPermutable Pointer to parent permutable.
28  *
29  * @return Pointer to created permutable.
30  */
31 PermutableSharedPtr Permutable::Factory::newPermutablePtr(const std::vector<PortSharedPtr>& inPorts,
32  const ViewSharedPtr& inViewPtr, const PermutableSharedPtr& inParentPermutable) throw (Error) {
33  try {
34  PermutableSharedPtr newPermutable;
35  create(newPermutable);
36  newPermutable->setPorts(inPorts);
37  if(inParentPermutable) {
38  inParentPermutable->addChildPermutable(newPermutable);
39  } else if(inViewPtr) {
40  inViewPtr->addPermutable(newPermutable);
41  }
42  return newPermutable;
43  } catch(Error& e) {
44  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
45  throw;
46  }
47 }
48 
49 /**
50  * Set the list of ports to this permutable.
51  * It will lead to a linear traversal on the list.
52  * So usage of this API is not recommended.
53  *
54  * @param[in] inSource Vector of ports
55  */
56 void Permutable::setPorts(const std::vector<PortSharedPtr>& inSource) throw (Error) {
57  std::vector<PortSharedPtr>::const_iterator port = inSource.begin();
58  std::vector<PortSharedPtr>::const_iterator end = inSource.end();
59  for(; port != end; ++port) {
60  try {
61  addPort(*port);
62  } catch(Error& e) {
63  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
64  throw;
65  }
66  }
67 }
68 
69 /**
70  * Add a port to the list of ports. Empty pointer is ignored.
71  *
72  * @param[in] inPort Pointer to port to be added.
73  *
74  * @exception Error Could not add port, because Port name is empty
75  */
76 void Permutable::addPort(const PortSharedPtr& inPort) throw (Error) {
77  if(!inPort) {
78  return;
79  }
80  std::string name = inPort->getName();
81  if(name.empty()) {
82  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
83  e.saveContextData("Port name", name);
84  throw e;
85  }
86  mPorts.push_back(inPort);
87 }
88 
89 /**
90  * Set the nested permutable.
91  *
92  * @param[in] inSource Vector containing permutables
93  * @exception Error Could not add permutable because pointer to the permutable does not exist
94  */
95 void Permutable::setChildren(const std::vector<PermutableSharedPtr>& inSource) throw (Error) {
96  std::vector<PermutableSharedPtr>::const_iterator entry = inSource.begin();
97  std::vector<PermutableSharedPtr>::const_iterator end = inSource.end();
98  for(; entry != end; ++entry) {
99  try {
100  addChildPermutable(*entry);
101  } catch(Error& e) {
102  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
103  throw;
104  }
105  }
106 }
107 
108 /**
109  * Add a nested permutable/nonpermutable to this permutable.
110  *
111  * @param[in] inPermutable Nested permutable/nonpermutable
112  * @exception Error Could not add permutable because pointer to the permutable does not exist
113  */
114 bool Permutable::addChildPermutable(const PermutableSharedPtr& inPermutable) throw (Error) {
115  if(!inPermutable) {
116  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
117  e.saveContextData("Pointer to the permutable object does not exist", inPermutable);
118  throw e;
119  }
120  if(inPermutable) {
121  mChildren.push_back(inPermutable);
122  return true;
123  } else {
124  return false;
125  }
126 }
127 
128 void Permutable::setIsNonPermutable(const bool& value) {
129  mIsNonPermutable = value;
130 }
131 
132 void Permutable::accept(BaseVisitor& visitor) throw (Error) {
133  try {
134  runVisitor(*this, visitor);
135  } catch(Error& e) {
136  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
137  throw;
138  }
139 }
140 
141 /**
142  * Get the total number of bits of the composition
143  * @return Number of bits
144  */
145 size_t Permutable::getSize() const {
146  size_t size = 0;
147  size_t pSize = 0;
148  size_t cSize = 0;
150 
151  std::vector<PermutableSharedPtr> outPermutables;
152  getChildren(outPermutables);
153  std::vector<PermutableSharedPtr>::iterator permutableIt = outPermutables.begin();
154 
155  std::vector<PortSharedPtr> outPorts;
156  getPorts(outPorts);
157  std::vector<PortSharedPtr>::iterator portIt = outPorts.begin();
158 
159  if(Permutable::ePermutableParent == pType) {
160  if(!outPorts.empty()) {
161  pSize = (*portIt)->getSize();
162  } else if(!outPermutables.empty()) {
163  for(; permutableIt != outPermutables.end(); permutableIt++) {
164  pSize += (*permutableIt)->getSize();
165  for(; portIt != outPorts.end(); portIt++) {
166  pSize += (*portIt)->getSize();
167  }
168  }
169  } else {
170  pSize = 0;
171  }
172  } else {
173  if(!outPorts.empty()) {
174  for(; portIt != outPorts.end(); portIt++) {
175  cSize += (*portIt)->getSize();
176  }
177  }
178  if(!outPermutables.empty()) {
179  for(; permutableIt != outPermutables.end(); permutableIt++) {
180  cSize += (*permutableIt)->getSize();
181  }
182  }
183  }
184  size = pSize + cSize;
185  return size;
186 }
187 
188 /**
189  * Set the permutable type.
190  *
191  * @param[in] inSource PermutableType
192  */
194  mPermutableType = inSource;
195 }
196 
198  mIsNonPermutable(false), mPermutableType() {}
199 
201 
202 } // namespace generic
203 } // namespace torc
virtual PermutableSharedPtr newPermutablePtr(const std::vector< PortSharedPtr > &inPorts, const ViewSharedPtr &inViewPtr, const PermutableSharedPtr &inParentPermutable=PermutableSharedPtr())
Definition: Permutable.cpp:31
void addPort(const PortSharedPtr &inPort)
Definition: Permutable.cpp:76
boost::shared_ptr< Permutable > PermutableSharedPtr
void setPorts(const std::vector< PortSharedPtr > &inSource)
Definition: Permutable.cpp:56
Permutable is used to describe a relationship in which ports are interchangeable. ...
Definition: Permutable.hpp:40
void setPermutableType(const PermutableType &inSource)
Definition: Permutable.cpp:193
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
std::string string
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
size_t getSize() const
Definition: Permutable.cpp:145
A base class for Visitor.
Definition: VisitorType.hpp:31
virtual void accept(BaseVisitor &visitor)
Definition: Permutable.cpp:132
const PermutableType getPermutableType() const
Definition: Permutable.hpp:238
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
PermutableType mPermutableType
Definition: Permutable.hpp:185
void setIsNonPermutable(const bool &value)
Definition: Permutable.cpp:128
void getChildren(std::vector< PermutableSharedPtr > &outPermutables) const
Definition: Permutable.hpp:203
void getPorts(std::vector< PortSharedPtr > &outPorts) const
Definition: Permutable.hpp:194
void setChildren(const std::vector< PermutableSharedPtr > &inSource)
Definition: Permutable.cpp:95
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Port > PortSharedPtr
bool addChildPermutable(const PermutableSharedPtr &inPermutable)
Definition: Permutable.cpp:114
An object that receives an inoutVisitor.
Definition: Visitable.hpp:38
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73