constant_string_view.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2019-2024 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_CONSTANT_STRING_VIEW_HPP
  8. #define BOOST_MYSQL_CONSTANT_STRING_VIEW_HPP
  9. #include <boost/mysql/string_view.hpp>
  10. #include <boost/mysql/detail/config.hpp>
  11. #include <boost/config.hpp>
  12. #include <type_traits>
  13. namespace boost {
  14. namespace mysql {
  15. /**
  16. * \brief (EXPERIMENTAL) A string view that should be known at compile-time.
  17. * \details
  18. * This type is used when a string function argument must always be known at compile-time
  19. * except in rare cases. See \ref format_sql format strings for an example.
  20. * \n
  21. * \par Object lifetimes
  22. * This type holds internally a \ref string_view, and follows the same lifetime rules as `string_view`.
  23. * We recommend to only use this type as a function argument, to provide compile-time checks.
  24. */
  25. class constant_string_view
  26. {
  27. string_view impl_;
  28. #ifndef BOOST_MYSQL_DOXYGEN
  29. constexpr constant_string_view(string_view value, int) noexcept : impl_(value) {}
  30. friend constexpr constant_string_view runtime(string_view) noexcept;
  31. #endif
  32. public:
  33. /**
  34. * \brief Consteval constructor.
  35. * \details
  36. * Constructs a \ref string_view from the passed argument.
  37. * \n
  38. * This function is `consteval`: it results in a compile-time error
  39. * if the passed value is not known at compile-time. You can bypass
  40. * this check using the \ref runtime function. This check works only
  41. * for C++20 and above. No check is performed for lower C++ standard versions.
  42. * \n
  43. * This constructor is only considered if a \ref string_view can be constructed
  44. * from the passed value.
  45. *
  46. * \par Exception safety
  47. * No-throw guarantee.
  48. *
  49. * \par Object lifetimes
  50. * Ownership is not transferred to the constructed object. As with `string_view`,
  51. * the user is responsible for keeping the original character buffer alive.
  52. */
  53. template <
  54. class T
  55. #ifndef BOOST_MYSQL_DOXYGEN
  56. ,
  57. class = typename std::enable_if<std::is_convertible<const T&, string_view>::value>::type
  58. #endif
  59. >
  60. BOOST_MYSQL_CONSTEVAL constant_string_view(const T& value) noexcept : impl_(value)
  61. {
  62. }
  63. /**
  64. * \brief Retrieves the underlying string view.
  65. *
  66. * \par Exception safety
  67. * No-throw guarantee.
  68. *
  69. * \par Object lifetimes
  70. * The returned view has the same lifetime rules as `*this`.
  71. */
  72. constexpr string_view get() const noexcept { return impl_; }
  73. };
  74. /**
  75. * \brief (EXPERIMENTAL) Creates a \ref constant_string_view from a runtime value.
  76. * \details
  77. * You can use this function to bypass the `consteval` check performed by \ref constant_string_view
  78. * constructor.
  79. * \n
  80. * Don't use this function unless you know what you are doing. `consteval` checks exist
  81. * for the sake of security. Make sure to only pass trusted values to the relevant API.
  82. *
  83. * \par Exception safety
  84. * No-throw guarantee.
  85. *
  86. * \par Object lifetimes
  87. * The returned value has the same lifetime semantics as the passed view.
  88. */
  89. constexpr constant_string_view runtime(string_view value) noexcept { return constant_string_view(value, 0); }
  90. } // namespace mysql
  91. } // namespace boost
  92. #endif