ValueBase.h 9.3 KB

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