ValueBase.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 List;
  53. class Dict;
  54. class ValueBaseVisitor {
  55. public:
  56. virtual ~ValueBaseVisitor() {}
  57. virtual void visit(const String& string) = 0;
  58. virtual void visit(const Integer& integer) = 0;
  59. virtual void visit(const List& list) = 0;
  60. virtual void visit(const Dict& dict) = 0;
  61. };
  62. class String:public ValueBase {
  63. public:
  64. typedef std::string ValueType;
  65. String(const ValueType& string);
  66. explicit String(const char* cstring);
  67. String(const char* data, size_t length);
  68. String(const unsigned char* data, size_t length);
  69. String();
  70. const ValueType& s() const;
  71. // Returns std::string.data() casted to unsigned char*.
  72. // Use s().size() to get length.
  73. const unsigned char* uc() const;
  74. static SharedHandle<String> g(const ValueType& string);
  75. static SharedHandle<String> g(const unsigned char* data, size_t length);
  76. virtual void accept(ValueBaseVisitor& visitor) const;
  77. private:
  78. ValueType str_;
  79. };
  80. class Integer:public ValueBase {
  81. public:
  82. typedef int64_t ValueType;
  83. Integer(ValueType integer);
  84. Integer();
  85. // Returns Integer.
  86. ValueType i() const;
  87. static SharedHandle<Integer> g(ValueType integer);
  88. virtual void accept(ValueBaseVisitor& visitor) const;
  89. private:
  90. ValueType integer_;
  91. };
  92. class List:public ValueBase {
  93. public:
  94. typedef std::vector<SharedHandle<ValueBase> > ValueType;
  95. List();
  96. // Appends given v to list.
  97. void append(const SharedHandle<ValueBase>& v);
  98. // Appeding string is so common that we provide shortcut function.
  99. void append(const String::ValueType& string);
  100. // Alias for append()
  101. List& operator<<(const SharedHandle<ValueBase>& v);
  102. // Returns the object at given index.
  103. const SharedHandle<ValueBase>& get(size_t index) const;
  104. // Set the object at given index.
  105. void set(size_t index, const SharedHandle<ValueBase>& v);
  106. // Returns the const reference of the object at the given index.
  107. const SharedHandle<ValueBase>& operator[](size_t index) const;
  108. // Returns a read/write iterator that points to the first object in
  109. // list.
  110. ValueType::iterator begin();
  111. // Returns a read/write iterator that points to the one past the
  112. // last object in list.
  113. ValueType::iterator end();
  114. // Returns a read/write read-only iterator that points to the first
  115. // object in list.
  116. ValueType::const_iterator begin() const;
  117. // Returns a read/write read-only iterator that points to the one
  118. // past the last object in list.
  119. ValueType::const_iterator end() const;
  120. // Returns size of list.
  121. size_t size() const;
  122. // Returns true if size of list is 0.
  123. bool empty() const;
  124. static SharedHandle<List> g();
  125. virtual void accept(ValueBaseVisitor& visitor) const;
  126. private:
  127. ValueType list_;
  128. };
  129. class Dict:public ValueBase {
  130. public:
  131. typedef std::map<std::string, SharedHandle<ValueBase> > ValueType;
  132. Dict();
  133. void put(const std::string& key, const SharedHandle<ValueBase>& vlb);
  134. // Putting string is so common that we provide shortcut function.
  135. void put(const std::string& key, const String::ValueType& string);
  136. const SharedHandle<ValueBase>& get(const std::string& key) const;
  137. // Returns the reference to object associated with given key. If
  138. // the key is not found, new pair with that key is created using
  139. // default values, which is then returned. In other words, this is
  140. // the same behavior of std::map's operator[].
  141. SharedHandle<ValueBase>& operator[](const std::string& key);
  142. // Returns the const reference to ojbect associated with given key.
  143. // If the key is not found, ValueBase::none is returned.
  144. const SharedHandle<ValueBase>& operator[](const std::string& key) const;
  145. // Returns true if the given key is found in dict.
  146. bool containsKey(const std::string& key) const;
  147. // Removes specified key from dict.
  148. void removeKey(const std::string& key);
  149. // Returns a read/write iterator that points to the first pair in
  150. // the dict.
  151. ValueType::iterator begin();
  152. // Returns a read/write read-only iterator that points to one past
  153. // the last pair in the dict.
  154. ValueType::iterator end();
  155. // Returns a read/write read-only iterator that points to the first
  156. // pair in the dict.
  157. ValueType::const_iterator begin() const;
  158. // Returns a read/write read-only iterator that points to one past
  159. // the last pair in the dict.
  160. ValueType::const_iterator end() const;
  161. // Returns size of Dict.
  162. size_t size() const;
  163. // Returns true if size of Dict is 0.
  164. bool empty() const;
  165. static SharedHandle<Dict> g();
  166. virtual void accept(ValueBaseVisitor& visitor) const;
  167. private:
  168. ValueType dict_;
  169. };
  170. template<typename T, typename T1, typename T2, typename T3>
  171. class DowncastValueBaseVisitor:public ValueBaseVisitor {
  172. private:
  173. const T* result_;
  174. public:
  175. DowncastValueBaseVisitor():result_(0) {}
  176. virtual void visit(const T& t)
  177. {
  178. result_ = &t;
  179. }
  180. virtual void visit(const T1& t1) {}
  181. virtual void visit(const T2& t2) {}
  182. virtual void visit(const T3& t3) {}
  183. const T* getResult() const
  184. {
  185. return result_;
  186. }
  187. void setResult(const T* r)
  188. {
  189. result_ = r;
  190. }
  191. };
  192. template<typename T, typename T1, typename T2, typename T3, typename VPtr>
  193. const T* downcast(const VPtr& v)
  194. {
  195. DowncastValueBaseVisitor<T, T1, T2, T3> visitor;
  196. v->accept(visitor);
  197. return visitor.getResult();
  198. }
  199. const String* asString(const ValueBase* v);
  200. String* asString(ValueBase* v);
  201. String* asString(const SharedHandle<ValueBase>& v);
  202. const Integer* asInteger(const ValueBase* v);
  203. Integer* asInteger(ValueBase* v);
  204. Integer* asInteger(const SharedHandle<ValueBase>& v);
  205. const List* asList(const ValueBase* v);
  206. List* asList(ValueBase* v);
  207. List* asList(const SharedHandle<ValueBase>& v);
  208. const Dict* asDict(const ValueBase* v);
  209. Dict* asDict(ValueBase* v);
  210. Dict* asDict(const SharedHandle<ValueBase>& v);
  211. } // namespace aria2
  212. #endif // D_VALUE_BASE_H