-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.cpp
128 lines (116 loc) · 3.41 KB
/
command.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//#include <functional>
//temp
#include <iostream>
#include <string>
#include <list>
#include <memory>
#include <functional>
#include "variable.hpp"
#include "command.hpp"
namespace sre { namespace console {
//== CommandFunc ==//
/** \class CommandFunc
* This is a functor class designed to be used with CommandDef.
* Any inherited class can be used with CommandDef that defines `operator()(const Command&)`.
* \warning The parameters of the CommandFunc must correlate with those of the CommandDef. **/
//== other CommandFunc children ==//
/** \class EmptyCommand
* A functor that does nothing but tell you that it did.**/
/** \class EchoCommand
* A functor that echoes it's argument **/
////
//void EchoCommand::operator() (Command cmd) {
//}
//== CommandDef ==//
/** \class CommandDef
* A definition for a command **/
CommandDef::CommandDef( const CommandDef& cmdDef) {
id = cmdDef.id;
parameters = std::unique_ptr<TypeList>(new TypeList(*cmdDef.parameters));
func = cmdDef.func;
}
CommandDef::CommandDef( std::string id)
{
// no parameters by default
this->id = id;
parameters = std::unique_ptr<TypeList>(new TypeList());
func = std::unique_ptr<CommandFunc>(new EmptyCommand);
}
/** Push a new parameter to the list of parameters
* \param[in] type Type of parameter **/
void CommandDef::push(Type type) {
parameters->push_back(type);
}
/** change or add the the CommandFunc **/
void CommandDef::setFunc (std::shared_ptr<CommandFunc> cmdFunc) {
this->func = cmdFunc;
}
/** Calls this Command's associated functor by passing it a Command (along with it's associated arguments).
* **/
/*std::string CommandDef::operator() (Command cmd) {
}*/
//== Command ==//
//// ctor ////
Command::Command(std::string id) {
this->id = id;
args = std::unique_ptr<TypeList>(new TypeList());
}
void Command::push(Type type) {
args->push_back(type);
}
//== Type ==//
//// ctor ////
Type::Type(std::string id) {
this->id = id;
}
Type::Type (const Type & type) {
*this = type;
}
//// asignment ////
Type& Type::operator= (const Type & type) {
this->id = type.id;
return *this;
}
//== Arg ==//
//// ctor ////
Arg::Arg(const Type& type, std::string value) {
this->type = std::unique_ptr<Type>(new Type(type));
this->value = value;
}
/////////////// namespace functions ////////////////
bool operator==(const Command& cmd, const CommandDef& def) {
TypeList& argList = *cmd.args;
TypeList& paramList = *def.parameters;
if (cmd.id == def.id)
{
if(argList.empty() && paramList.empty())
{
return true;
}
auto itArg = argList.begin();
auto itDef = paramList.begin();
// for each parameter
for(; itArg != argList.end(); ++itArg, ++itDef ) {
if ( itDef == paramList.end() ) { // too many arguments
std::cerr<<"too many arguments"<<std::endl;
return false;
}
else if ( itArg->id != itDef->id ) { // wrong type
std::cerr<<"wrong type"<<std::endl;
return false;
}
}
if (itDef != paramList.end()) { // not enough arguments
std::cerr<<"not enough arguments"<<std::endl;
return false;
}
else
return true;
} else {
return false;
}
}
bool operator==(const CommandDef& def, const Command& cmd) {
return (cmd == def);
}
}} // namespace