ValueBase.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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) : str_{&data[0], &data[length]}
  41. {
  42. }
  43. String::String(const unsigned char* data, size_t length)
  44. : str_{&data[0], &data[length]}
  45. {
  46. }
  47. String::String() {}
  48. const String::ValueType& String::s() const { return str_; }
  49. String::ValueType String::popValue() const { return std::move(str_); }
  50. const unsigned char* String::uc() const
  51. {
  52. return reinterpret_cast<const unsigned char*>(str_.data());
  53. }
  54. std::unique_ptr<String> String::g(const ValueType& string)
  55. {
  56. return make_unique<String>(string);
  57. }
  58. std::unique_ptr<String> String::g(ValueType&& string)
  59. {
  60. return make_unique<String>(std::move(string));
  61. }
  62. std::unique_ptr<String> String::g(const unsigned char* data, size_t length)
  63. {
  64. return make_unique<String>(data, length);
  65. }
  66. void String::accept(ValueBaseVisitor& v) const { v.visit(*this); }
  67. Integer::Integer(ValueType integer) : integer_{integer} {}
  68. Integer::Integer() : integer_{0} {}
  69. Integer::ValueType Integer::i() const { return integer_; }
  70. std::unique_ptr<Integer> Integer::g(ValueType integer)
  71. {
  72. return make_unique<Integer>(integer);
  73. }
  74. void Integer::accept(ValueBaseVisitor& v) const { v.visit(*this); }
  75. Bool::Bool(bool val) : val_{val} {}
  76. std::unique_ptr<Bool> Bool::gTrue() { return make_unique<Bool>(true); }
  77. std::unique_ptr<Bool> Bool::gFalse() { return make_unique<Bool>(false); }
  78. bool Bool::val() const { return val_; }
  79. void Bool::accept(ValueBaseVisitor& v) const { v.visit(*this); }
  80. Null::Null() {}
  81. std::unique_ptr<Null> Null::g() { return make_unique<Null>(); }
  82. void Null::accept(ValueBaseVisitor& v) const { v.visit(*this); }
  83. List::List() {}
  84. ValueBase* List::get(size_t index) const { return list_[index].get(); }
  85. void List::set(size_t index, std::unique_ptr<ValueBase> v)
  86. {
  87. list_[index] = std::move(v);
  88. }
  89. void List::pop_front() { list_.pop_front(); }
  90. void List::pop_back() { list_.pop_back(); }
  91. void List::append(std::unique_ptr<ValueBase> v)
  92. {
  93. list_.push_back(std::move(v));
  94. }
  95. void List::append(String::ValueType string)
  96. {
  97. list_.push_back(String::g(std::move(string)));
  98. }
  99. List& List::operator<<(std::unique_ptr<ValueBase> v)
  100. {
  101. list_.push_back(std::move(v));
  102. return *this;
  103. }
  104. ValueBase* List::operator[](size_t index) const { return list_[index].get(); }
  105. List::ValueType::iterator List::begin() { return list_.begin(); }
  106. List::ValueType::iterator List::end() { return list_.end(); }
  107. List::ValueType::const_iterator List::begin() const { return list_.begin(); }
  108. List::ValueType::const_iterator List::end() const { return list_.end(); }
  109. List::ValueType::const_iterator List::cbegin() const { return list_.cbegin(); }
  110. List::ValueType::const_iterator List::cend() const { return list_.cend(); }
  111. size_t List::size() const { return list_.size(); }
  112. bool List::empty() const { return list_.empty(); }
  113. std::unique_ptr<List> List::g() { return make_unique<List>(); }
  114. void List::accept(ValueBaseVisitor& v) const { v.visit(*this); }
  115. Dict::Dict() {}
  116. void Dict::put(std::string key, std::unique_ptr<ValueBase> vlb)
  117. {
  118. auto p = std::make_pair(std::move(key), std::move(vlb));
  119. auto r = dict_.insert(std::move(p));
  120. if (!r.second) {
  121. (*r.first).second = std::move(p.second);
  122. }
  123. }
  124. void Dict::put(std::string key, String::ValueType string)
  125. {
  126. put(std::move(key), String::g(std::move(string)));
  127. }
  128. ValueBase* Dict::get(const std::string& key) const
  129. {
  130. auto itr = dict_.find(key);
  131. if (itr == std::end(dict_)) {
  132. return nullptr;
  133. }
  134. else {
  135. return (*itr).second.get();
  136. }
  137. }
  138. ValueBase* Dict::operator[](const std::string& key) const { return get(key); }
  139. bool Dict::containsKey(const std::string& key) const
  140. {
  141. return dict_.count(key);
  142. }
  143. void Dict::removeKey(const std::string& key) { dict_.erase(key); }
  144. std::unique_ptr<ValueBase> Dict::popValue(const std::string& key)
  145. {
  146. auto i = dict_.find(key);
  147. if (i == std::end(dict_)) {
  148. return nullptr;
  149. }
  150. else {
  151. auto res = std::move((*i).second);
  152. dict_.erase(i);
  153. return res;
  154. }
  155. }
  156. Dict::ValueType::iterator Dict::begin() { return dict_.begin(); }
  157. Dict::ValueType::iterator Dict::end() { return dict_.end(); }
  158. Dict::ValueType::const_iterator Dict::begin() const { return dict_.begin(); }
  159. Dict::ValueType::const_iterator Dict::end() const { return dict_.end(); }
  160. Dict::ValueType::const_iterator Dict::cbegin() const { return dict_.cbegin(); }
  161. Dict::ValueType::const_iterator Dict::cend() const { return dict_.cend(); }
  162. size_t Dict::size() const { return dict_.size(); }
  163. bool Dict::empty() const { return dict_.empty(); }
  164. std::unique_ptr<Dict> Dict::g() { return make_unique<Dict>(); }
  165. void Dict::accept(ValueBaseVisitor& v) const { v.visit(*this); }
  166. } // namespace aria2