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 __InPortPublisher_hxx__ 00023 #define __InPortPublisher_hxx__ 00024 00025 #include "InPort.hxx" 00026 #include <typeinfo> 00027 00028 namespace CLAM 00029 { 00030 00031 template<typename Token> 00032 class InPortPublisher : public InPortBase 00033 { 00034 typedef InPort<Token> ProperInPort; 00035 00037 int GetSize() { return 0; } 00039 void SetSize(int newSize) {} 00041 int GetHop() { return 0; } 00043 void SetHop(int newHop) {} 00044 00045 public: 00046 typedef std::list< ProperInPort * > ProperInPortsList; 00047 00048 InPortPublisher( const std::string & name = "unnamed in port", Processing * proc = 0 ) 00049 : InPortBase( name, proc ) 00050 { 00051 } 00052 00053 virtual ~InPortPublisher() 00054 { 00055 if ( GetVisuallyConnectedOutPort() ) 00056 Disconnect(); 00057 } 00058 00059 void PublishInPort( InPortBase & in ) 00060 { 00061 try 00062 { 00063 ConcretePublishInPort( dynamic_cast<ProperInPort&>(in) ); 00064 } catch (...) // could be std::bad_cast ? 00065 { 00066 CLAM_ASSERT( false, 00067 "InPortPublisher<Token>::PublishInPort coudn't connect to outPort " 00068 "because was not templatized by the same Token type as InPortPublisher" ); 00069 } 00070 00071 } 00072 //why not pass InPortBase? still not needed to call from the "generic" interface 00073 void UnPublishInPort( ProperInPort& in ) 00074 { 00075 mPublishedInPortsList.remove(&in); 00076 } 00077 00078 void ConcretePublishInPort( ProperInPort & in ) 00079 { 00080 mPublishedInPortsList.push_back( &in ); 00081 } 00082 00083 00084 00085 bool CanConsume() 00086 { 00087 typename ProperInPortsList::iterator it; 00088 for(it=mPublishedInPortsList.begin(); it!=mPublishedInPortsList.end(); it++) 00089 if(!(*it)->CanConsume()) 00090 return false; 00091 return true; 00092 } 00093 00094 00095 typename ProperInPortsList::iterator BeginPublishedInPortsList() 00096 { 00097 return mPublishedInPortsList.begin(); 00098 } 00099 00100 typename ProperInPortsList::iterator EndPublishedInPortsList() 00101 { 00102 return mPublishedInPortsList.end(); 00103 } 00104 00106 void UnAttachRegion() 00107 { 00108 SetVisuallyConnectedOutPort( 0 ); 00109 typename ProperInPortsList::iterator it; 00110 for(it=mPublishedInPortsList.begin(); it!=mPublishedInPortsList.end(); it++) 00111 { 00112 (*it)->UnAttachRegion(); 00113 } 00114 } 00115 00116 bool IsPublisherOf( InPortBase& in) 00117 { 00118 // BIG TODO: go in-depth (search for publisher-publiser-inport) 00119 typename ProperInPortsList::iterator it; 00120 for(it=mPublishedInPortsList.begin(); it!=mPublishedInPortsList.end(); it++) 00121 { 00122 if( *it == &in) 00123 return true; 00124 } 00125 return false; 00126 } 00127 virtual const std::type_info & GetTypeId() const 00128 { 00129 return typeid(Token); 00130 }; 00131 protected: 00132 00133 ProperInPortsList mPublishedInPortsList; 00134 }; 00135 00136 } // namespace CLAM 00137 00138 #endif // __InPortPublisher_hxx__ 00139