ValueBase.h 9.4 KB

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