# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

command.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL 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, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
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         // is it a variable or a classic command?
00191         enum TType { Unknown, Command, Variable };
00192         TType Type;
00193 
00194         // static members
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 } // NLMISC
00274 
00275 
00276 #endif // NL_COMMAND_H
00277 
00278 /* End of command.h */