ValueBase.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_VALUE_BASE_H
  36. #define D_VALUE_BASE_H
  37. #include "common.h"
  38. #include <string>
  39. #include <vector>
  40. #include <map>
  41. #include "SharedHandle.h"
  42. namespace aria2 {
  43. class ValueBaseVisitor;
  44. class ValueBase {
  45. public:
  46. virtual ~ValueBase() {}
  47. virtual void accept(ValueBaseVisitor& visitor) const = 0;
  48. static const SharedHandle<ValueBase> none;
  49. };
  50. class String;
  51. class Integer;
  52. class Bool;
  53. class Null;
  54. class List;
  55. class Dict;
  56. class ValueBaseVisitor {
  57. public:
  58. virtual ~ValueBaseVisitor() {}
  59. virtual void visit(const String& string) = 0;
  60. virtual void visit(const Integer& integer) = 0;
  61. virtual void visit(const Bool& boolValue) = 0;
  62. virtual void visit(const Null& nullValue) = 0;
  63. virtual void visit(const List& list) = 0;
  64. virtual void visit(const Dict& dict) = 0;
  65. };
  66. class String:public ValueBase {
  67. public:
  68. typedef std::string ValueType;
  69. String(const ValueType& string);
  70. explicit String(const char* cstring);
  71. String(const char* data, size_t length);
  72. String(const unsigned char* data, size_t length);
  73. String();
  74. ~String();
  75. // Don't allow copying
  76. String(const String&);
  77. String& operator=(const String&);
  78. const ValueType& s() const;
  79. // Returns std::string.data() casted to unsigned char*.
  80. // Use s().size() to get length.
  81. const unsigned char* uc() const;
  82. static SharedHandle<String> g(const ValueType& string);
  83. static SharedHandle<String> g(const unsigned char* data, size_t length);
  84. virtual void accept(ValueBaseVisitor& visitor) const;
  85. private:
  86. ValueType str_;
  87. };
  88. class Integer:public ValueBase {
  89. public:
  90. typedef int64_t ValueType;
  91. Integer(ValueType integer);
  92. Integer();
  93. ~Integer();
  94. // Don't allow copying
  95. Integer(const Integer&);
  96. Integer& operator=(const Integer&);
  97. // Returns Integer.
  98. ValueType i() const;
  99. static SharedHandle<Integer> g(ValueType integer);
  100. virtual void accept(ValueBaseVisitor& visitor) const;
  101. private:
  102. ValueType integer_;
  103. };
  104. class Bool:public ValueBase {
  105. public:
  106. static SharedHandle<Bool> gTrue();
  107. static SharedHandle<Bool> gFalse();
  108. bool val() const;
  109. virtual void accept(ValueBaseVisitor& visitor) const;
  110. private:
  111. Bool(bool val);
  112. // Don't allow copying
  113. Bool(const Bool&);
  114. Bool& operator=(const Bool&);
  115. bool val_;
  116. static const SharedHandle<Bool> trueValue_;
  117. static const SharedHandle<Bool> falseValue_;
  118. };
  119. class Null:public ValueBase {
  120. public:
  121. static SharedHandle<Null> g();
  122. virtual void accept(ValueBaseVisitor& visitor) const;
  123. private:
  124. Null();
  125. // Don't allow copying
  126. Null(const Null&);
  127. Null& operator=(const Null&);
  128. static const SharedHandle<Null> nullValue_;
  129. };
  130. class List:public ValueBase {
  131. public:
  132. typedef std::vector<SharedHandle<ValueBase> > ValueType;
  133. List();
  134. ~List();
  135. // Don't allow copying
  136. List(const List&);
  137. List& operator=(const List&);
  138. // Appends given v to list.
  139. void append(const SharedHandle<ValueBase>& v);
  140. // Appeding string is so common that we provide shortcut function.
  141. void append(const String::ValueType& string);
  142. // Alias for append()
  143. List& operator<<(const SharedHandle<ValueBase>& v);
  144. // Returns the object at given index.
  145. const SharedHandle<ValueBase>& get(size_t index) const;
  146. // Set the object at given index.
  147. void set(size_t index, const SharedHandle<ValueBase>& v);
  148. // Returns the const reference of the object at the given index.
  149. const SharedHandle<ValueBase>& operator[](size_t index) const;
  150. // Returns a read/write iterator that points to the first object in
  151. // list.
  152. ValueType::iterator begin();
  153. // Returns a read/write iterator that points to the one past the
  154. // last object in list.
  155. ValueType::iterator end();
  156. // Returns a read/write read-only iterator that points to the first
  157. // object in list.
  158. ValueType::const_iterator begin() const;
  159. // Returns a read/write read-only iterator that points to the one
  160. // past the last object in list.
  161. ValueType::const_iterator end() const;
  162. // Returns size of list.
  163. size_t size() const;
  164. // Returns true if size of list is 0.
  165. bool empty() const;
  166. static SharedHandle<List> g();
  167. virtual void accept(ValueBaseVisitor& visitor) const;
  168. private:
  169. ValueType list_;
  170. };
  171. class Dict:public ValueBase {
  172. public:
  173. typedef std::map<std::string, SharedHandle<ValueBase> > ValueType;
  174. Dict();
  175. ~Dict();
  176. // Don't allow copying
  177. Dict(const Dict&);
  178. Dict& operator=(const Dict&);
  179. void put(const std::string& key, const SharedHandle<ValueBase>& vlb);
  180. // Putting string is so common that we provide shortcut function.
  181. void put(const std::string& key, const String::ValueType& string);
  182. const SharedHandle<ValueBase>& get(const std::string& key) const;
  183. // Returns the reference to object associated with given key. If
  184. // the key is not found, new pair with that key is created using
  185. // default values, which is then returned. In other words, this is
  186. // the same behavior of std::map's operator[].
  187. SharedHandle<ValueBase>& operator[](const std::string& key);
  188. // Returns the const reference to ojbect associated with given key.
  189. // If the key is not found, ValueBase::none is returned.
  190. const SharedHandle<ValueBase>& operator[](const std::string& key) const;
  191. // Returns true if the given key is found in dict.
  192. bool containsKey(const std::string& key) const;
  193. // Removes specified key from dict.
  194. void removeKey(const std::string& key);
  195. // Returns a read/write iterator that points to the first pair in
  196. // the dict.
  197. ValueType::iterator begin();
  198. // Returns a read/write read-only iterator that points to one past
  199. // the last pair in the dict.
  200. ValueType::iterator end();
  201. // Returns a read/write read-only iterator that points to the first
  202. // pair in the dict.
  203. ValueType::const_iterator begin() const;
  204. // Returns a read/write read-only iterator that points to one past
  205. // the last pair in the dict.
  206. ValueType::const_iterator end() const;
  207. // Returns size of Dict.
  208. size_t size() const;
  209. // Returns true if size of Dict is 0.
  210. bool empty() const;
  211. static SharedHandle<Dict> g();
  212. virtual void accept(ValueBaseVisitor& visitor) const;
  213. private:
  214. ValueType dict_;
  215. };
  216. class EmptyDowncastValueBaseVisitor:public ValueBaseVisitor {
  217. public:
  218. EmptyDowncastValueBaseVisitor() {}
  219. virtual void visit(const String& v) {}
  220. virtual void visit(const Integer& v) {}
  221. virtual void visit(const Bool& v) {}
  222. virtual void visit(const Null& v) {}
  223. virtual void visit(const List& v) {}
  224. virtual void visit(const Dict& v) {}
  225. };
  226. template<typename T>
  227. class DowncastValueBaseVisitor:public EmptyDowncastValueBaseVisitor {
  228. public:
  229. DowncastValueBaseVisitor():result_(0) {}
  230. virtual void visit(const T& t)
  231. {
  232. result_ = &t;
  233. }
  234. const T* getResult() const
  235. {
  236. return result_;
  237. }
  238. void setResult(const T* r)
  239. {
  240. result_ = r;
  241. }
  242. private:
  243. const T* result_;
  244. };
  245. template<typename T, typename VPtr>
  246. const T* downcast(const VPtr& v)
  247. {
  248. DowncastValueBaseVisitor<T> visitor;
  249. v->accept(visitor);
  250. return visitor.getResult();
  251. }
  252. const String* asString(const ValueBase* v);
  253. String* asString(ValueBase* v);
  254. String* asString(const SharedHandle<ValueBase>& v);
  255. const Integer* asInteger(const ValueBase* v);
  256. Integer* asInteger(ValueBase* v);
  257. Integer* asInteger(const SharedHandle<ValueBase>& v);
  258. const Bool* asBool(const ValueBase* v);
  259. Bool* asBool(const SharedHandle<ValueBase>& v);
  260. const Null* asNull(const ValueBase* v);
  261. Null* asNull(const SharedHandle<ValueBase>& v);
  262. const List* asList(const ValueBase* v);
  263. List* asList(ValueBase* v);
  264. List* asList(const SharedHandle<ValueBase>& v);
  265. const Dict* asDict(const ValueBase* v);
  266. Dict* asDict(ValueBase* v);
  267. Dict* asDict(const SharedHandle<ValueBase>& v);
  268. } // namespace aria2
  269. #endif // D_VALUE_BASE_H