ValueBase.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <memory>
  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 std::shared_ptr<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. template<typename InputIterator>
  74. String(InputIterator first, InputIterator last)
  75. : str_(first, last)
  76. {}
  77. String();
  78. ~String();
  79. // Don't allow copying
  80. String(const String&);
  81. String& operator=(const String&);
  82. const ValueType& s() const;
  83. // Returns std::string.data() casted to unsigned char*.
  84. // Use s().size() to get length.
  85. const unsigned char* uc() const;
  86. static std::shared_ptr<String> g(const ValueType& string);
  87. static std::shared_ptr<String> g(const unsigned char* data, size_t length);
  88. template<typename InputIterator>
  89. static std::shared_ptr<String> g(InputIterator first, InputIterator last)
  90. {
  91. return std::shared_ptr<String>(new String(first, last));
  92. }
  93. virtual void accept(ValueBaseVisitor& visitor) const;
  94. private:
  95. ValueType str_;
  96. };
  97. class Integer:public ValueBase {
  98. public:
  99. typedef int64_t ValueType;
  100. Integer(ValueType integer);
  101. Integer();
  102. ~Integer();
  103. // Don't allow copying
  104. Integer(const Integer&);
  105. Integer& operator=(const Integer&);
  106. // Returns Integer.
  107. ValueType i() const;
  108. static std::shared_ptr<Integer> g(ValueType integer);
  109. virtual void accept(ValueBaseVisitor& visitor) const;
  110. private:
  111. ValueType integer_;
  112. };
  113. class Bool:public ValueBase {
  114. public:
  115. static std::shared_ptr<Bool> gTrue();
  116. static std::shared_ptr<Bool> gFalse();
  117. bool val() const;
  118. virtual void accept(ValueBaseVisitor& visitor) const;
  119. private:
  120. Bool(bool val);
  121. // Don't allow copying
  122. Bool(const Bool&);
  123. Bool& operator=(const Bool&);
  124. bool val_;
  125. static const std::shared_ptr<Bool> trueValue_;
  126. static const std::shared_ptr<Bool> falseValue_;
  127. };
  128. class Null:public ValueBase {
  129. public:
  130. static std::shared_ptr<Null> g();
  131. virtual void accept(ValueBaseVisitor& visitor) const;
  132. private:
  133. Null();
  134. // Don't allow copying
  135. Null(const Null&);
  136. Null& operator=(const Null&);
  137. static const std::shared_ptr<Null> nullValue_;
  138. };
  139. class List:public ValueBase {
  140. public:
  141. typedef std::vector<std::shared_ptr<ValueBase> > ValueType;
  142. List();
  143. ~List();
  144. // Don't allow copying
  145. List(const List&);
  146. List& operator=(const List&);
  147. // Appends given v to list.
  148. void append(const std::shared_ptr<ValueBase>& v);
  149. // Appeding string is so common that we provide shortcut function.
  150. void append(const String::ValueType& string);
  151. // Alias for append()
  152. List& operator<<(const std::shared_ptr<ValueBase>& v);
  153. // Returns the object at given index.
  154. const std::shared_ptr<ValueBase>& get(size_t index) const;
  155. // Set the object at given index.
  156. void set(size_t index, const std::shared_ptr<ValueBase>& v);
  157. // Returns the const reference of the object at the given index.
  158. const std::shared_ptr<ValueBase>& operator[](size_t index) const;
  159. // Returns a read/write iterator that points to the first object in
  160. // list.
  161. ValueType::iterator begin();
  162. // Returns a read/write iterator that points to the one past the
  163. // last object in list.
  164. ValueType::iterator end();
  165. // Returns a read/write read-only iterator that points to the first
  166. // object in list.
  167. ValueType::const_iterator begin() const;
  168. // Returns a read/write read-only iterator that points to the one
  169. // past the last object in list.
  170. ValueType::const_iterator end() const;
  171. // Returns size of list.
  172. size_t size() const;
  173. // Returns true if size of list is 0.
  174. bool empty() const;
  175. static std::shared_ptr<List> g();
  176. virtual void accept(ValueBaseVisitor& visitor) const;
  177. private:
  178. ValueType list_;
  179. };
  180. class Dict:public ValueBase {
  181. public:
  182. typedef std::map<std::string, std::shared_ptr<ValueBase> > ValueType;
  183. Dict();
  184. ~Dict();
  185. // Don't allow copying
  186. Dict(const Dict&);
  187. Dict& operator=(const Dict&);
  188. void put(const std::string& key, const std::shared_ptr<ValueBase>& vlb);
  189. // Putting string is so common that we provide shortcut function.
  190. void put(const std::string& key, const String::ValueType& string);
  191. const std::shared_ptr<ValueBase>& get(const std::string& key) const;
  192. std::shared_ptr<ValueBase>& get(const std::string& key);
  193. // Returns the reference to object associated with given key. If
  194. // the key is not found, new pair with that key is created using
  195. // default values, which is then returned. In other words, this is
  196. // the same behavior of std::map's operator[].
  197. std::shared_ptr<ValueBase>& operator[](const std::string& key);
  198. // Returns the const reference to ojbect associated with given key.
  199. // If the key is not found, ValueBase::none is returned.
  200. const std::shared_ptr<ValueBase>& operator[](const std::string& key) const;
  201. // Returns true if the given key is found in dict.
  202. bool containsKey(const std::string& key) const;
  203. // Removes specified key from dict.
  204. void removeKey(const std::string& key);
  205. // Returns a read/write iterator that points to the first pair in
  206. // the dict.
  207. ValueType::iterator begin();
  208. // Returns a read/write read-only iterator that points to one past
  209. // the last pair in the dict.
  210. ValueType::iterator end();
  211. // Returns a read/write read-only iterator that points to the first
  212. // pair in the dict.
  213. ValueType::const_iterator begin() const;
  214. // Returns a read/write read-only iterator that points to one past
  215. // the last pair in the dict.
  216. ValueType::const_iterator end() const;
  217. // Returns size of Dict.
  218. size_t size() const;
  219. // Returns true if size of Dict is 0.
  220. bool empty() const;
  221. static std::shared_ptr<Dict> g();
  222. virtual void accept(ValueBaseVisitor& visitor) const;
  223. private:
  224. ValueType dict_;
  225. };
  226. class EmptyDowncastValueBaseVisitor:public ValueBaseVisitor {
  227. public:
  228. EmptyDowncastValueBaseVisitor() {}
  229. virtual void visit(const String& v) {}
  230. virtual void visit(const Integer& v) {}
  231. virtual void visit(const Bool& v) {}
  232. virtual void visit(const Null& v) {}
  233. virtual void visit(const List& v) {}
  234. virtual void visit(const Dict& v) {}
  235. };
  236. template<typename T>
  237. class DowncastValueBaseVisitor:public EmptyDowncastValueBaseVisitor {
  238. public:
  239. DowncastValueBaseVisitor():result_(0) {}
  240. virtual void visit(const T& t)
  241. {
  242. result_ = &t;
  243. }
  244. const T* getResult() const
  245. {
  246. return result_;
  247. }
  248. void setResult(const T* r)
  249. {
  250. result_ = r;
  251. }
  252. private:
  253. const T* result_;
  254. };
  255. template<typename T, typename VPtr>
  256. T* downcast(const VPtr& v)
  257. {
  258. if(v) {
  259. DowncastValueBaseVisitor<T> visitor;
  260. v->accept(visitor);
  261. return const_cast<T*>(visitor.getResult());
  262. } else {
  263. return 0;
  264. }
  265. }
  266. } // namespace aria2
  267. #endif // D_VALUE_BASE_H