ValueBase.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #include "ValueBase.h"
  36. namespace aria2 {
  37. const SharedHandle<ValueBase> ValueBase::none;
  38. String::String(const ValueType& string):str_(string) {}
  39. String::String(const char* cstring):str_(cstring) {}
  40. String::String(const char* data, size_t length):str_(&data[0], &data[length]) {}
  41. String::String(const unsigned char* data, size_t length):
  42. str_(&data[0], &data[length]) {}
  43. String::String() {}
  44. String::~String() {}
  45. const String::ValueType& String::s() const
  46. {
  47. return str_;
  48. }
  49. const unsigned char* String::uc() const
  50. {
  51. return reinterpret_cast<const unsigned char*>(str_.data());
  52. }
  53. SharedHandle<String> String::g(const ValueType& string)
  54. {
  55. return SharedHandle<String>(new String(string));
  56. }
  57. SharedHandle<String> String::g(const unsigned char* data, size_t length)
  58. {
  59. return SharedHandle<String>(new String(data, length));
  60. }
  61. void String::accept(ValueBaseVisitor& v) const
  62. {
  63. v.visit(*this);
  64. }
  65. Integer::Integer(ValueType integer):integer_(integer) {}
  66. Integer::Integer():integer_(0) {}
  67. Integer::~Integer() {}
  68. Integer::ValueType Integer::i() const
  69. {
  70. return integer_;
  71. }
  72. SharedHandle<Integer> Integer::g(ValueType integer)
  73. {
  74. return SharedHandle<Integer>(new Integer(integer));
  75. }
  76. void Integer::accept(ValueBaseVisitor& v) const
  77. {
  78. v.visit(*this);
  79. }
  80. List::List() {}
  81. List::~List() {}
  82. const SharedHandle<ValueBase>& List::get(size_t index) const
  83. {
  84. return list_[index];
  85. }
  86. void List::set(size_t index, const SharedHandle<ValueBase>& v)
  87. {
  88. list_[index] = v;
  89. }
  90. void List::append(const SharedHandle<ValueBase>& v)
  91. {
  92. list_.push_back(v);
  93. }
  94. void List::append(const String::ValueType& string)
  95. {
  96. list_.push_back(String::g(string));
  97. }
  98. List& List::operator<<(const SharedHandle<ValueBase>& v)
  99. {
  100. list_.push_back(v);
  101. return *this;
  102. }
  103. const SharedHandle<ValueBase>& List::operator[](size_t index) const
  104. {
  105. return list_[index];
  106. }
  107. List::ValueType::iterator List::begin()
  108. {
  109. return list_.begin();
  110. }
  111. List::ValueType::iterator List::end()
  112. {
  113. return list_.end();
  114. }
  115. List::ValueType::const_iterator List::begin() const
  116. {
  117. return list_.begin();
  118. }
  119. List::ValueType::const_iterator List::end() const
  120. {
  121. return list_.end();
  122. }
  123. size_t List::size() const
  124. {
  125. return list_.size();
  126. }
  127. bool List::empty() const
  128. {
  129. return list_.empty();
  130. }
  131. SharedHandle<List> List::g()
  132. {
  133. return SharedHandle<List>(new List());
  134. }
  135. void List::accept(ValueBaseVisitor& v) const
  136. {
  137. v.visit(*this);
  138. }
  139. Dict::Dict() {}
  140. Dict::~Dict() {}
  141. void Dict::put(const std::string& key, const SharedHandle<ValueBase>& vlb)
  142. {
  143. ValueType::value_type p = std::make_pair(key, vlb);
  144. std::pair<ValueType::iterator, bool> r = dict_.insert(p);
  145. if(!r.second) {
  146. (*r.first).second = vlb;
  147. }
  148. }
  149. void Dict::put(const std::string& key, const String::ValueType& string)
  150. {
  151. put(key, String::g(string));
  152. }
  153. const SharedHandle<ValueBase>& Dict::get(const std::string& key) const
  154. {
  155. ValueType::const_iterator itr = dict_.find(key);
  156. if(itr == dict_.end()) {
  157. return ValueBase::none;
  158. } else {
  159. return (*itr).second;
  160. }
  161. }
  162. SharedHandle<ValueBase>& Dict::operator[](const std::string& key)
  163. {
  164. return dict_[key];
  165. }
  166. const SharedHandle<ValueBase>& Dict::operator[](const std::string& key) const
  167. {
  168. return get(key);
  169. }
  170. bool Dict::containsKey(const std::string& key) const
  171. {
  172. return dict_.count(key) == 1;
  173. }
  174. void Dict::removeKey(const std::string& key)
  175. {
  176. dict_.erase(key);
  177. }
  178. Dict::ValueType::iterator Dict::begin()
  179. {
  180. return dict_.begin();
  181. }
  182. Dict::ValueType::iterator Dict::end()
  183. {
  184. return dict_.end();
  185. }
  186. Dict::ValueType::const_iterator Dict::begin() const
  187. {
  188. return dict_.begin();
  189. }
  190. Dict::ValueType::const_iterator Dict::end() const
  191. {
  192. return dict_.end();
  193. }
  194. size_t Dict::size() const
  195. {
  196. return dict_.size();
  197. }
  198. bool Dict::empty() const
  199. {
  200. return dict_.empty();
  201. }
  202. SharedHandle<Dict> Dict::g()
  203. {
  204. return SharedHandle<Dict>(new Dict());
  205. }
  206. void Dict::accept(ValueBaseVisitor& v) const
  207. {
  208. v.visit(*this);
  209. }
  210. const String* asString(const ValueBase* v)
  211. {
  212. if(v) {
  213. return downcast<String, Integer, List, Dict>(v);
  214. } else {
  215. return 0;
  216. }
  217. }
  218. String* asString(ValueBase* v)
  219. {
  220. if(v) {
  221. return const_cast<String*>(downcast<String, Integer, List, Dict>(v));
  222. } else {
  223. return 0;
  224. }
  225. }
  226. String* asString(const SharedHandle<ValueBase>& v)
  227. {
  228. if(v.get()) {
  229. return const_cast<String*>(downcast<String, Integer, List, Dict>(v));
  230. } else {
  231. return 0;
  232. }
  233. }
  234. const Integer* asInteger(const ValueBase* v)
  235. {
  236. if(v) {
  237. return downcast<Integer, String, List, Dict>(v);
  238. } else {
  239. return 0;
  240. }
  241. }
  242. Integer* asInteger(ValueBase* v)
  243. {
  244. if(v) {
  245. return const_cast<Integer*>(downcast<Integer, String, List, Dict>(v));
  246. } else {
  247. return 0;
  248. }
  249. }
  250. Integer* asInteger(const SharedHandle<ValueBase>& v)
  251. {
  252. if(v.get()) {
  253. return const_cast<Integer*>(downcast<Integer, String, List, Dict>(v));
  254. } else {
  255. return 0;
  256. }
  257. }
  258. const List* asList(const ValueBase* v)
  259. {
  260. if(v) {
  261. return downcast<List, String, Integer, Dict>(v);
  262. } else {
  263. return 0;
  264. }
  265. }
  266. List* asList(ValueBase* v)
  267. {
  268. if(v) {
  269. return const_cast<List*>(downcast<List, String, Integer, Dict>(v));
  270. } else {
  271. return 0;
  272. }
  273. }
  274. List* asList(const SharedHandle<ValueBase>& v)
  275. {
  276. if(v.get()) {
  277. return const_cast<List*>(downcast<List, String, Integer, Dict>(v));
  278. } else {
  279. return 0;
  280. }
  281. }
  282. const Dict* asDict(const ValueBase* v)
  283. {
  284. if(v) {
  285. return downcast<Dict, String, Integer, List>(v);
  286. } else {
  287. return 0;
  288. }
  289. }
  290. Dict* asDict(ValueBase* v)
  291. {
  292. if(v) {
  293. return const_cast<Dict*>(downcast<Dict, String, Integer, List>(v));
  294. } else {
  295. return 0;
  296. }
  297. }
  298. Dict* asDict(const SharedHandle<ValueBase>& v)
  299. {
  300. if(v.get()) {
  301. return const_cast<Dict*>(downcast<Dict, String, Integer, List>(v));
  302. } else {
  303. return 0;
  304. }
  305. }
  306. } // namespace aria2