Built include files

This commit is contained in:
Leonetienne 2021-06-05 12:41:46 +02:00
parent 8affa1f512
commit 839ab1ee2d
2 changed files with 207 additions and 150 deletions

View File

@ -217,14 +217,14 @@ void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv)
void Hazelnupp::ExpandAbbreviations() void Hazelnupp::ExpandAbbreviations()
{ {
// Abort if no abbreviations // Abort if no abbreviations
if (abbreviations.size() == 0) if (parameterAbreviations.size() == 0)
return; return;
for (std::string& arg : rawArgs) for (std::string& arg : rawArgs)
{ {
// Is arg registered as an abbreviation? // Is arg registered as an abbreviation?
auto abbr = abbreviations.find(arg); auto abbr = parameterAbreviations.find(arg);
if (abbr != abbreviations.end()) if (abbr != parameterAbreviations.end())
{ {
// Yes: replace arg with the long form // Yes: replace arg with the long form
arg = abbr->second; arg = abbr->second;
@ -252,6 +252,12 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
(constraint->wantedType == DATA_TYPE::LIST)) (constraint->wantedType == DATA_TYPE::LIST))
return new ListValue(); 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; return new VoidValue;
} }
@ -393,25 +399,35 @@ void Hazelnp::Hazelnupp::RegisterDescription(const std::string& parameter, const
return; 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? // Do we already have a description for this parameter?
const auto par = parameterDescriptions.find(parameter); if (!HasDescription(parameter))
if (par == parameterDescriptions.end())
// No? Then return "" // No? Then return ""
return ""; return Placeholders::g_emptyString;
// We do? Then return it // 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 // This will just do nothing if the entry does not exist
parameterDescriptions.erase(parameter); parameterDescriptions.erase(parameter);
return; return;
} }
void Hazelnp::Hazelnupp::ClearDescriptions()
{
parameterDescriptions.clear();
return;
}
std::string Hazelnupp::GenerateDocumentation() const std::string Hazelnupp::GenerateDocumentation() const
{ {
std::stringstream ss; std::stringstream ss;
@ -445,7 +461,7 @@ std::string Hazelnupp::GenerateDocumentation() const
// Collect abbreviations // Collect abbreviations
// first value is abbreviation, second is long form // 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? // Do we already have that param in the paramInfo set?
if (paramInfos.find(it.second) == paramInfos.end()) if (paramInfos.find(it.second) == paramInfos.end())
@ -456,7 +472,7 @@ std::string Hazelnupp::GenerateDocumentation() const
} }
// Collect constraints // Collect constraints
for (const auto& it : constraints) for (const auto& it : parameterConstraints)
{ {
// Do we already have that param in the paramInfo set? // Do we already have that param in the paramInfo set?
if (paramInfos.find(it.first) == paramInfos.end()) if (paramInfos.find(it.first) == paramInfos.end())
@ -524,7 +540,7 @@ std::string Hazelnupp::GenerateDocumentation() const
void Hazelnupp::ApplyConstraints() void Hazelnupp::ApplyConstraints()
{ {
// Enforce required parameters / default values // Enforce required parameters / default values
for (const auto& pc : constraints) for (const auto& pc : parameterConstraints)
// Parameter in question is not supplied // Parameter in question is not supplied
if (!HasParam(pc.second.key)) if (!HasParam(pc.second.key))
{ {
@ -555,6 +571,17 @@ void Hazelnupp::ApplyConstraints()
return; 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 const std::string& Hazelnupp::GetExecutableName() const
{ {
return executableName; 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) void Hazelnupp::RegisterAbbreviation(const std::string& abbrev, const std::string& target)
{ {
abbreviations.insert(std::pair<std::string, std::string>(abbrev, target)); parameterAbreviations.insert(std::pair<std::string, std::string>(abbrev, target));
return; return;
} }
const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const 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 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() void Hazelnupp::ClearAbbreviations()
{ {
abbreviations.clear(); parameterAbreviations.clear();
return; return;
} }
void Hazelnupp::RegisterConstraints(const std::vector<ParamConstraint>& constraints) void Hazelnupp::RegisterConstraint(const std::string& key, const ParamConstraint& constraint)
{ {
for (const ParamConstraint& pc : constraints) // Magic syntax, wooo
{ (parameterConstraints[key] = constraint).key = key;
// 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<std::string, ParamConstraint>(
pc.key,
pc
));
}
return; return;
} }
void Hazelnupp::ClearConstraints() void Hazelnupp::ClearConstraints()
{ {
constraints.clear(); parameterConstraints.clear();
return; return;
} }
@ -626,9 +648,9 @@ void Hazelnupp::SetCrashOnFail(bool crashOnFail)
const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) const 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 nullptr;
return &constraint->second; return &constraint->second;
@ -1165,11 +1187,12 @@ double VoidValue::GetFloat32() const
std::string VoidValue::GetString() const std::string VoidValue::GetString() const
{ {
throw HazelnuppValueNotConvertibleException(); return "";
} }
const std::vector<Value*>& VoidValue::GetList() const const std::vector<Value*>& VoidValue::GetList() const
{ {
throw HazelnuppValueNotConvertibleException(); static const std::vector<Value*> empty;
return empty;
} }

View File

@ -42,6 +42,19 @@ namespace Hazelnp
}; };
} }
/*** ../Hazelnupp/Placeholders.h ***/
#include <string>
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 ***/ /*** ../Hazelnupp/HazelnuppException.h ***/
#include <stdexcept> #include <stdexcept>
@ -168,10 +181,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;
@ -179,10 +191,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;
@ -190,9 +201,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 },
@ -201,9 +211,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;
@ -219,6 +226,13 @@ 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;
}; };
} }
@ -273,6 +287,101 @@ namespace Hazelnp
}; };
} }
/*** ../Hazelnupp/ListValue.h ***/
#include <vector>
namespace Hazelnp
{
/** Specializations for list values (uses std::vector<Value*>)
*/
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<Value*>& GetValue() const;
operator std::vector<Value*>() 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<Value*>& GetList() const override;
private:
std::vector<Value*> value;
};
}
/*** ../Hazelnupp/StringValue.h ***/
#include <string>
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<Value*>& GetList() const override;
private:
std::string value;
};
}
/*** ../Hazelnupp/VoidValue.h ***/ /*** ../Hazelnupp/VoidValue.h ***/
@ -302,10 +411,10 @@ namespace Hazelnp
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
double GetFloat32() const override; double GetFloat32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Returns an empty string
std::string GetString() const override; std::string GetString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Returns an empty list
const std::vector<Value*>& GetList() const; const std::vector<Value*>& GetList() const;
}; };
} }
@ -420,17 +529,30 @@ namespace Hazelnp
//! Will register an abbreviation (like -f for --force) //! Will register an abbreviation (like -f for --force)
void RegisterAbbreviation(const std::string& abbrev, const std::string& target); 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; const std::string& GetAbbreviation(const std::string& abbrev) const;
//! Will check wether or not an abbreviation is registered //! Will check wether or not an abbreviation is registered
bool HasAbbreviation(const std::string& abbrev) const; 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 //! 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
ParamConstraint GetConstraint(const std::string& parameter) const;
//! Will the constraint of a specific parameter
void ClearConstraint(const std::string& parameter);
//! Will delete all constraints //! Will delete all constraints
void ClearConstraints(); void ClearConstraints();
@ -460,11 +582,17 @@ namespace Hazelnp
//! Will return a short description for a parameter, if it exists. //! Will return a short description for a parameter, if it exists.
//! Empty string if it does not exist. //! 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. //! Will delete the description of a parameter if it exists.
void ClearDescription(const std::string& parameter); 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. //! Will generate a text-based documentation suited to show the user, for example on --help.
std::string GenerateDocumentation() const; std::string GenerateDocumentation() const;
@ -491,15 +619,16 @@ namespace Hazelnp
std::unordered_map<std::string, Parameter*> parameters; std::unordered_map<std::string, Parameter*> parameters;
//! These are abbreviations. Like, -f for --force. //! These are abbreviations. Like, -f for --force.
std::unordered_map<std::string, std::string> abbreviations; std::unordered_map<std::string, std::string> parameterAbreviations;
//! Parameter constraints, mapped to keys //! Parameter constraints, mapped to keys
std::unordered_map<std::string, ParamConstraint> constraints; std::unordered_map<std::string, ParamConstraint> parameterConstraints;
//! Raw argv //! Raw argv
std::vector<std::string> rawArgs; std::vector<std::string> rawArgs;
//! Short descriptions for parameters //! Short descriptions for parameters
//! First member is the abbreviation
std::unordered_map<std::string, std::string> parameterDescriptions; std::unordered_map<std::string, std::string> parameterDescriptions;
//! A brief description of the application to be added to the generated documentation. Optional. //! A brief description of the application to be added to the generated documentation. Optional.
@ -513,101 +642,6 @@ namespace Hazelnp
}; };
} }
/*** ../Hazelnupp/ListValue.h ***/
#include <vector>
namespace Hazelnp
{
/** Specializations for list values (uses std::vector<Value*>)
*/
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<Value*>& GetValue() const;
operator std::vector<Value*>() 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<Value*>& GetList() const override;
private:
std::vector<Value*> value;
};
}
/*** ../Hazelnupp/StringValue.h ***/
#include <string>
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<Value*>& GetList() const override;
private:
std::string value;
};
}
/*** ../Hazelnupp/FloatValue.h ***/ /*** ../Hazelnupp/FloatValue.h ***/
#include <ostream> #include <ostream>