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 __InPort_hxx__ 00023 #define __InPort_hxx__ 00024 00025 #include "WritingRegion.hxx" 00026 #include "ReadingRegion.hxx" 00027 #include <string> 00028 #include <typeinfo> 00029 00030 namespace CLAM 00031 { 00032 00033 class OutPortBase; 00034 class Processing; 00035 00036 class InPortBase 00037 { 00038 public: 00039 InPortBase( const std::string & name = "unnamed in port", Processing * proc = 0 ); 00040 virtual ~InPortBase(); 00041 OutPortBase * GetVisuallyConnectedOutPort(); 00042 void SetVisuallyConnectedOutPort( OutPortBase* ); 00043 const std::string & GetName(); 00044 bool HasProcessing(); 00045 Processing * GetProcessing(); 00046 virtual bool CanConsume()=0; 00047 virtual int GetSize()=0; 00048 virtual void SetSize(int newSize)=0; 00049 virtual int GetHop()=0; 00050 virtual void SetHop(int newHop)=0; 00051 virtual void UnAttachRegion()=0; 00052 void Disconnect(); 00053 virtual bool IsPublisherOf( InPortBase& ) { return false; } 00054 virtual const std::type_info& GetTypeId() const = 0; 00055 protected: 00056 OutPortBase * mVisuallyConnectedOutPort; 00057 std::string mName; 00058 Processing * mProcessing; 00059 }; 00060 00061 00062 template<typename Token> 00063 class InPort : public InPortBase 00064 { 00065 typedef WritingRegion<Token> ProperWritingRegion; 00066 typedef typename ProperWritingRegion::ProperReadingRegion ProperReadingRegion; 00067 00068 public: 00069 InPort( const std::string & name = "unnamed in port", Processing * proc = 0 ); 00070 virtual ~InPort(); 00071 00072 // XR: BIG TODO: make this method const! 00073 /*const*/ Token & GetData(int offset=0); 00074 00075 00076 void SetSize( int newSize ); 00077 int GetSize(); 00078 int GetHop(); 00079 void SetHop( int hop ); 00080 void Consume(); 00081 bool CanConsume(); 00082 00087 void AttachRegionToOutPort( OutPortBase * out, ProperWritingRegion & writer ); 00092 void UnAttachRegion(); 00093 virtual const std::type_info & GetTypeId() const 00094 { 00095 return typeid(Token); 00096 }; 00097 protected: 00098 00099 ProperReadingRegion mRegion; 00100 }; 00101 00102 00104 00105 template<class Token> 00106 InPort<Token>::InPort( const std::string & name, Processing * proc ) 00107 : InPortBase( name,proc ) 00108 { 00109 } 00110 00111 template<class Token> 00112 InPort<Token>::~InPort() 00113 { 00114 if(mVisuallyConnectedOutPort) 00115 Disconnect(); 00116 } 00117 00118 template<class Token> 00119 // XR BIG TODO: make this method const! 00120 /*const*/ Token & InPort<Token>::GetData( int offset ) 00121 { 00122 return mRegion[offset]; 00123 } 00124 00125 template<class Token> 00126 void InPort<Token>::SetSize( int newSize ) 00127 { 00128 mRegion.Size( newSize ); 00129 } 00130 00131 template<class Token> 00132 int InPort<Token>::GetSize() 00133 { 00134 return mRegion.Size(); 00135 } 00136 00137 template<class Token> 00138 int InPort<Token>::GetHop() 00139 { 00140 return mRegion.Hop(); 00141 } 00142 00143 template<class Token> 00144 void InPort<Token>::SetHop( int hop ) 00145 { 00146 mRegion.Hop(hop); 00147 } 00148 00149 template<class Token> 00150 void InPort<Token>::Consume() 00151 { 00152 mRegion.Consume(); 00153 } 00154 00155 template<class Token> 00156 bool InPort<Token>::CanConsume() 00157 { 00158 return mRegion.CanConsume(); 00159 } 00160 00161 template<class Token> 00162 void InPort<Token>::AttachRegionToOutPort( OutPortBase * out, ProperWritingRegion & writer ) 00163 { 00164 writer.LinkRegions( mRegion ); 00165 mVisuallyConnectedOutPort = out; 00166 } 00167 00168 template<class Token> 00169 void InPort<Token>::UnAttachRegion() 00170 { 00171 CLAM_DEBUG_ASSERT( mVisuallyConnectedOutPort, "InPort<T>::UnAttachRegion() - InPort is not connected" ); 00172 mRegion.ProducerRegion()->RemoveRegion( mRegion ); 00173 mVisuallyConnectedOutPort = 0; 00174 } 00175 00176 } // namespace CLAM 00177 00178 #endif // __InPort_hxx__ 00179