From 7db2647d7f2dfa529d3d45d63c43eb0091ba7aea Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Tue, 8 Jun 2021 14:02:24 +0200 Subject: [PATCH] Built release files --- INCLUDE/Hazelnupp.cpp | 224 +++++++++++------------ INCLUDE/Hazelnupp.h | 410 +++++++++++++++++++++--------------------- 2 files changed, 317 insertions(+), 317 deletions(-) diff --git a/INCLUDE/Hazelnupp.cpp b/INCLUDE/Hazelnupp.cpp index 42de706..e3f50e2 100644 --- a/INCLUDE/Hazelnupp.cpp +++ b/INCLUDE/Hazelnupp.cpp @@ -1,101 +1,24 @@ #include "Hazelnupp.h" -/*** ../Hazelnupp/FloatValue.cpp ***/ - -#include - -using namespace Hazelnp; - -FloatValue::FloatValue(const long double& value) - : - Value(DATA_TYPE::FLOAT), - value { value } -{ - return; -} - -Value* FloatValue::Deepcopy() const -{ - return new FloatValue(value); -} - -std::string FloatValue::GetAsOsString() const -{ - std::stringstream ss; - ss << "FloatValue: " << value; - return ss.str(); -} - -const long double& FloatValue::GetValue() const -{ - return value; -} - -FloatValue::operator long double() const -{ - return value; -} - -FloatValue::operator double() const -{ - return (double)value; -} - - - -long long int FloatValue::GetInt64() const -{ - return (long long int)value; -} - -int FloatValue::GetInt32() const -{ - return (int)value; -} - -long double FloatValue::GetFloat64() const -{ - return value; -} - -double FloatValue::GetFloat32() const -{ - return (double)value; -} - -std::string FloatValue::GetString() const -{ - std::stringstream ss; - ss << value; - - return ss.str(); -} - -const std::vector& FloatValue::GetList() const -{ - throw HazelnuppValueNotConvertibleException(); -} - - -/*** ../Hazelnupp/Hazelnupp.cpp ***/ +/*** ../Hazelnupp/CmdArgsInterface.cpp ***/ #include #include using namespace Hazelnp; -Hazelnupp::Hazelnupp() +CmdArgsInterface::CmdArgsInterface() { return; } -Hazelnupp::Hazelnupp(const int argc, const char* const* argv) +CmdArgsInterface::CmdArgsInterface(const int argc, const char* const* argv) { Parse(argc, argv); return; } -Hazelnupp::~Hazelnupp() +CmdArgsInterface::~CmdArgsInterface() { for (auto& it : parameters) delete it.second; @@ -105,7 +28,7 @@ Hazelnupp::~Hazelnupp() return; } -void Hazelnupp::Parse(const int argc, const char* const* argv) +void CmdArgsInterface::Parse(const int argc, const char* const* argv) { try { @@ -170,7 +93,7 @@ void Hazelnupp::Parse(const int argc, const char* const* argv) return; } -std::size_t Hazelnupp::ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par) +std::size_t CmdArgsInterface::ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par) { std::size_t i = parIndex; const std::string key = rawArgs[parIndex]; @@ -203,7 +126,7 @@ std::size_t Hazelnupp::ParseNextParameter(const std::size_t parIndex, Parameter* return i; } -void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv) +void CmdArgsInterface::PopulateRawArgs(const int argc, const char* const* argv) { rawArgs.clear(); rawArgs.reserve(argc); @@ -214,7 +137,7 @@ void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv) return; } -void Hazelnupp::ExpandAbbreviations() +void CmdArgsInterface::ExpandAbbreviations() { // Abort if no abbreviations if (parameterAbreviations.size() == 0) @@ -234,12 +157,12 @@ void Hazelnupp::ExpandAbbreviations() return; } -bool Hazelnupp::HasParam(const std::string& key) const +bool CmdArgsInterface::HasParam(const std::string& key) const { return parameters.find(key) != parameters.end(); } -Value* Hazelnupp::ParseValue(const std::vector& values, const ParamConstraint* constraint) +Value* CmdArgsInterface::ParseValue(const std::vector& values, const ParamConstraint* constraint) { // This is the raw (unconverted) data type the user provided DATA_TYPE rawInputType; @@ -398,40 +321,40 @@ Value* Hazelnupp::ParseValue(const std::vector& values, const Param return nullptr; } -bool Hazelnupp::GetCrashOnFail() const +bool CmdArgsInterface::GetCrashOnFail() const { return crashOnFail; } -void Hazelnupp::SetCatchHelp(bool catchHelp) +void CmdArgsInterface::SetCatchHelp(bool catchHelp) { this->catchHelp = catchHelp; return; } -bool Hazelnupp::GetCatchHelp() const +bool CmdArgsInterface::GetCatchHelp() const { return catchHelp; } -void Hazelnupp::SetBriefDescription(const std::string& description) +void CmdArgsInterface::SetBriefDescription(const std::string& description) { briefDescription = description; return; } -const std::string& Hazelnupp::GetBriefDescription() +const std::string& CmdArgsInterface::GetBriefDescription() { return briefDescription; } -void Hazelnp::Hazelnupp::RegisterDescription(const std::string& parameter, const std::string& description) +void Hazelnp::CmdArgsInterface::RegisterDescription(const std::string& parameter, const std::string& description) { parameterDescriptions[parameter] = description; return; } -const std::string& Hazelnp::Hazelnupp::GetDescription(const std::string& parameter) const +const std::string& Hazelnp::CmdArgsInterface::GetDescription(const std::string& parameter) const { // Do we already have a description for this parameter? if (!HasDescription(parameter)) @@ -442,25 +365,25 @@ const std::string& Hazelnp::Hazelnupp::GetDescription(const std::string& paramet return parameterDescriptions.find(parameter)->second; } -bool Hazelnupp::HasDescription(const std::string& parameter) const +bool CmdArgsInterface::HasDescription(const std::string& parameter) const { return parameterDescriptions.find(parameter) != parameterDescriptions.end(); } -void Hazelnupp::ClearDescription(const std::string& parameter) +void CmdArgsInterface::ClearDescription(const std::string& parameter) { // This will just do nothing if the entry does not exist parameterDescriptions.erase(parameter); return; } -void Hazelnp::Hazelnupp::ClearDescriptions() +void Hazelnp::CmdArgsInterface::ClearDescriptions() { parameterDescriptions.clear(); return; } -std::string Hazelnupp::GenerateDocumentation() const +std::string CmdArgsInterface::GenerateDocumentation() const { std::stringstream ss; @@ -574,7 +497,7 @@ std::string Hazelnupp::GenerateDocumentation() const return ss.str(); } -void Hazelnupp::ApplyConstraints() +void CmdArgsInterface::ApplyConstraints() { // Enforce required parameters / default values for (const auto& pc : parameterConstraints) @@ -611,23 +534,23 @@ void Hazelnupp::ApplyConstraints() return; } -ParamConstraint Hazelnupp::GetConstraint(const std::string& parameter) const +ParamConstraint CmdArgsInterface::GetConstraint(const std::string& parameter) const { return parameterConstraints.find(parameter)->second; } -void Hazelnupp::ClearConstraint(const std::string& parameter) +void CmdArgsInterface::ClearConstraint(const std::string& parameter) { parameterConstraints.erase(parameter); return; } -const std::string& Hazelnupp::GetExecutableName() const +const std::string& CmdArgsInterface::GetExecutableName() const { return executableName; } -const Value& Hazelnupp::operator[](const std::string& key) const +const Value& CmdArgsInterface::operator[](const std::string& key) const { // Throw exception if param is unknown if (!HasParam(key)) @@ -636,13 +559,13 @@ const Value& Hazelnupp::operator[](const std::string& key) const return *parameters.find(key)->second->GetValue(); } -void Hazelnupp::RegisterAbbreviation(const std::string& abbrev, const std::string& target) +void CmdArgsInterface::RegisterAbbreviation(const std::string& abbrev, const std::string& target) { parameterAbreviations.insert(std::pair(abbrev, target)); return; } -const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const +const std::string& CmdArgsInterface::GetAbbreviation(const std::string& abbrev) const { if (!HasAbbreviation(abbrev)) return Placeholders::g_emptyString; @@ -650,43 +573,43 @@ const std::string& Hazelnupp::GetAbbreviation(const std::string& abbrev) const return parameterAbreviations.find(abbrev)->second; } -bool Hazelnupp::HasAbbreviation(const std::string& abbrev) const +bool CmdArgsInterface::HasAbbreviation(const std::string& abbrev) const { return parameterAbreviations.find(abbrev) != parameterAbreviations.end(); } -void Hazelnupp::ClearAbbreviation(const std::string& abbrevation) +void CmdArgsInterface::ClearAbbreviation(const std::string& abbrevation) { parameterAbreviations.erase(abbrevation); return; } -void Hazelnupp::ClearAbbreviations() +void CmdArgsInterface::ClearAbbreviations() { parameterAbreviations.clear(); return; } -void Hazelnupp::RegisterConstraint(const std::string& key, const ParamConstraint& constraint) +void CmdArgsInterface::RegisterConstraint(const std::string& key, const ParamConstraint& constraint) { // Magic syntax, wooo (parameterConstraints[key] = constraint).key = key; return; } -void Hazelnupp::ClearConstraints() +void CmdArgsInterface::ClearConstraints() { parameterConstraints.clear(); return; } -void Hazelnupp::SetCrashOnFail(bool crashOnFail) +void CmdArgsInterface::SetCrashOnFail(bool crashOnFail) { this->crashOnFail = crashOnFail; return; } -const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) const +const ParamConstraint* CmdArgsInterface::GetConstraintForKey(const std::string& key) const { const auto constraint = parameterConstraints.find(key); @@ -697,6 +620,83 @@ const ParamConstraint* Hazelnupp::GetConstraintForKey(const std::string& key) co } +/*** ../Hazelnupp/FloatValue.cpp ***/ + +#include + +using namespace Hazelnp; + +FloatValue::FloatValue(const long double& value) + : + Value(DATA_TYPE::FLOAT), + value { value } +{ + return; +} + +Value* FloatValue::Deepcopy() const +{ + return new FloatValue(value); +} + +std::string FloatValue::GetAsOsString() const +{ + std::stringstream ss; + ss << "FloatValue: " << value; + return ss.str(); +} + +const long double& FloatValue::GetValue() const +{ + return value; +} + +FloatValue::operator long double() const +{ + return value; +} + +FloatValue::operator double() const +{ + return (double)value; +} + + + +long long int FloatValue::GetInt64() const +{ + return (long long int)value; +} + +int FloatValue::GetInt32() const +{ + return (int)value; +} + +long double FloatValue::GetFloat64() const +{ + return value; +} + +double FloatValue::GetFloat32() const +{ + return (double)value; +} + +std::string FloatValue::GetString() const +{ + std::stringstream ss; + ss << value; + + return ss.str(); +} + +const std::vector& FloatValue::GetList() const +{ + throw HazelnuppValueNotConvertibleException(); +} + + /*** ../Hazelnupp/IntValue.cpp ***/ #include diff --git a/INCLUDE/Hazelnupp.h b/INCLUDE/Hazelnupp.h index 2bc6425..1f2e039 100644 --- a/INCLUDE/Hazelnupp.h +++ b/INCLUDE/Hazelnupp.h @@ -1,47 +1,5 @@ #pragma once -/*** ../Hazelnupp/StringTools.h ***/ - -#include -#include -#include -#include - -namespace Hazelnp -{ - /** Internal helper class. Feel free to use it tho. - */ - class StringTools - { - public: - //! Will return wether or not a given char is in a string - static bool Contains(const std::string& str, const char c); - - //! Will replace a part of a string with another string - static std::string Replace(const std::string& str, const char find, const std::string& subst); - - //! Will replace a part of a string with another string - static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); - - //! Will return true if the given string consists only of digits (including signage) - static bool IsNumeric(const std::string& str, const bool allowDecimalPoint = false); - - //! Will convert the number in str to a number. - //! Returns wether or not the operation was successful. - //! Also returns wether the number is an integer, or floating point. If int, cast out_number to int. - static bool ParseNumber(const std::string& str, bool& out_isInt, long double& out_number); - - //! Will split a string by a delimiter char. The delimiter will be excluded! - static std::vector SplitString(const std::string& str, const char delimiter); - - //! Will split a string by a delimiter string. The delimiter will be excluded! - static std::vector SplitString(const std::string& str, const std::string& delimiter); - - //! Will make a string all lower-case - static std::string ToLower(const std::string& str); - }; -} - /*** ../Hazelnupp/Placeholders.h ***/ #include @@ -96,76 +54,6 @@ namespace Hazelnp } } -/*** ../Hazelnupp/ParamConstraint.h ***/ - -#include -#include - -namespace Hazelnp -{ - struct ParamConstraint - { - public: - //! Empty constructor - ParamConstraint() = default; - - //! Constructs a require constraint. - //! Think of the default value like of a list ofparameters. Like {"--width", "800"} - static ParamConstraint Require(const std::vector& defaultValue = {}, bool required = true) - { - ParamConstraint pc; - pc.defaultValue = defaultValue; - pc.required = required; - - return pc; - } - - //! Constructs a type-safety constraint - static ParamConstraint TypeSafety(DATA_TYPE requiredType, bool constrainType = true) - { - ParamConstraint pc; - pc.constrainType = constrainType; - pc.requiredType = requiredType; - - return pc; - } - - //! Whole constructor - ParamConstraint(bool constrainType, DATA_TYPE requiredType, const std::vector& defaultValue, bool required) - : - constrainType{ constrainType }, - requiredType{ requiredType }, - defaultValue{ defaultValue }, - required{ required } - { - return; - } - - //! Should this parameter be forced to be of a certain type? - //! Remember to set `constrainTo` to the wanted type - bool constrainType = false; - - //! Constrain the parameter to this value. Requires `constrainType` to be set to true. - DATA_TYPE requiredType = DATA_TYPE::VOID; - - //! The default value for this parameter. - //! Gets applied if this parameter was not given. - //! Think of this like a list of parameters. Like {"--width", "800"} - std::vector defaultValue; - - //! 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; - }; -} - /*** ../Hazelnupp/HazelnuppException.h ***/ #include @@ -265,6 +153,76 @@ namespace Hazelnp }; } +/*** ../Hazelnupp/ParamConstraint.h ***/ + +#include +#include + +namespace Hazelnp +{ + struct ParamConstraint + { + public: + //! Empty constructor + ParamConstraint() = default; + + //! Constructs a require constraint. + //! Think of the default value like of a list ofparameters. Like {"--width", "800"} + static ParamConstraint Require(const std::vector& defaultValue = {}, bool required = true) + { + ParamConstraint pc; + pc.defaultValue = defaultValue; + pc.required = required; + + return pc; + } + + //! Constructs a type-safety constraint + static ParamConstraint TypeSafety(DATA_TYPE requiredType, bool constrainType = true) + { + ParamConstraint pc; + pc.constrainType = constrainType; + pc.requiredType = requiredType; + + return pc; + } + + //! Whole constructor + ParamConstraint(bool constrainType, DATA_TYPE requiredType, const std::vector& defaultValue, bool required) + : + constrainType{ constrainType }, + requiredType{ requiredType }, + defaultValue{ defaultValue }, + required{ required } + { + return; + } + + //! Should this parameter be forced to be of a certain type? + //! Remember to set `constrainTo` to the wanted type + bool constrainType = false; + + //! Constrain the parameter to this value. Requires `constrainType` to be set to true. + DATA_TYPE requiredType = DATA_TYPE::VOID; + + //! The default value for this parameter. + //! Gets applied if this parameter was not given. + //! Think of this like a list of parameters. Like {"--width", "800"} + std::vector defaultValue; + + //! 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 CmdArgsInterface; + }; +} + /*** ../Hazelnupp/Value.h ***/ #include @@ -411,6 +369,53 @@ namespace Hazelnp }; } +/*** ../Hazelnupp/FloatValue.h ***/ + +#include + +namespace Hazelnp +{ + /** Specializations for floating point values (uses long double) + */ + class FloatValue : public Value + { + public: + FloatValue(const long double& value); + ~FloatValue() 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 long double& GetValue() const; + + operator long double() const; + operator double() const; + + //! Will return the data as a long long int + long long int GetInt64() const override; + //! Will return the data as an int + int GetInt32() const override; + + //! Will return the data as a long double + long double GetFloat64() const override; + //! Will return the data as a double + double GetFloat32() const override; + + //! Will return the data as a string + std::string GetString() const override; + + //! Throws HazelnuppValueNotConvertibleException + const std::vector& GetList() const override; + + private: + long double value; + }; +} + /*** ../Hazelnupp/IntValue.h ***/ @@ -458,6 +463,43 @@ namespace Hazelnp }; } +/*** ../Hazelnupp/VoidValue.h ***/ + + +namespace Hazelnp +{ + /** Specializations for void values. These house no value whatsoever, but only communicate information by merely existing. + */ + class VoidValue : public Value + { + public: + VoidValue(); + ~VoidValue() 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; + + //! 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; + + //! Returns an empty string + std::string GetString() const override; + + //! Returns an empty list + const std::vector& GetList() const; + }; +} + /*** ../Hazelnupp/Parameter.h ***/ #include @@ -488,7 +530,49 @@ namespace Hazelnp }; } -/*** ../Hazelnupp/Hazelnupp.h ***/ +/*** ../Hazelnupp/StringTools.h ***/ + +#include +#include +#include +#include + +namespace Hazelnp +{ + /** Internal helper class. Feel free to use it tho. + */ + class StringTools + { + public: + //! Will return wether or not a given char is in a string + static bool Contains(const std::string& str, const char c); + + //! Will replace a part of a string with another string + static std::string Replace(const std::string& str, const char find, const std::string& subst); + + //! Will replace a part of a string with another string + static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); + + //! Will return true if the given string consists only of digits (including signage) + static bool IsNumeric(const std::string& str, const bool allowDecimalPoint = false); + + //! Will convert the number in str to a number. + //! Returns wether or not the operation was successful. + //! Also returns wether the number is an integer, or floating point. If int, cast out_number to int. + static bool ParseNumber(const std::string& str, bool& out_isInt, long double& out_number); + + //! Will split a string by a delimiter char. The delimiter will be excluded! + static std::vector SplitString(const std::string& str, const char delimiter); + + //! Will split a string by a delimiter string. The delimiter will be excluded! + static std::vector SplitString(const std::string& str, const std::string& delimiter); + + //! Will make a string all lower-case + static std::string ToLower(const std::string& str); + }; +} + +/*** ../Hazelnupp/CmdArgsInterface.h ***/ #include #include @@ -497,13 +581,13 @@ namespace Hazelnp { /** The main class to interface with */ - class Hazelnupp + class CmdArgsInterface { public: - Hazelnupp(); - Hazelnupp(const int argc, const char* const* argv); + CmdArgsInterface(); + CmdArgsInterface(const int argc, const char* const* argv); - ~Hazelnupp(); + ~CmdArgsInterface(); //! Will parse command line arguments void Parse(const int argc, const char* const* argv); @@ -556,10 +640,10 @@ namespace Hazelnp //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr. bool GetCrashOnFail() const; - //! Sets whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not. + //! Sets whether the CmdArgsInterface should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not. void SetCatchHelp(bool catchHelp); - //! Retruns whether the Hazelnupp should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not. + //! Retruns whether the CmdArgsInterface should automatically catch the --help parameter, print the parameter documentation to stdout, and exit or not. bool GetCatchHelp() const; //! Sets a brief description of the application to be automatically added to the documentation. @@ -626,94 +710,10 @@ namespace Hazelnp //! A brief description of the application to be added to the generated documentation. Optional. std::string briefDescription; - //! If set to true, Hazelnupp will automatically catch the --help parameter, print the parameter documentation to stdout and exit. + //! If set to true, CmdArgsInterface will automatically catch the --help parameter, print the parameter documentation to stdout and exit. bool catchHelp = true; - //! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing. + //! If set to true, CmdArgsInterface will crash the application with output to stderr when an exception is thrown whilst parsing. bool crashOnFail = true; }; } - -/*** ../Hazelnupp/VoidValue.h ***/ - - -namespace Hazelnp -{ - /** Specializations for void values. These house no value whatsoever, but only communicate information by merely existing. - */ - class VoidValue : public Value - { - public: - VoidValue(); - ~VoidValue() 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; - - //! 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; - - //! Returns an empty string - std::string GetString() const override; - - //! Returns an empty list - const std::vector& GetList() const; - }; -} - -/*** ../Hazelnupp/FloatValue.h ***/ - -#include - -namespace Hazelnp -{ - /** Specializations for floating point values (uses long double) - */ - class FloatValue : public Value - { - public: - FloatValue(const long double& value); - ~FloatValue() 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 long double& GetValue() const; - - operator long double() const; - operator double() const; - - //! Will return the data as a long long int - long long int GetInt64() const override; - //! Will return the data as an int - int GetInt32() const override; - - //! Will return the data as a long double - long double GetFloat64() const override; - //! Will return the data as a double - double GetFloat32() const override; - - //! Will return the data as a string - std::string GetString() const override; - - //! Throws HazelnuppValueNotConvertibleException - const std::vector& GetList() const override; - - private: - long double value; - }; -}