summaryrefslogtreecommitdiff
path: root/src/ValueBase.h
blob: a262b853ae0df9984050d17e70b0a90dd03a491b (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
/* <!-- 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 --> */
#ifndef D_VALUE_BASE_H
#define D_VALUE_BASE_H

#include "common.h"

#include <string>
#include <vector>
#include <map>

#include "SharedHandle.h"

namespace aria2 {

class ValueBaseVisitor;

class ValueBase {
public:
  virtual ~ValueBase() {}

  virtual void accept(ValueBaseVisitor& visitor) const = 0;

  static const SharedHandle<ValueBase> none;
};

class String;
class Integer;
class List;
class Dict;

class ValueBaseVisitor {
public:
  virtual ~ValueBaseVisitor() {}

  virtual void visit(const String& string) = 0;

  virtual void visit(const Integer& integer) = 0;

  virtual void visit(const List& list) = 0;

  virtual void visit(const Dict& dict) = 0;
};

class String:public ValueBase {
public:
  typedef std::string ValueType;

  String(const ValueType& string);

  explicit String(const char* cstring);

  String(const char* data, size_t length);

  String(const unsigned char* data, size_t length);

  String();

  ~String();

  // Don't allow copying
  String(const String&);
  String& operator=(const String&);

  const ValueType& s() const;

  // Returns std::string.data() casted to unsigned char*.
  // Use s().size() to get length.
  const unsigned char* uc() const;

  static SharedHandle<String> g(const ValueType& string);

  static SharedHandle<String> g(const unsigned char* data, size_t length);

  virtual void accept(ValueBaseVisitor& visitor) const;
private:
  ValueType str_;
};

class Integer:public ValueBase {
public:
  typedef int64_t ValueType;

  Integer(ValueType integer);

  Integer();

  ~Integer();

  // Don't allow copying
  Integer(const Integer&);
  Integer& operator=(const Integer&);

  // Returns Integer.
  ValueType i() const;

  static SharedHandle<Integer> g(ValueType integer);

  virtual void accept(ValueBaseVisitor& visitor) const;
private:
  ValueType integer_;
};

class List:public ValueBase {
public:
  typedef std::vector<SharedHandle<ValueBase> > ValueType;

  List();

  ~List();

  // Don't allow copying
  List(const List&);
  List& operator=(const List&);

  // Appends given v to list.
  void append(const SharedHandle<ValueBase>& v);

  // Appeding string is so common that we provide shortcut function.
  void append(const String::ValueType& string);

  // Alias for append()
  List& operator<<(const SharedHandle<ValueBase>& v);

  // Returns the object at given index.
  const SharedHandle<ValueBase>& get(size_t index) const;

  // Set the object at given index.
  void set(size_t index, const SharedHandle<ValueBase>& v);

  // Returns the const reference of the object at the given index.
  const SharedHandle<ValueBase>& operator[](size_t index) const;

  // Returns a read/write iterator that points to the first object in
  // list.
  ValueType::iterator begin();

  // Returns a read/write iterator that points to the one past the
  // last object in list.
  ValueType::iterator end();

  // Returns a read/write read-only iterator that points to the first
  // object in list.
  ValueType::const_iterator begin() const;

  // Returns a read/write read-only iterator that points to the one
  // past the last object in list.
  ValueType::const_iterator end() const;

  // Returns size of list.
  size_t size() const;

  // Returns true if size of list is 0.
  bool empty() const;

  static SharedHandle<List> g();

  virtual void accept(ValueBaseVisitor& visitor) const;
private:
  ValueType list_;
};

class Dict:public ValueBase {
public:
  typedef std::map<std::string, SharedHandle<ValueBase> > ValueType;

  Dict();

  ~Dict();

  // Don't allow copying
  Dict(const Dict&);
  Dict& operator=(const Dict&);

  void put(const std::string& key, const SharedHandle<ValueBase>& vlb);

  // Putting string is so common that we provide shortcut function.
  void put(const std::string& key, const String::ValueType& string);

  const SharedHandle<ValueBase>& get(const std::string& key) const;

  // Returns the reference to object associated with given key.  If
  // the key is not found, new pair with that key is created using
  // default values, which is then returned. In other words, this is
  // the same behavior of std::map's operator[].
  SharedHandle<ValueBase>& operator[](const std::string& key);

  // Returns the const reference to ojbect associated with given key.
  // If the key is not found, ValueBase::none is returned.
  const SharedHandle<ValueBase>& operator[](const std::string& key) const;

  // Returns true if the given key is found in dict.
  bool containsKey(const std::string& key) const;

  // Removes specified key from dict.
  void removeKey(const std::string& key);

  // Returns a read/write iterator that points to the first pair in
  // the dict.
  ValueType::iterator begin();

  // Returns a read/write read-only iterator that points to one past
  // the last pair in the dict.
  ValueType::iterator end();

  // Returns a read/write read-only iterator that points to the first
  // pair in the dict.
  ValueType::const_iterator begin() const;

  // Returns a read/write read-only iterator that points to one past
  // the last pair in the dict.
  ValueType::const_iterator end() const;

  // Returns size of Dict.
  size_t size() const;

  // Returns true if size of Dict is 0.
  bool empty() const;

  static SharedHandle<Dict> g();

  virtual void accept(ValueBaseVisitor& visitor) const;
private:
  ValueType dict_;
};

template<typename T, typename T1, typename T2, typename T3>
class DowncastValueBaseVisitor:public ValueBaseVisitor {
private:
  const T* result_;
public:
  DowncastValueBaseVisitor():result_(0) {}

  virtual void visit(const T& t)
  {
    result_ = &t;
  }

  virtual void visit(const T1& t1) {}
  virtual void visit(const T2& t2) {}
  virtual void visit(const T3& t3) {}

  const T* getResult() const
  {
    return result_;
  }

  void setResult(const T* r)
  {
    result_ = r;
  }
};

template<typename T, typename T1, typename T2, typename T3, typename VPtr>
const T* downcast(const VPtr& v)
{
  DowncastValueBaseVisitor<T, T1, T2, T3> visitor;
  v->accept(visitor);
  return visitor.getResult();
}

const String* asString(const ValueBase* v);

String* asString(ValueBase* v);

String* asString(const SharedHandle<ValueBase>& v);

const Integer* asInteger(const ValueBase* v);

Integer* asInteger(ValueBase* v);

Integer* asInteger(const SharedHandle<ValueBase>& v);

const List* asList(const ValueBase* v);

List* asList(ValueBase* v);

List* asList(const SharedHandle<ValueBase>& v);

const Dict* asDict(const ValueBase* v);

Dict* asDict(ValueBase* v);

Dict* asDict(const SharedHandle<ValueBase>& v);

} // namespace aria2

#endif // D_VALUE_BASE_H