fix: memory leak

This commit is contained in:
TopchetoEU 2022-11-24 14:24:41 +02:00
parent 0a352f29a9
commit ee16dfbe71

View File

@ -1,176 +1,177 @@
#include "utils/data.hh" #include "utils/data.hh"
namespace ppc::data { namespace ppc::data {
bool value_t::is_null() const { bool value_t::is_null() const {
return type == type_t::Null; return type == type_t::Null;
} }
bool value_t::is_map() const { bool value_t::is_map() const {
return type == type_t::Map; return type == type_t::Map;
} }
bool value_t::is_array() const { bool value_t::is_array() const {
return type == type_t::Arr; return type == type_t::Arr;
} }
bool value_t::is_number() const { bool value_t::is_number() const {
return type == type_t::Num; return type == type_t::Num;
} }
bool value_t::is_string() const { bool value_t::is_string() const {
return type == type_t::Str; return type == type_t::Str;
} }
bool value_t::is_bool() const { bool value_t::is_bool() const {
return type == type_t::Bool; return type == type_t::Bool;
} }
array_t &value_t::array(const array_t &val) { array_t &value_t::array(const array_t &val) {
*this = val; *this = val;
return *this->val.arr; return *this->val.arr;
} }
map_t &value_t::map(const map_t &val) { map_t &value_t::map(const map_t &val) {
*this = val; *this = val;
return *this->val.map; return *this->val.map;
} }
number_t &value_t::number(number_t val) { number_t &value_t::number(number_t val) {
*this = val; *this = val;
return this->val.num; return this->val.num;
} }
string_t &value_t::string(const string_t &val) { string_t &value_t::string(const string_t &val) {
*this = val; *this = val;
return *this->val.str; return *this->val.str;
} }
bool_t &value_t::boolean(bool_t val) { bool_t &value_t::boolean(bool_t val) {
*this = val; *this = val;
return this->val.bl; return this->val.bl;
} }
array_t &value_t::array() { array_t &value_t::array() {
if (is_array()) return *val.arr; if (is_array()) return *val.arr;
else throw (std::string)"The value isn't an array."; else throw (std::string)"The value isn't an array.";
} }
map_t &value_t::map() { map_t &value_t::map() {
if (is_map()) return *val.map; if (is_map()) return *val.map;
else throw (std::string)"The value isn't a map."; else throw (std::string)"The value isn't a map.";
} }
number_t &value_t::number() { number_t &value_t::number() {
if (is_number()) return val.num; if (is_number()) return val.num;
else throw (std::string)"The value isn't a number."; else throw (std::string)"The value isn't a number.";
} }
string_t &value_t::string() { string_t &value_t::string() {
if (is_string()) return *val.str; if (is_string()) return *val.str;
else throw (std::string)"The value isn't a string."; else throw (std::string)"The value isn't a string.";
} }
bool_t &value_t::boolean() { bool_t &value_t::boolean() {
if (is_bool()) return val.bl; if (is_bool()) return val.bl;
else throw (std::string)"The value isn't a bool."; else throw (std::string)"The value isn't a bool.";
} }
const array_t &value_t::array() const { const array_t &value_t::array() const {
if (is_array()) return *val.arr; if (is_array()) return *val.arr;
else throw (std::string)"The value isn't an array."; else throw (std::string)"The value isn't an array.";
} }
const map_t &value_t::map() const { const map_t &value_t::map() const {
if (is_map()) return *val.map; if (is_map()) return *val.map;
else throw (std::string)"The value isn't a map."; else throw (std::string)"The value isn't a map.";
} }
number_t value_t::number() const { number_t value_t::number() const {
if (is_number()) return val.num; if (is_number()) return val.num;
else throw (std::string)"The value isn't a number."; else throw (std::string)"The value isn't a number.";
} }
const string_t &value_t::string() const { const string_t &value_t::string() const {
if (is_string()) return *val.str; if (is_string()) return *val.str;
else throw (std::string)"The value isn't a string."; else throw (std::string)"The value isn't a string.";
} }
bool_t value_t::boolean() const { bool_t value_t::boolean() const {
if (is_bool()) return val.bl; if (is_bool()) return val.bl;
else throw (std::string)"The value isn't a bool."; else throw (std::string)"The value isn't a bool.";
} }
value_t::value_t() { value_t::value_t() {
this->type = type_t::Null; this->type = type_t::Null;
} }
value_t::value_t(const array_t &val) { value_t::value_t(const array_t &val) {
this->type = type_t::Arr; this->type = type_t::Arr;
this->val.arr = new array_t(val); this->val.arr = new array_t(val);
} }
value_t::value_t(const map_t &val) { value_t::value_t(const map_t &val) {
this->type = type_t::Map; this->type = type_t::Map;
this->val.map = new map_t(val); this->val.map = new map_t(val);
} }
value_t::value_t(const string_t &val) { value_t::value_t(const string_t &val) {
this->type = type_t::Str; this->type = type_t::Str;
this->val.str = new string_t(val); this->val.str = new string_t(val);
} }
value_t::value_t(const char *val) { value_t::value_t(const char *val) {
this->type = type_t::Str; this->type = type_t::Str;
this->val.str = new string_t(val); this->val.str = new string_t(val);
} }
value_t::value_t(bool_t val) { value_t::value_t(bool_t val) {
this->type = type_t::Bool; this->type = type_t::Bool;
this->val.bl = val; this->val.bl = val;
} }
value_t::value_t(number_t val) { value_t::value_t(number_t val) {
this->type = type_t::Num; this->type = type_t::Num;
this->val.num = val; this->val.num = val;
} }
value_t::value_t(const value_t &other) { value_t::value_t(const value_t &other) {
type = other.type; type = other.type;
switch (other.type) { switch (other.type) {
case type_t::Map: case type_t::Map:
val.map = new map_t(*other.val.map); val.map = new map_t(*other.val.map);
break; break;
case type_t::Arr: case type_t::Arr:
val.arr = new array_t(*other.val.arr); val.arr = new array_t(*other.val.arr);
break; break;
case type_t::Str: case type_t::Str:
val.str = new string_t(*other.val.str); val.str = new string_t(*other.val.str);
break; break;
default: default:
val = other.val; val = other.val;
break; break;
} }
} }
value_t::~value_t() { value_t::~value_t() {
switch (type) { switch (type) {
case type_t::Map: case type_t::Map:
delete val.map; delete val.map;
break; break;
case type_t::Arr: case type_t::Arr:
delete val.arr; delete val.arr;
break; break;
case type_t::Str: case type_t::Str:
delete val.str; delete val.str;
break; break;
default: default:
break; break;
} }
} }
value_t::value_t(std::initializer_list<std::pair<std::string, value_t>> map): value_t::value_t(std::initializer_list<std::pair<std::string, value_t>> map):
value_t(map_t(map)) { } value_t(map_t(map)) { }
value_t &value_t::operator=(const value_t &other) { value_t &value_t::operator=(const value_t &other) {
type = other.type; this->~value_t();
switch (other.type) { type = other.type;
case type_t::Map: switch (other.type) {
val.map = new map_t(*other.val.map); case type_t::Map:
break; val.map = new map_t(*other.val.map);
case type_t::Arr: break;
val.arr = new array_t(*other.val.arr); case type_t::Arr:
break; val.arr = new array_t(*other.val.arr);
case type_t::Str: break;
val.str = new string_t(*other.val.str); case type_t::Str:
break; val.str = new string_t(*other.val.str);
default: break;
val = other.val; default:
break; val = other.val;
} break;
return *this; }
} return *this;
value_t &value_t::operator=(const char *other) { }
type = type_t::Str; value_t &value_t::operator=(const char *other) {
val.str = new string_t(other); type = type_t::Str;
return *this; val.str = new string_t(other);
} return *this;
}
}
}