ValueBase.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. const SharedHandle<Bool> Bool::trueValue_(new Bool(true));
  81. const SharedHandle<Bool> Bool::falseValue_(new Bool(false));
  82. SharedHandle<Bool> Bool::gTrue()
  83. {
  84. return trueValue_;
  85. }
  86. SharedHandle<Bool> Bool::gFalse()
  87. {
  88. return falseValue_;
  89. }
  90. bool Bool::val() const
  91. {
  92. return val_;
  93. }
  94. void Bool::accept(ValueBaseVisitor& v) const
  95. {
  96. v.visit(*this);
  97. }
  98. Bool::Bool(bool val):val_(val) {}
  99. const SharedHandle<Null> Null::nullValue_(new Null());
  100. SharedHandle<Null> Null::g()
  101. {
  102. return nullValue_;
  103. }
  104. void Null::accept(ValueBaseVisitor& v) const
  105. {
  106. v.visit(*this);
  107. }
  108. Null::Null() {}
  109. List::List() {}
  110. List::~List() {}
  111. const SharedHandle<ValueBase>& List::get(size_t index) const
  112. {
  113. return list_[index];
  114. }
  115. void List::set(size_t index, const SharedHandle<ValueBase>& v)
  116. {
  117. list_[index] = v;
  118. }
  119. void List::append(const SharedHandle<ValueBase>& v)
  120. {
  121. list_.push_back(v);
  122. }
  123. void List::append(const String::ValueType& string)
  124. {
  125. list_.push_back(String::g(string));
  126. }
  127. List& List::operator<<(const SharedHandle<ValueBase>& v)
  128. {
  129. list_.push_back(v);
  130. return *this;
  131. }
  132. const SharedHandle<ValueBase>& List::operator[](size_t index) const
  133. {
  134. return list_[index];
  135. }
  136. List::ValueType::iterator List::begin()
  137. {
  138. return list_.begin();
  139. }
  140. List::ValueType::iterator List::end()
  141. {
  142. return list_.end();
  143. }
  144. List::ValueType::const_iterator List::begin() const
  145. {
  146. return list_.begin();
  147. }
  148. List::ValueType::const_iterator List::end() const
  149. {
  150. return list_.end();
  151. }
  152. size_t List::size() const
  153. {
  154. return list_.size();
  155. }
  156. bool List::empty() const
  157. {
  158. return list_.empty();
  159. }
  160. SharedHandle<List> List::g()
  161. {
  162. return SharedHandle<List>(new List());
  163. }
  164. void List::accept(ValueBaseVisitor& v) const
  165. {
  166. v.visit(*this);
  167. }
  168. Dict::Dict() {}
  169. Dict::~Dict() {}
  170. void Dict::put(const std::string& key, const SharedHandle<ValueBase>& vlb)
  171. {
  172. ValueType::value_type p = std::make_pair(key, vlb);
  173. std::pair<ValueType::iterator, bool> r = dict_.insert(p);
  174. if(!r.second) {
  175. (*r.first).second = vlb;
  176. }
  177. }
  178. void Dict::put(const std::string& key, const String::ValueType& string)
  179. {
  180. put(key, String::g(string));
  181. }
  182. const SharedHandle<ValueBase>& Dict::get(const std::string& key) const
  183. {
  184. ValueType::const_iterator itr = dict_.find(key);
  185. if(itr == dict_.end()) {
  186. return ValueBase::none;
  187. } else {
  188. return (*itr).second;
  189. }
  190. }
  191. SharedHandle<ValueBase>& Dict::operator[](const std::string& key)
  192. {
  193. return dict_[key];
  194. }
  195. const SharedHandle<ValueBase>& Dict::operator[](const std::string& key) const
  196. {
  197. return get(key);
  198. }
  199. bool Dict::containsKey(const std::string& key) const
  200. {
  201. return dict_.count(key) == 1;
  202. }
  203. void Dict::removeKey(const std::string& key)
  204. {
  205. dict_.erase(key);
  206. }
  207. Dict::ValueType::iterator Dict::begin()
  208. {
  209. return dict_.begin();
  210. }
  211. Dict::ValueType::iterator Dict::end()
  212. {
  213. return dict_.end();
  214. }
  215. Dict::ValueType::const_iterator Dict::begin() const
  216. {
  217. return dict_.begin();
  218. }
  219. Dict::ValueType::const_iterator Dict::end() const
  220. {
  221. return dict_.end();
  222. }
  223. size_t Dict::size() const
  224. {
  225. return dict_.size();
  226. }
  227. bool Dict::empty() const
  228. {
  229. return dict_.empty();
  230. }
  231. SharedHandle<Dict> Dict::g()
  232. {
  233. return SharedHandle<Dict>(new Dict());
  234. }
  235. void Dict::accept(ValueBaseVisitor& v) const
  236. {
  237. v.visit(*this);
  238. }
  239. const String* asString(const ValueBase* v)
  240. {
  241. if(v) {
  242. return downcast<String>(v);
  243. } else {
  244. return 0;
  245. }
  246. }
  247. String* asString(ValueBase* v)
  248. {
  249. if(v) {
  250. return const_cast<String*>(downcast<String>(v));
  251. } else {
  252. return 0;
  253. }
  254. }
  255. String* asString(const SharedHandle<ValueBase>& v)
  256. {
  257. if(v.get()) {
  258. return const_cast<String*>(downcast<String>(v));
  259. } else {
  260. return 0;
  261. }
  262. }
  263. const Integer* asInteger(const ValueBase* v)
  264. {
  265. if(v) {
  266. return downcast<Integer>(v);
  267. } else {
  268. return 0;
  269. }
  270. }
  271. Integer* asInteger(ValueBase* v)
  272. {
  273. if(v) {
  274. return const_cast<Integer*>(downcast<Integer>(v));
  275. } else {
  276. return 0;
  277. }
  278. }
  279. Integer* asInteger(const SharedHandle<ValueBase>& v)
  280. {
  281. if(v.get()) {
  282. return const_cast<Integer*>(downcast<Integer>(v));
  283. } else {
  284. return 0;
  285. }
  286. }
  287. const Bool* asBool(const ValueBase* v)
  288. {
  289. if(v) {
  290. return downcast<Bool>(v);
  291. } else {
  292. return 0;
  293. }
  294. }
  295. Bool* asBool(const SharedHandle<ValueBase>& v)
  296. {
  297. if(v.get()) {
  298. return const_cast<Bool*>(downcast<Bool>(v));
  299. } else {
  300. return 0;
  301. }
  302. }
  303. const Null* asNull(const ValueBase* v)
  304. {
  305. if(v) {
  306. return downcast<Null>(v);
  307. } else {
  308. return 0;
  309. }
  310. }
  311. Null* asNull(const SharedHandle<ValueBase>& v)
  312. {
  313. if(v) {
  314. return const_cast<Null*>(downcast<Null>(v));
  315. } else {
  316. return 0;
  317. }
  318. }
  319. const List* asList(const ValueBase* v)
  320. {
  321. if(v) {
  322. return downcast<List>(v);
  323. } else {
  324. return 0;
  325. }
  326. }
  327. List* asList(ValueBase* v)
  328. {
  329. if(v) {
  330. return const_cast<List*>(downcast<List>(v));
  331. } else {
  332. return 0;
  333. }
  334. }
  335. List* asList(const SharedHandle<ValueBase>& v)
  336. {
  337. if(v.get()) {
  338. return const_cast<List*>(downcast<List>(v));
  339. } else {
  340. return 0;
  341. }
  342. }
  343. const Dict* asDict(const ValueBase* v)
  344. {
  345. if(v) {
  346. return downcast<Dict>(v);
  347. } else {
  348. return 0;
  349. }
  350. }
  351. Dict* asDict(ValueBase* v)
  352. {
  353. if(v) {
  354. return const_cast<Dict*>(downcast<Dict>(v));
  355. } else {
  356. return 0;
  357. }
  358. }
  359. Dict* asDict(const SharedHandle<ValueBase>& v)
  360. {
  361. if(v.get()) {
  362. return const_cast<Dict*>(downcast<Dict>(v));
  363. } else {
  364. return 0;
  365. }
  366. }
  367. } // namespace aria2