UnlabeledValueArg.h

Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  UnlabeledValueArg.h
00005  * 
00006  *  Copyright (c) 2003, Michael E. Smoot .
00007  *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
00008  *  All rights reverved.
00009  * 
00010  *  See the file COPYING in the top directory of this distribution for
00011  *  more information.
00012  *  
00013  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
00014  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00015  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
00016  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
00017  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
00018  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
00019  *  DEALINGS IN THE SOFTWARE.  
00020  *  
00021  *****************************************************************************/ 
00022 
00023 
00024 #ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H
00025 #define TCLAP_UNLABELED_VALUE_ARGUMENT_H
00026 
00027 #include <string>
00028 #include <vector>
00029 
00030 #include <tclap/ValueArg.h>
00031 #include <tclap/OptionalUnlabeledTracker.h>
00032 
00033 
00034 namespace TCLAP {
00035 
00042 template<class T>
00043 class UnlabeledValueArg : public ValueArg<T>
00044 {
00045 
00046   // If compiler has two stage name lookup (as gcc >= 3.4 does)
00047   // this is requried to prevent undef. symbols
00048 #ifndef __BORLANDC__
00049   using ValueArg<T>::_ignoreable;
00050   using ValueArg<T>::_hasBlanks;
00051   using ValueArg<T>::_extractValue;
00052   using ValueArg<T>::_typeDesc;
00053   using ValueArg<T>::_name;
00054   using ValueArg<T>::_description;
00055   using ValueArg<T>::_alreadySet;
00056   using ValueArg<T>::toString;
00057 #endif
00058 public:
00059 
00081   UnlabeledValueArg( const std::string& name, 
00082                      const std::string& desc, 
00083                      bool req,
00084                      T value,
00085                      const std::string& typeDesc,
00086                      bool ignoreable = false,
00087                      Visitor* v = NULL); 
00088 
00111   UnlabeledValueArg( const std::string& name, 
00112                      const std::string& desc, 
00113                      bool req,
00114                      T value,
00115                      const std::string& typeDesc,
00116                      CmdLineInterface& parser,
00117                      bool ignoreable = false,
00118                      Visitor* v = NULL );           
00119             
00139   UnlabeledValueArg( const std::string& name, 
00140                      const std::string& desc, 
00141                      bool req,
00142                      T value,
00143                      Constraint<T>* constraint,
00144                      bool ignoreable = false,
00145                      Visitor* v = NULL ); 
00146 
00147     
00168   UnlabeledValueArg( const std::string& name, 
00169                      const std::string& desc, 
00170                      bool req,
00171                      T value,
00172                      Constraint<T>* constraint,
00173                      CmdLineInterface& parser,
00174                      bool ignoreable = false,
00175                      Visitor* v = NULL);
00176             
00185   virtual bool processArg(int* i, std::vector<std::string>& args); 
00186 
00190   virtual std::string shortID(const std::string& val="val") const;
00191 
00195   virtual std::string longID(const std::string& val="val") const;
00196 
00200   virtual bool operator==(const Arg& a ) const;
00201 
00206   virtual void addToList( std::list<Arg*>& argList ) const;
00207 
00208 };
00209 
00213 template<class T>
00214 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00215                                         const std::string& desc, 
00216                                         bool req,
00217                                         T val,
00218                                         const std::string& typeDesc,
00219                                         bool ignoreable,
00220                                         Visitor* v)
00221   : ValueArg<T>("", name, desc, req, val, typeDesc, v)
00222 { 
00223   _ignoreable = ignoreable;
00224 
00225   OptionalUnlabeledTracker::check(req, toString());
00226 
00227 }
00228 
00229 template<class T>
00230 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00231                                         const std::string& desc, 
00232                                         bool req,
00233                                         T val,
00234                                         const std::string& typeDesc,
00235                                         CmdLineInterface& parser,
00236                                         bool ignoreable,
00237                                         Visitor* v)
00238   : ValueArg<T>("", name, desc, req, val, typeDesc, v)
00239 { 
00240   _ignoreable = ignoreable;
00241   OptionalUnlabeledTracker::check(req, toString());
00242   parser.add( this );
00243 }
00244 
00248 template<class T>
00249 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00250                                         const std::string& desc, 
00251                                         bool req,
00252                                         T val,
00253                                         Constraint<T>* constraint,
00254                                         bool ignoreable,
00255                                         Visitor* v)
00256   : ValueArg<T>("", name, desc, req, val, constraint, v)
00257 { 
00258   _ignoreable = ignoreable;
00259   OptionalUnlabeledTracker::check(req, toString());
00260 }
00261 
00262 template<class T>
00263 UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name, 
00264                                         const std::string& desc, 
00265                                         bool req,
00266                                         T val,
00267                                         Constraint<T>* constraint,
00268                                         CmdLineInterface& parser,
00269                                         bool ignoreable,
00270                                         Visitor* v)
00271   : ValueArg<T>("", name, desc, req, val, constraint,  v)
00272 { 
00273   _ignoreable = ignoreable;
00274   OptionalUnlabeledTracker::check(req, toString());
00275   parser.add( this );
00276 }
00277 
00281 template<class T>
00282 bool UnlabeledValueArg<T>::processArg(int *i, std::vector<std::string>& args) 
00283 {
00284   
00285   if ( _alreadySet )
00286     return false;
00287   
00288   if ( _hasBlanks( args[*i] ) )
00289     return false;
00290 
00291   // WEL: Do no allow unlabeld args that start with "-". If they are
00292   // allowed, then the parser cannot detect invalid options!
00293 
00294   if ( _ignoreable && Arg::ignoreRest() )
00295     return false;
00296 
00297   if (args[*i].find_first_of( Arg::flagStartString() ) == 0)
00298     return false;
00299 
00300   // never ignore an unlabeled arg
00301   // WEL: except for those that start with a "-"
00302   
00303   _extractValue( args[*i] );
00304   _alreadySet = true;
00305   return true;
00306 }
00307 
00311 template<class T>
00312 std::string UnlabeledValueArg<T>::shortID(const std::string&) const
00313 {
00314   std::string id = "<" + _typeDesc + ">";
00315 
00316   return id;
00317 }
00318 
00322 template<class T>
00323 std::string UnlabeledValueArg<T>::longID(const std::string&) const
00324 {
00325   // Ideally we would like to be able to use RTTI to return the name
00326   // of the type required for this argument.  However, g++ at least, 
00327   // doesn't appear to return terribly useful "names" of the types.  
00328   std::string id = "<" + _typeDesc + ">";
00329 
00330   return id;  
00331 }
00332 
00336 template<class T>
00337 bool UnlabeledValueArg<T>::operator==(const Arg& a ) const
00338 {
00339   if ( _name == a.getName() || _description == a.getDescription() )
00340     return true;
00341   else
00342     return false;
00343 }
00344 
00345 template<class T>
00346 void UnlabeledValueArg<T>::addToList( std::list<Arg*>& argList ) const
00347 {
00348   argList.push_back( const_cast<Arg*>(static_cast<const Arg* const>(this)) );
00349 }
00350 
00351 }
00352 #endif

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1