-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.cpp
132 lines (106 loc) · 2.57 KB
/
array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "array.h"
namespace jsonlite
{
Array::Array() : values()
{
}
Array::Array(const Value& value) {
internalAdd(value);
}
Array::Array(const Array& other) {
internalAdd(other);
}
Array::~Array() { clear(); }
size_t Array::size() const { return values.size(); }
bool Array::empty() const { return size() == 0; }
void Array::clear() {
container::iterator it = values.begin();
for(; it != values.end(); ++it)
delete *it;
values.clear();
}
bool Array::parse(std::istream& input) {
return parse(input, *this);
}
bool Array::parse(const std::string& input) {
std::istringstream iss(input);
return parse(iss, *this);
}
void Array::append(const Array& other) {
if(this != &other) {
values.push_back(new Value(other));
}
else {
internalAdd(Array(*this));
}
}
void Array::append(const Value& value) {
internalAdd(value);
}
bool Array::parse(std::istream& input, Array& array) {
if(!helper::match(input, "[")) {
return false;
}
if(helper::match(input, "]")) {
return true;
}
do {
Value* v = new Value;
if(!v->parse(input)) {
delete v;
break;
}
array.values.push_back(v);
} while(helper::match(input, ","));
return helper::match(input, "]");
}
void Array::internalAdd(const Array& other) {
if(this != &other) {
container::const_iterator it = other.values.begin();
for(; it != other.values.end(); ++it) {
values.push_back(new Value(**it));
}
}
else {
internalAdd(Array(*this));
}
}
void Array::internalAdd(const Value& value) {
values.push_back(new Value(value));
}
Array::constIterator Array::begin() const { return values.begin(); }
Array::constIterator Array::end() const { return values.end(); }
Array& Array::operator <<(const Array& other) {
internalAdd(other);
return *this;
}
Array& Array::operator <<(const Value& value) {
internalAdd(value);
return *this;
}
Array& Array::operator =(const Array& other) {
if(this != &other) {
clear();
internalAdd(other);
}
return *this;
}
Array& Array::operator =(const Value& value) {
clear();
internalAdd(value);
return *this;
}
std::ostream& operator <<(std::ostream& os, const Array& array) {
using namespace jsonlite;
using namespace helper;
os << "[";
Array::constIterator beg = array.begin(), end = array.end();
while(beg != end) {
os << *(*beg);
++beg;
if(beg != end)
os << ", ";
}
return os << "]";
}
}