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                 CLAM_ASSERT( SameType(GetTypeId(),out.GetTypeId()),
00075                 "OutPortPublisher<Token>::PublishOutPort coudn't connect to outPort "
00076                 "because was not templatized by the same Token type as OutPortPublisher" );
00077 
00078                 CLAM_ASSERT( not out.IsPublisher(),
00079                 "OutPortPublisher<Token>::PublishOutPort: can not publish a publisher (yet)");
00080 
00081                 ConcretePublishOutPort( static_cast<ProperOutPort&>(out) );
00082 
00083         }
00084         void UnpublishOutPort()
00085         {
00086                 mPublishedOutPort = 0;
00087         }
00088         bool IsPublisher() const
00089         {
00090                 return true;
00091 
00092         }
00093         void ConcretePublishOutPort( ProperOutPort & out )
00094         {
00095                 mPublishedOutPort = &out;
00096                 out.SetPublisher( *this );
00097         }
00098         
00099         void DisconnectFromIn( InPortBase& in)
00100         {
00101                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::DisconnectFromIn() A published port is missing. "
00102                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00103                 mPublishedOutPort->DisconnectFromIn( in );
00104                 mVisuallyConnectedPorts.remove(&in);
00105         }
00106         
00107         bool IsConnectableTo(InPortBase & in)
00108         {
00109                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::IsConnectableTo() A published port is missing. "
00110                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00111                 return mPublishedOutPort->IsConnectableTo( in );
00112         }
00113         
00114         bool IsVisuallyConnectedTo(InPortBase & in)
00115         {
00116                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher:IsVisuallyConnectedTo() A published port is missing. "
00117                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00118                 return mPublishedOutPort->IsVisuallyConnectedTo( in );
00119         }
00120         
00121         Token & GetData(int offset=0)
00122         {
00123                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::GetData() A published port is missing. "
00124                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00125                 return mPublishedOutPort->GetData( offset );
00126         }
00127         
00128         int GetSize()
00129         {
00130                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::GetSize() A published port is missing. "
00131                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00132                 return mPublishedOutPort->GetSize();
00133         }
00134         
00135         void SetSize(int newSize)
00136         {
00137                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::SetSize() A published port is missing. "
00138                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00139                 mPublishedOutPort->SetSize( newSize );
00140         }
00141         
00142         int GetHop()
00143         {
00144                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::GetHop() A published port is missing. "
00145                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00146                 return mPublishedOutPort->GetHop();
00147         }
00148         
00149         void SetHop(int newHop)
00150         {
00151                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::SetHop() A published port is missing. "
00152                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00153                 mPublishedOutPort->SetHop( newHop );
00154         }
00155 
00156         
00157         bool CanProduce()
00158         {
00159                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::CanProduce() A published port is missing. "
00160                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00161                 return mPublishedOutPort->CanProduce();
00162         }       
00163         
00164         void CenterEvenRegions()
00165         {
00166                 CLAM_ASSERT(mPublishedOutPort, "OutPortPublisher::CenterEvenRegions() A published port is missing. "
00167                                 "Consider using the method PublishOutPort( OutPortBase& out) ");
00168                 mPublishedOutPort->CenterEvenRegions();
00169         }
00170         Token & GetLastWrittenData( int offset = 0 )
00171         {
00172                 return mPublishedOutPort->GetLastWrittenData(offset);
00173         }
00174 
00175         virtual const std::type_info & GetTypeId() const 
00176         {
00177                 return typeid(Token);
00178         };
00179 
00180         
00181 protected:
00182         ProperOutPort * mPublishedOutPort;
00183 };
00184 
00185 
00186 } // namespace CLAM
00187 
00188 #endif // __OutPortPublisher_hxx__
00189 
Generated by  doxygen 1.6.3