diff --git a/Hazelnupp.vpp b/Hazelnupp.vpp index 0652e27..60a5f6d 100644 Binary files a/Hazelnupp.vpp and b/Hazelnupp.vpp differ diff --git a/Hazelnupp/Hazelnupp.cpp b/Hazelnupp/Hazelnupp.cpp index 2f6a0b2..fbc7214 100644 --- a/Hazelnupp/Hazelnupp.cpp +++ b/Hazelnupp/Hazelnupp.cpp @@ -548,24 +548,10 @@ void Hazelnupp::ClearAbbreviations() return; } -void Hazelnupp::RegisterConstraints(const std::vector& parameterConstraints) +void Hazelnupp::RegisterConstraint(const std::string& key, const ParamConstraint& constraint) { - for (const ParamConstraint& pc : parameterConstraints) - { - // 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( - pc.key, - pc - )); - } - + // Magic syntax, wooo + (parameterConstraints[key] = constraint).key = key; return; } diff --git a/Hazelnupp/Hazelnupp.h b/Hazelnupp/Hazelnupp.h index bd957e0..3d4b073 100644 --- a/Hazelnupp/Hazelnupp.h +++ b/Hazelnupp/Hazelnupp.h @@ -46,8 +46,10 @@ namespace Hazelnp //! Will delete all abbreviations void ClearAbbreviations(); - //! Will register parameter constraints - void RegisterConstraints(const std::vector& constraints); + //! Will register a constraint for a parameter. + //! 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 ParamConstraint GetConstraint(const std::string& parameter) const; diff --git a/Hazelnupp/ParamConstraint.h b/Hazelnupp/ParamConstraint.h index 4a10621..32a3981 100644 --- a/Hazelnupp/ParamConstraint.h +++ b/Hazelnupp/ParamConstraint.h @@ -13,10 +13,9 @@ namespace Hazelnp //! Constructs a require constraint. //! Think of the default value like of a list ofparameters. Like {"--width", "800"} - static ParamConstraint Require(const std::string& key, const std::vector& defaultValue = {}, bool required = true) + static ParamConstraint Require(const std::vector& defaultValue = {}, bool required = true) { ParamConstraint pc; - pc.key = key; pc.defaultValue = defaultValue; pc.required = required; @@ -24,10 +23,9 @@ namespace Hazelnp } //! 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; - pc.key = key; pc.constrainType = constrainType; pc.wantedType = wantedType; @@ -35,9 +33,8 @@ namespace Hazelnp } //! Whole constructor - ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector& defaultValue, bool required) + ParamConstraint(bool constrainType, DATA_TYPE wantedType, const std::vector& defaultValue, bool required) : - key{ key }, constrainType{ constrainType }, wantedType{ wantedType }, defaultValue{ defaultValue }, @@ -46,9 +43,6 @@ namespace Hazelnp return; } - //! The key of the parameter to constrain - std::string key; - //! Should this parameter be forced to be of a certain type? //! Remember to set `constrainTo` to the wanted type bool constrainType = false; @@ -64,5 +58,12 @@ namespace Hazelnp //! If set to true, and no default value set, //! an error will be produced if this parameter is not supplied by the user. bool required = false; + + private: + //! The parameter this constraint is for. + //! This value is automatically set by Hazelnupp. + std::string key; + + friend class Hazelnupp; }; } diff --git a/Hazelnupp/main.cpp b/Hazelnupp/main.cpp index 5e68c5d..719f0de 100644 --- a/Hazelnupp/main.cpp +++ b/Hazelnupp/main.cpp @@ -20,11 +20,9 @@ int main(int argc, char** argv) nupp.RegisterAbbreviation("-w", "--width"); nupp.RegisterAbbreviation("-h", "--height"); - nupp.RegisterConstraints({ - ParamConstraint::TypeSafety("--width", DATA_TYPE::FLOAT), - ParamConstraint("--name", true, DATA_TYPE::LIST, {"peter", "hannes"}, true), - ParamConstraint("--fruit", true, DATA_TYPE::STRING, {}, true) - }); + nupp.RegisterConstraint("--width", ParamConstraint::TypeSafety(DATA_TYPE::FLOAT)); + nupp.RegisterConstraint("--name", ParamConstraint(true, DATA_TYPE::LIST, {"peter", "hannes"}, true)); + nupp.RegisterConstraint("--fruit", ParamConstraint(true, DATA_TYPE::STRING, {}, true)); nupp.Parse(argc, argv);