MySQL 8.0.29
Source Code Documentation
unique_ptr.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2021, 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 also distributed 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 included with MySQL.
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 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
25#ifndef MYSQL_HARNESS_UNIQUEPTR_INCLUDED
26#define MYSQL_HARNESS_UNIQUEPTR_INCLUDED
27
28#include <assert.h> // <cassert> is flawed: assert() lands in global namespace on Ubuntu 14.04, not std::
29#include <functional>
30#include <memory>
31
32namespace mysql_harness {
33
34// clang-format off
35/* @brief: improved std::unique_ptr super-class
36 *
37 * std::unique_ptr has one problem: it's tedious to use with custom deleters.
38 * This class is a convenience super-class, which makes working with unique_ptr
39 * easier. Advantages:
40 *
41 * 1. Improved constructor for easier type declaration (deleter type is
42 * automatically resolved):
43 *
44 * UniquePtr<Foo> foo(new_foo(), foo_deleter);
45 * UniquePtr<Foo> bar = std::move(foo);
46 *
47 * instead of:
48 *
49 * std::unique_ptr<Foo, decltype(foo_deleter)> foo(new_foo(), foo_deleter);
50 * std::unique_ptr<Foo, decltype(foo_deleter)> bar = std::move(foo);
51 *
52 *
53 *
54 * 2. UniquePtr constructor ALWAYS defines a custom deleter, which is a must
55 * if we're passing unique_ptr accross DLL boundaries (the idea is to
56 * release the memory within the same DLL in which it was allocated).
57 * However, if all we need is a default deleter, it will implicitly define
58 * it for us:
59 *
60 * UniquePtr<Foo> foo(new_foo()); // std::default_delete is default 2nd parameter
61 *
62 * instead of:
63 *
64 * std::unique_ptr<Foo, std::default_delete> foo(new_foo(), std::default_delete());
65 *
66 *
67 *
68 * 3. In debug builds, it offers a safety feature to warn the developer, if
69 * (s)he forgets about the custom deleter. See comments above release()
70 * method
71 */
72// clang-format on
73template <typename T>
74class UniquePtr : public std::unique_ptr<T, std::function<void(T *)>> {
75 public:
76 UniquePtr() = default;
77
78 UniquePtr(T *ptr, std::function<void(T *)> deleter = std::default_delete<T>())
79 : std::unique_ptr<T, std::function<void(T *)>>(ptr, deleter) {}
80
81 UniquePtr(const UniquePtr<T> &) = delete;
83 : std::unique_ptr<T, std::function<void(T *)>>::unique_ptr(
84 std::move(other))
85#ifndef NDEBUG
86 ,
88#endif
89 {
90 }
91
92 UniquePtr &operator=(const UniquePtr<T> &) = delete;
94#ifndef NDEBUG
95 get_deleter_called_ = other.get_deleter_called_;
96#endif
97 std::unique_ptr<T, std::function<void(T *)>>::operator=(std::move(other));
98 return *this;
99 }
100
101// It is typical for developers never to worry about how they create and how
102// they delete objects. Since we're using custom deleter, we want to make sure
103// that the developer doesn't forget about that. We want to protect against a
104// situation like so:
105//
106// // some_place_1
107// UniquePtr<A> u_ptr(new A, my_custom_deleter(ptr); });
108//
109// // some_place_2
110// A* raw_ptr = u_ptr.release(); // dev forgot that u_ptr also stores the
111// // deleter to erase this object
112// ...
113// ...
114// delete raw_ptr; // KABOOOM!!!
115//
116// So the least we can do, is try to remind the developer if we notice the dev
117// didn't bother fetching the deleter.
118//
119#ifndef NDEBUG
120 T *release() {
121 assert(get_deleter_called_); // do you know what you're doing? (how are you
122 // going to delete this object?)
123 return std::unique_ptr<T, std::function<void(T *)>>::release();
124 }
125
127 get_deleter_called_ = true; // dev didn't forget about the deleter, good!
128 return std::unique_ptr<T, std::function<void(T *)>>::get_deleter();
129 }
130
131 private:
133#endif
134};
135
136} // namespace mysql_harness
137#endif //#ifndef MYSQL_HARNESS_UNIQUEPTR_INCLUDED
Definition: unique_ptr.h:74
UniquePtr & operator=(UniquePtr< T > &&other)
Definition: unique_ptr.h:93
T * release()
Definition: unique_ptr.h:120
UniquePtr(UniquePtr< T > &&other)
Definition: unique_ptr.h:82
UniquePtr< T >::deleter_type get_deleter()
Definition: unique_ptr.h:126
bool get_deleter_called_
Definition: unique_ptr.h:132
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:78
Definition: common.h:41
Definition: varlen_sort.h:183
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:2434