]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Parameters.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.cxx
index dd8cece79b4c21699b590c64b2d9512a3f71fc39..38a5f14af0b02729b9298d1290265e586cb20a51 100644 (file)
 #include <cstdlib>
 #include <sstream>
 
+// -------------------------------------------------------------------------
+#define cpPlugins_Interface_Parameters_SetMacro( TYPE )                 \
+  void cpPlugins::Interface::Parameters::                               \
+  SetValueAs##TYPE( const TString& name, const T##TYPE& v )             \
+  {                                                                     \
+    TParameters::iterator pIt = this->m_Parameters.find( name );        \
+    if( pIt == this->m_Parameters.end( ) )                              \
+      return;                                                           \
+    if( pIt->second.first != Self::TYPE )                               \
+      return;                                                           \
+    std::stringstream ss;                                               \
+    ss << v;                                                            \
+    pIt->second.second = ss.str( );                                     \
+  }
+
+cpPlugins_Interface_Parameters_SetMacro( String );
+cpPlugins_Interface_Parameters_SetMacro( Bool );
+cpPlugins_Interface_Parameters_SetMacro( Int );
+cpPlugins_Interface_Parameters_SetMacro( Uint );
+cpPlugins_Interface_Parameters_SetMacro( Real );
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Interface_Parameters_SetArrayMacro( TYPE, ATYPE )     \
+  void cpPlugins::Interface::Parameters::                               \
+  SetValueAs##TYPE( const TString& name, const TUint& n, ... )          \
+  {                                                                     \
+    TParameters::iterator pIt = this->m_Parameters.find( name );        \
+    if( pIt == this->m_Parameters.end( ) )                              \
+      return;                                                           \
+    if( pIt->second.first != Self::TYPE )                               \
+      return;                                                           \
+    va_list v_lst;                                                      \
+    va_start( v_lst, n );                                               \
+    std::stringstream ss;                                               \
+    for( TUint i = 0; i < n; ++i )                                      \
+      ss << va_arg( v_lst, ATYPE ) << ",";                              \
+    va_end( v_lst );                                                    \
+    pIt->second.second = ss.str( );                                     \
+  }
+
+cpPlugins_Interface_Parameters_SetArrayMacro( Index, int );
+cpPlugins_Interface_Parameters_SetArrayMacro( Point, double );
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Interface_Parameters_SetListMacro( TYPE )             \
+  void cpPlugins::Interface::Parameters::                               \
+  AddValueTo##TYPE##List( const TString& name, const T##TYPE& v )       \
+  {                                                                     \
+    TParameters::iterator pIt = this->m_Parameters.find( name );        \
+    if( pIt == this->m_Parameters.end( ) )                              \
+      return;                                                           \
+    if( pIt->second.first != Self::TYPE##List )                         \
+      return;                                                           \
+    std::stringstream ss;                                               \
+    ss << pIt->second.second << v << "#";                               \
+    pIt->second.second = ss.str( );                                     \
+  }
+
+cpPlugins_Interface_Parameters_SetListMacro( String );
+cpPlugins_Interface_Parameters_SetListMacro( Bool );
+cpPlugins_Interface_Parameters_SetListMacro( Int );
+cpPlugins_Interface_Parameters_SetListMacro( Uint );
+cpPlugins_Interface_Parameters_SetListMacro( Real );
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Interface_Parameters_SetArrayListMacro( TYPE, ATYPE ) \
+  void cpPlugins::Interface::Parameters::                               \
+  AddValueTo##TYPE##List( const TString& name, const TUint& n, ... )    \
+  {                                                                     \
+    TParameters::iterator pIt = this->m_Parameters.find( name );        \
+    if( pIt == this->m_Parameters.end( ) )                              \
+      return;                                                           \
+    if( pIt->second.first != Self::TYPE##List )                         \
+      return;                                                           \
+    va_list v_lst;                                                      \
+    va_start( v_lst, n );                                               \
+    std::stringstream ss;                                               \
+    ss << pIt->second.second;                                           \
+    for( TUint i = 0; i < n; ++i )                                      \
+      ss << va_arg( v_lst, ATYPE ) << ",";                              \
+    va_end( v_lst );                                                    \
+    ss << "#";                                                          \
+    pIt->second.second = ss.str( );                                     \
+  }
+
+cpPlugins_Interface_Parameters_SetArrayListMacro( Index, int );
+cpPlugins_Interface_Parameters_SetArrayListMacro( Point, double );
+
+// -------------------------------------------------------------------------
+#define cpPlugins_Interface_Parameters_ClearListMacro( TYPE )           \
+  void cpPlugins::Interface::Parameters::                               \
+  Clear##TYPE##List( const TString& name )                              \
+  {                                                                     \
+    TParameters::iterator pIt = this->m_Parameters.find( name );        \
+    if( pIt == this->m_Parameters.end( ) )                              \
+      return;                                                           \
+    if( pIt->second.first != Self::TYPE##List )                         \
+      return;                                                           \
+    pIt->second.second = "";                                            \
+  }
+
+cpPlugins_Interface_Parameters_ClearListMacro( String );
+cpPlugins_Interface_Parameters_ClearListMacro( Bool );
+cpPlugins_Interface_Parameters_ClearListMacro( Int );
+cpPlugins_Interface_Parameters_ClearListMacro( Uint );
+cpPlugins_Interface_Parameters_ClearListMacro( Real );
+cpPlugins_Interface_Parameters_ClearListMacro( Index );
+cpPlugins_Interface_Parameters_ClearListMacro( Point );
+
 // -------------------------------------------------------------------------
 cpPlugins::Interface::Parameters::
 Parameters( )
 {
-  this->m_Parameters.clear( );
+  this->Clear( );
 }
 
 // -------------------------------------------------------------------------
@@ -21,7 +130,7 @@ Parameters( const Self& other )
 cpPlugins::Interface::Parameters::
 ~Parameters( )
 {
-  this->m_Parameters.clear( );
+  this->Clear( );
 }
 
 // -------------------------------------------------------------------------
@@ -35,194 +144,350 @@ operator=( const Self& other )
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Parameters::
-Configure( const Self::Type& type, const std::string& name )
+Clear( )
 {
-  this->m_Parameters[ name ] = TParameter( type, "" );
+  this->m_Parameters.clear( );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Parameters::
-SetValueAsString( const std::string& name, const std::string& v )
+Configure( const Self::Type& type, const TString& name )
+{
+  this->m_Parameters[ name ] = TParameter( type, "" );
+}
+
+// -------------------------------------------------------------------------
+std::vector< cpPlugins::Interface::Parameters::TString >
+cpPlugins::Interface::Parameters::
+GetParameters( ) const
 {
-  TParameters::iterator pIt = this->m_Parameters.find( name );
+  std::vector< TString > parameters;
+  TParameters::const_iterator pIt = this->m_Parameters.begin( );
+  for( ; pIt != this->m_Parameters.end( ); ++pIt )
+    parameters.push_back( pIt->first );
+  return( parameters );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::Parameters::
+Type cpPlugins::Interface::Parameters::
+GetParameterType( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
   if( pIt == this->m_Parameters.end( ) )
-    return;
+    return( Self::NoType );
+  return( pIt->second.first );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Interface::Parameters::
+TString& cpPlugins::Interface::Parameters::
+GetRawValue( const TString& name ) const
+{
+  static const TString null_str = "";
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt == this->m_Parameters.end( ) )
+    return( null_str );
+  return( pIt->second.second );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Interface::Parameters::
+TString& cpPlugins::Interface::Parameters::
+GetValueAsString( const TString& name ) const
+{
+  static const TString null_str = "";
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt == this->m_Parameters.end( ) )
+    return( null_str );
   if( pIt->second.first != Self::String )
-    return;
+    return( null_str );
 
-  pIt->second.second = v;
+  return( pIt->second.second );
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::Parameters::
-SetValueAsInt( const std::string& name, const TInt& v )
+cpPlugins::Interface::Parameters::
+TBool cpPlugins::Interface::Parameters::
+GetValueAsBool( const TString& name ) const
 {
-  TParameters::iterator pIt = this->m_Parameters.find( name );
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
   if( pIt == this->m_Parameters.end( ) )
-    return;
+    return( TBool( false ) );
+  if( pIt->second.first != Self::Bool )
+    return( TBool( false ) );
+  return( TBool( std::atoi( pIt->second.second.c_str( ) ) == 1 ) );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::Parameters::
+TInt cpPlugins::Interface::Parameters::
+GetValueAsInt( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt == this->m_Parameters.end( ) )
+    return( TInt( 0 ) );
   if( pIt->second.first != Self::Int )
-    return;
+    return( TInt( 0 ) );
+  return( TInt( std::atoi( pIt->second.second.c_str( ) ) ) );
+}
 
-  std::stringstream ss;
-  ss << v;
-  pIt->second.second = ss.str( );
+// -------------------------------------------------------------------------
+cpPlugins::Interface::Parameters::
+TUint cpPlugins::Interface::Parameters::
+GetValueAsUint( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt == this->m_Parameters.end( ) )
+    return( TUint( 0 ) );
+  if( pIt->second.first != Self::Uint )
+    return( TUint( 0 ) );
+  return( TUint( std::atoi( pIt->second.second.c_str( ) ) ) );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Interface::Parameters::
+TReal cpPlugins::Interface::Parameters::
+GetValueAsReal( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt == this->m_Parameters.end( ) )
+    return( TReal( 0 ) );
+  if( pIt->second.first != Self::Real )
+    return( TReal( 0 ) );
+  return( TReal( std::atof( pIt->second.second.c_str( ) ) ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Parameters::
-SetValueAsUint( const std::string& name, const TUint& v )
+GetValueAsStringList(
+  std::vector< TString >& lst, const TString& name
+  ) const
 {
-  TParameters::iterator pIt = this->m_Parameters.find( name );
+  lst.clear( );
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
   if( pIt == this->m_Parameters.end( ) )
     return;
-  if( pIt->second.first != Self::Uint )
+  if( pIt->second.first != Self::StringList )
     return;
 
-  std::stringstream ss;
-  ss << v;
-  pIt->second.second = ss.str( );
+  std::istringstream ss( pIt->second.second );
+  std::string token;
+  while( std::getline( ss, token, '#' ) )
+    if( token != "" )
+      lst.push_back( token );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Parameters::
-SetValueAsReal( const std::string& name, const TReal& v )
+GetValueAsBoolList( std::vector< TBool >& lst, const TString& name ) const
 {
-  TParameters::iterator pIt = this->m_Parameters.find( name );
+  lst.clear( );
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
   if( pIt == this->m_Parameters.end( ) )
     return;
-  if( pIt->second.first != Self::Real )
+  if( pIt->second.first != Self::BoolList )
     return;
 
-  std::stringstream ss;
-  ss << v;
-  pIt->second.second = ss.str( );
+  std::istringstream ss( pIt->second.second );
+  std::string token;
+  while( std::getline( ss, token, '#' ) )
+    if( token != "" )
+      lst.push_back( TBool( std::atoi( token.c_str( ) ) == 1 ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Parameters::
-SetValueAsIndex( const std::string& name, const TUint& n, ... )
+GetValueAsIntList( std::vector< TInt >& lst, const TString& name ) const
 {
-  TParameters::iterator pIt = this->m_Parameters.find( name );
+  lst.clear( );
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
   if( pIt == this->m_Parameters.end( ) )
     return;
-  if( pIt->second.first != Self::Index )
+  if( pIt->second.first != Self::IntList )
     return;
 
-  va_list v_lst;
-  va_start( v_lst, n );
+  std::istringstream ss( pIt->second.second );
+  std::string token;
+  while( std::getline( ss, token, '#' ) )
+    if( token != "" )
+      lst.push_back( TInt( std::atoi( token.c_str( ) ) ) );
+}
 
-  std::stringstream ss;
-  for( TUint i = 0; i < n; ++i )
-    ss << va_arg( v_lst, long ) << ":";
-  va_end( v_lst );
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::Parameters::
+GetValueAsUintList( std::vector< TUint >& lst, const TString& name ) const
+{
+  lst.clear( );
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt == this->m_Parameters.end( ) )
+    return;
+  if( pIt->second.first != Self::UintList )
+    return;
 
-  pIt->second.second = ss.str( );
+  std::istringstream ss( pIt->second.second );
+  std::string token;
+  while( std::getline( ss, token, '#' ) )
+    if( token != "" )
+      lst.push_back( TUint( std::atoi( token.c_str( ) ) ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Parameters::
-SetValueAsPoint( const std::string& name, const TUint& n, ... )
+GetValueAsRealList( std::vector< TReal >& lst, const TString& name ) const
 {
-  TParameters::iterator pIt = this->m_Parameters.find( name );
+  lst.clear( );
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
   if( pIt == this->m_Parameters.end( ) )
     return;
-  if( pIt->second.first != Self::Point )
+  if( pIt->second.first != Self::RealList )
     return;
 
-  va_list v_lst;
-  va_start( v_lst, n );
+  std::istringstream ss( pIt->second.second );
+  std::string token;
+  while( std::getline( ss, token, '#' ) )
+    if( token != "" )
+      lst.push_back( TReal( std::atof( token.c_str( ) ) ) );
+}
 
-  std::stringstream ss;
-  for( TUint i = 0; i < n; ++i )
-    ss << va_arg( v_lst, double ) << ":";
-  va_end( v_lst );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Parameters::
+HasStringValue( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::String );
+  return( false );
+}
 
-  pIt->second.second = ss.str( );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Parameters::
+HasBoolValue( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::Bool );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-const std::string& cpPlugins::Interface::Parameters::
-GetValueAsString( const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasIntValue( const TString& name ) const
 {
-  static const std::string null_str = "";
   TParameters::const_iterator pIt = this->m_Parameters.find( name );
-  if( pIt == this->m_Parameters.end( ) )
-    return( null_str );
-  if( pIt->second.first != Self::String )
-    return( null_str );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::Int );
+  return( false );
+}
 
-  return( pIt->second.second );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Parameters::
+HasUintValue( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::Uint );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-cpPlugins::Interface::Parameters::
-TInt cpPlugins::Interface::Parameters::
-GetValueAsInt( const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasRealValue( const TString& name ) const
 {
-  static const std::string null_str = "";
   TParameters::const_iterator pIt = this->m_Parameters.find( name );
-  if( pIt == this->m_Parameters.end( ) )
-    return( TInt( 0 ) );
-  if( pIt->second.first != Self::Int )
-    return( TInt( 0 ) );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::Real );
+  return( false );
+}
 
-  return( std::atoi( pIt->second.second.c_str( ) ) );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Parameters::
+HasIndexValue( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::Index );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-cpPlugins::Interface::Parameters::
-TUint cpPlugins::Interface::Parameters::
-GetValueAsUint( const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasPointValue( const TString& name ) const
 {
-  static const std::string null_str = "";
   TParameters::const_iterator pIt = this->m_Parameters.find( name );
-  if( pIt == this->m_Parameters.end( ) )
-    return( TUint( 0 ) );
-  if( pIt->second.first != Self::Uint )
-    return( TUint( 0 ) );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::Point );
+  return( false );
+}
 
-  return( std::atoi( pIt->second.second.c_str( ) ) );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Parameters::
+HasStringListValue( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::StringList );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-cpPlugins::Interface::Parameters::
-TReal cpPlugins::Interface::Parameters::
-GetValueAsReal( const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasBoolListValue( const TString& name ) const
 {
-  static const std::string null_str = "";
   TParameters::const_iterator pIt = this->m_Parameters.find( name );
-  if( pIt == this->m_Parameters.end( ) )
-    return( TReal( 0 ) );
-  if( pIt->second.first != Self::Real )
-    return( TReal( 0 ) );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::BoolList );
+  return( false );
+}
 
-  return( std::atof( pIt->second.second.c_str( ) ) );
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Parameters::
+HasIntListValue( const TString& name ) const
+{
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::IntList );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::Parameters::
-GetValueAsStringList(
-  std::vector< std::string >& lst, const std::string& name
-  ) const
+bool cpPlugins::Interface::Parameters::
+HasUintListValue( const TString& name ) const
 {
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::UintList );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::Parameters::
-GetValueAsIntList( std::vector< TInt >& lst, const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasRealListValue( const TString& name ) const
 {
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::RealList );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::Parameters::
-GetValueAsUintList( std::vector< TUint >& lst, const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasIndexListValue( const TString& name ) const
 {
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::IndexList );
+  return( false );
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::Parameters::
-GetValueAsRealList( std::vector< TReal >& lst, const std::string& name ) const
+bool cpPlugins::Interface::Parameters::
+HasPointListValue( const TString& name ) const
 {
+  TParameters::const_iterator pIt = this->m_Parameters.find( name );
+  if( pIt != this->m_Parameters.end( ) )
+    return( pIt->second.first == Self::PointList );
+  return( false );
 }
 
 // eof - $RCSfile$