torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PortRefCreator.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_PORTREFCREATOR_HPP
17 #define TORC_GENERIC_PORTREFCREATOR_HPP
18 
20 
26 
27 namespace torc {
28 namespace generic {
29 
30 /**
31  * @brief Create port reference
32  *
33  * The PortRefCreator class is used to create scalar,
34  * vector and bundle port reference
35  */
36 template <typename _ReturnType>
38  public PortBundle::Visitor {
39 public:
40  void visit(ScalarPort& port) throw (Error);
41 
42  void visit(VectorPort& port) throw (Error);
43 
44  void visit(PortBundle& port) throw (Error);
45 
46  inline _ReturnType getReturnValue() const;
47 
48  PortRefCreator(const ObjectFactorySharedPtr& inFactory, const InstanceSharedPtr& inInstance,
50  ~PortRefCreator() throw ();
51 
52 private:
53  void setupCreatedPort(const PortSharedPtr& port, const PortReferenceSharedPtr& inPortRef)
54  throw (Error);
55 
59  _ReturnType mReturnValue;
60 };
61 
62 template <typename _ReturnType> void PortRefCreator<_ReturnType>::visit(ScalarPort& port)
63  throw (Error) {
64  try {
65  ScalarPortReferenceSharedPtr scalarPortRef;
66  mFactory->create(scalarPortRef);
67  setupCreatedPort(port.getSharedThis(), scalarPortRef);
68  } catch(Error& e) {
69  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
70  throw;
71  }
72 }
73 
74 template <typename _ReturnType> void PortRefCreator<_ReturnType>::visit(VectorPort& port)
75  throw (Error) {
76  try {
77  VectorPortReferenceSharedPtr vectorPortRef;
78  mFactory->create(vectorPortRef);
79  std::vector < size_t > limits;
80  port.getLimits(limits);
81  vectorPortRef->constructChildren(mFactory, limits);
82  setupCreatedPort(port.getSharedThis(), vectorPortRef);
83  } catch(Error& e) {
84  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
85  throw;
86  }
87 }
88 
89 template <typename _ReturnType> void PortRefCreator<_ReturnType>::visit(PortBundle& port)
90  throw (Error) {
91  try {
92  PortBundleReferenceSharedPtr portBundleRef;
93  mFactory->create(portBundleRef);
94  PortRefCreator<PortReferenceSharedPtr> creator(mFactory, mInstance, portBundleRef);
96  port.applyOnAllChildren(applier);
97  setupCreatedPort(port.getSharedThis(), portBundleRef);
98  mReturnValue = portBundleRef;
99  } catch(Error& e) {
100  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
101  throw;
102  }
103 }
104 
105 template <typename _ReturnType> inline _ReturnType PortRefCreator<_ReturnType>::getReturnValue()
106  const {
107  return mReturnValue;
108 }
109 
110 template <typename _ReturnType> PortRefCreator<_ReturnType>::PortRefCreator(
111  const ObjectFactorySharedPtr& inFactory, const InstanceSharedPtr& inInstance,
112  const PortBundleReferenceSharedPtr& inBundle) :
113  mFactory(inFactory), mInstance(inInstance), mBundle(inBundle), mReturnValue() {}
114 
115 template <typename _ReturnType> PortRefCreator<_ReturnType>::~PortRefCreator() throw () {}
116 
117 template <typename _ReturnType> void PortRefCreator<_ReturnType>::setupCreatedPort(
118  const PortSharedPtr& port, const PortReferenceSharedPtr& inPortRef) throw (Error) {
119  try {
120  inPortRef->bindToMasterPort(port);
121  if(mBundle) {
122  inPortRef->setParentCollection(mBundle);
123  mBundle->addChild(inPortRef);
124  } else {
125  inPortRef->setParent(mInstance);
126  mInstance->addPortReference(inPortRef);
127  }
128  } catch(Error& e) {
129  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
130  throw;
131  }
132 }
133 
134 } // namespace generic
135 } // namespace torc
136 
137 #endif // TORC_GENERIC_PORTREFCREATOR_HPP
An acyclic inoutVisitor implementation.
Definition: VisitorType.hpp:57
boost::shared_ptr< Instance > InstanceSharedPtr
void visit(ScalarPort &port)
_ReturnType getReturnValue() const
Represents a bundle of ports.
Definition: PortBundle.hpp:44
void setupCreatedPort(const PortSharedPtr &port, const PortReferenceSharedPtr &inPortRef)
boost::shared_ptr< ObjectFactory > ObjectFactorySharedPtr
boost::shared_ptr< ScalarPortReference > ScalarPortReferenceSharedPtr
ObjectFactorySharedPtr mFactory
PortBundleReferenceSharedPtr mBundle
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
Represents a port array.
Definition: VectorPort.hpp:45
boost::shared_ptr< PortReference > PortReferenceSharedPtr
Represents a standalone port.
Definition: ScalarPort.hpp:42
Create port reference.
boost::shared_ptr< Port > PortSharedPtr
PortRefCreator(const ObjectFactorySharedPtr &inFactory, const InstanceSharedPtr &inInstance, const PortBundleReferenceSharedPtr &inBundle=PortBundleReferenceSharedPtr())
boost::shared_ptr< VectorPortReference > VectorPortReferenceSharedPtr
boost::shared_ptr< PortBundleReference > PortBundleReferenceSharedPtr
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73