Namespacified

This commit is contained in:
Leonetienne 2021-06-03 13:42:40 +02:00
parent 697eb98621
commit 124b6e5b98
26 changed files with 443 additions and 383 deletions

View File

@ -1,12 +1,15 @@
#pragma once #pragma once
/** The different data types a paramater can be namespace Hazelnp
*/
enum class DATA_TYPE
{ {
VOID, /** The different data types a paramater can be
INT, */
FLOAT, enum class DATA_TYPE
STRING, {
LIST VOID,
}; INT,
FLOAT,
STRING,
LIST
};
}

View File

@ -2,6 +2,8 @@
#include "HazelnuppException.h" #include "HazelnuppException.h"
#include <sstream> #include <sstream>
using namespace Hazelnp;
FloatValue::FloatValue(const long double& value) FloatValue::FloatValue(const long double& value)
: :
Value(DATA_TYPE::FLOAT), Value(DATA_TYPE::FLOAT),

View File

@ -2,42 +2,45 @@
#include "Value.h" #include "Value.h"
#include <ostream> #include <ostream>
/** Specializations for floating point values (uses long double) namespace Hazelnp
*/
class FloatValue : public Value
{ {
public: /** Specializations for floating point values (uses long double)
FloatValue(const long double& value); */
~FloatValue() override {}; class FloatValue : public Value
{
public:
FloatValue(const long double& value);
~FloatValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;
//! Will return a string suitable for an std::ostream; //! Will return a string suitable for an std::ostream;
std::string GetAsOsString() const override; std::string GetAsOsString() const override;
//! Will return the raw value //! Will return the raw value
const long double& GetValue() const; const long double& GetValue() const;
operator long double () const; operator long double() const;
operator double() const; operator double() const;
//! Will return the data as a long long int //! Will return the data as a long long int
long long int GetInt64() const override; long long int GetInt64() const override;
//! Will return the data as an int //! Will return the data as an int
int GetInt32() const override; int GetInt32() const override;
//! Will return the data as a long double //! Will return the data as a long double
long double GetFloat64() const override; long double GetFloat64() const override;
//! Will return the data as a double //! Will return the data as a double
double GetFloat32() const override; double GetFloat32() const override;
//! Will return the data as a string //! Will return the data as a string
std::string GetString() const override; std::string GetString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
const std::vector<Value*>& GetList() const override; const std::vector<Value*>& GetList() const override;
private: private:
long double value; long double value;
}; };
}

View File

@ -9,6 +9,8 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
using namespace Hazelnp;
Hazelnupp::Hazelnupp() Hazelnupp::Hazelnupp()
{ {
return; return;

View File

@ -4,84 +4,87 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
/** The main class to interface with namespace Hazelnp
*/
class Hazelnupp
{ {
public: /** The main class to interface with
Hazelnupp(); */
Hazelnupp(const int argc, const char* const* argv); class Hazelnupp
{
public:
Hazelnupp();
Hazelnupp(const int argc, const char* const* argv);
~Hazelnupp(); ~Hazelnupp();
//! Will parse command line arguments //! Will parse command line arguments
void Parse(const int argc, const char* const* argv); void Parse(const int argc, const char* const* argv);
//! Will return argv[0], the name of the executable. //! Will return argv[0], the name of the executable.
const std::string& GetExecutableName() const; const std::string& GetExecutableName() const;
//! Will return the value given a key //! Will return the value given a key
const Value& operator[](const std::string& key) const; const Value& operator[](const std::string& key) const;
//! Will check wether a parameter exists given a key, or not //! Will check wether a parameter exists given a key, or not
bool HasParam(const std::string& key) const; bool HasParam(const std::string& key) const;
// Abbreviations // Abbreviations
//! 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)
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 all abbreviations //! Will delete all abbreviations
void ClearAbbreviations(); void ClearAbbreviations();
//! Will register parameter constraints //! Will register parameter constraints
void RegisterConstraints(const std::vector<ParamConstraint>& constraints); void RegisterConstraints(const std::vector<ParamConstraint>& constraints);
//! Will delete all constraints //! Will delete all constraints
void ClearConstraints(); void ClearConstraints();
//! Sets whether to crash the application, and print to stderr, when an exception is //! Sets whether to crash the application, and print to stderr, when an exception is
//! raised whilst parsing, or not. //! raised whilst parsing, or not.
void SetCrashOnFail(bool crashOnFail); void SetCrashOnFail(bool crashOnFail);
//! Gets whether the application crashes on an exception whilst parsing, and prints to stderr. //! Gets whether the application crashes on an exception whilst parsing, and prints to stderr.
bool GetCrashOnFail() const; bool GetCrashOnFail() const;
private: private:
//! Will translate the c-like args to an std::vector //! Will translate the c-like args to an std::vector
void PopulateRawArgs(const int argc, const char* const* argv); void PopulateRawArgs(const int argc, const char* const* argv);
//! Will replace all args matching an abbreviation with their long form (like -f for --force) //! Will replace all args matching an abbreviation with their long form (like -f for --force)
void ExpandAbbreviations(); void ExpandAbbreviations();
//! Will parse the next parameter. Returns the index of the next parameter. //! Will parse the next parameter. Returns the index of the next parameter.
std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par); std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
//! Will convert a vector of string-values to an actual Value //! Will convert a vector of string-values to an actual Value
Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr); Value* ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint = nullptr);
//! Will apply the loaded constraints on the loaded values, exluding types. //! Will apply the loaded constraints on the loaded values, exluding types.
void ApplyConstraints(); void ApplyConstraints();
//! Will return a pointer to a paramConstraint given a key. If there is no, it returns nullptr //! 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; const ParamConstraint* GetConstraintForKey(const std::string& key) const;
std::string executableName; //! The path of the executable. Always argv[0] std::string executableName; //! The path of the executable. Always argv[0]
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> abbreviations;
// Parameter constraints, mapped to keys // Parameter constraints, mapped to keys
std::unordered_map<std::string, ParamConstraint> constraints; std::unordered_map<std::string, ParamConstraint> constraints;
std::vector<std::string> rawArgs; std::vector<std::string> rawArgs;
//! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing. //! If set to true, Hazelnupp will crash the application with output to stderr when an exception is thrown whilst parsing.
bool crashOnFail = true; bool crashOnFail = true;
}; };
}

