sequence.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_DETAIL_SEQUENCE_HPP
  8. #define BOOST_MYSQL_DETAIL_SEQUENCE_HPP
  9. #include <boost/mysql/constant_string_view.hpp>
  10. #include <boost/mysql/format_sql.hpp>
  11. #include <boost/compat/to_array.hpp>
  12. #include <boost/compat/type_traits.hpp>
  13. #include <array>
  14. #include <cstddef>
  15. #include <functional>
  16. #include <iterator>
  17. #ifdef BOOST_MYSQL_HAS_CONCEPTS
  18. #include <concepts>
  19. #endif
  20. namespace boost {
  21. namespace mysql {
  22. namespace detail {
  23. template <class T>
  24. struct sequence_range_impl
  25. {
  26. using type = T;
  27. };
  28. template <class T>
  29. struct sequence_range_impl<std::reference_wrapper<T>>
  30. {
  31. using type = T&;
  32. };
  33. template <class T, std::size_t N>
  34. struct sequence_range_impl<T[N]>
  35. {
  36. using type = std::array<T, N>;
  37. };
  38. template <class T>
  39. using sequence_range_type = sequence_range_impl<compat::remove_cvref_t<T>>;
  40. template <class Range>
  41. Range&& cast_range(Range&& range)
  42. {
  43. return std::forward<Range>(range);
  44. }
  45. template <class T, std::size_t N>
  46. std::array<compat::remove_cv_t<T>, N> cast_range(T (&a)[N])
  47. {
  48. return compat::to_array(a);
  49. }
  50. template <class T, std::size_t N>
  51. std::array<compat::remove_cv_t<T>, N> cast_range(T (&&a)[N])
  52. {
  53. return compat::to_array(std::move(a));
  54. }
  55. // TODO: should this be Range&&?
  56. template <class Range, class FormatFn>
  57. void do_format_sequence(Range& range, const FormatFn& fn, constant_string_view glue, format_context_base& ctx)
  58. {
  59. bool is_first = true;
  60. for (auto it = std::begin(range); it != std::end(range); ++it)
  61. {
  62. if (!is_first)
  63. ctx.append_raw(glue);
  64. is_first = false;
  65. fn(*it, ctx);
  66. }
  67. }
  68. #ifdef BOOST_MYSQL_HAS_CONCEPTS
  69. template <class FormatFn, class Range>
  70. concept format_fn_for_range = requires(const FormatFn& format_fn, Range&& range, format_context_base& ctx) {
  71. { std::begin(range) != std::end(range) } -> std::convertible_to<bool>;
  72. format_fn(*std::begin(range), ctx);
  73. std::end(range);
  74. };
  75. #endif
  76. } // namespace detail
  77. } // namespace mysql
  78. } // namespace boost
  79. #endif