00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef TORC_GENERIC_OM_PORTREFCREATOR_HPP
00017 #define TORC_GENERIC_OM_PORTREFCREATOR_HPP
00018
00019 #include "torc/generic/om/PointerTypes.hpp"
00020 #include "torc/generic/om/DumpRestoreConfig.hpp"
00021
00022 #include "torc/generic/om/ScalarPort.hpp"
00023 #include "torc/generic/om/VectorPort.hpp"
00024 #include "torc/generic/om/PortBundle.hpp"
00025 #include "torc/generic/om/ObjectFactory.hpp"
00026 #include "torc/generic/om/VisitorApplier.hpp"
00027
00028 namespace torc {
00029
00030 namespace generic {
00031
00032
00033
00034
00035
00036
00037
00038 template<typename _ReturnType>
00039 class PortRefCreator :
00040 public ScalarPort::Visitor,
00041 public VectorPort::Visitor,
00042 public PortBundle::Visitor {
00043 public:
00044 void
00045 visit( ScalarPort &port ) throw(Error);
00046
00047 void
00048 visit( VectorPort &port ) throw(Error);
00049
00050 void
00051 visit( PortBundle &port ) throw(Error);
00052
00053 inline _ReturnType
00054 getReturnValue() const throw();
00055
00056 PortRefCreator(
00057 const ObjectFactorySharedPtr &inFactory,
00058 const InstanceSharedPtr &inInstance,
00059 const PortBundleReferenceSharedPtr &inBundle
00060 = PortBundleReferenceSharedPtr() );
00061 ~PortRefCreator() throw();
00062
00063 private:
00064 void
00065 setupCreatedPort( const PortSharedPtr &port,
00066 const PortReferenceSharedPtr &inPortRef ) throw(Error);
00067
00068 ObjectFactorySharedPtr mFactory;
00069 InstanceSharedPtr mInstance;
00070 PortBundleReferenceSharedPtr mBundle;
00071 _ReturnType mReturnValue;
00072 };
00073
00074 template<typename _ReturnType>
00075 void
00076 PortRefCreator<_ReturnType>::visit( ScalarPort &port ) throw(Error) {
00077 try
00078 {
00079 ScalarPortReferenceSharedPtr scalarPortRef;
00080 mFactory->create( scalarPortRef );
00081 setupCreatedPort( port.getSharedThis(), scalarPortRef );
00082 }
00083 catch( Error &e )
00084 {
00085 e.setCurrentLocation( __FUNCTION__, __FILE__, __LINE__ );
00086 throw;
00087 }
00088 }
00089
00090 template<typename _ReturnType>
00091 void
00092 PortRefCreator<_ReturnType>::visit( VectorPort &port ) throw(Error) {
00093 try
00094 {
00095 VectorPortReferenceSharedPtr vectorPortRef;
00096 mFactory->create( vectorPortRef );
00097 std::vector< size_t > limits;
00098 port.getLimits( limits );
00099 vectorPortRef->constructChildren( mFactory, limits );
00100 setupCreatedPort( port.getSharedThis(), vectorPortRef );
00101 }
00102 catch( Error &e )
00103 {
00104 e.setCurrentLocation( __FUNCTION__, __FILE__, __LINE__ );
00105 throw;
00106 }
00107 }
00108
00109 template<typename _ReturnType>
00110 void
00111 PortRefCreator<_ReturnType>::visit( PortBundle &port ) throw(Error) {
00112 try
00113 {
00114 PortBundleReferenceSharedPtr portBundleRef;
00115 mFactory->create( portBundleRef );
00116 PortRefCreator<PortReferenceSharedPtr> creator(
00117 mFactory, mInstance, portBundleRef );
00118 VisitorApplier< PortRefCreator<PortReferenceSharedPtr> >
00119 applier( creator );
00120 port.applyOnAllChildren( applier );
00121 setupCreatedPort( port.getSharedThis(), portBundleRef );
00122 mReturnValue = portBundleRef;
00123 }
00124 catch( Error &e )
00125 {
00126 e.setCurrentLocation( __FUNCTION__, __FILE__, __LINE__ );
00127 throw;
00128 }
00129 }
00130
00131 template<typename _ReturnType>
00132 inline _ReturnType
00133 PortRefCreator<_ReturnType>::getReturnValue() const throw() {
00134 return mReturnValue;
00135 }
00136
00137 template<typename _ReturnType>
00138 PortRefCreator<_ReturnType>::PortRefCreator(
00139 const ObjectFactorySharedPtr &inFactory,
00140 const InstanceSharedPtr &inInstance,
00141 const PortBundleReferenceSharedPtr &inBundle )
00142 :mFactory( inFactory ),
00143 mInstance( inInstance ),
00144 mBundle( inBundle ),
00145 mReturnValue() {
00146 }
00147
00148 template<typename _ReturnType>
00149 PortRefCreator<_ReturnType>::~PortRefCreator() throw() {
00150 }
00151
00152 template<typename _ReturnType>
00153 void
00154 PortRefCreator<_ReturnType>::setupCreatedPort( const PortSharedPtr &port,
00155 const PortReferenceSharedPtr &inPortRef ) throw(Error) {
00156 try
00157 {
00158 inPortRef->bindToMasterPort( port );
00159 if( mBundle )
00160 {
00161 inPortRef->setParentCollection( mBundle );
00162 mBundle->addChild( inPortRef );
00163 }
00164 else
00165 {
00166 inPortRef->setParent( mInstance );
00167 mInstance->addPortReference( inPortRef );
00168 }
00169 }
00170 catch( Error &e )
00171 {
00172 e.setCurrentLocation( __FUNCTION__, __FILE__, __LINE__ );
00173 throw;
00174 }
00175 }
00176
00177 }
00178
00179 }
00180 #endif // TORC_GENERIC_OM_PORTREFCREATOR_HPP