From 839ab1ee2d9c224d74399dbe9a0ac7501e07dc04 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 5 Jun 2021 12:41:46 +0200 Subject: [PATCH] Built include files --- INCLUDE/Hazelnupp.cpp | 99 +++++++++------- INCLUDE/Hazelnupp.h | 258 ++++++++++++++++++++++++------------------ 2 files changed, 207 insertions(+), 150 deletions(-) diff --git a/INCLUDE/Hazelnupp.cpp b/INCLUDE/Hazelnupp.cpp index 0f07f8d..3d9e6d6 100644 --- a/INCLUDE/Hazelnupp.cpp +++ b/INCLUDE/Hazelnupp.cpp @@ -217,14 +217,14 @@ void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv) void Hazelnupp::ExpandAbbreviations() { // Abort if no abbreviations - if (abbreviations.size() == 0) + if (parameterAbreviations.size() == 0) return; for (std::string& arg : rawArgs) { // Is arg registered as an abbreviation? - auto abbr = abbreviations.find(arg); - if (abbr != abbreviations.end()) + auto abbr = parameterAbreviations.find(arg); + if (abbr != parameterAbreviations.end()) { // Yes: replace arg with the long form arg = abbr->second; @@ -252,6 +252,12 @@ Value* Hazelnupp::ParseValue(const std::vector& values, const Param (constraint->wantedType == DATA_TYPE::LIST)) return new ListValue(); + // Is a string forced via a constraint? If yes, return an empty string + if ((constrainType) && + (constraint->wantedType == DATA_TYPE::STRING)) + return new StringValue(""); + + // Else, just return the void type return new VoidValue; } @@ -393,25 +399,35 @@ void Hazelnp::Hazelnupp::RegisterDescription(const std::string& parameter, const return; } -const std::string Hazelnp::Hazelnupp::GetDescription(const std::string& parameter) const +const std::string& Hazelnp::Hazelnupp::GetDescription(const std::string& parameter) const { // Do we already have a description for this parameter? - const auto par = parameterDescriptions.find(parameter); - if (par == parameterDescriptions.end()) + if (!HasDescription(parameter)) // No? Then return "" - return ""; + return Placeholders::g_emptyString; // We do? Then return it - return par->second; + return parameterDescriptions.find(parameter)->second; } -void Hazelnp::Hazelnupp::ClearDescription(const std::string& parameter) +bool Hazelnupp::HasDescription(const std::string& parameter) const +{ + return parameterDescriptions.find(parameter) != parameterDescriptions.end(); +} + +void Hazelnupp::ClearDescription(const std::string& parameter) { // This will just do nothing if the entry does not exist parameterDescriptions.erase(parameter); return; } +void Hazelnp::Hazelnupp::ClearDescriptions() +{ + parameterDescriptions.clear(); + return; +} + std::string Hazelnupp::GenerateDocumentation() const { std::stringstream ss; @@ -445,7 +461,7 @@ std::string Hazelnupp::GenerateDocumentation() const // Collect abbreviations // first value is abbreviation, second is long form - for (const auto& it : abbreviations) + for (const auto& it : parameterAbreviations) { // Do we already have that param in the paramInfo set? if (paramInfos.find(it.second) == paramInfos.end()) @@ -456,7 +472,7 @@ std::string Hazelnupp::GenerateDocumentation() const } // Collect constraints - for (const auto& it : constraints) + for (const auto& it : parameterConstraints) { // Do we already have that param in the paramInfo set? if (paramInfos.find(it.first) == paramInfos.end()) @@ -524,7 +540,7 @@ std::string Hazelnupp::GenerateDocumentation() const void Hazelnupp::ApplyConstraints() { // Enforce required parameters / default values - for (const auto& pc : constraints) + for (const auto& pc : parameterConstraints) // Parameter in question is not supplied if (!HasParam(pc.second.key)) { @@ -555,6 +571,17 @@ void Hazelnupp::ApplyConstraints() return; } +ParamConstraint Hazelnupp::GetConstraint(const std::string& parameter) const +{ + return parameterConstraints.find(parameter)->second; +} + +void Hazelnupp::ClearConstraint(const std::string& parameter) +{ + parameterConstraints.erase(parameter); + return; +} + const std::string& Hazelnupp::GetExecutableName() const { return executableName; @@ -571,50 +598,45 @@ const Value& Hazelnupp::operator[](const std::string& key) const void Hazelnupp::RegisterAbbreviation(const std::string& abbrev, const std::string& target) { - abbreviations.insert(std::pair(abbrev, target)); + parameterAbreviations.insert(std::pair(abbrev, target)); return; } const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const { - return abbreviations.find(abbrev)->second; + if (!HasAbbreviation(abbrev)) + return Placeholders::g_emptyString; + + return parameterAbreviations.find(abbrev)->second; } bool Hazelnupp::HasAbbreviation(const std::string& abbrev) const { - return abbreviations.find(abbrev) != abbreviations.end(); + return parameterAbreviations.find(abbrev) != parameterAbreviations.end(); +} + +void Hazelnupp::ClearAbbreviation(const std::string& abbrevation) +{ + parameterAbreviations.erase(abbrevation); + return; } void Hazelnupp::ClearAbbreviations() { - abbreviations.clear(); + parameterAbreviations.clear(); return; } -void Hazelnupp::RegisterConstraints(const std::vector& constraints) +void Hazelnupp::RegisterConstraint(const std::string& key, const ParamConstraint& constraint) { - for (const ParamConstraint& pc : constraints) - { - // Does this constraint already exist? - const auto constraint = this->constraints.find(pc.key); - // If yes, replace it. - if (constraint != this->constraints.end()) - constraint->second = pc; - - // Else, create a new pair - else - this->constraints.insert(std::pair( - pc.key, - pc - )); - } - + // Magic syntax, wooo + (parameterConstraints[key] = constraint).key = key; return; } void Hazelnupp::ClearConstraints() { - constraints.clear(); + parameterConstraints.clear(); return; } @@ -626,9 +648,9 @@ void Hazelnupp::SetCrashOnFail(bool crashOnFail) const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) const { - const auto constraint = constraints.find(key); + const auto constraint = parameterConstraints.find(key); - if (constraint == constraints.end()) + if (constraint == parameterConstraints.end()) return nullptr; return &constraint->second; @@ -1165,11 +1187,12 @@ double VoidValue::GetFloat32() const std::string VoidValue::GetString() const { - throw HazelnuppValueNotConvertibleException(); + return ""; } const std::vector& VoidValue::GetList() const { - throw HazelnuppValueNotConvertibleException(); + static const std::vector empty; + return empty; } diff --git a/INCLUDE/Hazelnupp.h b/INCLUDE/Hazelnupp.h index a04e934..86e5dcd 100644 --- a/INCLUDE/Hazelnupp.h +++ b/INCLUDE/Hazelnupp.h @@ -42,6 +42,19 @@ namespace Hazelnp }; } +/*** ../Hazelnupp/Placeholders.h ***/ + +#include + +namespace Hazelnp +{ + namespace Placeholders + { + //! The only purpose of this is to provide the ability to return an empty string as an error for std::string& methods. + static const std::string g_emptyString; + } +} + /*** ../Hazelnupp/HazelnuppException.h ***/ #include @@ -168,10 +181,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; @@ -179,10 +191,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; @@ -190,9 +201,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 }, @@ -201,9 +211,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; @@ -219,6 +226,13 @@ 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; }; } @@ -273,6 +287,101 @@ namespace Hazelnp }; } +/*** ../Hazelnupp/ListValue.h ***/ + +#include + +namespace Hazelnp +{ + /** Specializations for list values (uses std::vector) + */ + class ListValue : public Value + { + public: + ListValue(); + ~ListValue() override; + + //! Will return a deeopopy of this object + Value* Deepcopy() const override; + + //! Will return a string suitable for an std::ostream; + std::string GetAsOsString() const override; + + //! Will add this value to the list + void AddValue(const Value* value); + + //! Will return the raw value + const std::vector& GetValue() const; + + operator std::vector() const; + + //! Throws HazelnuppValueNotConvertibleException + long long int GetInt64() const override; + //! Throws HazelnuppValueNotConvertibleException + int GetInt32() const override; + + //! Throws HazelnuppValueNotConvertibleException + long double GetFloat64() const override; + //! Throws HazelnuppValueNotConvertibleException + double GetFloat32() const override; + + //! Throws HazelnuppValueNotConvertibleException + std::string GetString() const override; + + //! Will return this values list + const std::vector& GetList() const override; + + private: + std::vector value; + }; +} + +/*** ../Hazelnupp/StringValue.h ***/ + +#include + +namespace Hazelnp +{ + /** Specializations for string values (uses std::string) + */ + class StringValue : public Value + { + public: + StringValue(const std::string& value); + ~StringValue() override {}; + + //! Will return a deeopopy of this object + Value* Deepcopy() const override; + + //! Will return a string suitable for an std::ostream; + std::string GetAsOsString() const override; + + //! Will return the raw value + const std::string& GetValue() const; + + operator std::string() const; + + //! Throws HazelnuppValueNotConvertibleException + long long int GetInt64() const override; + //! Throws HazelnuppValueNotConvertibleException + int GetInt32() const override; + + //! Throws HazelnuppValueNotConvertibleException + long double GetFloat64() const override; + //! Throws HazelnuppValueNotConvertibleException + double GetFloat32() const override; + + //! Will return this value as a string + std::string GetString() const override; + + //! Throws HazelnuppValueNotConvertibleException + const std::vector& GetList() const override; + + private: + std::string value; + }; +} + /*** ../Hazelnupp/VoidValue.h ***/ @@ -302,10 +411,10 @@ namespace Hazelnp //! Throws HazelnuppValueNotConvertibleException double GetFloat32() const override; - //! Throws HazelnuppValueNotConvertibleException + //! Returns an empty string std::string GetString() const override; - //! Throws HazelnuppValueNotConvertibleException + //! Returns an empty list const std::vector& GetList() const; }; } @@ -420,17 +529,30 @@ namespace Hazelnp //! Will register an abbreviation (like -f for --force) void RegisterAbbreviation(const std::string& abbrev, const std::string& target); - //! Will return the long form of an abbreviation (like --force for -f) + //! Will return the long form of an abbreviation (like --force for -f) + //! Returns "" if no match is found const std::string& GetAbbreviation(const std::string& abbrev) const; //! Will check wether or not an abbreviation is registered bool HasAbbreviation(const std::string& abbrev) const; + //! Will delete the abbreviation for a given parameter. + //! IMPORTANT: This parameter is the abbreviation! Not the long form! + void ClearAbbreviation(const std::string& abbrevation); + //! 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; + + //! Will the constraint of a specific parameter + void ClearConstraint(const std::string& parameter); //! Will delete all constraints void ClearConstraints(); @@ -460,11 +582,17 @@ namespace Hazelnp //! Will return a short description for a parameter, if it exists. //! Empty string if it does not exist. - const std::string GetDescription(const std::string& parameter) const; + const std::string& GetDescription(const std::string& parameter) const; + + //! Returns whether or not a given parameter has a registered description + bool HasDescription(const std::string& parameter) const; //! Will delete the description of a parameter if it exists. void ClearDescription(const std::string& parameter); + //! Will delete all parameter descriptions + void ClearDescriptions(); + //! Will generate a text-based documentation suited to show the user, for example on --help. std::string GenerateDocumentation() const; @@ -491,15 +619,16 @@ namespace Hazelnp std::unordered_map parameters; //! These are abbreviations. Like, -f for --force. - std::unordered_map abbreviations; + std::unordered_map parameterAbreviations; //! Parameter constraints, mapped to keys - std::unordered_map constraints; + std::unordered_map parameterConstraints; //! Raw argv std::vector rawArgs; //! Short descriptions for parameters + //! First member is the abbreviation std::unordered_map parameterDescriptions; //! A brief description of the application to be added to the generated documentation. Optional. @@ -513,101 +642,6 @@ namespace Hazelnp }; } -/*** ../Hazelnupp/ListValue.h ***/ - -#include - -namespace Hazelnp -{ - /** Specializations for list values (uses std::vector) - */ - class ListValue : public Value - { - public: - ListValue(); - ~ListValue() override; - - //! Will return a deeopopy of this object - Value* Deepcopy() const override; - - //! Will return a string suitable for an std::ostream; - std::string GetAsOsString() const override; - - //! Will add this value to the list - void AddValue(const Value* value); - - //! Will return the raw value - const std::vector& GetValue() const; - - operator std::vector() const; - - //! Throws HazelnuppValueNotConvertibleException - long long int GetInt64() const override; - //! Throws HazelnuppValueNotConvertibleException - int GetInt32() const override; - - //! Throws HazelnuppValueNotConvertibleException - long double GetFloat64() const override; - //! Throws HazelnuppValueNotConvertibleException - double GetFloat32() const override; - - //! Throws HazelnuppValueNotConvertibleException - std::string GetString() const override; - - //! Will return this values list - const std::vector& GetList() const override; - - private: - std::vector value; - }; -} - -/*** ../Hazelnupp/StringValue.h ***/ - -#include - -namespace Hazelnp -{ - /** Specializations for string values (uses std::string) - */ - class StringValue : public Value - { - public: - StringValue(const std::string& value); - ~StringValue() override {}; - - //! Will return a deeopopy of this object - Value* Deepcopy() const override; - - //! Will return a string suitable for an std::ostream; - std::string GetAsOsString() const override; - - //! Will return the raw value - const std::string& GetValue() const; - - operator std::string() const; - - //! Throws HazelnuppValueNotConvertibleException - long long int GetInt64() const override; - //! Throws HazelnuppValueNotConvertibleException - int GetInt32() const override; - - //! Throws HazelnuppValueNotConvertibleException - long double GetFloat64() const override; - //! Throws HazelnuppValueNotConvertibleException - double GetFloat32() const override; - - //! Will return this value as a string - std::string GetString() const override; - - //! Throws HazelnuppValueNotConvertibleException - const std::vector& GetList() const override; - - private: - std::string value; - }; -} - /*** ../Hazelnupp/FloatValue.h ***/ #include