Args.h

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2000-2001  The Exult Team
00003  *  Copyright (C) 2002-2003  The Pentagram Team
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #ifndef _ARGS_H_
00021 #define _ARGS_H_
00022 
00023 // Handy argument processor. I'm certain the implementation could be better
00024 // but it suffices quite well at the moment.
00025 
00026 #include "common_types.h"
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 class Args
00032 {
00033         public:
00034                 Args()  { };
00035                 ~Args() { };
00036         
00037         struct Option
00038         {
00039                 Option() : option(""), valuetype(no_type) { };
00040                 // bool
00041                 Option(const char *option_cstr, bool *value, const bool defaultvalue=true)
00042                         : option(option_cstr), _bool_val(value), _bool_default(defaultvalue),
00043                         valuetype(Option::type_bool)
00044                 {  /* Odd... looks like the 'default' value for bool, isn't. It's the value you set it to, if the flag is found*/ };
00045                 // string
00046                 Option(const char *option_cstr, std::string *value, const char *defaultvalue=0)
00047                         : option(option_cstr), _str_val(value),
00048                         _str_default(defaultvalue?defaultvalue:""), valuetype(Option::type_str)
00049                 { *_str_val=_str_default; };
00050                 // sint
00051                 Option(const char *option_cstr, sint32 *value, const sint32 defaultvalue=true)
00052                         : option(option_cstr), _sint_val(value), _sint_default(defaultvalue),
00053                         valuetype(Option::type_sint)
00054                 { *_sint_val=_sint_default; };
00055                 // uint
00056                 Option(const char *option_cstr, uint32 *value, const uint32 defaultvalue=true)
00057                         : option(option_cstr), _uint_val(value), _uint_default(defaultvalue),
00058                         valuetype(Option::type_uint)
00059                 { *_uint_val=_uint_default; };
00060                                  
00061                 ~Option() { };
00062                 
00063                 std::string option;
00064                 
00065                 bool        *_bool_val;
00066                 std::string *_str_val;
00067                 sint32      *_sint_val;
00068                 uint32      *_uint_val;
00069 
00070                 bool        _bool_default;
00071                 std::string _str_default;
00072                 sint32      _sint_default;
00073                 uint32      _uint_default;
00074                 
00075                 enum { no_type=0, type_bool, type_str, type_sint, type_uint } valuetype;
00076         };
00077         
00078         std::vector<Option> options;
00079         
00080         // bool
00081         inline void declare(const char *option_cstr, bool *value, const bool defaultvalue=true)
00082         { options.push_back(Option(option_cstr, value, defaultvalue)); };
00083         // string
00084         inline void declare(const char *option_cstr, std::string *value, const char *defaultvalue=0)
00085         { options.push_back(Option(option_cstr, value, defaultvalue)); };
00086         // sint
00087         inline void declare(const char *option_cstr, sint32 *value, const sint32 defaultvalue=0)
00088         { options.push_back(Option(option_cstr, value, defaultvalue)); };
00089         // uint
00090         inline void declare(const char *option_cstr, uint32 *value, const uint32 defaultvalue=0)
00091         { options.push_back(Option(option_cstr, value, defaultvalue)); };
00092 
00093         void process(const sint32 argc, const char * const * const argv);
00094 };
00095 
00096 #endif
00097 

Generated on Fri Jul 27 22:27:09 2007 for pentagram by  doxygen 1.4.7