-
Notifications
You must be signed in to change notification settings - Fork 33
/
diagnostics.hpp
132 lines (115 loc) · 3.61 KB
/
diagnostics.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_MYSQL_DIAGNOSTICS_HPP
#define BOOST_MYSQL_DIAGNOSTICS_HPP
#include <boost/mysql/string_view.hpp>
#include <boost/mysql/detail/access.hpp>
#include <string>
namespace boost {
namespace mysql {
/**
* \brief Contains additional information about errors.
* \details
* This class is a container for additional diagnostics about an operation that
* failed. It can contain server-generated messages (\ref server_message) or client-side messages
* (\ref client_message). More members may be added in the future.
*/
class diagnostics
{
public:
/**
* \brief Constructs a diagnostics object with empty error messages.
* \par Exception safety
* No-throw guarantee.
*/
diagnostics() = default;
/**
* \brief Gets the client-generated error message.
* \details
* Contrary to \ref server_message, the client message never contains any string data
* returned by the server, and is always ASCII-encoded. If you're using the static interface,
* it may contain C++ type identifiers, too.
*
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* The returned view is valid as long as `*this` is alive, hasn't been assigned-to
* or moved-from, and \ref clear hasn't been called. Moving `*this` invalidates the view.
*/
string_view client_message() const noexcept
{
return impl_.is_server ? string_view() : string_view(impl_.msg);
}
/**
* \brief Gets the server-generated error message.
* \details
* It's encoded according to `character_set_results` character set, which
* usually matches the connection's character set. It may potentially contain user input.
*
* \par Exception safety
* No-throw guarantee.
*
* \par Object lifetimes
* The returned view is valid as long as `*this` is alive, hasn't been assigned-to
* or moved-from, and \ref clear hasn't been called. Moving `*this` invalidates the view.
*/
string_view server_message() const noexcept
{
return impl_.is_server ? string_view(impl_.msg) : string_view();
}
/**
* \brief Clears the error messages.
* \par Exception safety
* No-throw guarantee.
*/
void clear() noexcept
{
impl_.is_server = false;
impl_.msg.clear();
}
private:
#ifndef BOOST_MYSQL_DOXYGEN
struct
{
bool is_server{};
std::string msg;
void assign_client(std::string from)
{
msg = std::move(from);
is_server = false;
}
void assign_server(std::string from)
{
msg = std::move(from);
is_server = true;
}
} impl_;
friend bool operator==(const diagnostics& lhs, const diagnostics& rhs) noexcept;
friend struct detail::access;
#endif
};
/**
* \relates diagnostics
* \brief Compares two diagnostics objects.
* \par Exception safety
* No-throw guarantee.
*/
inline bool operator==(const diagnostics& lhs, const diagnostics& rhs) noexcept
{
return lhs.impl_.is_server == rhs.impl_.is_server && lhs.impl_.msg == rhs.impl_.msg;
}
/**
* \relates diagnostics
* \brief Compares two diagnostics objects.
* \par Exception safety
* No-throw guarantee.
*/
inline bool operator!=(const diagnostics& lhs, const diagnostics& rhs) noexcept { return !(lhs == rhs); }
} // namespace mysql
} // namespace boost
#endif