Browse Source

Util: Add constructor Value(Type)

master
Riyyi 2 years ago
parent
commit
4a7ed92519
  1. 43
      src/util/json/value.cpp
  2. 2
      src/util/json/value.h

43
src/util/json/value.cpp

@ -35,6 +35,12 @@ Value& Value::operator=(const Value& other)
return *this;
}
Value::Value(Type type)
: m_type(type)
{
create();
}
Value::Value(bool value)
: m_type(Type::Bool)
{
@ -180,16 +186,43 @@ const Value& Value::operator[](const std::string& key) const
// ------------------------------------------
void Value::create()
{
switch (m_type) {
case Type::Bool:
m_value.asBool = false;
break;
case Type::Number:
m_value.asDouble = 0.0;
break;
case Type::String:
m_value.asString = new std::string;
break;
case Type::Array:
m_value.asArray = new Array;
break;
case Type::Object:
m_value.asObject = new Object;
break;
default:
break;
}
}
void Value::clear()
{
if (m_type == Type::String) {
switch (m_type) {
case Type::String:
delete m_value.asString;
}
if (m_type == Type::Array) {
break;
case Type::Array:
delete m_value.asArray;
}
if (m_type == Type::Object) {
break;
case Type::Object:
delete m_value.asObject;
break;
default:
break;
}
}

2
src/util/json/value.h

@ -37,6 +37,7 @@ public:
// Assignment operator
Value& operator=(const Value& other);
Value(Type type);
Value(bool value);
Value(int value);
Value(double value);
@ -73,6 +74,7 @@ public:
const Object& asObject() const { return *m_value.asObject; }
private:
void create();
void clear();
void copyFrom(const Value& other);

Loading…
Cancel
Save