cmtkCommandLineVector.txx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 2009 SRI International
00004 //
00005 //  This file is part of the Computational Morphometry Toolkit.
00006 //
00007 //  http://www.nitrc.org/projects/cmtk/
00008 //
00009 //  The Computational Morphometry Toolkit is free software: you can
00010 //  redistribute it and/or modify it under the terms of the GNU General Public
00011 //  License as published by the Free Software Foundation, either version 3 of
00012 //  the License, or (at your option) any later version.
00013 //
00014 //  The Computational Morphometry Toolkit is distributed in the hope that it
00015 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00016 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU General Public License along
00020 //  with the Computational Morphometry Toolkit.  If not, see
00021 //  <http://www.gnu.org/licenses/>.
00022 //
00023 //  $Revision: 779 $
00024 //
00025 //  $LastChangedDate: 2009-11-16 15:33:49 -0800 (Mon, 16 Nov 2009) $
00026 //
00027 //  $LastChangedBy: torstenrohlfing $
00028 //
00029 */
00030 
00031 #include <typeinfo>
00032 #include <sstream>
00033 
00034 template<class T>
00035 void
00036 cmtk::CommandLine::Vector<T>
00037 ::Evaluate( const size_t argc, const char* argv[], size_t& index )
00038 {
00039   if ( !this->m_HasBeenUsed )
00040     {
00041     this->m_pVector->resize(0);
00042     this->m_HasBeenUsed = true;
00043     }
00044 
00045   if ( index+1 < argc ) 
00046     {
00047     ++index;
00048     // first, replace all commas with spaces, so we can simply use a stringstream for parsing the vector elements
00049     std::string str( argv[index] );
00050     for ( size_t i = 0; i < str.length(); ++i )
00051       {
00052       if ( str[i] == ',' )
00053         str[i] = ' ';
00054       }
00055 
00056     // new read values from the whitespaced argument
00057     std::istringstream strm( str );
00058     while ( strm.good() && ! strm.eof() )
00059       {
00060       T nextValue;
00061       strm >> nextValue;
00062       this->m_pVector->push_back( nextValue );
00063       }
00064 
00065     } 
00066   else
00067     {
00068     throw( Exception( "Vector command line option needs an argument.", index ) );
00069     }
00070 }
00071 
00072 template<class T>
00073 mxml_node_t* 
00074 cmtk::CommandLine::Vector<T>
00075 ::MakeXML(  mxml_node_t *const parent ) const 
00076 {
00077   if ( ! (this->m_Properties & PROPS_NOXML) )
00078     {
00079     const std::string typeName = std::string ( CommandLineTypeTraits<T>::GetName() ) + std::string( "-vector" );    
00080     mxml_node_t *node = mxmlNewElement( parent, typeName.c_str() );
00081     
00082     // write any attributes the user might have set
00083     for ( std::map<std::string,std::string>::const_iterator attrIt = this->m_Attributes.begin(); attrIt != this->m_Attributes.end(); ++attrIt )
00084       {
00085       mxmlElementSetAttr( node, attrIt->first.c_str(), attrIt->second.c_str() );
00086       }
00087     
00088     mxmlElementSetAttr( node, "multiple", "true" );
00089     
00090     return node;
00091     }
00092   return NULL;
00093 }
00094 
00095 template<class T>
00096 std::string
00097 cmtk::CommandLine::Vector<T>
00098 ::GetParamTypeString() const
00099 {
00100   const std::string& singleItemString = Item::Helper<T>::GetParamTypeString( this );
00101   return singleItemString+std::string("[,")+singleItemString+std::string(",...]");
00102 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines