Changed RegisterConstraint to a more streamlined approach

This commit is contained in:
Leonetienne 2021-06-05 12:16:50 +02:00
parent d6c1384991
commit dee1a57da7
5 changed files with 20 additions and 33 deletions

Binary file not shown.

View File

@ -548,24 +548,10 @@ void Hazelnupp::ClearAbbreviations()
return; return;
} }
void Hazelnupp::RegisterConstraints(const std::vector<ParamConstraint>& parameterConstraints) void Hazelnupp::RegisterConstraint(const std::string& key, const ParamConstraint& constraint)
{ {
for (const ParamConstraint& pc : parameterConstraints) // Magic syntax, wooo
{ (parameterConstraints[key] = constraint).key = key;
// Does this constraint already exist?
const auto constraint = this->parameterConstraints.find(pc.key);
// If yes, replace it.
if (constraint != this->parameterConstraints.end())
constraint->second = pc;
// Else, create a new pair
else
this->parameterConstraints.insert(std::pair<std::string, ParamConstraint>(
pc.key,
pc
));
}
return; return;
} }

View File

@ -46,8 +46,10 @@ namespace Hazelnp
//! Will delete all abbreviations //! Will delete all abbreviations
void ClearAbbreviations(); void ClearAbbreviations();
//! Will register parameter constraints //! Will register a constraint for a parameter.
void RegisterConstraints(const std::vector<ParamConstraint>& constraints); //! IMPORTANT: Any parameter can only have ONE constraint. Applying a new one will overwrite the old one!
//! Construct the ParamConstraint struct yourself to combine Require and TypeSafety! You can also use the ParamConstraint constructor!
void RegisterConstraint(const std::string& key, const ParamConstraint& constraint);
//! Will return the constraint information for a specific parameter //! Will return the constraint information for a specific parameter
ParamConstraint GetConstraint(const std::string& parameter) const; ParamConstraint GetConstraint(const std::string& parameter) const;

View File

@ -13,10 +13,9 @@ namespace Hazelnp
//! Constructs a require constraint. //! Constructs a require constraint.
//! Think of the default value like of a list ofparameters. Like {"--width", "800"} //! Think of the default value like of a list ofparameters. Like {"--width", "800"}
static ParamConstraint Require(const std::string& key, const std::vector<std::string>& defaultValue = {}, bool required = true) static ParamConstraint Require(const std::vector<std::string>& defaultValue = {}, bool required = true)
{ {
ParamConstraint pc; ParamConstraint pc;
pc.key = key;
pc.defaultValue = defaultValue; pc.defaultValue = defaultValue;
pc.required = required; pc.required = required;
@ -24,10 +23,9 @@ namespace Hazelnp
} }
//! Constructs a type-safety constraint //! Constructs a type-safety constraint
static ParamConstraint TypeSafety(const std::string& key, DATA_TYPE wantedType, bool constrainType = true) static ParamConstraint TypeSafety(DATA_TYPE wantedType, bool constrainType = true)
{ {
ParamConstraint pc; ParamConstraint pc;
pc.key = key;
pc.constrainType = constrainType; pc.constrainType = constrainType;
pc.wantedType = wantedType; pc.wantedType = wantedType;
@ -35,9 +33,8 @@ namespace Hazelnp
} }
//! Whole constructor //! Whole constructor
ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector<std::string>& defaultValue, bool required) ParamConstraint(bool constrainType, DATA_TYPE wantedType, const std::vector<std::string>& defaultValue, bool required)
: :
key{ key },
constrainType{ constrainType }, constrainType{ constrainType },
wantedType{ wantedType }, wantedType{ wantedType },
defaultValue{ defaultValue }, defaultValue{ defaultValue },
@ -46,9 +43,6 @@ namespace Hazelnp
return; return;
} }
//! The key of the parameter to constrain
std::string key;
//! Should this parameter be forced to be of a certain type? //! Should this parameter be forced to be of a certain type?
//! Remember to set `constrainTo` to the wanted type //! Remember to set `constrainTo` to the wanted type
bool constrainType = false; bool constrainType = false;
@ -64,5 +58,12 @@ namespace Hazelnp
//! If set to true, and no default value set, //! If set to true, and no default value set,
//! an error will be produced if this parameter is not supplied by the user. //! an error will be produced if this parameter is not supplied by the user.
bool required = false; bool required = false;
private:
//! The parameter this constraint is for.
//! This value is automatically set by Hazelnupp.
std::string key;
friend class Hazelnupp;
}; };
} }

View File

@ -20,11 +20,9 @@ int main(int argc, char** argv)
nupp.RegisterAbbreviation("-w", "--width"); nupp.RegisterAbbreviation("-w", "--width");
nupp.RegisterAbbreviation("-h", "--height"); nupp.RegisterAbbreviation("-h", "--height");
nupp.RegisterConstraints({ nupp.RegisterConstraint("--width", ParamConstraint::TypeSafety(DATA_TYPE::FLOAT));
ParamConstraint::TypeSafety("--width", DATA_TYPE::FLOAT), nupp.RegisterConstraint("--name", ParamConstraint(true, DATA_TYPE::LIST, {"peter", "hannes"}, true));
ParamConstraint("--name", true, DATA_TYPE::LIST, {"peter", "hannes"}, true), nupp.RegisterConstraint("--fruit", ParamConstraint(true, DATA_TYPE::STRING, {}, true));
ParamConstraint("--fruit", true, DATA_TYPE::STRING, {}, true)
});
nupp.Parse(argc, argv); nupp.Parse(argc, argv);