00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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