From 7173650df9c44398a60bc35d1363c477fdf10752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Fri, 23 Dec 2022 12:09:12 +0100 Subject: [PATCH] Migrate AnyType to std::variant AnyType did resource management wrong. Not that it's particularly nice now, but it's at least correct. There's a change I want to make later that was blocked by this. --- src/lua/TPTSTypes.cpp | 56 +++++++++++++++---------------------------- src/lua/TPTSTypes.h | 5 ++-- 2 files changed, 21 insertions(+), 40 deletions(-) diff --git a/src/lua/TPTSTypes.cpp b/src/lua/TPTSTypes.cpp index 3019f5c53..b8eea89e9 100644 --- a/src/lua/TPTSTypes.cpp +++ b/src/lua/TPTSTypes.cpp @@ -11,22 +11,12 @@ ValueType AnyType::GetType() return type; } -AnyType::AnyType(const AnyType & v): - type(v.type), - value(v.value) -{ - if(type == TypeString) - value.str = new String(*(value.str)); - else if(type == TypePoint) - value.pt = new ui::Point(*(value.pt)); -} - AnyType::operator NumberType() { if (type == TypeNumber) - return NumberType(value.num); + return NumberType(std::get(value)); else if (type == TypeFloat) - return NumberType(int(value.numf)); + return NumberType(int(std::get(value))); else throw InvalidConversionException(type, TypeNumber); } @@ -34,9 +24,9 @@ AnyType::operator NumberType() AnyType::operator FloatType() { if (type == TypeNumber) - return FloatType(float(value.num)); + return FloatType(float(std::get(value))); else if (type == TypeFloat) - return FloatType(value.numf); + return FloatType(std::get(value)); else throw InvalidConversionException(type, TypeFloat); } @@ -47,13 +37,13 @@ AnyType::operator StringType() { return StringType(String::Build(((NumberType *)this)->Value())); } - else if(type == TypeString && value.str) + else if(type == TypeString && std::holds_alternative(value)) { - return StringType(*(value.str)); + return StringType(std::get(value)); } - else if (type == TypePoint && value.pt) + else if (type == TypePoint && std::holds_alternative(value)) { - ui::Point thisPoint = *(value.pt); + ui::Point thisPoint = std::get(value); return StringType(String::Build(thisPoint.X, ",", thisPoint.Y)); } else @@ -65,12 +55,12 @@ AnyType::operator PointType() { if(type == TypePoint) { - return PointType(*(value.pt)); + return PointType(std::get(value)); } else if(type == TypeString) { int x, y; - if(String::Split comma = (*value.str).SplitNumber(x)) + if(String::Split comma = std::get(value).SplitNumber(x)) if(comma.After().BeginsWith(",")) if(String::Split end = comma.After().Substr(1).SplitNumber(y)) if(!end.After().size()) @@ -81,63 +71,55 @@ AnyType::operator PointType() throw InvalidConversionException(type, TypePoint); } -AnyType::~AnyType() -{ - if(type == TypeString) - delete value.str; - else if(type == TypePoint) - delete value.pt; -} - //Number Type NumberType::NumberType(int number): AnyType(TypeNumber, ValueValue()) { - value.num = number; + value = number; } int NumberType::Value() { - return value.num; + return std::get(value); } //Float Type FloatType::FloatType(float number): AnyType(TypeFloat, ValueValue()) { - value.numf = number; + value = number; } float FloatType::Value() { - return value.numf; + return std::get(value); } //String type StringType::StringType(String string): AnyType(TypeString, ValueValue()) { - value.str = new String(string); + value = string; } String StringType::Value() { - return *value.str; + return std::get(value); } //Point type PointType::PointType(ui::Point point): AnyType(TypePoint, ValueValue()) { - value.pt = new ui::Point(point); + value = point; } PointType::PointType(int pointX, int pointY): AnyType(TypePoint, ValueValue()) { - value.pt = new ui::Point(pointX, pointY); + value = ui::Point(pointX, pointY); } ui::Point PointType::Value() { - return *value.pt; + return std::get(value); } diff --git a/src/lua/TPTSTypes.h b/src/lua/TPTSTypes.h index 3dfc67961..0e27722ad 100644 --- a/src/lua/TPTSTypes.h +++ b/src/lua/TPTSTypes.h @@ -4,9 +4,10 @@ #include "common/String.h" #include "gui/interface/Point.h" +#include enum ValueType { TypeNumber, TypeFloat, TypePoint, TypeString, TypeNull, TypeFunction }; -typedef union { int num; float numf; String* str; ui::Point* pt; } ValueValue; +typedef std::variant ValueValue; class GeneralException { @@ -34,7 +35,6 @@ protected: ValueValue value; public: AnyType(ValueType type_, ValueValue value_); - AnyType(const AnyType & v); operator NumberType(); operator FloatType(); operator StringType(); @@ -80,7 +80,6 @@ public: return "Unknown"; } } - ~AnyType(); }; class InvalidConversionException: public GeneralException