]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Ports.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Ports.cxx
diff --git a/lib/cpPlugins/Ports.cxx b/lib/cpPlugins/Ports.cxx
new file mode 100644 (file)
index 0000000..b16de91
--- /dev/null
@@ -0,0 +1,223 @@
+// =========================================================================
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// =========================================================================
+
+#include <cpPlugins/Ports.h>
+#include <cpPlugins/DataObject.h>
+#include <cpPlugins/Functor.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::BasePort::
+BasePort( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::BasePort::
+~BasePort( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::InputPort::
+InputPort( )
+  : Superclass( ),
+    m_Required( false ),
+    m_Multiple( false )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::InputPort::
+~InputPort( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::DataObject* cpPlugins::InputPort::
+Get( const unsigned int& i )
+{
+  if( i < this->m_Data.size( ) )
+  {
+    if( this->m_Data[ i ] )
+      return( this->m_Data[ i ].get( ) );
+    else
+      return( NULL );
+  }
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::DataObject* cpPlugins::InputPort::
+Get( const unsigned int& i ) const
+{
+  if( i < this->m_Data.size( ) )
+  {
+    if( this->m_Data[ i ] )
+      return( this->m_Data[ i ].get( ) );
+    else
+      return( NULL );
+  }
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::InputPort::
+Count( ) const
+{
+  return( this->m_Data.size( ) );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::InputPort::
+IsValid( ) const
+{
+  if( this->m_Required )
+  {
+    if( this->m_Data.size( ) > 0 )
+      return( bool( this->m_Data[ 0 ] ) );
+    else
+      return( false );
+  }
+  else
+    return( true );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::InputPort::
+Set( cpPlugins::DataObject* data )
+{
+  if( this->m_Multiple || this->m_Template.get( ) != NULL )
+  {
+    if( data->IsCompatible( this->m_Template.get( ) ) )
+    {
+      this->m_Data.push_back(
+        data->CastSharedPtr< cpPlugins::DataObject >( )
+        );
+      return( true );
+    }
+    else
+      return( false );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::InputPort::
+Update( )
+{
+  bool is_updated = true;
+  unsigned int N = 0;
+  for( SharedPtr& d: this->m_Data )
+  {
+    if( d )
+    {
+      N++;
+      if( !( d->IsUpdated( ) ) )
+      {
+        d->Update( );
+        is_updated = false;
+
+      } // end if
+    } // end if
+  } // end for
+  if( this->m_Required && N == 0 )
+    cpPluginsErrorMacro( << "Input is needed, but not yet assigned." );
+  return( is_updated );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::OutputPort::
+OutputPort( )
+  : Superclass( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::OutputPort::
+~OutputPort( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::DataObject* cpPlugins::OutputPort::
+Get( )
+{
+  if( this->m_Data )
+    return( this->m_Data.get( ) );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::DataObject* cpPlugins::OutputPort::
+Get( ) const
+{
+  if( this->m_Data )
+    return( this->m_Data.get( ) );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::FunctorPort::
+FunctorPort( )
+  : Superclass( ),
+    m_Required( false )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::FunctorPort::
+~FunctorPort( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Functor* cpPlugins::FunctorPort::
+Get( )
+{
+  if( this->m_Functor )
+    return( this->m_Functor.get( ) );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Functor* cpPlugins::FunctorPort::
+Get( ) const
+{
+  if( this->m_Functor )
+    return( this->m_Functor.get( ) );
+  else
+    return( NULL );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::FunctorPort::
+Set( cpPlugins::Functor* f )
+{
+  if( f != NULL )
+    this->m_Functor = f->CastSharedPtr< cpPlugins::Functor >( );
+  else
+    this->m_Functor.reset( );
+  return( true );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::FunctorPort::
+Update( )
+{
+  if( this->m_Functor )
+  {
+    if( !( this->m_Functor->IsUpdated( ) ) )
+      this->m_Functor->Update( );
+    return( true );
+  } // end if
+  return( false );
+}
+
+// eof - $RCSfile$