torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VectorPortBit.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 
16 #ifndef HAVE_CONFIG_H
17 #include "torc/generic/config.h"
18 #endif
19 
20 #ifdef GENOM_SERIALIZATION
21 #include <boost/archive/binary_iarchive.hpp>
22 #include <boost/archive/binary_oarchive.hpp>
23 #include <boost/serialization/base_object.hpp>
24 #include <boost/serialization/export.hpp>
25 #endif //GENOM_SERIALIZATION
27 #include "torc/generic/Log.hpp"
28 #include "torc/generic/Net.hpp"
31 
32 #ifdef GENOM_SERIALIZATION
33 BOOST_CLASS_EXPORT(torc::generic::VectorPortBit)
34 #endif //GENOM_SERIALIZATION
35 namespace torc {
36 namespace generic {
37 
38 void VectorPortBit::accept(BaseVisitor& inoutVisitor) throw (Error) {
39  try {
40  runVisitor(*this, inoutVisitor);
41  } catch(Error& e) {
42  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
43  throw;
44  }
45 }
46 
48  if(!inNet) {
49  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
50  e.saveContextData("Pointer to the Net object does not exist", inNet);
51  throw e;
52  }
53  if(inNet->getSize() != getSize()) {
54  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
55  e.saveContextData("Net Size", inNet->getSize());
56  e.saveContextData("Vector port bit Size", getSize());
57  throw e;
58  }
59  Connectable::Connection newConnection;
60  try {
61  ConnectionHandler handler(inNet);
62  handler.connectPortToNet(getSharedThis());
63  newConnection = Connectable::connect(inNet);
64  } catch(Error& e) {
65  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
66  throw;
67  }
68  return newConnection;
69 }
70 
71 void VectorPortBit::disconnect(const Connection& inConnection) throw (Error) {
72  NetSharedPtr net = *inConnection;
73  if(!net) {
74  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
75  e.saveContextData("Pointer to the Net object does not exist", net);
76  throw e;
77  }
78  try {
79  ConnectionHandler handler(net);
80  handler.disconnectPortFromNet(getSharedThis());
81  Connectable::disconnect(inConnection);
82  } catch(Error& e) {
83  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
84  throw;
85  }
86 }
87 
88 void VectorPortBit::getConnectedNets(std::vector<NetSharedPtr>& outNets,
89  bool inSkipChildConnections) const throw (Error) {
90  //First get connections exclusive to me
91  Port::getConnectedNets(outNets);
92 
93  //Append cousins from nets that have been connected to my
94  //parent
95  if(!inSkipChildConnections) {
96  VectorBit<Net>::List parentConnections;
97  Composite<Port>::Pointer parent = getParentCollection();
98  if(!parent) {
99  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
100  e.saveContextData("Pointer to parent collection does not exist", parent);
101  throw e;
102  }
103  if(eCompositionTypeVector != parent->getCompositionType()) {
104  Error e(eMessageIdErrorCompositionTypeMismatch, __FUNCTION__, __FILE__, __LINE__);
105  e.saveContextData("Composition type mismatch", parent->getCompositionType());
106  throw e;
107  }
108  parent->getConnectedNets(parentConnections, true);
109  VectorBit<Net>::List::iterator connNet = parentConnections.begin();
110  VectorBit<Net>::List::iterator connNetEnd = parentConnections.end();
111  const std::vector<VectorBit<Net>::SizeType>& myIndex = getIndices();
112  for(; connNet != connNetEnd; ++connNet) {
114  if(eCompositionTypeVector == (*connNet)->getCompositionType()) {
115  cousin = (*connNet)->get(myIndex);
116  //We don't want to mess up preservervation
117  //by calling getChildren()
118  } else //Must be a bundle
119  {
120  VectorBit<Net>::List bChildren;
121  (*connNet)->getChildren(bChildren);
122  cousin = bChildren[getAbsoluteIndex()];
123  //We assume list is a vector
124  }
125  if(!cousin) {
126  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
127  e.saveContextData("Pointer to item does not exist", cousin);
128  throw e;
129  }
130  outNets.push_back(cousin);
131  }
132  }
133  return;
134 }
135 
137 
139  torc::generic::log("Vector Port bit destroyed\n");
140 }
141 
142 #ifdef GENOM_SERIALIZATION
143 template <class Archive> void
144 VectorPortBit::serialize(Archive& ar, unsigned int) {
145  ar & boost::serialization::base_object<Port>(*this);
146  ar & boost::serialization::base_object<VectorBit<Port> >(*this);
147 }
148 
149 #endif //GENOM_SERIALIZATION
150 } // namespace generic
151 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
void log(const char *fmt,...)
Definition: Log.cpp:89
virtual void getChildren(List &outChildren) const
Definition: VectorBit.hpp:140
virtual CompositionType getCompositionType() const =0
virtual void getConnectedNets(std::vector< NetSharedPtr > &outNets, bool inSkipChildConnections=false) const
Represents a bit of a port.
void disconnectPortFromNet(const PortSharedPtr &inPort)
Interface for objects that can be composed within each other.
Definition: Composite.hpp:45
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
void connectPortToNet(const PortSharedPtr &inPort)
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
virtual void getConnectedNets(std::vector< NetSharedPtr > &outNets, bool inSkipChildConnections=false) const
Definition: Connectable.cpp:45
boost::shared_ptr< Net > NetSharedPtr
A base class for Visitor.
Definition: VisitorType.hpp:31
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
virtual void accept(BaseVisitor &inoutVisitor)
Interface for an EDIF port object.
virtual const Pointer get(const std::vector< SizeType > &inIndices) const =0
virtual Connection connect(const NetSharedPtr &inNet)
Represents a single element of a vector composition.
Definition: VectorBit.hpp:41
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73