CLAM-Development  1.4.0
InPort.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 __InPort_hxx__
23 #define __InPort_hxx__
24 
25 #include "WritingRegion.hxx"
26 #include "ReadingRegion.hxx"
27 #include <string>
28 #include <typeinfo>
29 
30 namespace CLAM
31 {
32 
33 class OutPortBase;
34 class Processing;
35 
37 {
38 public:
39  InPortBase( const std::string & name = "unnamed in port", Processing * proc = 0 );
40  virtual ~InPortBase();
43  const std::string & GetName();
44  bool HasProcessing();
46  virtual bool CanConsume()=0;
47  virtual int GetSize()=0;
48  virtual void SetSize(int newSize)=0;
49  virtual int GetHop()=0;
50  virtual void SetHop(int newHop)=0;
51  virtual void UnAttachRegion()=0;
52  void Disconnect();
53  virtual bool IsPublisherOf( InPortBase& ) const { return false; }
54  virtual bool IsPublisher() const { return false; }
55  virtual const std::type_info& GetTypeId() const = 0;
56 protected:
58  std::string mName;
60 };
61 
62 
63 template<typename Token>
64 class InPort : public InPortBase
65 {
68 
69 public:
70  InPort( const std::string & name = "unnamed in port", Processing * proc = 0 );
71  virtual ~InPort();
72 
73  // XR: BIG TODO: make this method const!
74  /*const*/ Token & GetData(int offset=0);
75 
76 
77  void SetSize( int newSize );
78  int GetSize();
79  int GetHop();
80  void SetHop( int hop );
81  void Consume();
82  bool CanConsume();
83 
93  void UnAttachRegion();
94  virtual const std::type_info & GetTypeId() const
95  {
96  return typeid(Token);
97  };
98 protected:
99 
100  ProperReadingRegion mRegion;
101 };
102 
103 
105 
106 template<class Token>
107 InPort<Token>::InPort( const std::string & name, Processing * proc )
108  : InPortBase( name,proc )
109 {
110 }
111 
112 template<class Token>
114 {
115  if(mVisuallyConnectedOutPort)
116  Disconnect();
117 }
118 
119 template<class Token>
120 // XR BIG TODO: make this method const!
121 /*const*/ Token & InPort<Token>::GetData( int offset )
122 {
123  return mRegion[offset];
124 }
125 
126 template<class Token>
127 void InPort<Token>::SetSize( int newSize )
128 {
129  mRegion.Size( newSize );
130 }
131 
132 template<class Token>
134 {
135  return mRegion.Size();
136 }
137 
138 template<class Token>
140 {
141  return mRegion.Hop();
142 }
143 
144 template<class Token>
145 void InPort<Token>::SetHop( int hop )
146 {
147  mRegion.Hop(hop);
148 }
149 
150 template<class Token>
152 {
153  mRegion.Consume();
154 }
155 
156 template<class Token>
158 {
159  return mRegion.CanConsume();
160 }
161 
162 template<class Token>
164 {
165  writer.LinkRegions( mRegion );
166  mVisuallyConnectedOutPort = out;
167 }
168 
169 template<class Token>
171 {
172  CLAM_DEBUG_ASSERT( mVisuallyConnectedOutPort, "InPort<T>::UnAttachRegion() - InPort is not connected" );
173  mRegion.ProducerRegion()->RemoveRegion( mRegion );
174  mVisuallyConnectedOutPort = 0;
175 }
176 
177 } // namespace CLAM
178 
179 #endif // __InPort_hxx__
180