diff --git a/Hazelnupp.vpp b/Hazelnupp.vpp index 8cffd19..fc6b34f 100644 Binary files a/Hazelnupp.vpp and b/Hazelnupp.vpp differ diff --git a/Hazelnupp/FloatValue.cpp b/Hazelnupp/FloatValue.cpp index 4201074..20d3aba 100644 --- a/Hazelnupp/FloatValue.cpp +++ b/Hazelnupp/FloatValue.cpp @@ -21,6 +21,11 @@ std::string FloatValue::GetAsOsString() const return ss.str(); } +const long double& FloatValue::GetValue() const +{ + return value; +} + FloatValue::operator long double() const { return value; diff --git a/Hazelnupp/FloatValue.h b/Hazelnupp/FloatValue.h index 0d03d69..9d1173f 100644 --- a/Hazelnupp/FloatValue.h +++ b/Hazelnupp/FloatValue.h @@ -13,6 +13,9 @@ public: //! 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; diff --git a/Hazelnupp/IntValue.cpp b/Hazelnupp/IntValue.cpp index 11f1d34..198e823 100644 --- a/Hazelnupp/IntValue.cpp +++ b/Hazelnupp/IntValue.cpp @@ -21,14 +21,14 @@ std::string IntValue::GetAsOsString() const return ss.str(); } -IntValue::operator long long int() const +const long long int& IntValue::GetValue() const { return value; } -IntValue::operator long int() const +IntValue::operator long long int() const { - return (long int)value; + return value; } IntValue::operator int() const diff --git a/Hazelnupp/IntValue.h b/Hazelnupp/IntValue.h index 14f45ec..1e2554f 100644 --- a/Hazelnupp/IntValue.h +++ b/Hazelnupp/IntValue.h @@ -12,8 +12,10 @@ public: //! 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; + operator long long int() const; - operator long int() const; operator int() const; private: diff --git a/Hazelnupp/ListValue.cpp b/Hazelnupp/ListValue.cpp index 5d5e23a..82cfff9 100644 --- a/Hazelnupp/ListValue.cpp +++ b/Hazelnupp/ListValue.cpp @@ -34,6 +34,11 @@ void ListValue::AddValue(const Value* value) return; } +const std::vector& ListValue::GetValue() const +{ + return value; +} + std::string ListValue::GetAsOsString() const { std::stringstream ss; diff --git a/Hazelnupp/ListValue.h b/Hazelnupp/ListValue.h index 4215d0f..7aad97a 100644 --- a/Hazelnupp/ListValue.h +++ b/Hazelnupp/ListValue.h @@ -17,6 +17,9 @@ public: //! Will add this value to the list void AddValue(const Value* value); + //! Will return the raw value + const std::vector& GetValue() const; + operator std::vector() const; private: diff --git a/Hazelnupp/StringValue.cpp b/Hazelnupp/StringValue.cpp index b2fa5e6..2849f00 100644 --- a/Hazelnupp/StringValue.cpp +++ b/Hazelnupp/StringValue.cpp @@ -21,6 +21,11 @@ std::string StringValue::GetAsOsString() const return ss.str(); } +const std::string& StringValue::GetValue() const +{ + return value; +} + StringValue::operator std::string() const { return value; diff --git a/Hazelnupp/StringValue.h b/Hazelnupp/StringValue.h index 1d13a53..dd0b797 100644 --- a/Hazelnupp/StringValue.h +++ b/Hazelnupp/StringValue.h @@ -13,6 +13,9 @@ public: //! Will return a string suitable for an std::ostream; std::string GetAsOsString() const override; + //! Will return the raw value + const std::string& GetValue() const; + operator std::string() const; private: diff --git a/Hazelnupp/Value.cpp b/Hazelnupp/Value.cpp index ada73b9..d69c0f6 100644 --- a/Hazelnupp/Value.cpp +++ b/Hazelnupp/Value.cpp @@ -1,8 +1,65 @@ #include "Value.h" +#include "IntValue.h" +#include "FloatValue.h" +#include "StringValue.h" +#include "ListValue.h" Value::Value(DATA_TYPE type) : - type { type } + type{ type } { return; } + +DATA_TYPE Value::GetDataType() const +{ + return type; +} + +long long int Value::GetInt64() const +{ + if (type != DATA_TYPE::INT) + throw std::bad_cast(); + + return ((IntValue*)this)->GetValue(); +} + +int Value::GetInt32() const +{ + if (type != DATA_TYPE::INT) + throw std::bad_cast(); + + return (int)((IntValue*)this)->GetValue(); +} + +long double Value::GetFloat64() const +{ + if (type != DATA_TYPE::FLOAT) + throw std::bad_cast(); + + return ((FloatValue*)this)->GetValue(); +} + +double Value::GetFloat32() const +{ + if (type != DATA_TYPE::FLOAT) + throw std::bad_cast(); + + return (double)((FloatValue*)this)->GetValue(); +} + +const std::string& Value::GetString() const +{ + if (type != DATA_TYPE::STRING) + throw std::bad_cast(); + + return ((StringValue*)this)->GetValue(); +} + +const std::vector& Value::GetList() const +{ + if (type != DATA_TYPE::LIST) + throw std::bad_cast(); + + return ((ListValue*)this)->GetValue(); +} diff --git a/Hazelnupp/Value.h b/Hazelnupp/Value.h index c303644..72afff1 100644 --- a/Hazelnupp/Value.h +++ b/Hazelnupp/Value.h @@ -1,6 +1,7 @@ #pragma once #include "DataType.h" #include +#include class Value { @@ -8,14 +9,33 @@ public: //! Will return a deeopopy of this object virtual Value* Deepcopy() const = 0; - //! Will return a string suitable for an std::ostream; + //! 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(); } + //! Will attempt to return the integer data (long long), if the type matches + long long int GetInt64() const; + //! Will attempt to return the integer data (int), if the type matches + int GetInt32() const; + + //! Will attempt to return the floating-point data (long double), if the type matches + long double GetFloat64() const; + //! Will attempt to return the floating-point data (double), if the type matches + double GetFloat32() const; + + //! Will attempt to return the string-data, if the type matches + const std::string& GetString() const; + + //! Will attempt to return the list-data, if the type matches + const std::vector& GetList() const; + protected: Value(DATA_TYPE type); diff --git a/Hazelnupp/main.cpp b/Hazelnupp/main.cpp index 36996ae..f44e452 100644 --- a/Hazelnupp/main.cpp +++ b/Hazelnupp/main.cpp @@ -28,7 +28,7 @@ int main(int argc, char** argv) if (args.HasParam("--word")) { - int i = *(IntValue*)args["--word"]; + int i = args["--word"]->GetInt32(); std::cout << i << std::endl; } else