CLAM-Development  1.4.0
ReadingRegion.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef ReadingRegion_hxx
23 #define ReadingRegion_hxx
24 
25 #include "Region.hxx"
26 #include "StreamImpl.hxx"
27 
28 namespace CLAM
29 {
30 
31 template<class WritingRegion>
32 class ReadingRegion : public Region
33 {
34  typedef typename WritingRegion::ProperStream ProperStream;
35  typedef typename WritingRegion::ProperToken ProperToken;
36 
37 public:
38  ReadingRegion();
40 
41  void LinkAndNotifySizeToStream( ProperStream& stream );
42  ProperStream& Stream();
46  bool CanConsume();
52  void Consume();
53  void LinkProducerRegion( Region& writing);
54 
55  // const?? TODO
56  ProperToken& operator[](int offset);
57 
59  void RemoveProducer();
60 
63 
64 private:
65  void SizeChanged(const int & newSize);
66 
67  ProperStream* mAttachedStream;
68  Region* mProducingRegion;
69 };
70 
72 
73 template<class WritingRegion>
75  : mAttachedStream(0), mProducingRegion(0)
76 {
77 }
78 
79 template<class WritingRegion>
81 {
82  if(mProducingRegion)
83  mProducingRegion->RemoveRegion( *this );
84 }
85 
86 template<class WritingRegion>
88 {
89  mAttachedStream = &stream;
90  mAttachedStream->NewReadingRegionSize( *this );
91 }
92 
93 template<class WritingRegion>
94 typename ReadingRegion<WritingRegion>::ProperStream & ReadingRegion<WritingRegion>::Stream()
95 {
96  return *(mAttachedStream);
97 }
98 
99 template<class WritingRegion>
101 {
102  if(!mProducingRegion)
103  return false;
104  return ProducerRegion()->Pos() >= Pos()+Size();
105 }
106 
107 template<class WritingRegion>
109 {
110  CLAM_DEBUG_ASSERT( CanConsume(), "ReadingRegion::Consume() - region can't consume" );
111  Pos( Pos() + Hop() );
112  mAttachedStream->ReaderHasAdvanced( *this );
113 }
114 
115 template<class WritingRegion>
117 {
118  mProducingRegion = &writing;
119  // it starts at the same position than the writer is in this exact moment
120  Pos( writing.Pos() );
121  BeginDistance( writing.BeginDistance() );
122 }
123 
124 template<class WritingRegion>
125 typename ReadingRegion<WritingRegion>::ProperToken & ReadingRegion<WritingRegion>::operator[](int offset)
126 {
127  CLAM_DEBUG_ASSERT( mAttachedStream, "ReadingRegion operator[] - No attached stream" );
128  CLAM_DEBUG_ASSERT( 0 <= offset && offset < Size(), "ReadingRegion operator[] - Index out of bounds" );
129  CLAM_DEBUG_ASSERT( CanConsume(), "ReadingRegion operator[] - region can't consume" );
130 
131  int physicalIndex = BeginDistance() + offset;
132 
133  return mAttachedStream->operator[](physicalIndex);
134 }
135 
136 template<class WritingRegion>
138 {
139  return mProducingRegion;
140 }
141 
142  template<class WritingRegion>
144 {
145  mProducingRegion = 0;
146  mAttachedStream = 0;
147 }
148 
149 template<class WritingRegion>
151 {
152  throw 0;
153 }
154 
155 template<class WritingRegion>
157 {
158  throw 0;
159 }
160 
161 template<class WritingRegion>
162 void ReadingRegion<WritingRegion>::SizeChanged(const int & newSize)
163 {
164  if (mAttachedStream)
165  mAttachedStream->NewReadingRegionSize(*this);
166 }
167 
168 } // namespace CLAM
169 
170 #endif // __ReadingRegion_hxx__
171