params_ref.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. // Copyright (c) 2022 Alan de Freitas ([email protected])
  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. template<class FwdIt>
  151. auto
  152. params_ref::
  153. replace(
  154. iterator from,
  155. iterator to,
  156. FwdIt first,
  157. FwdIt last) ->
  158. iterator
  159. {
  160. /* If you get a compile error here, it
  161. means that the iterators you passed
  162. do not meet the requirements stated
  163. in the documentation.
  164. */
  165. static_assert(
  166. std::is_convertible<
  167. typename std::iterator_traits<
  168. FwdIt>::reference,
  169. param_view>::value,
  170. "Type requirements not met");
  171. return iterator(
  172. u_->edit_params(
  173. from.it_, to.it_,
  174. detail::make_params_iter(
  175. first, last)),
  176. opt_);
  177. }
  178. //------------------------------------------------
  179. //
  180. // implementation
  181. //
  182. //------------------------------------------------
  183. template<class FwdIt>
  184. void
  185. params_ref::
  186. assign(FwdIt first, FwdIt last,
  187. std::forward_iterator_tag)
  188. {
  189. u_->edit_params(
  190. begin().it_,
  191. end().it_,
  192. detail::make_params_iter(
  193. first, last));
  194. }
  195. template<class FwdIt>
  196. auto
  197. params_ref::
  198. insert(
  199. iterator before,
  200. FwdIt first,
  201. FwdIt last,
  202. std::forward_iterator_tag) ->
  203. iterator
  204. {
  205. return iterator(
  206. u_->edit_params(
  207. before.it_,
  208. before.it_,
  209. detail::make_params_iter(
  210. first, last)),
  211. opt_);
  212. }
  213. } // urls
  214. } // boost
  215. #endif