MySQL 8.4.0
Source Code Documentation
unique_ptr.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_UNIQUEPTR_INCLUDED
27#define MYSQL_HARNESS_UNIQUEPTR_INCLUDED
28
29#include <assert.h> // <cassert> is flawed: assert() lands in global namespace on Ubuntu 14.04, not std::
30#include <functional>
31#include <memory>
32
33namespace mysql_harness {
34
35// clang-format off
36/* @brief: improved std::unique_ptr super-class
37 *
38 * std::unique_ptr has one problem: it's tedious to use with custom deleters.
39 * This class is a convenience super-class, which makes working with unique_ptr
40 * easier. Advantages:
41 *
42 * 1. Improved constructor for easier type declaration (deleter type is
43 * automatically resolved):
44 *
45 * UniquePtr<Foo> foo(new_foo(), foo_deleter);
46 * UniquePtr<Foo> bar = std::move(foo);
47 *
48 * instead of:
49 *
50 * std::unique_ptr<Foo, decltype(foo_deleter)> foo(new_foo(), foo_deleter);
51 * std::unique_ptr<Foo, decltype(foo_deleter)> bar = std::move(foo);
52 *
53 *
54 *
55 * 2. UniquePtr constructor ALWAYS defines a custom deleter, which is a must
56 * if we're passing unique_ptr across DLL boundaries (the idea is to
57 * release the memory within the same DLL in which it was allocated).
58 * However, if all we need is a default deleter, it will implicitly define
59 * it for us:
60 *
61 * UniquePtr<Foo> foo(new_foo()); // std::default_delete is default 2nd parameter
62 *
63 * instead of:
64 *
65 * std::unique_ptr<Foo, std::default_delete> foo(new_foo(), std::default_delete());
66 *
67 *
68 *
69 * 3. In debug builds, it offers a safety feature to warn the developer, if
70 * (s)he forgets about the custom deleter. See comments above release()
71 * method
72 */
73// clang-format on
74template <typename T>
75class UniquePtr : public std::unique_ptr<T, std::function<void(T *)>> {
76 public:
77 UniquePtr() = default;
78
79 UniquePtr(T *ptr, std::function<void(T *)> deleter = std::default_delete<T>())
80 : std::unique_ptr<T, std::function<void(T *)>>(ptr, deleter) {}
81
82 UniquePtr(const UniquePtr<T> &) = delete;
84 : std::unique_ptr<T, std::function<void(T *)>>::unique_ptr(
85 std::move(other))
86#ifndef NDEBUG
87 ,
89#endif
90 {
91 }
92
93 UniquePtr &operator=(const UniquePtr<T> &) = delete;
95#ifndef NDEBUG
96 get_deleter_called_ = other.get_deleter_called_;
97#endif
98 std::unique_ptr<T, std::function<void(T *)>>::operator=(std::move(other));
99 return *this;
100 }
101
102// It is typical for developers never to worry about how they create and how
103// they delete objects. Since we're using custom deleter, we want to make sure
104// that the developer doesn't forget about that. We want to protect against a
105// situation like so:
106//
107// // some_place_1
108// UniquePtr<A> u_ptr(new A, my_custom_deleter(ptr); });
109//
110// // some_place_2
111// A* raw_ptr = u_ptr.release(); // dev forgot that u_ptr also stores the
112// // deleter to erase this object
113// ...
114// ...
115// delete raw_ptr; // KABOOOM!!!
116//
117// So the least we can do, is try to remind the developer if we notice the dev
118// didn't bother fetching the deleter.
119//
120#ifndef NDEBUG
121 T *release() {
122 assert(get_deleter_called_); // do you know what you're doing? (how are you
123 // going to delete this object?)
124 return std::unique_ptr<T, std::function<void(T *)>>::release();
125 }
126
128 get_deleter_called_ = true; // dev didn't forget about the deleter, good!
129 return std::unique_ptr<T, std::function<void(T *)>>::get_deleter();
130 }
131
132 private:
134#endif
135};
136
137} // namespace mysql_harness
138#endif //#ifndef MYSQL_HARNESS_UNIQUEPTR_INCLUDED
Definition: unique_ptr.h:75
UniquePtr & operator=(UniquePtr< T > &&other)
Definition: unique_ptr.h:94
T * release()
Definition: unique_ptr.h:121
UniquePtr(UniquePtr< T > &&other)
Definition: unique_ptr.h:83
UniquePtr< T >::deleter_type get_deleter()
Definition: unique_ptr.h:127
bool get_deleter_called_
Definition: unique_ptr.h:133
UniquePtr & operator=(const UniquePtr< T > &)=delete
UniquePtr(const UniquePtr< T > &)=delete
UniquePtr(T *ptr, std::function< void(T *)> deleter=std::default_delete< T >())
Definition: unique_ptr.h:79
Definition: common.h:42
Definition: gcs_xcom_synode.h:64
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2438