]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Ports.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Ports.cxx
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 #include <cpPlugins/Ports.h>
6 #include <cpPlugins/DataObject.h>
7 #include <cpPlugins/Functor.h>
8
9 // -------------------------------------------------------------------------
10 cpPlugins::BasePort::
11 BasePort( )
12 {
13 }
14
15 // -------------------------------------------------------------------------
16 cpPlugins::BasePort::
17 ~BasePort( )
18 {
19 }
20
21 // -------------------------------------------------------------------------
22 cpPlugins::InputPort::
23 InputPort( )
24   : Superclass( ),
25     m_Required( false ),
26     m_Multiple( false )
27 {
28 }
29
30 // -------------------------------------------------------------------------
31 cpPlugins::InputPort::
32 ~InputPort( )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 cpPlugins::DataObject* cpPlugins::InputPort::
38 Get( const unsigned int& i )
39 {
40   if( i < this->m_Data.size( ) )
41   {
42     if( this->m_Data[ i ] )
43       return( this->m_Data[ i ].get( ) );
44     else
45       return( NULL );
46   }
47   else
48     return( NULL );
49 }
50
51 // -------------------------------------------------------------------------
52 const cpPlugins::DataObject* cpPlugins::InputPort::
53 Get( const unsigned int& i ) const
54 {
55   if( i < this->m_Data.size( ) )
56   {
57     if( this->m_Data[ i ] )
58       return( this->m_Data[ i ].get( ) );
59     else
60       return( NULL );
61   }
62   else
63     return( NULL );
64 }
65
66 // -------------------------------------------------------------------------
67 unsigned int cpPlugins::InputPort::
68 Count( ) const
69 {
70   return( this->m_Data.size( ) );
71 }
72
73 // -------------------------------------------------------------------------
74 bool cpPlugins::InputPort::
75 IsValid( ) const
76 {
77   if( this->m_Required )
78   {
79     if( this->m_Data.size( ) > 0 )
80       return( bool( this->m_Data[ 0 ] ) );
81     else
82       return( false );
83   }
84   else
85     return( true );
86 }
87
88 // -------------------------------------------------------------------------
89 bool cpPlugins::InputPort::
90 Set( cpPlugins::DataObject* data )
91 {
92   if( this->m_Multiple || this->m_Template.get( ) != NULL )
93   {
94     if( data->IsCompatible( this->m_Template.get( ) ) )
95     {
96       this->m_Data.push_back(
97         data->CastSharedPtr< cpPlugins::DataObject >( )
98         );
99       return( true );
100     }
101     else
102       return( false );
103   }
104   else
105     return( false );
106 }
107
108 // -------------------------------------------------------------------------
109 bool cpPlugins::InputPort::
110 Update( )
111 {
112   bool is_updated = true;
113   unsigned int N = 0;
114   for( SharedPtr& d: this->m_Data )
115   {
116     if( d )
117     {
118       N++;
119       if( !( d->IsUpdated( ) ) )
120       {
121         d->Update( );
122         is_updated = false;
123
124       } // end if
125     } // end if
126   } // end for
127   if( this->m_Required && N == 0 )
128     cpPluginsErrorMacro( << "Input is needed, but not yet assigned." );
129   return( is_updated );
130 }
131
132 // -------------------------------------------------------------------------
133 cpPlugins::OutputPort::
134 OutputPort( )
135   : Superclass( )
136 {
137 }
138
139 // -------------------------------------------------------------------------
140 cpPlugins::OutputPort::
141 ~OutputPort( )
142 {
143 }
144
145 // -------------------------------------------------------------------------
146 cpPlugins::DataObject* cpPlugins::OutputPort::
147 Get( )
148 {
149   if( this->m_Data )
150     return( this->m_Data.get( ) );
151   else
152     return( NULL );
153 }
154
155 // -------------------------------------------------------------------------
156 const cpPlugins::DataObject* cpPlugins::OutputPort::
157 Get( ) const
158 {
159   if( this->m_Data )
160     return( this->m_Data.get( ) );
161   else
162     return( NULL );
163 }
164
165 // -------------------------------------------------------------------------
166 cpPlugins::FunctorPort::
167 FunctorPort( )
168   : Superclass( ),
169     m_Required( false )
170 {
171 }
172
173 // -------------------------------------------------------------------------
174 cpPlugins::FunctorPort::
175 ~FunctorPort( )
176 {
177 }
178
179 // -------------------------------------------------------------------------
180 cpPlugins::Functor* cpPlugins::FunctorPort::
181 Get( )
182 {
183   if( this->m_Functor )
184     return( this->m_Functor.get( ) );
185   else
186     return( NULL );
187 }
188
189 // -------------------------------------------------------------------------
190 const cpPlugins::Functor* cpPlugins::FunctorPort::
191 Get( ) const
192 {
193   if( this->m_Functor )
194     return( this->m_Functor.get( ) );
195   else
196     return( NULL );
197 }
198
199 // -------------------------------------------------------------------------
200 bool cpPlugins::FunctorPort::
201 Set( cpPlugins::Functor* f )
202 {
203   if( f != NULL )
204     this->m_Functor = f->CastSharedPtr< cpPlugins::Functor >( );
205   else
206     this->m_Functor.reset( );
207   return( true );
208 }
209
210 // -------------------------------------------------------------------------
211 bool cpPlugins::FunctorPort::
212 Update( )
213 {
214   if( this->m_Functor )
215   {
216     if( !( this->m_Functor->IsUpdated( ) ) )
217       this->m_Functor->Update( );
218     return( true );
219   } // end if
220   return( false );
221 }
222
223 // eof - $RCSfile$