Added Getters for data-types in Value

This commit is contained in:
Leonetienne 2021-06-02 15:58:14 +02:00
parent ec2e7a06ff
commit c412e879a1
12 changed files with 110 additions and 7 deletions

Binary file not shown.

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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:

View File

@ -34,6 +34,11 @@ void ListValue::AddValue(const Value* value)
return;
}
const std::vector<Value*>& ListValue::GetValue() const
{
return value;
}
std::string ListValue::GetAsOsString() const
{
std::stringstream ss;

View File

@ -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<Value*>& GetValue() const;
operator std::vector<Value*>() const;
private:

View File

@ -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;

View File

@ -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:

View File

@ -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*>& Value::GetList() const
{
if (type != DATA_TYPE::LIST)
throw std::bad_cast();
return ((ListValue*)this)->GetValue();
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "DataType.h"
#include <ostream>
#include <vector>
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<Value*>& GetList() const;
protected:
Value(DATA_TYPE type);

View File

@ -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