29 #include "internal/berryCommandUtils.h"
37 const uint ParameterizedCommand::HASH_CODE_NOT_COMPUTED = 0;
38 const uint ParameterizedCommand::HASH_FACTOR = 89;
39 const uint ParameterizedCommand::HASH_INITIAL =
qHash(
"berry::ParameterizedCommand");
42 const QList<Parameterization>& params)
43 : command(command), hashCode(HASH_CODE_NOT_COMPUTED)
47 throw Poco::NullPointerException(
48 "A parameterized command cannot have a null command");
51 QList<IParameter::Pointer> parameters;
54 parameters = command->GetParameters();
55 }
catch (
const NotDefinedException* )
59 if (!params.empty() && !parameters.empty())
61 for (
int j = 0; j < parameters.size(); j++)
63 for (
int i = 0; i < params.size(); i++)
65 if (parameters[j] == params[i].GetParameter())
67 this->parameterizations.push_back(params[i]);
77 const bool thisDefined = this->command->IsDefined();
78 const bool otherDefined = command->command->IsDefined();
79 if (!thisDefined || !otherDefined)
81 return CommandUtils::Compare(thisDefined, otherDefined) < 0;
93 catch (
const NotDefinedException* )
95 throw CommandException(
96 "Concurrent modification of a command's defined state");
109 if (!(this->command == command->command))
114 return CommandUtils::Equals(this->parameterizations, command->parameterizations);
125 return command->ExecuteWithChecks(excEvent);
135 return command->GetId();
142 QTextStream nameBuffer(&name);
143 nameBuffer << command->GetName() <<
" (";
144 const unsigned int parameterizationCount = (
unsigned int) parameterizations.size();
145 for (
unsigned int i = 0; i < parameterizationCount; i++)
148 nameBuffer << parameterization.
GetParameter()->GetName() <<
": ";
153 catch (
const ParameterValuesException& )
162 if (i + 1 < parameterizationCount)
175 QHash<QString, QString> parameterMap;
176 for (
int i = 0; i < parameterizations.size(); i++)
179 parameterMap.insert(parameterization.
GetParameter()->GetId(),
187 if (hashCode == HASH_CODE_NOT_COMPUTED)
189 hashCode = HASH_INITIAL * HASH_FACTOR + (command ? command->HashCode() : 0);
190 hashCode = hashCode * HASH_FACTOR;
191 for (
int i = 0; i < parameterizations.size(); i++)
193 hashCode += parameterizations[i].HashCode();
196 if (hashCode == HASH_CODE_NOT_COMPUTED)
206 const QString escapedId(this->Escape(this->
GetId()));
208 if (parameterizations.empty())
214 QTextStream buffer(&str);
217 for (
int i = 0; i < parameterizations.size(); i++)
227 const QString parameterId(parameterization.
GetParameter()->GetId());
228 const QString escapedParameterId(this->Escape(parameterId));
230 buffer << escapedParameterId;
232 const QString parameterValue(parameterization.
GetValue());
233 if (!parameterValue.isEmpty())
235 const QString escapedParameterValue(this->Escape(parameterValue));
237 << escapedParameterValue;
249 QTextStream buffer(&str);
250 buffer <<
"ParameterizedCommand(" << command->ToString() <<
","
251 << CommandUtils::ToString(parameterizations) <<
")";
255 QList<ParameterizedCommand::Pointer>
258 QList<IParameter::Pointer> parameters(command->GetParameters());
260 typedef QList<QList<Parameterization> > ExpandedParamsType;
261 const ExpandedParamsType expansion(ExpandParameters(0, parameters));
262 QList<ParameterizedCommand::Pointer> combinations;
264 for (ExpandedParamsType::const_iterator expansionItr = expansion.begin();
265 expansionItr != expansion.end(); ++expansionItr)
267 QList<Parameterization> combination(*expansionItr);
269 QList<Parameterization> parameterizations(combination);
272 combinations.push_back(pCmd);
279 const QHash<QString, Object::Pointer>& parameters)
282 if (parameters.empty())
290 QList<Parameterization> parms;
293 for (QHash<QString, Object::Pointer>::const_iterator i = parameters.begin();
294 i != parameters.end(); ++i)
296 QString key(i.key());
309 QString val = i.value()->ToString();
317 QString val(valueConverter->ConvertToString(i.value()));
322 QString val = i.value()->ToString();
333 catch (
const NotDefinedException* )
336 catch (
const ParameterValueConversionException* )
342 QString ParameterizedCommand::Escape(
const QString& rawText)
347 for (QString::const_iterator i = rawText.begin();
348 i != rawText.end(); ++i)
351 QString::value_type c = *i;
364 if (buffer.isEmpty())
371 QList<QList<Parameterization> > ParameterizedCommand::ExpandParameters(
372 unsigned int startIndex,
const QList<IParameter::Pointer>& parameters)
374 typedef QList<QList<Parameterization> > ReturnType;
376 const int nextIndex = startIndex + 1;
377 const bool noMoreParameters = (nextIndex >= parameters.size());
380 ReturnType parameterizations;
382 if (parameter->IsOptional())
384 parameterizations.push_back(QList<Parameterization>());
387 IParameterValues* values =
nullptr;
390 values = parameter->GetValues();
392 catch (
const ParameterValuesException& )
394 if (noMoreParameters)
396 return parameterizations;
400 return ExpandParameters(nextIndex, parameters);
403 const QHash<QString,QString> parameterValues = values->GetParameterValues();
404 for (IParameter::ParameterValues::const_iterator parameterValueItr =
405 parameterValues.begin(); parameterValueItr != parameterValues.end(); ++parameterValueItr)
407 QList<Parameterization> combination;
408 combination.push_back(
409 Parameterization(parameter, parameterValueItr.value()));
410 parameterizations.push_back(combination);
414 if (noMoreParameters)
417 return parameterizations;
421 ReturnType suffixes(ExpandParameters(nextIndex, parameters));
422 if (suffixes.empty())
425 return parameterizations;
428 ReturnType returnValue;
429 for (ReturnType::iterator suffixItr = suffixes.begin(); suffixItr
430 != suffixes.end(); ++suffixItr)
432 for (ReturnType::iterator combinationItr = parameterizations.begin(); combinationItr
433 != parameterizations.end(); ++combinationItr)
435 QList<Parameterization> newCombination(*combinationItr);
436 newCombination.append(*suffixItr);
437 returnValue.push_back(newCombination);
static const char PARAMETER_START_CHAR
Light weight base class for most BlueBerry classes.
SmartPointer< const IParameter > GetParameter() const
static const char ESCAPE_CHAR
Implements transparent reference counting.
static ParameterizedCommand::Pointer GenerateCommand(const SmartPointer< Command > command, const QHash< QString, Object::Pointer > ¶meters)
Object::Pointer ExecuteWithChecks(const Object::ConstPointer &trigger, const Object::Pointer &applicationContext)
berry::SmartPointer< Self > Pointer
static const char PARAMETER_END_CHAR
QString ToString() const override
QHash< QString, QString > GetParameterMap() const
ParameterizedCommand(const SmartPointer< Command > &command, const QList< Parameterization > ¶meterizations)
static const char PARAMETER_SEPARATOR_CHAR
static const char ID_VALUE_CHAR
bool operator==(const Object *object) const override
uint HashCode() const override
SmartPointer< Command > GetCommand() const
QString GetValueName() const
static QList< ParameterizedCommand::Pointer > GenerateCombinations(const SmartPointer< Command > command)
bool operator<(const Object *object) const override
uint qHash(const berry::Object &o)