00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_COMMAND_H
00027 #define NL_COMMAND_H
00028
00029 #include "nel/misc/types_nl.h"
00030
00031 #include <string>
00032 #include <map>
00033 #include <vector>
00034 #include <sstream>
00035 #include <istream>
00036
00037 #include "nel/misc/stream.h"
00038
00039 #include "nel/misc/log.h"
00040
00041
00042 namespace NLMISC {
00043
00044
00071 #define NLMISC_COMMAND(__name,__help,__args) \
00072 struct __name##Class: public ICommand \
00073 { \
00074 __name##Class() : NLMISC::ICommand(#__name,__help,__args) { } \
00075 virtual bool execute(const std::vector<std::string> &args, NLMISC::CLog &log); \
00076 }; \
00077 __name##Class __name##Instance; \
00078 bool __name##Class::execute(const std::vector<std::string> &args, NLMISC::CLog &log)
00079
00080
00081
00101 #define NLMISC_VARIABLE(__type,__var,__help) \
00102 CVariable<__type> __var##Instance(#__var, __help " (" #__type ")", &__var)
00103
00104
00105
00136 #define NLMISC_DYNVARIABLE(__type,__name,__help) \
00137 class __name##Class : public NLMISC::ICommand \
00138 { \
00139 public: \
00140 __name##Class () : NLMISC::ICommand(#__name, __help " (" #__type ")", "[<value>]") { Type = Variable; } \
00141 virtual bool execute(const std::vector<std::string> &args, NLMISC::CLog &log) \
00142 { \
00143 if (args.size() == 1) \
00144 { \
00145 std::stringstream ls (args[0]); \
00146 __type p; \
00147 ls >> p; \
00148 pointer (&p, false, log); \
00149 } \
00150 if (args.size() >= 0) \
00151 { \
00152 __type p; \
00153 pointer (&p, true, log); \
00154 std::stringstream ls; \
00155 ls << "Variable " << _CommandName << " = " << p; \
00156 log.displayNL(ls.str().c_str()); \
00157 } \
00158 if (args.size() > 1) \
00159 return false; \
00160 \
00161 return true; \
00162 } \
00163 \
00164 void pointer(__type *pointer, bool get, NLMISC::CLog &log); \
00165 }; \
00166 __name##Class __name##Instance; \
00167 void __name##Class::pointer(__type *pointer, bool get, NLMISC::CLog &log)
00168
00169
00176 class ICommand
00177 {
00178 public:
00179
00181 ICommand(const char *commandName, const char *commandHelp, const char *commandArgs);
00182
00183 virtual ICommand::~ICommand();
00184
00185 virtual bool execute(const std::vector<std::string> &args, NLMISC::CLog &log) = 0;
00186
00187 std::string HelpString;
00188 std::string CommandArgs;
00189
00190
00191 enum TType { Unknown, Command, Variable };
00192 TType Type;
00193
00194
00195
00196 typedef std::map<std::string, ICommand *> TCommand;
00197
00198 static TCommand *Commands;
00199 static bool CommandsInit;
00200
00203 static void execute (const std::string &commandWithArgs, NLMISC::CLog &log, bool quiet = false);
00204
00209 static void expand (std::string &commandName, NLMISC::CLog &log=*InfoLog);
00210
00211 static void serialCommands (IStream &f);
00212
00213 protected:
00214
00215 std::string _CommandName;
00216 };
00217
00219 struct CSerialCommand
00220 {
00221 CSerialCommand () : Name ("<Unknown>"), Type(ICommand::Unknown) { }
00222 CSerialCommand (std::string n, ICommand::TType t) : Name (n), Type(t) { }
00223
00224 std::string Name;
00225 ICommand::TType Type;
00226
00227 void serial (IStream &f)
00228 {
00229 f.serial (Name);
00230 f.serialEnum (Type);
00231 }
00232 };
00233
00234
00235
00236
00237
00245 template <class T>
00246 class CVariable : public ICommand
00247 {
00248 public:
00249 CVariable (const char *commandName, const char *commandHelp, T *pointer) : NLMISC::ICommand(commandName, commandHelp, "[<value>]"), _Pointer(pointer) { Type = Variable; }
00250 virtual bool execute(const std::vector<std::string> &args, NLMISC::CLog &log)
00251 {
00252 if (args.size() == 1)
00253 {
00254 std::stringstream s2 (args[0]);
00255 s2 >> *_Pointer;
00256 }
00257 if (args.size() >= 0)
00258 {
00259 std::stringstream s;
00260 s << "Variable " << _CommandName << " = " << *_Pointer;
00261 log.displayNL(s.str().c_str());
00262 }
00263 if (args.size() > 1)
00264 return false;
00265
00266 return true;
00267 }
00268 private:
00269 T *_Pointer;
00270 };
00271
00272
00273 }
00274
00275
00276 #endif // NL_COMMAND_H
00277
00278