Util: Forward declare Json::Value in Array and Object class
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Riyyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "util/json/array.h"
|
||||||
|
#include "util/json/value.h"
|
||||||
|
|
||||||
|
namespace Json {
|
||||||
|
|
||||||
|
void Array::emplace_back(Value value)
|
||||||
|
{
|
||||||
|
m_values.emplace_back(std::move(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value& Array::at(size_t index)
|
||||||
|
{
|
||||||
|
if (index + 1 > m_values.size()) {
|
||||||
|
m_values.resize(index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_values.at(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Json
|
||||||
+4
-13
@@ -12,10 +12,11 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "util/json/parser.h"
|
#include "util/json/parser.h"
|
||||||
#include "util/json/value.h"
|
|
||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
|
class Value;
|
||||||
|
|
||||||
class Array {
|
class Array {
|
||||||
public:
|
public:
|
||||||
Array() {}
|
Array() {}
|
||||||
@@ -30,19 +31,9 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void emplace_back(Value value)
|
void emplace_back(Value value);
|
||||||
{
|
|
||||||
m_values.emplace_back(std::move(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
Value& at(size_t index)
|
Value& at(size_t index);
|
||||||
{
|
|
||||||
if (index + 1 > m_values.size()) {
|
|
||||||
m_values.resize(index + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_values.at(index);
|
|
||||||
}
|
|
||||||
Value& operator[](size_t index) { return at(index); }
|
Value& operator[](size_t index) { return at(index); }
|
||||||
const Value& at(size_t index) const { return m_values.at(index); }
|
const Value& at(size_t index) const { return m_values.at(index); }
|
||||||
const Value& operator[](size_t index) const { return m_values.at(index); }
|
const Value& operator[](size_t index) const { return m_values.at(index); }
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Riyyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "util/json/object.h"
|
||||||
|
#include "util/json/value.h"
|
||||||
|
|
||||||
|
namespace Json {
|
||||||
|
|
||||||
|
void Object::emplace(const std::string& name, Value value)
|
||||||
|
{
|
||||||
|
m_members.emplace(name, std::move(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value& Object::at(const std::string& name)
|
||||||
|
{
|
||||||
|
if (m_members.find(name) == m_members.end()) {
|
||||||
|
emplace(name, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_members.at(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Json
|
||||||
+4
-13
@@ -12,10 +12,11 @@
|
|||||||
#include <utility> // move
|
#include <utility> // move
|
||||||
|
|
||||||
#include "util/json/parser.h"
|
#include "util/json/parser.h"
|
||||||
#include "util/json/value.h"
|
|
||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
|
class Value;
|
||||||
|
|
||||||
class Object {
|
class Object {
|
||||||
public:
|
public:
|
||||||
Object() {}
|
Object() {}
|
||||||
@@ -26,19 +27,9 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void emplace(const std::string& name, Value value)
|
void emplace(const std::string& name, Value value);
|
||||||
{
|
|
||||||
m_members.emplace(name, std::move(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
Value& at(const std::string& name)
|
Value& at(const std::string& name);
|
||||||
{
|
|
||||||
if (m_members.find(name) == m_members.end()) {
|
|
||||||
emplace(name, {});
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_members.at(name);
|
|
||||||
}
|
|
||||||
Value& operator[](const std::string& name) { return at(name); }
|
Value& operator[](const std::string& name) { return at(name); }
|
||||||
const Value& at(const std::string& name) const { return m_members.at(name); }
|
const Value& at(const std::string& name) const { return m_members.at(name); }
|
||||||
const Value& operator[](const std::string& name) const { return m_members.at(name); }
|
const Value& operator[](const std::string& name) const { return m_members.at(name); }
|
||||||
|
|||||||
Reference in New Issue
Block a user