InPort.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 __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& ) const { return false; }
00054         virtual bool IsPublisher() const  { return false; }
00055         virtual const std::type_info& GetTypeId() const = 0;
00056 protected:
00057         OutPortBase * mVisuallyConnectedOutPort;
00058         std::string mName;
00059         Processing * mProcessing;
00060 };
00061 
00062 
00063 template<typename Token>
00064 class InPort : public InPortBase
00065 {
00066         typedef WritingRegion<Token> ProperWritingRegion;
00067         typedef typename ProperWritingRegion::ProperReadingRegion ProperReadingRegion;
00068 
00069 public:
00070         InPort( const std::string & name = "unnamed in port", Processing * proc = 0 );
00071         virtual ~InPort();
00072 
00073         // XR: BIG TODO: make this method const!
00074         /*const*/ Token & GetData(int offset=0);
00075         
00076         
00077         void SetSize( int newSize );
00078         int GetSize();
00079         int GetHop();
00080         void SetHop( int hop );
00081         void Consume();
00082         bool CanConsume();
00083 
00088         void AttachRegionToOutPort( OutPortBase * out, ProperWritingRegion & writer );
00093         void UnAttachRegion();
00094         virtual const std::type_info & GetTypeId() const 
00095         {
00096                 return typeid(Token);
00097         };
00098 protected:
00099 
00100         ProperReadingRegion mRegion;
00101 };
00102 
00103 
00105 
00106 template<class Token>
00107 InPort<Token>::InPort( const std::string & name, Processing * proc )
00108         : InPortBase( name,proc )
00109 {
00110 }
00111 
00112 template<class Token> 
00113 InPort<Token>::~InPort()
00114 {
00115         if(mVisuallyConnectedOutPort)
00116                 Disconnect();
00117 }
00118 
00119 template<class Token>
00120 // XR BIG TODO: make this method const!
00121 /*const*/ Token & InPort<Token>::GetData( int offset )
00122 {
00123         return mRegion[offset];
00124 }
00125 
00126 template<class Token>
00127 void InPort<Token>::SetSize( int newSize )
00128 {
00129         mRegion.Size( newSize );
00130 }
00131 
00132 template<class Token>
00133 int InPort<Token>::GetSize()
00134 {
00135         return mRegion.Size();
00136 }
00137 
00138 template<class Token>
00139 int InPort<Token>::GetHop()
00140 {
00141         return mRegion.Hop();
00142 }
00143 
00144 template<class Token>
00145 void InPort<Token>::SetHop( int hop )
00146 {
00147         mRegion.Hop(hop);
00148 }
00149 
00150 template<class Token>
00151 void InPort<Token>::Consume() 
00152 {
00153         mRegion.Consume();
00154 }
00155 
00156 template<class Token>
00157 bool InPort<Token>::CanConsume()
00158 {
00159         return mRegion.CanConsume();
00160 }
00161 
00162 template<class Token>
00163 void InPort<Token>::AttachRegionToOutPort( OutPortBase * out, ProperWritingRegion & writer )
00164 {
00165         writer.LinkRegions( mRegion );
00166         mVisuallyConnectedOutPort = out;
00167 }
00168 
00169 template<class Token>
00170 void InPort<Token>::UnAttachRegion()
00171 {
00172         CLAM_DEBUG_ASSERT( mVisuallyConnectedOutPort, "InPort<T>::UnAttachRegion() - InPort is not connected" );
00173         mRegion.ProducerRegion()->RemoveRegion( mRegion );
00174         mVisuallyConnectedOutPort = 0;
00175 }
00176 
00177 } // namespace CLAM
00178 
00179 #endif // __InPort_hxx__
00180 
Generated by  doxygen 1.6.3