View File

@ -1,65 +1,68 @@
#pragma once #pragma once
#include <stdexcept> #include <stdexcept>
/** Generic hazelnupp exception namespace Hazelnp
*/
class HazelnuppException : public std::exception
{ {
public: /** Generic hazelnupp exception
HazelnuppException() {}; */
HazelnuppException(const std::string& msg) : message{ msg } {}; class HazelnuppException : public std::exception
//! Will return an error message
const std::string& What() const
{ {
return message; public:
} HazelnuppException() {};
HazelnuppException(const std::string& msg) : message{ msg } {};
protected: //! Will return an error message
std::string message; const std::string& What() const
}; {
return message;
}
/** Gets thrown when an non-existent key gets dereferenced protected:
*/ std::string message;
class HazelnuppInvalidKeyException : public HazelnuppException };
{
public:
HazelnuppInvalidKeyException() : HazelnuppException() {};
HazelnuppInvalidKeyException(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 /** Gets thrown when an non-existent key gets dereferenced
*/ */
class HazelnuppValueNotConvertibleException : public HazelnuppException class HazelnuppInvalidKeyException : public HazelnuppException
{ {
public: public:
HazelnuppValueNotConvertibleException() : HazelnuppException() {}; HazelnuppInvalidKeyException() : HazelnuppException() {};
HazelnuppValueNotConvertibleException(const std::string& msg) : HazelnuppException(msg) {}; HazelnuppInvalidKeyException(const std::string& msg) : HazelnuppException(msg) {};
}; };
/** Gets thrown something bad happens because of parameter constraints /** Gets thrown when an attempt is made to retrieve the wrong data type from a value, when the value not convertible
*/ */
class HazelnuppConstraintException : public HazelnuppException class HazelnuppValueNotConvertibleException : public HazelnuppException
{ {
public: public:
HazelnuppConstraintException() : HazelnuppException() {}; HazelnuppValueNotConvertibleException() : HazelnuppException() {};
HazelnuppConstraintException(const std::string& msg) : HazelnuppException(msg) {}; 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 /** Gets thrown something bad happens because of parameter constraints
*/ */
class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException class HazelnuppConstraintException : public HazelnuppException
{ {
public: public:
HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {}; HazelnuppConstraintException() : HazelnuppException() {};
HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {}; 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 /** Gets thrown when a parameter is of a type that does not match the required type, and is not convertible to it
*/ */
class HazelnuppConstraintMissingValue : public HazelnuppConstraintException class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException
{ {
public: public:
HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {}; HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {};
HazelnuppConstraintMissingValue(const std::string& msg) : HazelnuppConstraintException(msg) {}; 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) {};
};
}

View File

@ -2,6 +2,8 @@
#include "HazelnuppException.h" #include "HazelnuppException.h"
#include <sstream> #include <sstream>
using namespace Hazelnp;
IntValue::IntValue(const long long int& value) IntValue::IntValue(const long long int& value)
: :
Value(DATA_TYPE::INT), Value(DATA_TYPE::INT),

View File

@ -1,43 +1,46 @@
#pragma once #pragma once
#include "Value.h" #include "Value.h"
/** Specializations for integer values (uses long long int) namespace Hazelnp
*/
class IntValue : public Value
{ {
public: /** Specializations for integer values (uses long long int)
IntValue(const long long int& value); */
~IntValue() override {}; class IntValue : public Value
{
public:
IntValue(const long long int& value);
~IntValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;
//! Will return a string suitable for an std::ostream; //! Will return a string suitable for an std::ostream;
std::string GetAsOsString() const override; std::string GetAsOsString() const override;
//! Will return the raw value //! Will return the raw value
const long long int& GetValue() const; const long long int& GetValue() const;
operator long long int() const; operator long long int() const;
operator int() const; operator int() const;
//! Will return the data as a long long int //! Will return the data as a long long int
long long int GetInt64() const override; long long int GetInt64() const override;
//! Will return the data as an int //! Will return the data as an int
int GetInt32() const override; int GetInt32() const override;
//! Will return the data as a long double //! Will return the data as a long double
long double GetFloat64() const override; long double GetFloat64() const override;
//! Will return the data as a double //! Will return the data as a double
double GetFloat32() const override; double GetFloat32() const override;
//! Will return the data as a string //! Will return the data as a string
std::string GetString() const override; std::string GetString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
const std::vector<Value*>& GetList() const override; const std::vector<Value*>& GetList() const override;
private: private:
long long int value; long long int value;
}; };
}

View File

@ -2,6 +2,8 @@
#include "HazelnuppException.h" #include "HazelnuppException.h"
#include <sstream> #include <sstream>
using namespace Hazelnp;
ListValue::ListValue() : ListValue::ListValue() :
Value(DATA_TYPE::LIST) Value(DATA_TYPE::LIST)
{ {

View File

@ -2,44 +2,47 @@
#include "Value.h" #include "Value.h"
#include <vector> #include <vector>
/** Specializations for list values (uses std::vector<Value*>) namespace Hazelnp
*/
class ListValue : public Value
{ {
public: /** Specializations for list values (uses std::vector<Value*>)
ListValue(); */
~ListValue() override; class ListValue : public Value
{
public:
ListValue();
~ListValue() override;
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;
//! Will return a string suitable for an std::ostream; //! Will return a string suitable for an std::ostream;
std::string GetAsOsString() const override; std::string GetAsOsString() const override;
//! Will add this value to the list //! Will add this value to the list
void AddValue(const Value* value); void AddValue(const Value* value);
//! Will return the raw value //! Will return the raw value
const std::vector<Value*>& GetValue() const; const std::vector<Value*>& GetValue() const;
operator std::vector<Value*>() const; operator std::vector<Value*>() const;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
long long int GetInt64() const override; long long int GetInt64() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
int GetInt32() const override; int GetInt32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
long double GetFloat64() const override; long double GetFloat64() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
double GetFloat32() const override; double GetFloat32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
std::string GetString() const override; std::string GetString() const override;
//! Will return this values list //! Will return this values list
const std::vector<Value*>& GetList() const override; const std::vector<Value*>& GetList() const override;
private: private:
std::vector<Value*> value; std::vector<Value*> value;
}; };
}

View File

@ -3,63 +3,66 @@
#include <string> #include <string>
#include <vector> #include <vector>
struct ParamConstraint namespace Hazelnp
{ {
public: struct ParamConstraint
//! 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<std::string>& defaultValue = {}, bool required = true)
{ {
ParamConstraint pc; public:
pc.key = key; //! Empty constructor
pc.defaultValue = defaultValue; ParamConstraint() = default;
pc.required = required;
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<std::string>& defaultValue = {}, bool required = true)
{
ParamConstraint pc;
pc.key = key;
pc.defaultValue = defaultValue;
pc.required = required;
//! Constructs a type-safety constraint return pc;
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; //! 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 return pc;
ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector<std::string>& defaultValue, bool required) }
:
key { key },
constrainType { constrainType },
wantedType { wantedType },
defaultValue { defaultValue },
required{ required }
{
return;
}
//! The key of the parameter to constrain //! Whole constructor
std::string key; ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector<std::string>& defaultValue, bool required)
:
key{ key },
constrainType{ constrainType },
wantedType{ wantedType },
defaultValue{ defaultValue },
required{ required }
{
return;
}
//! Should this parameter be forced to be of a certain type? //! The key of the parameter to constrain
//! Remember to set `constrainTo` to the wanted type std::string key;
bool constrainType = false;
//! Constrain the parameter to this value. Requires `constrainType` to be set to true. //! Should this parameter be forced to be of a certain type?
DATA_TYPE wantedType = DATA_TYPE::VOID; //! Remember to set `constrainTo` to the wanted type
bool constrainType = false;
//! The default value for this parameter. //! Constrain the parameter to this value. Requires `constrainType` to be set to true.
//! Gets applied if this parameter was not given. DATA_TYPE wantedType = DATA_TYPE::VOID;
//! Think of this like a list of parameters. Like {"--width", "800"}
std::vector<std::string> defaultValue;
//! If set to true, and no default value set, //! The default value for this parameter.
//! an error will be produced if this parameter is not supplied by the user. //! Gets applied if this parameter was not given.
bool required = false; //! Think of this like a list of parameters. Like {"--width", "800"}
}; std::vector<std::string> 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;
};
}

View File

@ -1,5 +1,7 @@
#include "Parameter.h" #include "Parameter.h"
using namespace Hazelnp;
Parameter::Parameter(const std::string& key, const ::Value* value) Parameter::Parameter(const std::string& key, const ::Value* value)
: :
key{ key } key{ key }

View File

@ -3,24 +3,27 @@
#include <string> #include <string>
#include <ostream> #include <ostream>
class Parameter namespace Hazelnp
{ {
public: class Parameter
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)
{ {
return os << "{ Key: \"" << p.key << "\" -> " << *p.value << " }"; public:
} explicit Parameter(const std::string& key, const Value* value);
~Parameter();
private: //! Will return the key of this parameter
std::string key; const std::string& Key() const;
::Value* value;
}; //! 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;
};
}

View File

@ -1,5 +1,7 @@
#include "StringTools.h" #include "StringTools.h"
using namespace Hazelnp;
bool StringTools::Contains(const std::string& str, const char c) bool StringTools::Contains(const std::string& str, const char c)
{ {
for (const char& i : str) for (const char& i : str)

View File

@ -4,34 +4,37 @@
#include <vector> #include <vector>
#include <cmath> #include <cmath>
/** Internal helper class. Feel free to use it tho. namespace Hazelnp
*/
class StringTools
{ {
public: /** Internal helper class. Feel free to use it tho.
//! Will return wether or not a given char is in a string */
static bool Contains(const std::string& str, const char c); 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 //! 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); 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 //! 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); 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) //! Will return true if the given string consists only of digits (including signage)
static bool IsNumeric(const std::string& str, const bool allowDecimalPoint = false); static bool IsNumeric(const std::string& str, const bool allowDecimalPoint = false);
//! Will convert the number in str to a number. //! Will convert the number in str to a number.
//! Returns wether or not the operation was successful. //! 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. //! 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); 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! //! Will split a string by a delimiter char. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string& str, const char delimiter); static std::vector<std::string> SplitString(const std::string& str, const char delimiter);
//! Will split a string by a delimiter string. The delimiter will be excluded! //! Will split a string by a delimiter string. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter); static std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter);
//! Will make a string all lower-case //! Will make a string all lower-case
static std::string ToLower(const std::string& str); static std::string ToLower(const std::string& str);
}; };
}

View File

@ -2,6 +2,8 @@
#include "HazelnuppException.h" #include "HazelnuppException.h"
#include <sstream> #include <sstream>
using namespace Hazelnp;
StringValue::StringValue(const std::string& value) StringValue::StringValue(const std::string& value)
: :
Value(DATA_TYPE::STRING), Value(DATA_TYPE::STRING),

View File

@ -2,41 +2,44 @@
#include "Value.h" #include "Value.h"
#include <string> #include <string>
/** Specializations for string values (uses std::string) namespace Hazelnp
*/
class StringValue : public Value
{ {
public: /** Specializations for string values (uses std::string)
StringValue(const std::string& value); */
~StringValue() override {}; class StringValue : public Value
{
public:
StringValue(const std::string& value);
~StringValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;
//! Will return a string suitable for an std::ostream; //! Will return a string suitable for an std::ostream;
std::string GetAsOsString() const override; std::string GetAsOsString() const override;
//! Will return the raw value //! Will return the raw value
const std::string& GetValue() const; const std::string& GetValue() const;
operator std::string() const; operator std::string() const;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
long long int GetInt64() const override; long long int GetInt64() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
int GetInt32() const override; int GetInt32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
long double GetFloat64() const override; long double GetFloat64() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
double GetFloat32() const override; double GetFloat32() const override;
//! Will return this value as a string //! Will return this value as a string
std::string GetString() const override; std::string GetString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
const std::vector<Value*>& GetList() const override; const std::vector<Value*>& GetList() const override;
private: private:
std::string value; std::string value;
}; };
}

View File

@ -1,5 +1,7 @@
#include "Value.h" #include "Value.h"
using namespace Hazelnp;
Value::Value(DATA_TYPE type) Value::Value(DATA_TYPE type)
: :
type{ type } type{ type }

View File

@ -3,45 +3,48 @@
#include <ostream> #include <ostream>
#include <vector> #include <vector>
/** Abstract class for values namespace Hazelnp
*/
class Value
{ {
public: /** Abstract class for values
virtual ~Value() {}; */
class 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)
{ {
return os << v.GetAsOsString(); public:
} virtual ~Value() {};
//! Will attempt to return the integer data (long long) //! Will return a deeopopy of this object
virtual long long int GetInt64() const = 0; virtual Value* Deepcopy() const = 0;
//! Will attempt to return the integer data (int)
virtual int GetInt32() const = 0;
//! Will attempt to return the floating-point data (long double) //! Will return a string suitable for an std::ostream
virtual long double GetFloat64() const = 0; virtual std::string GetAsOsString() const = 0;
//! Will attempt to return the floating-point data (double)
virtual double GetFloat32() const = 0;
//! Will attempt to return the string-data //! Will return the data type of this value
virtual std::string GetString() const = 0; DATA_TYPE GetDataType() const;
//! Will attempt to return the list-data friend std::ostream& operator<< (std::ostream& os, const Value& v)
virtual const std::vector<Value*>& GetList() const = 0; {
return os << v.GetAsOsString();
}
protected: //! Will attempt to return the integer data (long long)
Value(DATA_TYPE type); 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<Value*>& GetList() const = 0;
protected:
Value(DATA_TYPE type);
DATA_TYPE type;
};
}

View File

@ -1,6 +1,8 @@
#include "VoidValue.h" #include "VoidValue.h"
#include "HazelnuppException.h" #include "HazelnuppException.h"
using namespace Hazelnp;
VoidValue::VoidValue() VoidValue::VoidValue()
: :
Value(DATA_TYPE::VOID) Value(DATA_TYPE::VOID)

View File

@ -1,33 +1,36 @@
#pragma once #pragma once
#include "Value.h" #include "Value.h"
/** Specializations for void values. These house no value whatsoever, but only communicate information by merely existing. namespace Hazelnp
*/
class VoidValue : public Value
{ {
public: /** Specializations for void values. These house no value whatsoever, but only communicate information by merely existing.
VoidValue(); */
~VoidValue() override {}; class VoidValue : public Value
{
public:
VoidValue();
~VoidValue() override {};
//! Will return a deeopopy of this object //! Will return a deeopopy of this object
Value* Deepcopy() const override; Value* Deepcopy() const override;
//! Will return a string suitable for an std::ostream; //! Will return a string suitable for an std::ostream;
std::string GetAsOsString() const override; std::string GetAsOsString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
long long int GetInt64() const override; long long int GetInt64() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
int GetInt32() const override; int GetInt32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
long double GetFloat64() const override; long double GetFloat64() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
double GetFloat32() const override; double GetFloat32() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
std::string GetString() const override; std::string GetString() const override;
//! Throws HazelnuppValueNotConvertibleException //! Throws HazelnuppValueNotConvertibleException
const std::vector<Value*>& GetList() const; const std::vector<Value*>& GetList() const;
}; };
}

View File

@ -2,6 +2,8 @@
#include "Hazelnupp.h" #include "Hazelnupp.h"
#include "IntValue.h" #include "IntValue.h"
using namespace Hazelnp;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
while (1) while (1)

View File

@ -3,6 +3,7 @@
#include "../Hazelnupp/Hazelnupp.h" #include "../Hazelnupp/Hazelnupp.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Hazelnp;
namespace TestHazelnupp namespace TestHazelnupp
{ {

View File

@ -4,6 +4,7 @@
#include "../Hazelnupp/HazelnuppException.h" #include "../Hazelnupp/HazelnuppException.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Hazelnp;
namespace TestHazelnupp namespace TestHazelnupp
{ {

View File

@ -4,6 +4,7 @@
#include "../Hazelnupp/HazelnuppException.h" #include "../Hazelnupp/HazelnuppException.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Hazelnp;
namespace TestHazelnupp namespace TestHazelnupp
{ {

View File

@ -4,6 +4,7 @@
#include "../Hazelnupp/HazelnuppException.h" #include "../Hazelnupp/HazelnuppException.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Hazelnp;
namespace TestHazelnupp namespace TestHazelnupp
{ {