Browse Source

Util: Add constructor Value(Type)

master
Riyyi 3 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; return *this;
} }
Value::Value(Type type)
: m_type(type)
{
create();
}
Value::Value(bool value) Value::Value(bool value)
: m_type(Type::Bool) : 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() void Value::clear()
{ {
if (m_type == Type::String) { switch (m_type) {
case Type::String:
delete m_value.asString; delete m_value.asString;
} break;
if (m_type == Type::Array) { case Type::Array:
delete m_value.asArray; delete m_value.asArray;
} break;
if (m_type == Type::Object) { case Type::Object:
delete m_value.asObject; delete m_value.asObject;
break;
default:
break;
} }
} }

2
src/util/json/value.h

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

Loading…
Cancel
Save