OutPortPublisher.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef __OutPortPublisher_hxx__
00023 #define __OutPortPublisher_hxx__
00024 
00025 #include "OutPort.hxx"
00026 
00027 namespace CLAM
00028 {
00029 
00030 template< typename Token >
00031 class OutPortPublisher : public OutPortBase
00032 {
00033         typedef OutPort<Token> ProperOutPort;
00034 public:
00035         OutPortPublisher( const std::string & name = "unnamed out port publisher", Processing * proc = 0 )
00036                 : OutPortBase( name, proc ), mPublishedOutPort(0)
00037         {
00038                 
00039         }
00040 
00041         virtual ~OutPortPublisher()
00042         {
00043                 //disconnect published port
00044                 if (mPublishedOutPort) 
00045                 {
00046                         mPublishedOutPort->UnsetPublisher();
00047                         mPublishedOutPort->DisconnectFromAll();
00048                         mPublishedOutPort = 0;
00049                 }
00050                 //disconnect visually connected inports
00051                 for (InPortsList::iterator it = BeginVisuallyConnectedInPorts();
00052                         it != EndVisuallyConnectedInPorts();
00053                         it++ )
00054                 (*it)->SetVisuallyConnectedOutPort(0);
00055                 
00056         }
00057 
00058         void DisconnectFromAll()
00059         {
00060                 CLAM_DEBUG_ASSERT( mPublishedOutPort != 0, "OutPortPublisher - no out port published" );
00061                 mPublishedOutPort->DisconnectFromAll();
00062                 mVisuallyConnectedPorts.clear();
00063         }
00064         void ConnectToIn( InPortBase& in)
00065         {
00066                 CLAM_DEBUG_ASSERT( mPublishedOutPort != 0, "OutPortPublisher - no out port published" );
00067                 mPublishedOutPort->ConnectToIn( in );
00068                 in.SetVisuallyConnectedOutPort( this );
00069                 mVisuallyConnectedPorts.push_back(&in);
00070         }
00071         
00072         void PublishOutPort( OutPortBase & out )
00073         {
00074                 try
00075                 {
00076                         ConcretePublishOutPort( dynamic_cast<ProperOutPort&>(out) );
00077                 } catch (...) // could be std::bad_cast ?
00078                 {
00079                         CLAM_ASSERT( false,
00080                         "OutPortPublisher<Token>::PublishOutPort coudn't connect to outPort "
00081                         "because was not templatized by the same Token type as OutPortPublisher" );
00082                 }
00083 
00084         }
00085         void UnpublishOutPort()
00086         {
00087                 mPublishedOutPort = 0;
00088         }
00089         void ConcretePublishOutPort( ProperOutPort & out )
00090         {
00091                 mPublishedOutPort = &out;
00092                 out.SetPublisher( *this );
00093         }
00094         
00095         void DisconnectFromIn( InPortBase& in)
00096         {
00097                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::DisconnectFromIn() A published port is missing. "
00098                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00099                 mPublishedOutPort->DisconnectFromIn( in );
00100                 mVisuallyConnectedPorts.remove(&in);
00101         }
00102         
00103         bool IsConnectableTo(InPortBase & in)
00104         {
00105                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::IsConnectableTo() A published port is missing. "
00106                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00107                 return mPublishedOutPort->IsConnectableTo( in );
00108         }
00109         
00110         bool IsVisuallyConnectedTo(InPortBase & in)
00111         {
00112                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher:IsVisuallyConnectedTo() A published port is missing. "
00113                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00114                 return mPublishedOutPort->IsVisuallyConnectedTo( in );
00115         }
00116         
00117         Token & GetData(int offset=0)
00118         {
00119                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::GetData() A published port is missing. "
00120                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00121                 return mPublishedOutPort->GetData( offset );
00122         }
00123         
00124         int GetSize()
00125         {
00126                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::GetSize() A published port is missing. "
00127                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00128                 return mPublishedOutPort->GetSize();
00129         }
00130         
00131         void SetSize(int newSize)
00132         {
00133                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::SetSize() A published port is missing. "
00134                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00135                 mPublishedOutPort->SetSize( newSize );
00136         }
00137         
00138         int GetHop()
00139         {
00140                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::GetHop() A published port is missing. "
00141                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00142                 return mPublishedOutPort->GetHop();
00143         }
00144         
00145         void SetHop(int newHop)
00146         {
00147                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::SetHop() A published port is missing. "
00148                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00149                 mPublishedOutPort->SetHop( newHop );
00150         }
00151 
00152         
00153         bool CanProduce()
00154         {
00155                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::CanProduce() A published port is missing. "
00156                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00157                 return mPublishedOutPort->CanProduce();
00158         }       
00159         
00160         void CenterEvenRegions()
00161         {
00162                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::CenterEvenRegions() A published port is missing. "
00163                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00164                 mPublishedOutPort->CenterEvenRegions();
00165         }
00166         Token & GetLastWrittenData( int offset = 0 )
00167         {
00168                 return OutPort<Token>::GetLastWrittenData( *mPublishedOutPort, offset );
00169         }
00170 
00171         static Token & GetLastWrittenData( OutPortBase &, int offset = 0);
00172         virtual const std::type_info & GetTypeId() const 
00173         {
00174                 return typeid(Token);
00175         };
00176 
00177         
00178 protected:
00179         ProperOutPort * mPublishedOutPort;
00180 };
00181 
00182 template<class Token>
00183 Token & OutPortPublisher<Token>::GetLastWrittenData( OutPortBase & out, int offset )
00184 {
00185         try
00186         {
00187                 OutPortPublisher<Token>& concreteOut = dynamic_cast< OutPortPublisher<Token>& >(out);
00188                 return concreteOut.GetLastWrittenData( offset );
00189         }
00190         catch(...)
00191         {
00192                 CLAM_ASSERT( false, "OutPortPublisher<Token>::DumpDataWithLastToken - Passed an outport of wrong type" );
00193         }
00194         return *(Token *)NULL;
00195                 
00196 }
00197 
00198 
00199 
00200 
00201 } // namespace CLAM
00202 
00203 #endif // __OutPortPublisher_hxx__
00204 

Generated on Tue Aug 12 22:33:43 2008 for CLAM by  doxygen 1.5.5