-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObject.cpp
More file actions
227 lines (193 loc) · 5.65 KB
/
Object.cpp
File metadata and controls
227 lines (193 loc) · 5.65 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "Object.h"
#include "Value.h"
#include <string>
#include <iostream>
#include <assert.h>
void Object::swap(Object& other){
auto tmpCounter = other.refCounter;
other.refCounter = this->refCounter;
this->refCounter = tmpCounter;
auto tmpMapStr = other.stringToValue;
other.stringToValue = this->stringToValue;
this->stringToValue = tmpMapStr;
auto tmpMapDouble = other.doubleToValue;
other.doubleToValue = this->doubleToValue;
this->doubleToValue = tmpMapDouble;
}
void Object::clear(){
assert(refCounter == 0);
for(auto it : stringToValue){
delete it.second;
}
for(auto it : doubleToValue){
delete it.second;
}
}
Object::Object() {
refCounter = 0;
stringToValue = std::map<const std::string, Value*>();
doubleToValue = std::map<double, Value*>();
}
Object::Object(const Object& other) {
refCounter = 0;
for(auto it : other.stringToValue){
stringToValue[it.first] = it.second->clone();
}
for(auto it : other.doubleToValue){
doubleToValue[it.first] = it.second->clone();
}
}
Object::~Object() {
assert(refCounter == 0);
clear();
}
Object::Object(Object &&other){
refCounter = other.refCounter;
stringToValue = other.stringToValue;
doubleToValue = other.doubleToValue;
other.refCounter = 0;
other.stringToValue = std::map<const std::string, Value*>();
other.doubleToValue = std::map<double, Value*>();
}
Object& Object::operator=(const Object& other){
Object tmp(other);
swap(tmp); //copy and swap idiom
return *this;
}
Object& Object::operator=(Object&& other){
if(this->refCounter == 0)
clear();
refCounter = other.refCounter;
stringToValue = other.stringToValue;
doubleToValue = other.doubleToValue;
other.refCounter = 0;
other.stringToValue = std::map<const std::string, Value*>();
other.doubleToValue = std::map<double, Value*>();
return *this;
}
void Object::incRefCounter() {
refCounter++;
}
void Object::decRefCounter() {
assert(refCounter > 0);
refCounter--;
}
unsigned Object::getRefCounter() const {
return refCounter;
}
Value* Object::operator[](const std::string& key) const {
auto s2v = stringToValue.find(key);
if (s2v == stringToValue.end()) {
return nullptr;
}
return s2v->second;
}
Value* Object::operator[](double key) const {
auto d2v = doubleToValue.find(key);
if (d2v == doubleToValue.end()) {
return nullptr;
}
return d2v->second;
}
Value* Object::operator[](const Value* key) const {
if (key->isNumber())
return operator[](key->toNumber());
else if (key->isString())
return operator[](key->toString());
assert(false);
}
Value Object::getAndRemove(const std::string& key) {
auto tmp = stringToValue.find(key);
if(tmp == stringToValue.end())
return Value(Value::Type::NilType);
auto retVal = *tmp->second;
delete tmp->second;
stringToValue.erase(key);
return retVal;
}
Value Object::getAndRemove(double key) {
auto tmp = doubleToValue.find(key);
if(tmp == doubleToValue.end())
return Value(Value::Type::NilType);
auto retVal = *tmp->second;
delete tmp->second;
doubleToValue.erase(key);
return retVal;
}
void Object::set(const std::string& key, const Value& value) {
if(stringToValue.find(key) != stringToValue.end())
delete stringToValue[key];
stringToValue[key] = value.clone();
}
void Object::set(double key, const Value& value) {
if(doubleToValue.find(key) != doubleToValue.end())
delete doubleToValue[key];
doubleToValue[key] = value.clone();
}
void Object::set(const Value* key, const Value& value) {
if (key->isNumber())
set(key->toNumber(), value);
else if (key->isString())
set(key->toString(), value);
else
assert(false);
}
void Object::remove(const std::string& key) {
getAndRemove(key);
}
void Object::remove(double key) {
getAndRemove(key);
}
void Object::remove(const Value* key){
assert(key->isNumber() || key->isString());
(key->isString())?remove(std::string(key->toString())):remove(key->toNumber());
}
unsigned Object::getTotal() const {
return stringToValue.size() + doubleToValue.size();
}
void Object::apply(const Applier& f) {
for (std::pair<const std::string, Value*> e : stringToValue) {
f(Value((e.first).c_str()), e.second);
}
for (std::pair<double, Value*> e : doubleToValue) {
f(Value(e.first), e.second);
}
}
void Object::visit(const Visitor& f) const {
for (std::pair<const std::string, Value*> e : stringToValue) {
f(Value((e.first).c_str()), e.second);
}
for (std::pair<double, Value*> e : doubleToValue) {
f(Value(e.first), e.second);
}
}
std::string Object::toString() const {
std::string toReturn = "[";
std::string tmp;
bool flag = true;
bool isArray = true;
if (stringToValue.size() == 0) {
double counter = 0.0;
for (std::pair<double, Value*> e : doubleToValue) {
if (e.first != counter++)
isArray = false;
}
}
for (std::pair<const std::string, Value*> e : stringToValue) {
if (!flag)
toReturn += ", ";
toReturn += " {\"" + e.first + "\" : " + e.second->makeString() + "}";
flag = false;
}
for (std::pair<double, Value*> e : doubleToValue) {
if (!flag)
toReturn += ", ";
if (isArray)
toReturn += e.second->makeString();
else
toReturn += " {" + std::to_string(e.first) + " : " + e.second->makeString() + "}";
flag = false;
}
toReturn += " ]";
return toReturn;
}