- Timestamp:
- Aug 27, 2010, 7:29:49 PM (14 years ago)
- Location:
- code/branches/consolecommands3/src/libraries/core/command
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/core/command/CommandEvaluation.cc
r7228 r7230 49 49 this->bPossibleArgumentsRetrieved_ = false; 50 50 this->possibleArguments_.clear(); 51 52 this->tokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0'); 51 this->bEvaluatedParams_ = false; 52 this->bTriedToEvaluatedParams_ = false; 53 this->numberOfEvaluatedParams_ = 0; 54 55 this->tokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); 53 56 } 54 57 … … 78 81 } 79 82 80 int CommandEvaluation::execute() const83 int CommandEvaluation::execute() 81 84 { 82 85 int error; … … 85 88 } 86 89 87 MultiType CommandEvaluation::query(int* error) const90 MultiType CommandEvaluation::query(int* error) 88 91 { 89 92 if (error) … … 103 106 104 107 if (this->execCommand_ && this->execCommand_->isActive() && this->execCommand_->hasAccess()) 105 return this->execCommand_->getExecutor()->parse(this->tokens_.subSet(this->execArgumentsOffset_).join(), error, " ", false); 108 { 109 if (!this->bTriedToEvaluatedParams_) 110 this->evaluateParams(false); 111 112 if (this->bEvaluatedParams_) 113 { 114 COUT(0) << "call evaluated" << std::endl; 115 COUT(6) << "CE_execute (evaluation): " << this->execCommand_->getName() << " with " << this->numberOfEvaluatedParams_ << " params: " << this->param_[0] << ' ' << this->param_[1] << ' ' << this->param_[2] << ' ' << this->param_[3] << ' ' << this->param_[4] << std::endl; 116 switch (this->numberOfEvaluatedParams_) 117 { 118 case 0: return (*this->execCommand_->getExecutor())(); 119 case 1: return (*this->execCommand_->getExecutor())(this->param_[0]); 120 case 2: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1]); 121 case 3: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2]); 122 case 4: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2], this->param_[3]); 123 case 5: 124 default: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]); 125 } 126 } 127 else 128 { 129 COUT(0) << "call parsed" << std::endl; 130 COUT(5) << "CE_execute: " << this->string_ << "\n"; 131 return this->execCommand_->getExecutor()->parse(this->tokens_.subSet(this->execArgumentsOffset_), error, " "); 132 } 133 } 106 134 else 107 135 return MT_Type::Null; 108 136 } 109 137 110 std::string CommandEvaluation::complete() const 138 int CommandEvaluation::evaluateParams(bool bPrintError) 139 { 140 COUT(0) << "evaluate params" << std::endl; 141 this->bTriedToEvaluatedParams_ = true; 142 143 if (!this->execCommand_) 144 { 145 if (bPrintError) 146 COUT(1) << "Error: Can't evaluate params, no console command assigned." << std::endl; 147 return CommandExecutor::Error; 148 } 149 150 int error; 151 this->numberOfEvaluatedParams_ = this->execCommand_->getExecutor()->evaluateParams(this->tokens_.subSet(this->execArgumentsOffset_), this->param_, &error, " "); 152 if (!error) 153 this->bEvaluatedParams_ = true; 154 else if (bPrintError) 155 COUT(1) << "Error: Can't evaluate params, not enough arguments given." << std::endl; 156 157 return error; 158 } 159 160 void CommandEvaluation::setEvaluatedParameter(unsigned int index, const MultiType& param) 161 { 162 if (index < MAX_FUNCTOR_ARGUMENTS) 163 this->param_[index] = param; 164 } 165 166 MultiType CommandEvaluation::getEvaluatedParameter(unsigned int index) const 167 { 168 if (index < MAX_FUNCTOR_ARGUMENTS) 169 return this->param_[index]; 170 171 return MT_Type::Null; 172 } 173 174 std::string CommandEvaluation::complete() 111 175 { 112 176 if (!this->hintCommand_ || !this->hintCommand_->isActive()) … … 122 186 else 123 187 { 124 std::string output ;125 for (unsigned int i = 0; i < this->getNumberOfArguments() - 1; ++i)126 output += this->getToken(i) + ' ';188 std::string output = this->string_.substr(0, this->string_.find_last_of(' ') + 1); 189 // for (unsigned int i = 0; i < this->getNumberOfArguments() - 1; ++i) 190 // output += this->getToken(i) + ' '; 127 191 128 192 output += CommandEvaluation::getCommonBegin(this->possibleArguments_); … … 131 195 } 132 196 133 std::string CommandEvaluation::hint() const197 std::string CommandEvaluation::hint() 134 198 { 135 199 if (!this->hintCommand_ || !this->hintCommand_->isActive()) … … 165 229 } 166 230 167 void CommandEvaluation::retrievePossibleArguments() const231 void CommandEvaluation::retrievePossibleArguments() 168 232 { 169 233 this->bPossibleArgumentsRetrieved_ = true; -
code/branches/consolecommands3/src/libraries/core/command/CommandEvaluation.h
r7228 r7230 34 34 #include <string> 35 35 36 #include "ArgumentCompletionListElement.h"37 36 #include "util/SubString.h" 38 37 #include "util/MultiType.h" 38 #include "ArgumentCompletionListElement.h" 39 #include "Functor.h" 39 40 40 41 namespace orxonox … … 47 48 CommandEvaluation(); 48 49 49 int execute() const;50 MultiType query(int* error = 0) const;50 int execute(); 51 MultiType query(int* error = 0); 51 52 52 std::string complete() const; 53 std::string hint() const; 53 std::string complete(); 54 std::string hint(); 55 56 int evaluateParams(bool bPrintError = false); 54 57 55 58 inline bool isValid() const … … 59 62 { return this->execCommand_; } 60 63 61 // void setEvaluatedParameter(unsigned int index, MultiTypeparam);62 //MultiType getEvaluatedParameter(unsigned int index) const;64 void setEvaluatedParameter(unsigned int index, const MultiType& param); 65 MultiType getEvaluatedParameter(unsigned int index) const; 63 66 64 67 private: … … 69 72 const std::string& getToken(unsigned int i) const; 70 73 71 void retrievePossibleArguments() const;74 void retrievePossibleArguments(); 72 75 73 76 static void strip(ArgumentCompletionList& list, const std::string& fragment); … … 84 87 unsigned int execArgumentsOffset_; 85 88 unsigned int hintArgumentsOffset_; 86 mutable bool bPossibleArgumentsRetrieved_; 87 mutable ArgumentCompletionList possibleArguments_; 89 bool bPossibleArgumentsRetrieved_; 90 ArgumentCompletionList possibleArguments_; 91 92 bool bEvaluatedParams_; 93 bool bTriedToEvaluatedParams_; 94 unsigned int numberOfEvaluatedParams_; 95 MultiType param_[MAX_FUNCTOR_ARGUMENTS]; 88 96 }; 89 97 } -
code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc
r7229 r7230 67 67 COUT(0) << "evaluate" << std::endl; 68 68 evaluation = CommandExecutor::evaluate(command); 69 evaluation.evaluateParams(); 69 70 CommandExecutor::getInstance().cache(command, evaluation); 70 71 } -
code/branches/consolecommands3/src/libraries/core/command/Executor.cc
r7228 r7230 50 50 } 51 51 52 MultiType Executor::parse(const std::string& params, int* error, const std::string& delimiter, bool bPrintError) const52 MultiType Executor::parse(const std::string& arguments, int* error, const std::string& delimiter, bool bPrintError) const 53 53 { 54 if (error)55 *error = CommandExecutor::Success;54 return this->parse(SubString(arguments, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'), error, delimiter, bPrintError); 55 } 56 56 57 MultiType Executor::parse(const SubString& arguments, int* error, const std::string& delimiter, bool bPrintError) const 58 { 59 MultiType param[MAX_FUNCTOR_ARGUMENTS]; 60 unsigned int paramCount = this->evaluateParams(arguments, param, error, delimiter); 61 62 if (error && *error) 63 { 64 if (bPrintError) 65 COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << arguments.join() << ")." << std::endl; 66 return MT_Type::Null; 67 } 68 69 switch (paramCount) 70 { 71 case 0: return (*this->functor_)(); 72 case 1: return (*this->functor_)(param[0]); 73 case 2: return (*this->functor_)(param[0], param[1]); 74 case 3: return (*this->functor_)(param[0], param[1], param[2]); 75 case 4: return (*this->functor_)(param[0], param[1], param[2], param[3]); 76 case 5: 77 default: return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]); 78 } 79 } 80 81 int Executor::evaluateParams(const SubString& arguments, MultiType param[MAX_FUNCTOR_ARGUMENTS], int* error, const std::string& delimiter) const 82 { 57 83 unsigned int paramCount = this->functor_->getParamCount(); 84 unsigned int argumentCount = arguments.size(); 58 85 59 if (paramCount == 0) 86 // if there are not enough params given, check if there are default values 87 for (unsigned int i = argumentCount; i < paramCount; i++) 60 88 { 61 COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl; 62 return (*this->functor_)(); 63 } 64 else if (paramCount == 1) 65 { 66 const std::string& temp = getStripped(params); 67 if (!temp.empty()) 89 if (this->defaultValue_[i].null()) 68 90 { 69 COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl;70 return (*this->functor_)(params);71 }72 else if (!this->defaultValue_[0].null())73 {74 COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl;75 return (*this->functor_)(this->defaultValue_[0]);76 }77 else78 {79 if (bPrintError)80 COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << temp << ")." << std::endl;81 91 if (error) 82 92 *error = CommandExecutor::Incomplete; 83 return MT_Type::Null; 84 } 85 } 86 else 87 { 88 SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); 89 90 for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) 91 { 92 if (this->defaultValue_[i].null()) 93 { 94 if (bPrintError) 95 COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << params << ")." << std::endl; 96 if (error) 97 *error = CommandExecutor::Incomplete; 98 return MT_Type::Null; 99 } 100 } 101 102 MultiType param[MAX_FUNCTOR_ARGUMENTS]; 103 COUT(5) << "Calling Executor " << this->name_ << " through parser with " << paramCount << " parameters, using " << tokens.size() << " tokens ("; 104 for (unsigned int i = 0; i < tokens.size() && i < MAX_FUNCTOR_ARGUMENTS; i++) 105 { 106 param[i] = tokens[i]; 107 if (i != 0) 108 { 109 COUT(5) << ", "; 110 } 111 COUT(5) << tokens[i]; 112 } 113 COUT(5) << ") and " << std::max((int)paramCount - (int)tokens.size(), 0) << " default values ("; 114 for (unsigned int i = tokens.size(); i < paramCount; i++) 115 { 116 param[i] = this->defaultValue_[i]; 117 if (i != 0) 118 { 119 COUT(5) << ", "; 120 } 121 COUT(5) << this->defaultValue_[i]; 122 } 123 COUT(5) << ")." << std::endl; 124 125 if ((tokens.size() > paramCount) && (this->functor_->getTypenameParam(paramCount - 1) == "string")) 126 param[paramCount - 1] = tokens.subSet(paramCount - 1).join(); 127 128 switch(paramCount) 129 { 130 case 2: 131 return (*this->functor_)(param[0], param[1]); 132 case 3: 133 return (*this->functor_)(param[0], param[1], param[2]); 134 case 4: 135 return (*this->functor_)(param[0], param[1], param[2], param[3]); 136 case 5: 137 return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]); 93 return 0; 138 94 } 139 95 } 140 96 141 return MT_Type::Null; 142 } 97 // assign all given arguments to the multitypes 98 for (unsigned int i = 0; i < std::min(argumentCount, MAX_FUNCTOR_ARGUMENTS); i++) 99 param[i] = arguments[i]; 143 100 144 bool Executor::evaluate(const std::string& params, MultiType param[5], const std::string& delimiter) const145 {146 unsigned int paramCount = this->functor_->getParamCount();101 // fill the remaining multitypes with default values 102 for (unsigned int i = argumentCount; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 103 param[i] = this->defaultValue_[i]; 147 104 148 if (paramCount == 1) 149 { 150 // only one param: check if there are params given, otherwise try to use default values 151 if (!getStripped(params).empty()) 152 { 153 param[0] = params; 154 this->functor_->evaluateParam(0, param[0]); 155 return true; 156 } 157 else if (!this->defaultValue_[0].null()) 158 { 159 param[0] = this->defaultValue_[0]; 160 this->functor_->evaluateParam(0, param[0]); 161 return true; 162 } 163 return false; 164 } 165 else 166 { 167 // more than one param 168 SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); 105 // assign the remaining arguments all to the last parameter if it is a string 106 if ((paramCount <= MAX_FUNCTOR_ARGUMENTS) &&(argumentCount > paramCount) && (paramCount == 1 || this->functor_->getTypenameParam(paramCount - 1) == "string")) 107 param[paramCount - 1] = arguments.subSet(paramCount - 1).join(delimiter); 169 108 170 // if there are not enough params given, check if there are default values 171 for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) 172 if (this->defaultValue_[i].null()) 173 return false; 109 // evaluate the param types through the functor 110 for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 111 this->functor_->evaluateParam(i, param[i]); 174 112 175 // assign all given arguments to the multitypes 176 for (unsigned int i = 0; i < std::min(tokens.size(), MAX_FUNCTOR_ARGUMENTS); i++) 177 param[i] = tokens[i]; 178 179 // fill the remaining multitypes with default values 180 for (unsigned int i = tokens.size(); i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 181 param[i] = this->defaultValue_[i]; 182 183 // evaluate the param types through the functor 184 for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 185 this->functor_->evaluateParam(i, param[i]); 186 187 return true; 188 } 113 if (error) 114 *error = CommandExecutor::Success; 115 return paramCount; 189 116 } 190 117 -
code/branches/consolecommands3/src/libraries/core/command/Executor.h
r7228 r7230 59 59 { return (*this->functor_)(param1, param2, param3, param4, param5); } 60 60 61 MultiType parse(const std::string& params, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const; 62 63 bool evaluate(const std::string& params, MultiType param[5], const std::string& delimiter = " ") const; 61 MultiType parse(const std::string& arguments, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const; 62 MultiType parse(const SubString& arguments, int* error = 0, const std::string& delimiter = " ", bool bPrintError = false) const; 63 64 int evaluateParams(const SubString& arguments, MultiType param[MAX_FUNCTOR_ARGUMENTS], int* error = 0, const std::string& delimiter = " ") const; 64 65 65 66 inline void setFunctor(const FunctorPtr& functor)
Note: See TracChangeset
for help on using the changeset viewer.