- removed pointers to POD inside the union

This commit is contained in:
Niels 2013-07-10 14:57:51 +02:00
parent 4e6a400a23
commit 52c2cb8ce7
2 changed files with 41 additions and 45 deletions

View file

@ -51,15 +51,15 @@ JSON::JSON(json_t type) : _type(type) {
break; break;
} }
case (boolean): { case (boolean): {
_value.boolean = new boolean_t(); _value.boolean = false;
break; break;
} }
case (number): { case (number): {
_value.number = new number_t(); _value.number = 0;
break; break;
} }
case (number_float): { case (number_float): {
_value.number_float = new number_float_t(); _value.number_float = 0.0;
break; break;
} }
case (null): { case (null): {
@ -71,9 +71,9 @@ JSON::JSON(json_t type) : _type(type) {
JSON::JSON(const std::string& s) : _type(string), _value(new string_t(s)) {} JSON::JSON(const std::string& s) : _type(string), _value(new string_t(s)) {}
JSON::JSON(const char* s) : _type(string), _value(new string_t(s)) {} JSON::JSON(const char* s) : _type(string), _value(new string_t(s)) {}
JSON::JSON(char* s) : _type(string), _value(new string_t(s)) {} JSON::JSON(char* s) : _type(string), _value(new string_t(s)) {}
JSON::JSON(const bool b) : _type(boolean), _value(new boolean_t(b)) {} JSON::JSON(const bool b) : _type(boolean), _value(b) {}
JSON::JSON(const int i) : _type(number), _value(new number_t(i)) {} JSON::JSON(const int i) : _type(number), _value(i) {}
JSON::JSON(const double f) : _type(number_float), _value(new number_float_t(f)) {} JSON::JSON(const double f) : _type(number_float), _value(f) {}
JSON::JSON(array_t a) : _type(array), _value(new array_t(a)) {} JSON::JSON(array_t a) : _type(array), _value(new array_t(a)) {}
JSON::JSON(object_t o) : _type(object), _value(new object_t(o)) {} JSON::JSON(object_t o) : _type(object), _value(new object_t(o)) {}
@ -97,15 +97,15 @@ JSON::JSON(const JSON& o) : _type(o._type) {
break; break;
} }
case (boolean): { case (boolean): {
_value.boolean = new boolean_t(*o._value.boolean); _value.boolean = o._value.boolean;
break; break;
} }
case (number): { case (number): {
_value.number = new number_t(*o._value.number); _value.number = o._value.number;
break; break;
} }
case (number_float): { case (number_float): {
_value.number_float = new number_float_t(*o._value.number_float); _value.number_float = o._value.number_float;
break; break;
} }
case (null): { case (null): {
@ -147,15 +147,12 @@ JSON& JSON::operator=(const JSON& o) {
break; break;
} }
case (boolean): { case (boolean): {
delete _value.boolean;
break; break;
} }
case (number): { case (number): {
delete _value.number;
break; break;
} }
case (number_float): { case (number_float): {
delete _value.number_float;
break; break;
} }
case (null): { case (null): {
@ -178,15 +175,15 @@ JSON& JSON::operator=(const JSON& o) {
break; break;
} }
case (boolean): { case (boolean): {
_value.boolean = new boolean_t(*o._value.boolean); _value.boolean = o._value.boolean;
break; break;
} }
case (number): { case (number): {
_value.number = new number_t(*o._value.number); _value.number = o._value.number;
break; break;
} }
case (number_float): { case (number_float): {
_value.number_float = new number_float_t(*o._value.number_float); _value.number_float = o._value.number_float;
break; break;
} }
case (null): { case (null): {
@ -214,15 +211,12 @@ JSON::~JSON() {
break; break;
} }
case (boolean): { case (boolean): {
delete _value.boolean;
break; break;
} }
case (number): { case (number): {
delete _value.number;
break; break;
} }
case (number_float): { case (number_float): {
delete _value.number_float;
break; break;
} }
case (null): { case (null): {
@ -249,9 +243,9 @@ JSON::operator const std::string() const {
JSON::operator int() const { JSON::operator int() const {
switch (_type) { switch (_type) {
case (number): case (number):
return *_value.number; return _value.number;
case (number_float): case (number_float):
return static_cast<number_t>(*_value.number_float); return static_cast<number_t>(_value.number_float);
default: default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON number"); throw std::runtime_error("cannot cast " + _typename() + " to JSON number");
} }
@ -260,9 +254,9 @@ JSON::operator int() const {
JSON::operator double() const { JSON::operator double() const {
switch (_type) { switch (_type) {
case (number): case (number):
return static_cast<number_float_t>(*_value.number); return static_cast<number_float_t>(_value.number);
case (number_float): case (number_float):
return *_value.number_float; return _value.number_float;
default: default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON number"); throw std::runtime_error("cannot cast " + _typename() + " to JSON number");
} }
@ -271,7 +265,7 @@ JSON::operator double() const {
JSON::operator bool() const { JSON::operator bool() const {
switch (_type) { switch (_type) {
case (boolean): case (boolean):
return *_value.boolean; return _value.boolean;
default: default:
throw std::runtime_error("cannot cast " + _typename() + " to JSON Boolean"); throw std::runtime_error("cannot cast " + _typename() + " to JSON Boolean");
} }
@ -288,15 +282,15 @@ const std::string JSON::toString() const {
} }
case (boolean): { case (boolean): {
return *_value.boolean ? "true" : "false"; return _value.boolean ? "true" : "false";
} }
case (number): { case (number): {
return to_string(*_value.number); return to_string(_value.number);
} }
case (number_float): { case (number_float): {
return to_string(*_value.number_float); return to_string(_value.number_float);
} }
case (array): { case (array): {
@ -606,21 +600,23 @@ bool JSON::operator==(const JSON& o) const {
} }
case (boolean): { case (boolean): {
if (o._type == boolean) { if (o._type == boolean) {
return *_value.boolean == *o._value.boolean; return _value.boolean == o._value.boolean;
} }
} }
case (number): { case (number): {
if (o._type == number or o._type == number_float) { if (o._type == number) {
number_t a = *this; return _value.number == o._value.number;
number_t b = o; }
return a == b; if (o._type == number_float) {
return _value.number == static_cast<number_t>(o._value.number_float);
} }
} }
case (number_float): { case (number_float): {
if (o._type == number or o._type == number_float) { if (o._type == number) {
number_float_t a = *this; return _value.number_float == static_cast<number_float_t>(o._value.number);
number_float_t b = o; }
return a == b; if (o._type == number_float) {
return _value.number_float == o._value.number_float;
} }
} }
} }
@ -854,14 +850,14 @@ void JSON::parser::parse(JSON& result) {
case ('t'): { case ('t'): {
parseTrue(); parseTrue();
result._type = boolean; result._type = boolean;
result._value.boolean = new boolean_t(true); result._value.boolean = true;
break; break;
} }
case ('f'): { case ('f'): {
parseFalse(); parseFalse();
result._type = boolean; result._type = boolean;
result._value.boolean = new boolean_t(false); result._value.boolean = false;
break; break;
} }
@ -883,11 +879,11 @@ void JSON::parser::parse(JSON& result) {
if (tmp.find(".") == std::string::npos) { if (tmp.find(".") == std::string::npos) {
// integer (we use atof, because it can cope with e) // integer (we use atof, because it can cope with e)
result._type = number; result._type = number;
result._value.number = new number_t(std::atof(tmp.c_str())); result._value.number = std::atof(tmp.c_str());
} else { } else {
// float // float
result._type = number_float; result._type = number_float;
result._value.number_float = new number_float_t(std::atof(tmp.c_str())); result._value.number_float = std::atof(tmp.c_str());
} }
break; break;
} else { } else {

View file

@ -58,17 +58,17 @@ class JSON {
array_t* array; array_t* array;
object_t* object; object_t* object;
string_t* string; string_t* string;
boolean_t* boolean; boolean_t boolean;
number_t* number; number_t number;
number_float_t* number_float; number_float_t number_float;
value() {} value() {}
value(array_t* array): array(array) {} value(array_t* array): array(array) {}
value(object_t* object): object(object) {} value(object_t* object): object(object) {}
value(string_t* string): string(string) {} value(string_t* string): string(string) {}
value(boolean_t* boolean) : boolean(boolean) {} value(boolean_t boolean) : boolean(boolean) {}
value(number_t* number) : number(number) {} value(number_t number) : number(number) {}
value(number_float_t* number_float) : number_float(number_float) {} value(number_float_t number_float) : number_float(number_float) {}
}; };
private: private: