Merge branch 'main/atys-live' into ryzom/ark-features
[ryzomcore.git] / nel / samples / misc / command / main.cpp
blobe4d1daffc7b6c9206abbf9f48b0a93df72310440
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <iostream>
18 #include <sstream>
20 #include "nel/misc/types_nl.h"
21 #include "nel/misc/debug.h"
22 #include "nel/misc/log.h"
23 #include "nel/misc/displayer.h"
25 // include the command system
26 #include "nel/misc/command.h"
27 #include "nel/misc/variable.h"
31 // Commands
34 // with this macro, you can execute a specific command in real time.
35 // this is an example of a command, the first param is the name of the command
36 // (note that it is *not* a string). the second param is the help string that will
37 // be displayed when the user calls the help command. the third param is the
38 // usage of the command. it can the an empty string when the command doesn't
39 // need any parameters.
40 // when the program is launched, you can type the name of the command with
41 // parameters if needed and it will be executed.
42 NLMISC_COMMAND(square,"display the square of the parameter","<value>")
44 // this code will be executed when the user will call this command.
45 // you have access to some variables:
46 // - args is a vector of strings. they are the user parameters, you have to
47 // check if enough parameters are present and if they are in a good
48 // format.
49 // - log is a CLog object that is used to display the result of the command,
50 // if any. see the log example to know how to use CLog.
52 // this function must return true if the command successfully executed
53 // or false is something wrong happens. for example, if there are not enough
54 // parameters, return false.
56 // check args, if there is not the right number of parameters, return bad status.
57 if (args.size() != 1) return false;
59 uint32 val;
60 NLMISC::fromString(args[0], val);
62 // display the result.
63 log.displayNL("The square of %d is %d", val, val*val);
65 // return ok status.
66 return true;
70 // Dummy variables
73 bool MyVar1;
74 uint8 MyVar2;
75 sint8 MyVar3;
76 uint16 MyVar4;
77 sint16 MyVar5;
78 uint32 MyVar6;
79 sint32 MyVar7;
80 uint64 MyVar8;
81 sint64 MyVar9;
82 std::string MyVar10;
83 ucchar MyVar13;
84 float MyVar14;
85 double MyVar15;
88 // Create commands on all variables
91 // with this macro, you can get or set the variable in real time.
92 // this is the easiest way to set or get a variable in real time. you have a direct
93 // access to the variable so you can use this macro.
94 // first param is the type of the variable. second one is the variable name,
95 // and the third one is an help string that describes what the variable does.
96 // when the program is executed, you can type the variable name with no parameter
97 // to see the value of the variable or type the variable name with one parameter
98 // (the value you want to set) to change the variable contents.
99 NLMISC_VARIABLE(bool, MyVar1, "a dummy variable");
100 NLMISC_VARIABLE(uint8, MyVar2, "a dummy variable");
101 NLMISC_VARIABLE(sint8, MyVar3, "a dummy variable");
102 NLMISC_VARIABLE(uint16, MyVar4, "a dummy variable");
103 NLMISC_VARIABLE(sint16, MyVar5, "a dummy variable");
104 NLMISC_VARIABLE(uint32, MyVar6, "a dummy variable");
105 NLMISC_VARIABLE(sint32, MyVar7, "a dummy variable");
106 NLMISC_VARIABLE(uint64, MyVar8, "a dummy variable");
107 NLMISC_VARIABLE(sint64, MyVar9, "a dummy variable");
108 NLMISC_VARIABLE(std::string, MyVar10, "a dummy variable");
109 NLMISC_VARIABLE(ucchar, MyVar13, "a dummy variable");
110 NLMISC_VARIABLE(float, MyVar14, "a dummy variable");
111 NLMISC_VARIABLE(double, MyVar15, "a dummy variable");
114 // DynVariable
117 class CDynVar
119 private:
120 int _PrivateVar;
121 public:
122 int get() { return _PrivateVar; }
123 void set(int val) { _PrivateVar = val; }
126 CDynVar dv;
128 // with this macro, you can get or set the variable in real time.
129 // this is an example of dynamic variable. the goal of this macro is to provide an
130 // easy way to get or set a variable when you don't have a direct access on this variable.
131 // first param is the type of the variable. second one is the name of the variable.
132 // the third one is the help string about this variable (what this variable does).
133 // when the program is executed, you can type the variable name with no parameter
134 // to see the value of the variable or type the variable name with one parameter
135 // (the value you want to set) to change the variable contents.
136 NLMISC_DYNVARIABLE(int,PrivVar,"dummy variable")
138 // this code will be executed to get or set the variable.
139 // you have to provide a way to do that.
140 // the 'get' boolean says if you have to get or set the variable.
141 // the 'pointer' is a pointer on the variable you have to read or write.
142 // if 'get' is true, you have to set '*pointer' with the value of your variable.
143 // if 'get' is false, you have to set your variable with the value of *pointer
144 if (get)
145 *pointer = dv.get();
146 else
147 dv.set(*pointer);
152 // Main
155 // look at the log sample
156 NLMISC::CLog mylog;
157 // look at the log sample
158 NLMISC::CStdDisplayer mysd;
160 int main (int argc, char **argv)
162 // look at the debug sample
163 NLMISC::createDebug();
165 // look at the log sample
166 mylog.addDisplayer(&mysd);
168 printf("Type a command, 'help' or 'quit'\n");
170 while (true)
172 // display prompt
173 std::cout << "> ";
174 fflush(stdout);
176 // get command line
177 std::string commandLine;
178 std::getline(std::cin, commandLine, '\n');
180 if (commandLine == "quit")
181 break;
183 // execute the command line. it will try to find the command or the
184 // variable and call the associated code
185 NLMISC::ICommand::execute(commandLine, mylog);
188 return EXIT_SUCCESS;