Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

option.hh

00001 /*
00002 *  Name:      option.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Option for PREFS
00005 *  Date:      $Date: 2003/04/14 00:18:35 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_UTIL_PREFS_OPTION__
00027 #define _MPCL_UTIL_PREFS_OPTION__
00028 
00029 #include <iostream>
00030 #include <vector>
00031 #include "../../io/output_streamable.hh"
00032 #include "../../text/string.hh"
00033 #include "defs.hh"
00034 
00035 
00037 namespace mpcl
00038 {
00039 
00041   namespace util
00042   {
00043 
00045     namespace prefs
00046     {
00047 
00048       using io::IOutputStreamable;
00049       using text::TString;
00050 
00055       class TOption : public IOutputStreamable<>
00056       {
00057 
00058         private:
00059 
00061           static const TString   _kyDefaultHelpString;
00062 
00064           TString   yName;
00065 
00067           TString   yShortcut;
00068 
00070           TString   yDefaultValue;
00071 
00073           TString   yHelp;
00074 
00076           std::vector<TString>   tValueVector;
00077 
00079           bool   gIsAtCmdLine;
00080 
00082           bool   gIsParameterAble;
00083 
00084 
00085         public:
00086 
00087           typedef
00088             std::vector<TString>::size_type
00089             size_type;
00090 
00091 
00092         protected:
00093 
00094           //
00095           //  S E L E C T O R S
00096           //
00097           void write (std::basic_ostream<char_type, traits_type>& rtTARGET_OSTREAM) const;
00098 
00099 
00100         public:
00101 
00102           //
00103           //  C O N S T R U C T O R S
00104           //
00105 
00107           TOption (void)                               :
00108             IOutputStreamable<> ()                     ,
00109             yName               ()                     ,
00110             yShortcut           ()                     ,
00111             yDefaultValue       ()                     ,
00112             yHelp               (_kyDefaultHelpString) ,
00113             tValueVector        ()                     ,
00114             gIsAtCmdLine        (false)                ,
00115             gIsParameterAble    (true)                 {}
00116 
00121           TOption (const TOption& rktSOURCE_OPTION)                 :
00122             IOutputStreamable<> ()                                  ,
00123             yName               (rktSOURCE_OPTION.yName)            ,
00124             yShortcut           (rktSOURCE_OPTION.yShortcut)        ,
00125             yDefaultValue       (rktSOURCE_OPTION.yDefaultValue)    ,
00126             yHelp               (rktSOURCE_OPTION.yHelp)            ,
00127             tValueVector        (rktSOURCE_OPTION.tValueVector)     ,
00128             gIsAtCmdLine        (rktSOURCE_OPTION.gIsAtCmdLine)     ,
00129             gIsParameterAble    (rktSOURCE_OPTION.gIsParameterAble) {}
00130 
00136           TOption (const TString& rkyNAME, const TString& rkySHORTCUT) :
00137             IOutputStreamable<> ()                                     ,
00138             yName               (rkyNAME)                              ,
00139             yShortcut           (rkySHORTCUT)                          ,
00140             yDefaultValue       ()                                     ,
00141             yHelp               (_kyDefaultHelpString)                 ,
00142             tValueVector        ()                                     ,
00143             gIsAtCmdLine        (false)                                ,
00144             gIsParameterAble    (false)                                {}
00145 
00152           TOption ( const TString& rkyNAME          ,
00153                     const TString& rkySHORTCUT      ,
00154                     const TString& rkyDEFAULT_VALUE )  :
00155             IOutputStreamable<> ()                     ,
00156             yName               (rkyNAME)              ,
00157             yShortcut           (rkySHORTCUT)          ,
00158             yDefaultValue       (rkyDEFAULT_VALUE)     ,
00159             yHelp               (_kyDefaultHelpString) ,
00160             tValueVector        ()                     ,
00161             gIsAtCmdLine        (false)                ,
00162             gIsParameterAble    (true)                 {}
00163 
00165           virtual ~TOption (void) {}
00166 
00171           void addValue (const TString& rkyVALUE)
00172           {
00173             if ( !gIsParameterAble )
00174             {
00175               throw TNotParameterAbleException (yName.c_str(), __FILE__, __LINE__);
00176             }
00177             else
00178             {
00179               tValueVector.push_back (rkyVALUE);
00180             }
00181           }
00182 
00184           TOption& operator = (const TOption& rktSOURCE_OPTION)
00185           {
00186             yName            = rktSOURCE_OPTION.yName;
00187             yShortcut        = rktSOURCE_OPTION.yShortcut;
00188             yDefaultValue    = rktSOURCE_OPTION.yDefaultValue;
00189             yHelp            = rktSOURCE_OPTION.yHelp;
00190             tValueVector     = rktSOURCE_OPTION.tValueVector;
00191             gIsAtCmdLine     = rktSOURCE_OPTION.gIsAtCmdLine;
00192             gIsParameterAble = rktSOURCE_OPTION.gIsParameterAble;
00193             return *this;
00194           }
00195 
00197           void setCmdLinePresence (void)
00198           {
00199             gIsAtCmdLine = true;
00200           }
00201 
00206           void setUsageString (const TString& rkyVALUE)
00207           {
00208             yHelp = rkyVALUE;
00209           }
00210 
00215           void setValue (const TString& rkyVALUE)
00216           {
00217             tValueVector.clear();
00218             addValue (rkyVALUE);
00219           }
00220 
00221 
00222         public:
00223 
00224           //
00225           //  S E L E C T O R S
00226           //
00227 
00232           const TString& getValue (void) const
00233             throw (TNotParameterAbleException)
00234           {
00235             if ( !gIsParameterAble )
00236             {
00237               throw TNotParameterAbleException (yName.c_str(), __FILE__, __LINE__);
00238             }
00239             return ( tValueVector.empty() ) ? yDefaultValue : tValueVector.back();
00240           }
00241 
00247           const std::vector<TString>& getValues (void) const
00248             throw (TNotParameterAbleException)
00249           {
00250             if ( !gIsParameterAble )
00251             {
00252               throw TNotParameterAbleException (yName.c_str(), __FILE__, __LINE__);
00253             }
00254             return tValueVector;
00255           }
00256 
00262           bool hasValue (const TString& rkySOURCE_VALUE) const;
00263 
00268           TString help (void) const
00269           {
00270             return yHelp;
00271           }
00272 
00277           bool isAtCmdLine (void) const
00278           {
00279             return gIsAtCmdLine;
00280           }
00281 
00286           bool isEmpty (void) const
00287           {
00288             return ( yName.empty() );
00289           }
00290 
00295           bool isParameterAble (void) const
00296           {
00297             return gIsParameterAble;
00298           }
00299 
00304           TString name (void) const
00305           {
00306             return yName;
00307           }
00308 
00314           const TString& operator [] (size_type zINDEX) const
00315           {
00316             return tValueVector [zINDEX];
00317           }
00318 
00324           TString& operator [] (size_type zINDEX)
00325           {
00326             return tValueVector [zINDEX];
00327           }
00328 
00335           bool operator == (const TOption& rktSOURCE_OPTION) const
00336           {
00337             return ( yName == rktSOURCE_OPTION.yName );
00338           }
00339 
00344           TString shortcut (void) const
00345           {
00346             return yShortcut;
00347           }
00348 
00353           size_type valueCount (void) const
00354           {
00355             return tValueVector.size();
00356           }
00357 
00358       };  // class TOption
00359 
00360     }  // namespace prefs
00361 
00362   }  // namespace util
00363 
00364 }  // namespace mpcl
00365 
00366 
00367 #endif  // not _MPCL_UTIL_PREFS_OPTION__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18