Browse Source

Util: Forward declare Json::Value in Array and Object class

master
Riyyi 2 years ago
parent
commit
d9cfd3f7c1
  1. 26
      src/util/json/array.cpp
  2. 17
      src/util/json/array.h
  3. 26
      src/util/json/object.cpp
  4. 17
      src/util/json/object.h

26
src/util/json/array.cpp

@ -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

17
src/util/json/array.h

@ -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); }

26
src/util/json/object.cpp

@ -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

17
src/util/json/object.h

@ -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); }

Loading…
Cancel
Save