ValueBase.cc 7.1 KB

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