cmdline.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Reality Rift Studios                            *
00003  *   http://www.realityrift.com  -  mattias@realityrift.com                *
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                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef neo_application_cmdline_h
00022 #define neo_application_cmdline_h
00023 
00024 #include "base.h"
00025 #include "../core/array.h"
00026 #include "../util/command.h"
00027 
00028 #include <string>
00029 
00030 namespace neo {
00031 namespace application {
00032 
00034 
00036 class CmdBase
00037 {
00038         public:
00039                                                                                                         
00045                                                            CmdBase( const std::string &shortname, const std::string &longname );
00046 
00049         virtual                                           ~CmdBase() { }
00050 
00051         bool                                               operator == ( const std::string &rhs) const;
00052 
00053         virtual void                                       execute() = 0;
00054 
00055     protected:
00056 
00058         std::string                                        _short;
00059 
00061         std::string                                        _long;
00062 };
00063 
00064 
00065 template< typename T > class CmdImpl
00066 {
00067     public:
00068 
00069         template< typename F > CmdImpl( F fun ) :
00070             _callback( fun )
00071         {
00072         }
00073 
00074         virtual ~CmdImpl() { }
00075 
00076         util::Command< void, TYPELIST_1( T ) >             _callback;
00077 
00078         virtual void execute( T data )
00079         {
00080             _callback( data );
00081         }
00082 
00083         virtual void execute() { }
00084 };
00085 
00086 
00087 template <> class CmdImpl< core::NullType >
00088 {
00089     public:
00090 
00091         template< typename F > CmdImpl( F fun ) :
00092             _callback( fun )
00093         {
00094         }
00095 
00096         virtual ~CmdImpl() { }
00097 
00098         util::Command< void, util::type::NullType >              _callback;
00099 
00100         virtual void execute()
00101         {
00102             _callback();
00103         }
00104 
00105         virtual void execute( util::type::NullType& )
00106         {
00107             _callback();
00108         }
00109 };
00110 
00111 
00113 
00115 template< typename T > class Cmd : public CmdBase
00116 {
00117     public:
00118 
00125         template< typename F >                             Cmd( const std::string &shortname, const std::string &longname, F callback, T defaultvalue );
00126 
00132         template< typename F >                             Cmd( const std::string &shortname, const std::string &longname, F callback );
00133 
00136         virtual                                           ~Cmd();
00137 
00138         const T                                            getData();
00139 
00140         virtual void                                       execute();
00141 
00142     protected:
00143 
00145         T                                                 _data;
00146 
00148         T                                                 _default;
00149 
00151         bool                                              _userSupplied;
00152 
00154         int                                               _numArgs;
00155 
00157         CmdImpl< T >                                     *_impl;
00158 };
00159 
00160 
00162 
00164 class CmdLine
00165 {
00166     public:
00167 
00170                                                            CmdLine();
00171 
00172         virtual                                           ~CmdLine();
00173 
00174         void                                               addCmd( CmdBase *cmd );
00175 
00176     protected:
00177 
00178         void                                               parseCmds( int argc, char **argv );
00179 
00180         CmdBase                                           *getCmd( const std::string &name );
00181 
00182         core::Array< CmdBase* >                           _cmds;
00183 };
00184 
00185 
00186 #include "cmdline.inl"
00187 
00188 }
00189 }
00190 
00191 #endif

Generated on Sat Feb 17 20:50:48 2007 for NeoEngine 2 - Evolution by  doxygen 1.5.1