params_ref.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_IMPL_PARAMS_REF_HPP
  11. #define BOOST_URL_IMPL_PARAMS_REF_HPP
  12. #include <boost/url/params_view.hpp>
  13. #include <boost/url/detail/any_params_iter.hpp>
  14. #include <boost/url/detail/except.hpp>
  15. #include <boost/url/grammar/recycled.hpp>
  16. #include <boost/assert.hpp>
  17. namespace boost {
  18. namespace urls {
  19. inline
  20. params_ref::
  21. params_ref(
  22. url_base& u,
  23. encoding_opts opt) noexcept
  24. : params_base(u.impl_, opt)
  25. , u_(&u)
  26. {
  27. }
  28. //------------------------------------------------
  29. //
  30. // Special Members
  31. //
  32. //------------------------------------------------
  33. inline
  34. params_ref::
  35. params_ref(
  36. params_ref const& other,
  37. encoding_opts opt) noexcept
  38. : params_ref(*other.u_, opt)
  39. {
  40. }
  41. inline
  42. auto
  43. params_ref::
  44. operator=(std::initializer_list<
  45. param_view> init) ->
  46. params_ref&
  47. {
  48. assign(init);
  49. return *this;
  50. }
  51. //------------------------------------------------
  52. //
  53. // Modifiers
  54. //
  55. //------------------------------------------------
  56. inline
  57. void
  58. params_ref::
  59. clear() noexcept
  60. {
  61. u_->remove_query();
  62. }
  63. //------------------------------------------------
  64. template<class FwdIt>
  65. void
  66. params_ref::
  67. assign(FwdIt first, FwdIt last)
  68. {
  69. /* If you get a compile error here, it
  70. means that the iterators you passed
  71. do not meet the requirements stated
  72. in the documentation.
  73. */
  74. static_assert(
  75. std::is_convertible<
  76. typename std::iterator_traits<
  77. FwdIt>::reference,
  78. param_view>::value,
  79. "Type requirements not met");
  80. assign(first, last,
  81. typename std::iterator_traits<
  82. FwdIt>::iterator_category{});
  83. }
  84. inline
  85. auto
  86. params_ref::
  87. append(
  88. param_view const& p) ->
  89. iterator
  90. {
  91. return insert(end(), p);
  92. }
  93. inline
  94. auto
  95. params_ref::
  96. append(
  97. std::initializer_list<
  98. param_view> init) ->
  99. iterator
  100. {
  101. return insert(end(), init);
  102. }
  103. template<class FwdIt>
  104. auto
  105. params_ref::
  106. append(FwdIt first, FwdIt last) ->
  107. iterator
  108. {
  109. /* If you get a compile error here, it
  110. means that the iterators you passed
  111. do not meet the requirements stated
  112. in the documentation.
  113. */
  114. static_assert(
  115. std::is_convertible<
  116. typename std::iterator_traits<
  117. FwdIt>::reference,
  118. param_view>::value,
  119. "Type requirements not met");
  120. return insert(
  121. end(), first, last);
  122. }
  123. template<class FwdIt>
  124. auto
  125. params_ref::
  126. insert(
  127. iterator before,
  128. FwdIt first,
  129. FwdIt last) ->
  130. iterator
  131. {
  132. /* If you get a compile error here, it
  133. means that the iterators you passed
  134. do not meet the requirements stated
  135. in the documentation.
  136. */
  137. static_assert(
  138. std::is_convertible<
  139. typename std::iterator_traits<
  140. FwdIt>::reference,
  141. param_view>::value,
  142. "Type requirements not met");
  143. return insert(
  144. before,
  145. first,
  146. last,
  147. typename std::iterator_traits<
  148. FwdIt>::iterator_category{});
  149. }
  150. inline
  151. auto
  152. params_ref::
  153. erase(
  154. iterator pos) noexcept ->
  155. iterator
  156. {
  157. return erase(
  158. pos,
  159. std::next(pos));
  160. }
  161. inline
  162. auto
  163. params_ref::
  164. erase(
  165. iterator first,
  166. iterator last) noexcept ->
  167. iterator
  168. {
  169. string_view s("", 0);
  170. return iterator(
  171. u_->edit_params(
  172. first.it_,
  173. last.it_,
  174. detail::query_iter(s)),
  175. opt_);
  176. }
  177. template<class FwdIt>
  178. auto
  179. params_ref::
  180. replace(
  181. iterator from,
  182. iterator to,
  183. FwdIt first,
  184. FwdIt last) ->
  185. iterator
  186. {
  187. /* If you get a compile error here, it
  188. means that the iterators you passed
  189. do not meet the requirements stated
  190. in the documentation.
  191. */
  192. static_assert(
  193. std::is_convertible<
  194. typename std::iterator_traits<
  195. FwdIt>::reference,
  196. param_view>::value,
  197. "Type requirements not met");
  198. return iterator(
  199. u_->edit_params(
  200. from.it_, to.it_,
  201. detail::make_params_iter(
  202. first, last)),
  203. opt_);
  204. }
  205. //------------------------------------------------
  206. //
  207. // implementation
  208. //
  209. //------------------------------------------------
  210. template<class FwdIt>
  211. void
  212. params_ref::
  213. assign(FwdIt first, FwdIt last,
  214. std::forward_iterator_tag)
  215. {
  216. u_->edit_params(
  217. begin().it_,
  218. end().it_,
  219. detail::make_params_iter(
  220. first, last));
  221. }
  222. template<class FwdIt>
  223. auto
  224. params_ref::
  225. insert(
  226. iterator before,
  227. FwdIt first,
  228. FwdIt last,
  229. std::forward_iterator_tag) ->
  230. iterator
  231. {
  232. return iterator(
  233. u_->edit_params(
  234. before.it_,
  235. before.it_,
  236. detail::make_params_iter(
  237. first, last)),
  238. opt_);
  239. }
  240. } // urls
  241. } // boost
  242. #endif