diagnostics.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Copyright (c) 2019-2023 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_DIAGNOSTICS_HPP
  8. #define BOOST_MYSQL_DIAGNOSTICS_HPP
  9. #include <boost/mysql/string_view.hpp>
  10. #include <boost/mysql/detail/auxiliar/access_fwd.hpp>
  11. #include <ostream>
  12. #include <string>
  13. namespace boost {
  14. namespace mysql {
  15. /**
  16. * \brief Contains additional information about errors.
  17. * \details
  18. * This class is a container for additional diagnostics about an operation that
  19. * failed. Currently, it's used to hold any error messages sent by the server on
  20. * error (\ref server_message). More members may be added in the future.
  21. */
  22. class diagnostics
  23. {
  24. public:
  25. /**
  26. * \brief Constructs a diagnostics object with an empty error message.
  27. * \par Exception safety
  28. * No-throw guarantee.
  29. */
  30. diagnostics() = default;
  31. /**
  32. * \brief Gets the server-generated error message.
  33. * \details
  34. * It's encoded according to `character_set_results` character set, which
  35. * usually matches the connection's character set. It may potentially contain user input.
  36. *
  37. * \par Exception safety
  38. * No-throw guarantee.
  39. *
  40. * \par Object lifetimes
  41. * The returned view is valid as long as `*this` is alive, hasn't been assigned-to
  42. * or moved-from, and \ref clear hasn't been called. Moving `*this` invalidates the view.
  43. */
  44. string_view server_message() const noexcept { return msg_; }
  45. /**
  46. * \brief Clears the error message.
  47. * \par Exception safety
  48. * No-throw guarantee.
  49. */
  50. void clear() noexcept { msg_.clear(); }
  51. private:
  52. std::string msg_;
  53. #ifndef BOOST_MYSQL_DOXYGEN
  54. friend struct detail::diagnostics_access;
  55. #endif
  56. };
  57. /**
  58. * \relates diagnostics
  59. * \brief Compares two diagnostics objects.
  60. * \par Exception safety
  61. * No-throw guarantee.
  62. */
  63. inline bool operator==(const diagnostics& lhs, const diagnostics& rhs) noexcept
  64. {
  65. return lhs.server_message() == rhs.server_message();
  66. }
  67. /**
  68. * \relates diagnostics
  69. * \brief Compares two diagnostics objects.
  70. * \par Exception safety
  71. * No-throw guarantee.
  72. */
  73. inline bool operator!=(const diagnostics& lhs, const diagnostics& rhs) noexcept { return !(lhs == rhs); }
  74. } // namespace mysql
  75. } // namespace boost
  76. #include <boost/mysql/impl/diagnostics.hpp>
  77. #endif