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 ReadingRegion_hxx 00023 #define ReadingRegion_hxx 00024 00025 #include "Region.hxx" 00026 #include "StreamImpl.hxx" 00027 00028 namespace CLAM 00029 { 00030 00031 template<class WritingRegion> 00032 class ReadingRegion : public Region 00033 { 00034 typedef typename WritingRegion::ProperStream ProperStream; 00035 typedef typename WritingRegion::ProperToken ProperToken; 00036 00037 public: 00038 ReadingRegion(); 00039 ~ReadingRegion(); 00040 00041 void LinkAndNotifySizeToStream( ProperStream& stream ); 00042 ProperStream& Stream(); 00046 bool CanConsume(); 00052 void Consume(); 00053 void LinkProducerRegion( Region& writing); 00054 00055 // const?? TODO 00056 ProperToken& operator[](int offset); 00057 00058 Region* ProducerRegion(); 00059 void RemoveProducer(); 00060 00061 ReadingRegionsIterator BeginReaders(); 00062 ReadingRegionsIterator EndReaders(); 00063 00064 private: 00065 void SizeChanged(const int & newSize); 00066 00067 ProperStream* mAttachedStream; 00068 Region* mProducingRegion; 00069 }; 00070 00072 00073 template<class WritingRegion> 00074 ReadingRegion<WritingRegion>::ReadingRegion() 00075 : mAttachedStream(0), mProducingRegion(0) 00076 { 00077 } 00078 00079 template<class WritingRegion> 00080 ReadingRegion<WritingRegion>::~ReadingRegion() 00081 { 00082 if(mProducingRegion) 00083 mProducingRegion->RemoveRegion( *this ); 00084 } 00085 00086 template<class WritingRegion> 00087 void ReadingRegion<WritingRegion>::LinkAndNotifySizeToStream( ProperStream& stream ) 00088 { 00089 mAttachedStream = &stream; 00090 mAttachedStream->NewReadingRegionSize( *this ); 00091 } 00092 00093 template<class WritingRegion> 00094 typename ReadingRegion<WritingRegion>::ProperStream & ReadingRegion<WritingRegion>::Stream() 00095 { 00096 return *(mAttachedStream); 00097 } 00098 00099 template<class WritingRegion> 00100 bool ReadingRegion<WritingRegion>::CanConsume() 00101 { 00102 if(!mProducingRegion) 00103 return false; 00104 return ProducerRegion()->Pos() >= Pos()+Size(); 00105 } 00106 00107 template<class WritingRegion> 00108 void ReadingRegion<WritingRegion>::Consume() 00109 { 00110 CLAM_DEBUG_ASSERT( CanConsume(), "ReadingRegion::Consume() - region can't consume" ); 00111 Pos( Pos() + Hop() ); 00112 mAttachedStream->ReaderHasAdvanced( *this ); 00113 } 00114 00115 template<class WritingRegion> 00116 void ReadingRegion<WritingRegion>::LinkProducerRegion( Region& writing) 00117 { 00118 mProducingRegion = &writing; 00119 // it starts at the same position than the writer is in this exact moment 00120 Pos( writing.Pos() ); 00121 BeginDistance( writing.BeginDistance() ); 00122 } 00123 00124 template<class WritingRegion> 00125 typename ReadingRegion<WritingRegion>::ProperToken & ReadingRegion<WritingRegion>::operator[](int offset) 00126 { 00127 CLAM_DEBUG_ASSERT( mAttachedStream, "ReadingRegion operator[] - No attached stream" ); 00128 CLAM_DEBUG_ASSERT( 0 <= offset && offset < Size(), "ReadingRegion operator[] - Index out of bounds" ); 00129 CLAM_DEBUG_ASSERT( CanConsume(), "ReadingRegion operator[] - region can't consume" ); 00130 00131 int physicalIndex = BeginDistance() + offset; 00132 00133 return mAttachedStream->operator[](physicalIndex); 00134 } 00135 00136 template<class WritingRegion> 00137 Region* ReadingRegion<WritingRegion>::ProducerRegion() 00138 { 00139 return mProducingRegion; 00140 } 00141 00142 template<class WritingRegion> 00143 void ReadingRegion<WritingRegion>::RemoveProducer() 00144 { 00145 mProducingRegion = 0; 00146 mAttachedStream = 0; 00147 } 00148 00149 template<class WritingRegion> 00150 typename ReadingRegion<WritingRegion>::ReadingRegionsIterator ReadingRegion<WritingRegion>::BeginReaders() 00151 { 00152 throw 0; 00153 } 00154 00155 template<class WritingRegion> 00156 typename ReadingRegion<WritingRegion>::ReadingRegionsIterator ReadingRegion<WritingRegion>::EndReaders() 00157 { 00158 throw 0; 00159 } 00160 00161 template<class WritingRegion> 00162 void ReadingRegion<WritingRegion>::SizeChanged(const int & newSize) 00163 { 00164 if (mAttachedStream) 00165 mAttachedStream->NewReadingRegionSize(*this); 00166 } 00167 00168 } // namespace CLAM 00169 00170 #endif // __ReadingRegion_hxx__ 00171