torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InternalUtilityFunctions.hpp
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 TORC_GENERIC_INTERNALUTILITYFUNCTIONS_HPP
17 #define TORC_GENERIC_INTERNALUTILITYFUNCTIONS_HPP
18 
19 namespace torc {
20 namespace generic {
21 
22 template <typename _Connectable> void findLeafConnectable(std::vector<std::string>& nestedNames,
23  boost::shared_ptr<_Connectable>& conn) throw (Error) {
24  std::vector<std::string>::reverse_iterator name = nestedNames.rbegin();
25  std::vector<std::string>::reverse_iterator end = nestedNames.rend();
26  ++name;
27  if(!conn) {
28  Error e(eMessageIdErrorNullPointer, __FUNCTION__, __FILE__, __LINE__);
29  throw e;
30  }
31  for(; name != end; ++name) {
32  if(eCompositionTypeBundle != conn->getCompositionType()) {
33  Error e(eMessageIdErrorUnsupoortedOperation, __FUNCTION__, __FILE__, __LINE__);
34  e.saveContextData("Name", *name);
35  e.saveContextData("Operation", std::string("Find child"));
36  throw e;
37  }
38  std::vector < boost::shared_ptr<_Connectable> > children;
39  conn->getChildren(children);
40  bool found = false;
41  for(typename std::vector<boost::shared_ptr<_Connectable> >::iterator it = children.begin();
42  it != children.end(); ++it) {
43  if((*it)->getName() == *name) {
44  conn = (*it);
45  found = true;
46  break;
47  }
48  }
49  if(!found) {
50  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
51  e.saveContextData("Name", (*name));
52  throw;
53  }
54  }
55 }
56 
57 template <typename _Connectable> void connectNetToElement(const std::vector<size_t>& inIndices,
58  const boost::shared_ptr<_Connectable>& inConn, const NetSharedPtr& inNet) throw (Error) {
59  try {
60  if(inIndices.empty()) {
61  //TBD::CHECK WIDTH
62  inConn->connect(inNet);
63  } else {
64  boost::shared_ptr<_Connectable> bit = inConn->get(inIndices);
65  bit->connect(inNet);
66  }
67  } catch(Error& e) {
68  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
69  throw;
70  }
71 }
72 
73 template <typename _Base, typename _Derived> struct IndexFinder : _Derived::Visitor {
74  std::vector<size_t> operator()(const boost::shared_ptr<_Base>& inPtr) {
75  inPtr->accept(*this);
76  return mIndices;
77  }
78 
79  void visit(_Derived& inObj) throw (Error) {
80  try {
81  mIndices = inObj.getIndices();
82  } catch(Error& e) {
83  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
84  throw;
85  }
86  }
87 
88  IndexFinder() : _Derived::Visitor(), mIndices() {
89  }
90  ~IndexFinder() throw () {
91  }
92 
93  std::vector<size_t> mIndices;
94 };
95 
96 } //namespace generic
97 } //namespace torc
98 
99 #endif // TORC_GENERIC_INTERNALUTILITYFUNCTIONS_HPP
void findLeafConnectable(std::vector< std::string > &nestedNames, boost::shared_ptr< _Connectable > &conn)
void connectNetToElement(const std::vector< size_t > &inIndices, const boost::shared_ptr< _Connectable > &inConn, const NetSharedPtr &inNet)
std::string string
std::vector< size_t > operator()(const boost::shared_ptr< _Base > &inPtr)
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
boost::shared_ptr< Net > NetSharedPtr
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73