MySQL 8.0.39
Source Code Documentation
buffer_view.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/// @addtogroup Replication
25/// @{
26///
27/// @file buffer_view.h
28///
29/// Class that groups a pointer+size as one object, without managing
30/// the memory for it.
31
32#ifndef MYSQL_BUFFER_BUFFER_VIEW_H_
33#define MYSQL_BUFFER_BUFFER_VIEW_H_
34
35#include <cassert> // assert
36#include <string> // std::string
37#ifndef NDEBUG
38#include <sstream> // std::ostringstream
39#endif
40
41namespace mysqlns::buffer {
42
43/// Non-owning view of a memory buffer with a fixed size.
44///
45/// This is a minimal class holding just a pointer and a size. It
46/// does not have a read/write position (@see Rw_buffer_sequence). It
47/// does not have methods to grow the buffer (@see
48/// Growable_buffer_sequence).
49template <class Char_tp = unsigned char>
51 public:
52 using Char_t = Char_tp;
53 /// The 'size' type. Keep this equal to
54 /// buffer::Grow_calculator::Size_t
55 using Size_t = std::size_t;
56 using Iterator_t = Char_t *;
57 using Const_iterator_t = const Char_t *;
58
59 /// Create a new Buffer_view with the specified size and data.
61 if (data == nullptr) assert(size == 0);
62 }
63
64 /// Create a new "null Buffer_view": pointer is nullptr and size is
65 /// 0.
66 Buffer_view() = default;
67
68 /// Shallow copy constructor.
69 ///
70 /// @note The data pointer is copied but not the contents.
71 Buffer_view(const Buffer_view &) = default;
72
73 /// Default move constructor.
74 Buffer_view(Buffer_view &&) noexcept = default;
75
76 /// Shallow copy assignment operator.
77 ///
78 /// @note The data pointer is copied but not the contents.
79 Buffer_view &operator=(const Buffer_view &) = default;
80
81 /// Default move assignment operator.
82 Buffer_view &operator=(Buffer_view &&) noexcept = default;
83
84 /// Default delete operator.
85 virtual ~Buffer_view() = default;
86
87 /// Return const pointer to the data.
88 const Char_t *data() const { return m_data; }
89
90 /// Return non-const pointer to the data.
91 Char_t *data() { return m_data; }
92
93 /// Return pointer to the first character of the data.
94 Iterator_t begin() { return m_data; }
95
96 /// Return pointer to one-past-the-last character of the data.
97 Iterator_t end() { return m_data + m_size; }
98
99 /// Return pointer to the first character of the data.
100 Const_iterator_t begin() const { return m_data; }
101
102 /// Return pointer to one-past-the-last character of the data.
103 Const_iterator_t end() const { return m_data + m_size; }
104
105 /// Return const pointer to the first character of the data.
106 Const_iterator_t cbegin() const { return m_data; }
107
108 /// Return const pointer to one-past-the-last character of the data.
109 Const_iterator_t cend() const { return m_data + m_size; }
110
111 /// Return the number of bytes.
112 Size_t size() const { return m_size; }
113
114 /// Return a copy of this object as a `std::string`.
115 template <class Str_char_t = char,
116 class Str_traits_t = std::char_traits<Str_char_t>,
117 class Str_allocator_t = std::allocator<Str_char_t>>
118 std::basic_string<Str_char_t, Str_traits_t, Str_allocator_t> str(
119 const Str_allocator_t &allocator = Str_allocator_t()) const {
120 return std::basic_string<Str_char_t, Str_traits_t, Str_allocator_t>(
121 reinterpret_cast<const Str_char_t *>(m_data), m_size, allocator);
122 }
123
124 /// In debug mode, return a string with debug info.
125 ///
126 /// @param show_contents If true, includes the buffer contents.
127 /// Otherwise, just pointers and sizes.
128 std::string debug_string([[maybe_unused]] bool show_contents = false) const {
129#ifdef NDEBUG
130 return "";
131#else
133 // clang-format off
134 ss << "Buffer_view(ptr=" << (const void *)this
135 << ", data=" << (const void *)data()
136 << ", size=" << size();
137 // clang-format on
138 if (show_contents && begin() != nullptr)
139 ss << ", contents=\""
140 << std::string(reinterpret_cast<const char *>(begin()), size())
141 << "\"";
142 ss << ")";
143 return ss.str();
144#endif
145 }
146
147 private:
148 /// Pointer to the data.
149 Char_t *m_data{nullptr};
150
151 /// The number of bytes.
153};
154
155} // namespace mysqlns::buffer
156
157/// @} (end of group Replication)
158
159#endif // MYSQL_BUFFER_BUFFER_VIEW_H_
Non-owning view of a memory buffer with a fixed size.
Definition: buffer_view.h:50
Size_t m_size
The number of bytes.
Definition: buffer_view.h:152
Const_iterator_t cbegin() const
Return const pointer to the first character of the data.
Definition: buffer_view.h:106
Char_t * data()
Return non-const pointer to the data.
Definition: buffer_view.h:91
Buffer_view(const Buffer_view &)=default
Shallow copy constructor.
Iterator_t begin()
Return pointer to the first character of the data.
Definition: buffer_view.h:94
Char_t * m_data
Pointer to the data.
Definition: buffer_view.h:149
Char_tp Char_t
Definition: buffer_view.h:52
Buffer_view(Buffer_view &&) noexcept=default
Default move constructor.
std::size_t Size_t
The 'size' type.
Definition: buffer_view.h:55
Const_iterator_t begin() const
Return pointer to the first character of the data.
Definition: buffer_view.h:100
Buffer_view(Char_t *data, Size_t size)
Create a new Buffer_view with the specified size and data.
Definition: buffer_view.h:60
std::basic_string< Str_char_t, Str_traits_t, Str_allocator_t > str(const Str_allocator_t &allocator=Str_allocator_t()) const
Return a copy of this object as a std::string.
Definition: buffer_view.h:118
const Char_t * Const_iterator_t
Definition: buffer_view.h:57
Const_iterator_t end() const
Return pointer to one-past-the-last character of the data.
Definition: buffer_view.h:103
std::string debug_string(bool show_contents=false) const
In debug mode, return a string with debug info.
Definition: buffer_view.h:128
Buffer_view()=default
Create a new "null Buffer_view": pointer is nullptr and size is 0.
const Char_t * data() const
Return const pointer to the data.
Definition: buffer_view.h:88
Char_t * Iterator_t
Definition: buffer_view.h:56
Size_t size() const
Return the number of bytes.
Definition: buffer_view.h:112
Iterator_t end()
Return pointer to one-past-the-last character of the data.
Definition: buffer_view.h:97
Const_iterator_t cend() const
Return const pointer to one-past-the-last character of the data.
Definition: buffer_view.h:109
Definition: buffer_sequence_view.h:51
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870