ValueBase.cc 6.4 KB

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