summaryrefslogtreecommitdiff
path: root/src/ValueBase.cc
blob: ab388c360d09ea53a07c938e905c5b453d58deff (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2010 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "ValueBase.h"

namespace aria2 {

const SharedHandle<ValueBase> ValueBase::none;

String::String(const ValueType& string):str_(string) {}

String::String(const char* cstring):str_(cstring) {}

String::String(const char* data, size_t length):str_(&data[0], &data[length]) {}

String::String(const unsigned char* data, size_t length):
  str_(&data[0], &data[length]) {}

String::String() {}

String::~String() {}

const String::ValueType& String::s() const
{
  return str_;
}

const unsigned char* String::uc() const
{
  return reinterpret_cast<const unsigned char*>(str_.data());
}

SharedHandle<String> String::g(const ValueType& string)
{
  return SharedHandle<String>(new String(string));
}

SharedHandle<String> String::g(const unsigned char* data, size_t length)
{
  return SharedHandle<String>(new String(data, length));
}

void String::accept(ValueBaseVisitor& v) const
{
  v.visit(*this);
}

Integer::Integer(ValueType integer):integer_(integer) {}

Integer::Integer():integer_(0) {}

Integer::~Integer() {}

Integer::ValueType Integer::i() const
{
  return integer_;
}

SharedHandle<Integer> Integer::g(ValueType integer)
{
  return SharedHandle<Integer>(new Integer(integer));
}

void Integer::accept(ValueBaseVisitor& v) const
{
  v.visit(*this);
}

const SharedHandle<Bool> Bool::trueValue_(new Bool(true));
const SharedHandle<Bool> Bool::falseValue_(new Bool(false));

SharedHandle<Bool> Bool::gTrue()
{
  return trueValue_;
}

SharedHandle<Bool> Bool::gFalse()
{
  return falseValue_;
}

bool Bool::val() const
{
  return val_;
}

void Bool::accept(ValueBaseVisitor& v) const
{
  v.visit(*this);
}

Bool::Bool(bool val):val_(val) {}

const SharedHandle<Null> Null::nullValue_(new Null());

SharedHandle<Null> Null::g()
{
  return nullValue_;
}

void Null::accept(ValueBaseVisitor& v) const
{
  v.visit(*this);
}

Null::Null() {}

List::List() {}

List::~List() {}

const SharedHandle<ValueBase>& List::get(size_t index) const
{
  return list_[index];
}

void List::set(size_t index, const SharedHandle<ValueBase>& v)
{
  list_[index] = v;
}

void List::append(const SharedHandle<ValueBase>& v)
{
  list_.push_back(v);
}

void List::append(const String::ValueType& string)
{
  list_.push_back(String::g(string));
}

List& List::operator<<(const SharedHandle<ValueBase>& v)
{
  list_.push_back(v);
  return *this;
}

const SharedHandle<ValueBase>& List::operator[](size_t index) const
{
  return list_[index];
}

List::ValueType::iterator List::begin()
{
  return list_.begin();
}

List::ValueType::iterator List::end()
{
  return list_.end();
}

List::ValueType::const_iterator List::begin() const
{
  return list_.begin();
}

List::ValueType::const_iterator List::end() const
{
  return list_.end();
}

size_t List::size() const
{
  return list_.size();
}

bool List::empty() const
{
  return list_.empty();
}

SharedHandle<List> List::g()
{
  return SharedHandle<List>(new List());
}

void List::accept(ValueBaseVisitor& v) const
{
  v.visit(*this);
}

Dict::Dict() {}

Dict::~Dict() {}

void Dict::put(const std::string& key, const SharedHandle<ValueBase>& vlb)
{
  ValueType::value_type p = std::make_pair(key, vlb);
  std::pair<ValueType::iterator, bool> r = dict_.insert(p);
  if(!r.second) {
    (*r.first).second = vlb;
  }
}

void Dict::put(const std::string& key, const String::ValueType& string)
{
  put(key, String::g(string));
}

const SharedHandle<ValueBase>& Dict::get(const std::string& key) const
{
  ValueType::const_iterator itr = dict_.find(key);
  if(itr == dict_.end()) {
    return ValueBase::none;
  } else {
    return (*itr).second;
  }
}

SharedHandle<ValueBase>& Dict::operator[](const std::string& key)
{
  return dict_[key];
}

const SharedHandle<ValueBase>& Dict::operator[](const std::string& key) const
{
  return get(key);
}

bool Dict::containsKey(const std::string& key) const
{
  return dict_.count(key) == 1;
}

void Dict::removeKey(const std::string& key)
{
  dict_.erase(key);
}

Dict::ValueType::iterator Dict::begin()
{
  return dict_.begin();
}

Dict::ValueType::iterator Dict::end()
{
  return dict_.end();
}

Dict::ValueType::const_iterator Dict::begin() const
{
  return dict_.begin();
}

Dict::ValueType::const_iterator Dict::end() const
{
  return dict_.end();
}

size_t Dict::size() const
{
  return dict_.size();
}

bool Dict::empty() const
{
  return dict_.empty();
}

SharedHandle<Dict> Dict::g()
{
  return SharedHandle<Dict>(new Dict());
}
void Dict::accept(ValueBaseVisitor& v) const
{
  v.visit(*this);
}

const String* asString(const ValueBase* v)
{
  if(v) {
    return downcast<String>(v);
  } else {
    return 0;
  }
}

String* asString(ValueBase* v)
{
  if(v) {
    return const_cast<String*>(downcast<String>(v));
  } else {
    return 0;
  }
}

String* asString(const SharedHandle<ValueBase>& v)
{
  if(v.get()) {
    return const_cast<String*>(downcast<String>(v));
  } else {
    return 0;
  }
}

const Integer* asInteger(const ValueBase* v)
{
  if(v) {
    return downcast<Integer>(v);
  } else {
    return 0;
  }
}

Integer* asInteger(ValueBase* v)
{
  if(v) {
    return const_cast<Integer*>(downcast<Integer>(v));
  } else {
    return 0;
  }
}

Integer* asInteger(const SharedHandle<ValueBase>& v)
{
  if(v.get()) {
    return const_cast<Integer*>(downcast<Integer>(v));
  } else {
    return 0;
  }
}

const Bool* asBool(const ValueBase* v)
{
  if(v) {
    return downcast<Bool>(v);
  } else {
    return 0;
  }
}

Bool* asBool(const SharedHandle<ValueBase>& v)
{
  if(v.get()) {
    return const_cast<Bool*>(downcast<Bool>(v));
  } else {
    return 0;
  }
}

const Null* asNull(const ValueBase* v)
{
  if(v) {
    return downcast<Null>(v);
  } else {
    return 0;
  }
}

Null* asNull(const SharedHandle<ValueBase>& v)
{
  if(v) {
    return const_cast<Null*>(downcast<Null>(v));
  } else {
    return 0;
  }
}

const List* asList(const ValueBase* v)
{
  if(v) {
    return downcast<List>(v);
  } else {
    return 0;
  }
}

List* asList(ValueBase* v)
{
  if(v) {
    return const_cast<List*>(downcast<List>(v));
  } else {
    return 0;
  }
}

List* asList(const SharedHandle<ValueBase>& v)
{
  if(v.get()) {
    return const_cast<List*>(downcast<List>(v));
  } else {
    return 0;
  }
}

const Dict* asDict(const ValueBase* v)
{
  if(v) {
    return downcast<Dict>(v);
  } else {
    return 0;
  }
}

Dict* asDict(ValueBase* v)
{
  if(v) {
    return const_cast<Dict*>(downcast<Dict>(v));
  } else {
    return 0;
  }
}

Dict* asDict(const SharedHandle<ValueBase>& v)
{
  if(v.get()) {
    return const_cast<Dict*>(downcast<Dict>(v));
  } else {
    return 0;
  }
}

} // namespace aria2