From 124b6e5b98a4993801e1af24f7626bd9f3a0aead Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Thu, 3 Jun 2021 13:42:40 +0200 Subject: [PATCH] Namespacified --- Hazelnupp/DataType.h | 21 +++--- Hazelnupp/FloatValue.cpp | 2 + Hazelnupp/FloatValue.h | 61 ++++++++-------- Hazelnupp/Hazelnupp.cpp | 2 + Hazelnupp/Hazelnupp.h | 115 ++++++++++++++++--------------- Hazelnupp/HazelnuppException.h | 111 ++++++++++++++--------------- Hazelnupp/IntValue.cpp | 2 + Hazelnupp/IntValue.h | 61 ++++++++-------- Hazelnupp/ListValue.cpp | 2 + Hazelnupp/ListValue.h | 63 +++++++++-------- Hazelnupp/ParamConstraint.h | 103 +++++++++++++-------------- Hazelnupp/Parameter.cpp | 2 + Hazelnupp/Parameter.h | 39 ++++++----- Hazelnupp/StringTools.cpp | 2 + Hazelnupp/StringTools.h | 49 ++++++------- Hazelnupp/StringValue.cpp | 2 + Hazelnupp/StringValue.h | 59 ++++++++-------- Hazelnupp/Value.cpp | 2 + Hazelnupp/Value.h | 71 ++++++++++--------- Hazelnupp/VoidValue.cpp | 2 + Hazelnupp/VoidValue.h | 49 ++++++------- Hazelnupp/main.cpp | 2 + Test_Hazelnupp/Abbreviations.cpp | 1 + Test_Hazelnupp/Basics.cpp | 1 + Test_Hazelnupp/Constraints.cpp | 1 + Test_Hazelnupp/Conversion.cpp | 1 + 26 files changed, 443 insertions(+), 383 deletions(-) diff --git a/Hazelnupp/DataType.h b/Hazelnupp/DataType.h index 94c7183..b045ee4 100644 --- a/Hazelnupp/DataType.h +++ b/Hazelnupp/DataType.h @@ -1,12 +1,15 @@ #pragma once -/** The different data types a paramater can be -*/ -enum class DATA_TYPE +namespace Hazelnp { - VOID, - INT, - FLOAT, - STRING, - LIST -}; + /** The different data types a paramater can be + */ + enum class DATA_TYPE + { + VOID, + INT, + FLOAT, + STRING, + LIST + }; +} diff --git a/Hazelnupp/FloatValue.cpp b/Hazelnupp/FloatValue.cpp index 61e63f9..90fa8d0 100644 --- a/Hazelnupp/FloatValue.cpp +++ b/Hazelnupp/FloatValue.cpp @@ -2,6 +2,8 @@ #include "HazelnuppException.h" #include +using namespace Hazelnp; + FloatValue::FloatValue(const long double& value) : Value(DATA_TYPE::FLOAT), diff --git a/Hazelnupp/FloatValue.h b/Hazelnupp/FloatValue.h index 0a63a30..24b85fc 100644 --- a/Hazelnupp/FloatValue.h +++ b/Hazelnupp/FloatValue.h @@ -2,42 +2,45 @@ #include "Value.h" #include -/** Specializations for floating point values (uses long double) -*/ -class FloatValue : public Value +namespace Hazelnp { -public: - FloatValue(const long double& value); - ~FloatValue() override {}; + /** 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 deeopopy of this object + Value* Deepcopy() const override; - //! Will return a string suitable for an std::ostream; - std::string GetAsOsString() 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; + //! Will return the raw value + const long double& GetValue() const; - operator long double () const; - operator double() 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 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 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; + //! Will return the data as a string + std::string GetString() const override; - //! Throws HazelnuppValueNotConvertibleException - const std::vector& GetList() const override; + //! Throws HazelnuppValueNotConvertibleException + const std::vector& GetList() const override; -private: - long double value; -}; + private: + long double value; + }; +} diff --git a/Hazelnupp/Hazelnupp.cpp b/Hazelnupp/Hazelnupp.cpp index 34aed1a..195555a 100644 --- a/Hazelnupp/Hazelnupp.cpp +++ b/Hazelnupp/Hazelnupp.cpp @@ -9,6 +9,8 @@ #include #include +using namespace Hazelnp; + Hazelnupp::Hazelnupp() { return; diff --git a/Hazelnupp/Hazelnupp.h b/Hazelnupp/Hazelnupp.h index 6aee1a7..47f6f84 100644 --- a/Hazelnupp/Hazelnupp.h +++ b/Hazelnupp/Hazelnupp.h @@ -4,84 +4,87 @@ #include #include -/** The main class to interface with -*/ -class Hazelnupp +namespace Hazelnp { -public: - Hazelnupp(); - Hazelnupp(const int argc, const char* const* argv); + /** The main class to interface with + */ + class Hazelnupp + { + public: + Hazelnupp(); + Hazelnupp(const int argc, const char* const* argv); - ~Hazelnupp(); + ~Hazelnupp(); - //! Will parse command line arguments - void Parse(const int argc, const char* const* argv); + //! Will parse command line arguments + void Parse(const int argc, const char* const* argv); - //! Will return argv[0], the name of the executable. - const std::string& GetExecutableName() const; + //! Will return argv[0], the name of the executable. + const std::string& GetExecutableName() const; - //! Will return the value given a key - const Value& operator[](const std::string& key) const; + //! Will return the value given a key + const Value& operator[](const std::string& key) const; - //! Will check wether a parameter exists given a key, or not - bool HasParam(const std::string& key) const; + //! Will check wether a parameter exists given a key, or not + bool HasParam(const std::string& key) const; - // Abbreviations - //! Will register an abbreviation (like -f for --force) - void RegisterAbbreviation(const std::string& abbrev, const std::string& target); + // Abbreviations + //! 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) - const std::string& GetAbbreviation(const std::string& abbrev) const; + //! Will return the long form of an abbreviation (like --force for -f) + 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 check wether or not an abbreviation is registered + bool HasAbbreviation(const std::string& abbrev) const; - //! Will delete all abbreviations - void ClearAbbreviations(); + //! Will delete all abbreviations + void ClearAbbreviations(); - //! Will register parameter constraints - void RegisterConstraints(const std::vector& constraints); + //! Will register parameter constraints + void RegisterConstraints(const std::vector& constraints); - //! Will delete all constraints - void ClearConstraints(); + //! Will delete all constraints + void ClearConstraints(); - //! Sets whether to crash the application, and print to stderr, when an exception is - //! raised whilst parsing, or not. - void SetCrashOnFail(bool crashOnFail); + //! Sets whether to crash the application, and print to stderr, when an exception is + //! raised whilst parsing, or not. + void SetCrashOnFail(bool crashOnFail); - //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr. - bool GetCrashOnFail() const; + //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr. + bool GetCrashOnFail() const; -private: - //! Will translate the c-like args to an std::vector - void PopulateRawArgs(const int argc, const char* const* argv); + private: + //! Will translate the c-like args to an std::vector + void PopulateRawArgs(const int argc, const char* const* argv); - //! Will replace all args matching an abbreviation with their long form (like -f for --force) - void ExpandAbbreviations(); + //! Will replace all args matching an abbreviation with their long form (like -f for --force) + void ExpandAbbreviations(); - //! Will parse the next parameter. Returns the index of the next parameter. - std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par); + //! Will parse the next parameter. Returns the index of the next parameter. + std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par); - //! Will convert a vector of string-values to an actual Value - Value* ParseValue(const std::vector& values, const ParamConstraint* constraint = nullptr); + //! Will convert a vector of string-values to an actual Value + Value* ParseValue(const std::vector& values, const ParamConstraint* constraint = nullptr); - //! Will apply the loaded constraints on the loaded values, exluding types. - void ApplyConstraints(); + //! Will apply the loaded constraints on the loaded values, exluding types. + void ApplyConstraints(); - //! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr - const ParamConstraint* GetConstraintForKey(const std::string& key) const; + //! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr + const ParamConstraint* GetConstraintForKey(const std::string& key) const; - std::string executableName; //! The path of the executable. Always argv[0] - std::unordered_map parameters; + std::string executableName; //! The path of the executable. Always argv[0] + std::unordered_map parameters; - // These are abbreviations. Like, -f for --force. - std::unordered_map abbreviations; + // These are abbreviations. Like, -f for --force. + std::unordered_map abbreviations; - // Parameter constraints, mapped to keys - std::unordered_map constraints; + // Parameter constraints, mapped to keys + std::unordered_map constraints; - std::vector rawArgs; + std::vector rawArgs; - //! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing. - bool crashOnFail = true; -}; + //! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing. + bool crashOnFail = true; + }; +} diff --git a/Hazelnupp/HazelnuppException.h b/Hazelnupp/HazelnuppException.h index a800137..0e90170 100644 --- a/Hazelnupp/HazelnuppException.h +++ b/Hazelnupp/HazelnuppException.h @@ -1,65 +1,68 @@ #pragma once #include -/** Generic hazelnupp exception -*/ -class HazelnuppException : public std::exception +namespace Hazelnp { -public: - HazelnuppException() {}; - HazelnuppException(const std::string& msg) : message{ msg } {}; - - //! Will return an error message - const std::string& What() const + /** Generic hazelnupp exception + */ + class HazelnuppException : public std::exception { - return message; - } + public: + HazelnuppException() {}; + HazelnuppException(const std::string& msg) : message{ msg } {}; -protected: - std::string message; -}; + //! Will return an error message + const std::string& What() const + { + return message; + } -/** Gets thrown when an non-existent key gets dereferenced -*/ -class HazelnuppInvalidKeyException : public HazelnuppException -{ -public: - HazelnuppInvalidKeyException() : HazelnuppException() {}; - HazelnuppInvalidKeyException(const std::string& msg) : HazelnuppException(msg) {}; -}; + protected: + std::string message; + }; -/** Gets thrown when an attempt is made to retrieve the wrong data type from a value, when the value not convertible -*/ -class HazelnuppValueNotConvertibleException : public HazelnuppException -{ -public: - HazelnuppValueNotConvertibleException() : HazelnuppException() {}; - HazelnuppValueNotConvertibleException(const std::string& msg) : HazelnuppException(msg) {}; -}; + /** Gets thrown when an non-existent key gets dereferenced + */ + class HazelnuppInvalidKeyException : public HazelnuppException + { + public: + HazelnuppInvalidKeyException() : HazelnuppException() {}; + HazelnuppInvalidKeyException(const std::string& msg) : HazelnuppException(msg) {}; + }; -/** Gets thrown something bad happens because of parameter constraints -*/ -class HazelnuppConstraintException : public HazelnuppException -{ -public: - HazelnuppConstraintException() : HazelnuppException() {}; - HazelnuppConstraintException(const std::string& msg) : HazelnuppException(msg) {}; -}; + /** Gets thrown when an attempt is made to retrieve the wrong data type from a value, when the value not convertible + */ + class HazelnuppValueNotConvertibleException : public HazelnuppException + { + public: + HazelnuppValueNotConvertibleException() : HazelnuppException() {}; + HazelnuppValueNotConvertibleException(const std::string& msg) : HazelnuppException(msg) {}; + }; -/** Gets thrown when a parameter is of a type that does not match the required type, and is not convertible to it -*/ -class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException -{ -public: - HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {}; - HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {}; -}; + /** Gets thrown something bad happens because of parameter constraints + */ + class HazelnuppConstraintException : public HazelnuppException + { + public: + HazelnuppConstraintException() : HazelnuppException() {}; + HazelnuppConstraintException(const std::string& msg) : HazelnuppException(msg) {}; + }; -/** Gets thrown when a parameter constrained to be required is not provided, and has no default value set -*/ -class HazelnuppConstraintMissingValue : public HazelnuppConstraintException -{ -public: - HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {}; - HazelnuppConstraintMissingValue(const std::string& msg) : HazelnuppConstraintException(msg) {}; -}; + /** Gets thrown when a parameter is of a type that does not match the required type, and is not convertible to it + */ + class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException + { + public: + HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {}; + HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {}; + }; + + /** Gets thrown when a parameter constrained to be required is not provided, and has no default value set + */ + class HazelnuppConstraintMissingValue : public HazelnuppConstraintException + { + public: + HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {}; + HazelnuppConstraintMissingValue(const std::string& msg) : HazelnuppConstraintException(msg) {}; + }; +} diff --git a/Hazelnupp/IntValue.cpp b/Hazelnupp/IntValue.cpp index 1d1d1da..ae50140 100644 --- a/Hazelnupp/IntValue.cpp +++ b/Hazelnupp/IntValue.cpp @@ -2,6 +2,8 @@ #include "HazelnuppException.h" #include +using namespace Hazelnp; + IntValue::IntValue(const long long int& value) : Value(DATA_TYPE::INT), diff --git a/Hazelnupp/IntValue.h b/Hazelnupp/IntValue.h index d247b27..652bc71 100644 --- a/Hazelnupp/IntValue.h +++ b/Hazelnupp/IntValue.h @@ -1,43 +1,46 @@ #pragma once #include "Value.h" -/** Specializations for integer values (uses long long int) -*/ -class IntValue : public Value +namespace Hazelnp { -public: - IntValue(const long long int& value); - ~IntValue() override {}; + /** Specializations for integer values (uses long long int) + */ + class IntValue : public Value + { + public: + IntValue(const long long int& value); + ~IntValue() override {}; - //! Will return a deeopopy of this object - Value* Deepcopy() const 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 a string suitable for an std::ostream; + std::string GetAsOsString() const override; - //! Will return the raw value - const long long int& GetValue() const; + //! Will return the raw value + const long long int& GetValue() const; - operator long long int() const; - operator int() const; + operator long long int() const; + operator int() 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 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 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; + //! Will return the data as a string + std::string GetString() const override; - //! Throws HazelnuppValueNotConvertibleException - const std::vector& GetList() const override; + //! Throws HazelnuppValueNotConvertibleException + const std::vector& GetList() const override; -private: - long long int value; -}; + private: + long long int value; + }; +} diff --git a/Hazelnupp/ListValue.cpp b/Hazelnupp/ListValue.cpp index 15f9efd..a3bac9c 100644 --- a/Hazelnupp/ListValue.cpp +++ b/Hazelnupp/ListValue.cpp @@ -2,6 +2,8 @@ #include "HazelnuppException.h" #include +using namespace Hazelnp; + ListValue::ListValue() : Value(DATA_TYPE::LIST) { diff --git a/Hazelnupp/ListValue.h b/Hazelnupp/ListValue.h index 25169e4..64c709f 100644 --- a/Hazelnupp/ListValue.h +++ b/Hazelnupp/ListValue.h @@ -2,44 +2,47 @@ #include "Value.h" #include -/** Specializations for list values (uses std::vector) -*/ -class ListValue : public Value +namespace Hazelnp { -public: - ListValue(); - ~ListValue() override; + /** 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 deeopopy of this object + Value* Deepcopy() const override; - //! Will return a string suitable for an std::ostream; - std::string GetAsOsString() 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 add this value to the list + void AddValue(const Value* value); - //! Will return the raw value - const std::vector& GetValue() const; + //! Will return the raw value + const std::vector& GetValue() const; - operator std::vector() const; + operator std::vector() const; - //! Throws HazelnuppValueNotConvertibleException - long long int GetInt64() const override; - //! Throws HazelnuppValueNotConvertibleException - int GetInt32() 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; + //! Throws HazelnuppValueNotConvertibleException + long double GetFloat64() const override; + //! Throws HazelnuppValueNotConvertibleException + double GetFloat32() const override; - //! Throws HazelnuppValueNotConvertibleException - std::string GetString() const override; + //! Throws HazelnuppValueNotConvertibleException + std::string GetString() const override; - //! Will return this values list - const std::vector& GetList() const override; + //! Will return this values list + const std::vector& GetList() const override; -private: - std::vector value; -}; + private: + std::vector value; + }; +} diff --git a/Hazelnupp/ParamConstraint.h b/Hazelnupp/ParamConstraint.h index d7ec74f..4a10621 100644 --- a/Hazelnupp/ParamConstraint.h +++ b/Hazelnupp/ParamConstraint.h @@ -3,63 +3,66 @@ #include #include -struct ParamConstraint +namespace Hazelnp { -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::string& key, const std::vector& defaultValue = {}, bool required = true) + struct ParamConstraint { - ParamConstraint pc; - pc.key = key; - pc.defaultValue = defaultValue; - pc.required = required; + public: + //! Empty constructor + ParamConstraint() = default; - return pc; - } + //! 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) + { + ParamConstraint pc; + pc.key = key; + pc.defaultValue = defaultValue; + pc.required = required; - //! Constructs a type-safety constraint - static ParamConstraint TypeSafety(const std::string& key, DATA_TYPE wantedType, bool constrainType = true) - { - ParamConstraint pc; - pc.key = key; - pc.constrainType = constrainType; - pc.wantedType = wantedType; + return pc; + } - return pc; - } + //! Constructs a type-safety constraint + static ParamConstraint TypeSafety(const std::string& key, DATA_TYPE wantedType, bool constrainType = true) + { + ParamConstraint pc; + pc.key = key; + pc.constrainType = constrainType; + pc.wantedType = wantedType; - //! Whole constructor - ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector& defaultValue, bool required) - : - key { key }, - constrainType { constrainType }, - wantedType { wantedType }, - defaultValue { defaultValue }, - required{ required } - { - return; - } + return pc; + } - //! 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; + //! Whole constructor + ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector& defaultValue, bool required) + : + key{ key }, + constrainType{ constrainType }, + wantedType{ wantedType }, + defaultValue{ defaultValue }, + required{ required } + { + return; + } - //! Constrain the parameter to this value. Requires `constrainType` to be set to true. - DATA_TYPE wantedType = DATA_TYPE::VOID; + //! The key of the parameter to constrain + std::string key; - //! 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; + //! Should this parameter be forced to be of a certain type? + //! Remember to set `constrainTo` to the wanted type + bool constrainType = false; - //! 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; -}; + //! Constrain the parameter to this value. Requires `constrainType` to be set to true. + DATA_TYPE wantedType = 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; + }; +} diff --git a/Hazelnupp/Parameter.cpp b/Hazelnupp/Parameter.cpp index 9c00d6d..647398a 100644 --- a/Hazelnupp/Parameter.cpp +++ b/Hazelnupp/Parameter.cpp @@ -1,5 +1,7 @@ #include "Parameter.h" +using namespace Hazelnp; + Parameter::Parameter(const std::string& key, const ::Value* value) : key{ key } diff --git a/Hazelnupp/Parameter.h b/Hazelnupp/Parameter.h index 12712e0..5759f37 100644 --- a/Hazelnupp/Parameter.h +++ b/Hazelnupp/Parameter.h @@ -3,24 +3,27 @@ #include #include -class Parameter +namespace Hazelnp { -public: - explicit Parameter(const std::string& key, const Value* value); - ~Parameter(); - - //! Will return the key of this parameter - const std::string& Key() const; - - //! Will return the value of this parameter - const Value* GetValue() const; - - friend std::ostream& operator<< (std::ostream& os, const Parameter& p) + class Parameter { - return os << "{ Key: \"" << p.key << "\" -> " << *p.value << " }"; - } + public: + explicit Parameter(const std::string& key, const Value* value); + ~Parameter(); -private: - std::string key; - ::Value* value; -}; + //! Will return the key of this parameter + const std::string& Key() const; + + //! Will return the value of this parameter + const Value* GetValue() const; + + friend std::ostream& operator<< (std::ostream& os, const Parameter& p) + { + return os << "{ Key: \"" << p.key << "\" -> " << *p.value << " }"; + } + + private: + std::string key; + Hazelnp::Value* value; + }; +} diff --git a/Hazelnupp/StringTools.cpp b/Hazelnupp/StringTools.cpp index 453e5c9..300c507 100644 --- a/Hazelnupp/StringTools.cpp +++ b/Hazelnupp/StringTools.cpp @@ -1,5 +1,7 @@ #include "StringTools.h" +using namespace Hazelnp; + bool StringTools::Contains(const std::string& str, const char c) { for (const char& i : str) diff --git a/Hazelnupp/StringTools.h b/Hazelnupp/StringTools.h index 5963842..25533d7 100644 --- a/Hazelnupp/StringTools.h +++ b/Hazelnupp/StringTools.h @@ -4,34 +4,37 @@ #include #include -/** Internal helper class. Feel free to use it tho. -*/ -class StringTools +namespace Hazelnp { -public: - //! Will return wether or not a given char is in a string - static bool Contains(const std::string& str, const char c); + /** 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 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 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 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 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 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 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); -}; + //! Will make a string all lower-case + static std::string ToLower(const std::string& str); + }; +} diff --git a/Hazelnupp/StringValue.cpp b/Hazelnupp/StringValue.cpp index c7bfb53..b16e10b 100644 --- a/Hazelnupp/StringValue.cpp +++ b/Hazelnupp/StringValue.cpp @@ -2,6 +2,8 @@ #include "HazelnuppException.h" #include +using namespace Hazelnp; + StringValue::StringValue(const std::string& value) : Value(DATA_TYPE::STRING), diff --git a/Hazelnupp/StringValue.h b/Hazelnupp/StringValue.h index 87baafd..8632f7b 100644 --- a/Hazelnupp/StringValue.h +++ b/Hazelnupp/StringValue.h @@ -2,41 +2,44 @@ #include "Value.h" #include -/** Specializations for string values (uses std::string) -*/ -class StringValue : public Value +namespace Hazelnp { -public: - StringValue(const std::string& value); - ~StringValue() override {}; + /** 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 deeopopy of this object + Value* Deepcopy() const override; - //! Will return a string suitable for an std::ostream; - std::string GetAsOsString() 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; + //! Will return the raw value + const std::string& GetValue() const; - operator std::string() const; + operator std::string() const; - //! Throws HazelnuppValueNotConvertibleException - long long int GetInt64() const override; - //! Throws HazelnuppValueNotConvertibleException - int GetInt32() 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; + //! 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; + //! Will return this value as a string + std::string GetString() const override; - //! Throws HazelnuppValueNotConvertibleException - const std::vector& GetList() const override; + //! Throws HazelnuppValueNotConvertibleException + const std::vector& GetList() const override; -private: - std::string value; -}; + private: + std::string value; + }; +} diff --git a/Hazelnupp/Value.cpp b/Hazelnupp/Value.cpp index 7b3b43e..d6dbde3 100644 --- a/Hazelnupp/Value.cpp +++ b/Hazelnupp/Value.cpp @@ -1,5 +1,7 @@ #include "Value.h" +using namespace Hazelnp; + Value::Value(DATA_TYPE type) : type{ type } diff --git a/Hazelnupp/Value.h b/Hazelnupp/Value.h index 88300e4..4adcdd0 100644 --- a/Hazelnupp/Value.h +++ b/Hazelnupp/Value.h @@ -3,45 +3,48 @@ #include #include -/** Abstract class for values -*/ -class Value +namespace Hazelnp { -public: - virtual ~Value() {}; - - //! Will return a deeopopy of this object - virtual Value* Deepcopy() const = 0; - - //! Will return a string suitable for an std::ostream - virtual std::string GetAsOsString() const = 0; - - //! Will return the data type of this value - DATA_TYPE GetDataType() const; - - friend std::ostream& operator<< (std::ostream& os, const Value& v) + /** Abstract class for values + */ + class Value { - return os << v.GetAsOsString(); - } + public: + virtual ~Value() {}; - //! Will attempt to return the integer data (long long) - virtual long long int GetInt64() const = 0; - //! Will attempt to return the integer data (int) - virtual int GetInt32() const = 0; + //! Will return a deeopopy of this object + virtual Value* Deepcopy() const = 0; - //! Will attempt to return the floating-point data (long double) - virtual long double GetFloat64() const = 0; - //! Will attempt to return the floating-point data (double) - virtual double GetFloat32() const = 0; + //! Will return a string suitable for an std::ostream + virtual std::string GetAsOsString() const = 0; - //! Will attempt to return the string-data - virtual std::string GetString() const = 0; + //! Will return the data type of this value + DATA_TYPE GetDataType() const; - //! Will attempt to return the list-data - virtual const std::vector& GetList() const = 0; + friend std::ostream& operator<< (std::ostream& os, const Value& v) + { + return os << v.GetAsOsString(); + } -protected: - Value(DATA_TYPE type); + //! Will attempt to return the integer data (long long) + virtual long long int GetInt64() const = 0; + //! Will attempt to return the integer data (int) + virtual int GetInt32() const = 0; - DATA_TYPE type; -}; + //! Will attempt to return the floating-point data (long double) + virtual long double GetFloat64() const = 0; + //! Will attempt to return the floating-point data (double) + virtual double GetFloat32() const = 0; + + //! Will attempt to return the string-data + virtual std::string GetString() const = 0; + + //! Will attempt to return the list-data + virtual const std::vector& GetList() const = 0; + + protected: + Value(DATA_TYPE type); + + DATA_TYPE type; + }; +} diff --git a/Hazelnupp/VoidValue.cpp b/Hazelnupp/VoidValue.cpp index e119c39..a3d4c86 100644 --- a/Hazelnupp/VoidValue.cpp +++ b/Hazelnupp/VoidValue.cpp @@ -1,6 +1,8 @@ #include "VoidValue.h" #include "HazelnuppException.h" +using namespace Hazelnp; + VoidValue::VoidValue() : Value(DATA_TYPE::VOID) diff --git a/Hazelnupp/VoidValue.h b/Hazelnupp/VoidValue.h index 58019d3..96a81ae 100644 --- a/Hazelnupp/VoidValue.h +++ b/Hazelnupp/VoidValue.h @@ -1,33 +1,36 @@ #pragma once #include "Value.h" -/** Specializations for void values. These house no value whatsoever, but only communicate information by merely existing. -*/ -class VoidValue : public Value +namespace Hazelnp { -public: - VoidValue(); - ~VoidValue() override {}; + /** 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 deeopopy of this object + Value* Deepcopy() const override; - //! Will return a string suitable for an std::ostream; - std::string GetAsOsString() 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 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 + long double GetFloat64() const override; + //! Throws HazelnuppValueNotConvertibleException + double GetFloat32() const override; - //! Throws HazelnuppValueNotConvertibleException - std::string GetString() const override; + //! Throws HazelnuppValueNotConvertibleException + std::string GetString() const override; - //! Throws HazelnuppValueNotConvertibleException - const std::vector& GetList() const; -}; + //! Throws HazelnuppValueNotConvertibleException + const std::vector& GetList() const; + }; +} diff --git a/Hazelnupp/main.cpp b/Hazelnupp/main.cpp index 6a4eb80..542297a 100644 --- a/Hazelnupp/main.cpp +++ b/Hazelnupp/main.cpp @@ -2,6 +2,8 @@ #include "Hazelnupp.h" #include "IntValue.h" +using namespace Hazelnp; + int main(int argc, char** argv) { while (1) diff --git a/Test_Hazelnupp/Abbreviations.cpp b/Test_Hazelnupp/Abbreviations.cpp index 94b94db..d5b817b 100644 --- a/Test_Hazelnupp/Abbreviations.cpp +++ b/Test_Hazelnupp/Abbreviations.cpp @@ -3,6 +3,7 @@ #include "../Hazelnupp/Hazelnupp.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace Hazelnp; namespace TestHazelnupp { diff --git a/Test_Hazelnupp/Basics.cpp b/Test_Hazelnupp/Basics.cpp index 4eec2e7..3c6a5d0 100644 --- a/Test_Hazelnupp/Basics.cpp +++ b/Test_Hazelnupp/Basics.cpp @@ -4,6 +4,7 @@ #include "../Hazelnupp/HazelnuppException.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace Hazelnp; namespace TestHazelnupp { diff --git a/Test_Hazelnupp/Constraints.cpp b/Test_Hazelnupp/Constraints.cpp index 4ef77bb..ca307b7 100644 --- a/Test_Hazelnupp/Constraints.cpp +++ b/Test_Hazelnupp/Constraints.cpp @@ -4,6 +4,7 @@ #include "../Hazelnupp/HazelnuppException.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace Hazelnp; namespace TestHazelnupp { diff --git a/Test_Hazelnupp/Conversion.cpp b/Test_Hazelnupp/Conversion.cpp index ae4a195..d7b85d7 100644 --- a/Test_Hazelnupp/Conversion.cpp +++ b/Test_Hazelnupp/Conversion.cpp @@ -4,6 +4,7 @@ #include "../Hazelnupp/HazelnuppException.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace Hazelnp; namespace TestHazelnupp {