Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2017 Chun-wei Fan <fanchunwei@src.gnome.org>
4 : : // SPDX-FileCopyrightText: 2018-2020 Canonical, Ltd
5 : : // SPDX-FileCopyrightText: 2018, 2024 Philip Chimento <philip.chimento@gmail.com>
6 : :
7 : : #pragma once
8 : :
9 : : #include <config.h>
10 : :
11 : : #include <stdlib.h>
12 : :
13 : : #include <concepts> // for same_as
14 : : #include <format>
15 : : #include <type_traits>
16 : : #include <utility>
17 : :
18 : : #include <glib-object.h>
19 : : #include <glib.h>
20 : : // IWYU pragma: no_forward_declare _GVariant
21 : :
22 : : #include <js/Utility.h> // for UniqueChars
23 : :
24 : : // Auto pointers. We don't use GLib's g_autofree and friends because they only
25 : : // work on GCC and Clang, and we try to support MSVC where possible. But this is
26 : : // C++, so we use C++ classes.
27 : :
28 : : namespace Gjs {
29 : :
30 : : // A sentinel object used to pick the AutoPointer constructor that adds a
31 : : // reference: AutoFoo foo{pointer, TakeOwnership{}};
32 : : struct TakeOwnership {};
33 : :
34 : : template <typename F = void>
35 : : using AutoPointerRefFunction = F* (*)(F*);
36 : :
37 : : template <typename F = void>
38 : : using AutoPointerFreeFunction = void (*)(F*);
39 : :
40 : : // Workaround for ubsan bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71962
41 : : namespace detail {
42 : : template <auto ptr>
43 : : concept NotNull =
44 : : !std::is_same_v<std::integral_constant<decltype(ptr), ptr>,
45 : : std::integral_constant<decltype(ptr), nullptr>>;
46 : : }
47 : :
48 : : template <typename T, typename F = void,
49 : : AutoPointerFreeFunction<F> free_func = free,
50 : : AutoPointerRefFunction<F> ref_func = nullptr>
51 : : struct AutoPointer {
52 : : using Tp =
53 : : std::conditional_t<std::is_array_v<T>, std::remove_extent_t<T>, T>;
54 : : using Ptr = std::add_pointer_t<Tp>;
55 : : using ConstPtr = std::add_pointer_t<std::add_const_t<Tp>>;
56 : : using RvalueRef = std::add_lvalue_reference_t<Tp>;
57 : :
58 : : protected:
59 : : using BaseType = AutoPointer<T, F, free_func, ref_func>;
60 : :
61 : : public:
62 : 531995 : constexpr AutoPointer(Ptr ptr = nullptr) // NOLINT(runtime/explicit)
63 : 531995 : : m_ptr(ptr) {}
64 : : explicit constexpr AutoPointer(std::same_as<Tp> auto ptr[])
65 : : requires std::is_array_v<T>
66 : : : m_ptr(ptr) {}
67 : :
68 : 2896 : constexpr AutoPointer(Ptr ptr, const TakeOwnership&) : AutoPointer(ptr) {
69 : 2896 : m_ptr = copy();
70 : 2896 : }
71 : 8 : constexpr AutoPointer(ConstPtr ptr, const TakeOwnership& o)
72 : 8 : : AutoPointer(const_cast<Ptr>(ptr), o) {}
73 : 176 : constexpr AutoPointer(AutoPointer&& other) : AutoPointer() {
74 : 176 : this->swap(other);
75 : 176 : }
76 : 242 : constexpr AutoPointer(AutoPointer const& other) : AutoPointer() {
77 : 242 : *this = other;
78 : 242 : }
79 : :
80 : 82002 : constexpr AutoPointer& operator=(Ptr ptr) {
81 : 82002 : reset(ptr);
82 : 82002 : return *this;
83 : : }
84 : :
85 : 24911 : constexpr AutoPointer& operator=(AutoPointer&& other) {
86 : 24911 : this->swap(other);
87 : 24911 : return *this;
88 : : }
89 : :
90 : 261 : constexpr AutoPointer& operator=(AutoPointer const& other) {
91 : 261 : AutoPointer dup{other.get(), TakeOwnership{}};
92 : 261 : this->swap(dup);
93 : 522 : return *this;
94 : 261 : }
95 : :
96 : 9335 : constexpr Ptr operator->()
97 : : requires(!std::is_array_v<T>)
98 : : {
99 : 9335 : return m_ptr;
100 : : }
101 : :
102 : 12739 : constexpr ConstPtr operator->() const
103 : : requires(!std::is_array_v<T>)
104 : : {
105 : 12739 : return m_ptr;
106 : : }
107 : :
108 : 9 : constexpr RvalueRef operator[](int index)
109 : : requires std::is_array_v<T>
110 : : {
111 : 9 : return m_ptr[index];
112 : : }
113 : :
114 : 609922 : constexpr std::add_const_t<RvalueRef> operator[](int index) const
115 : : requires std::is_array_v<T>
116 : : {
117 : 609922 : return m_ptr[index];
118 : : }
119 : :
120 : 1 : constexpr Tp operator*() const { return *m_ptr; }
121 : 460262 : constexpr operator Ptr() { return m_ptr; }
122 : 100352 : constexpr operator Ptr() const { return m_ptr; }
123 : : constexpr operator ConstPtr() const { return m_ptr; }
124 : 55457 : constexpr operator bool() const { return m_ptr != nullptr; }
125 : :
126 : 896490 : [[nodiscard]] constexpr Ptr get() const { return m_ptr; }
127 : 214619 : [[nodiscard]] constexpr Ptr* out() { return &m_ptr; }
128 : : [[nodiscard]]
129 : : constexpr ConstPtr* out() const {
130 : : return const_cast<ConstPtr*>(&m_ptr);
131 : : }
132 : :
133 : 34030 : constexpr Ptr release() {
134 : 34030 : auto* ptr = m_ptr;
135 : 34030 : m_ptr = nullptr;
136 : 34030 : return ptr;
137 : : }
138 : :
139 : 748937 : constexpr void reset(Ptr ptr = nullptr) {
140 : 748937 : Ptr old_ptr = m_ptr;
141 : 748937 : m_ptr = ptr;
142 : :
143 : : if constexpr (detail::NotNull<free_func>) {
144 [ + + ]: 704215 : if (old_ptr)
145 : : free_func(reinterpret_cast<F*>(old_ptr));
146 : : }
147 : 748937 : }
148 : :
149 : 25351 : constexpr void swap(AutoPointer& other) {
150 : 25351 : std::swap(this->m_ptr, other.m_ptr);
151 : 25351 : }
152 : :
153 : 640940 : constexpr ~AutoPointer() { reset(); }
154 : :
155 : : [[nodiscard]]
156 : 2920 : constexpr Ptr copy() const
157 : : requires(!std::is_array_v<T> && detail::NotNull<ref_func>)
158 : : {
159 [ + + ]: 2920 : return m_ptr ? reinterpret_cast<Ptr>(
160 : 2916 : ref_func(reinterpret_cast<F*>(m_ptr)))
161 : 4 : : nullptr;
162 : : }
163 : :
164 : : template <typename C>
165 : : [[nodiscard]]
166 : 4160 : constexpr C* as() const {
167 : 4160 : return const_cast<C*>(reinterpret_cast<const C*>(m_ptr));
168 : : }
169 : :
170 : : private:
171 : : Ptr m_ptr;
172 : : };
173 : :
174 : : template <typename T, typename F, AutoPointerFreeFunction<F> free_func,
175 : : AutoPointerRefFunction<F> ref_func>
176 : 1 : constexpr bool operator==(AutoPointer<T, F, free_func, ref_func> const& lhs,
177 : : AutoPointer<T, F, free_func, ref_func> const& rhs) {
178 : 1 : return lhs.get() == rhs.get();
179 : : }
180 : :
181 : : template <typename T>
182 : 350 : using AutoFree = AutoPointer<T>;
183 : :
184 : : struct AutoCharFuncs {
185 : : // This return type is required by AutoChar
186 : : // NOLINTNEXTLINE(custom-avoid-char-pointer-return)
187 : 20 : static char* dup(char* str) { return g_strdup(str); }
188 [ - + ]: 144443 : static void free(char* str) { g_free(str); }
189 : : };
190 : : using AutoChar =
191 : 144443 : AutoPointer<char, char, AutoCharFuncs::free, AutoCharFuncs::dup>;
192 : :
193 : : // This moves a string owned by the JS runtime into the GLib domain. This is
194 : : // only possible because currently, js_free() and g_free() both ultimately call
195 : : // free(). It would cause crashes if SpiderMonkey were to stop supporting
196 : : // embedders using the system allocator in the future. In that case, this
197 : : // function would have to copy the string.
198 : : [[nodiscard]]
199 : 5980 : inline AutoChar js_chars_to_glib(JS::UniqueChars&& js_chars) {
200 : 5980 : return {js_chars.release()};
201 : : }
202 : :
203 : 84971 : using AutoStrv = AutoPointer<char*, char*, g_strfreev, g_strdupv>;
204 : :
205 : : template <typename T>
206 : 81495 : using AutoUnref = AutoPointer<T, void, g_object_unref, g_object_ref>;
207 : :
208 : : using AutoGVariant =
209 : : AutoPointer<GVariant, GVariant, g_variant_unref, g_variant_ref>;
210 : :
211 : : using AutoParam =
212 : 427 : AutoPointer<GParamSpec, GParamSpec, g_param_spec_unref, g_param_spec_ref>;
213 : :
214 : : using AutoGClosure =
215 : 298 : AutoPointer<GClosure, GClosure, g_closure_unref, g_closure_ref>;
216 : :
217 : : template <typename V>
218 : 59865 : constexpr void AutoPointerDeleter(auto v) {
219 : : if constexpr (std::is_array_v<V>)
220 [ + - ]: 74663 : delete[] reinterpret_cast<std::remove_extent_t<V>*>(v);
221 : : else
222 [ + - ]: 44768 : delete v;
223 : 59865 : }
224 : :
225 : : template <typename T>
226 : 59865 : using AutoCppPointer = AutoPointer<T, T, AutoPointerDeleter<T>>;
227 : :
228 : : template <typename T = GTypeClass>
229 : 1209 : class AutoTypeClass : public AutoPointer<T, void, &g_type_class_unref> {
230 : 1209 : explicit AutoTypeClass(void* ptr = nullptr)
231 : 1209 : : AutoPointer<T, void, g_type_class_unref>(static_cast<T*>(ptr)) {}
232 : :
233 : : public:
234 : 1209 : explicit AutoTypeClass(GType gtype)
235 : 1209 : : AutoTypeClass(g_type_class_ref(gtype)) {}
236 : : };
237 : :
238 : : template <typename T>
239 : : struct SmartPointer : AutoPointer<T> {
240 : : using AutoPointer<T>::AutoPointer;
241 : : };
242 : :
243 : : template <>
244 : : struct SmartPointer<char*> : AutoStrv {
245 : : using AutoStrv::AutoPointer;
246 : : };
247 : :
248 : : template <>
249 : : struct SmartPointer<GStrv> : AutoStrv {
250 : : using AutoStrv::AutoPointer;
251 : : };
252 : :
253 : : template <>
254 : : struct SmartPointer<GObject> : AutoUnref<GObject> {
255 : : using AutoUnref<GObject>::AutoPointer;
256 : : };
257 : :
258 : : template <>
259 : : struct SmartPointer<GVariant> : AutoGVariant {
260 : : using AutoGVariant::AutoPointer;
261 : : };
262 : :
263 : : template <>
264 : 81 : struct SmartPointer<GList> : AutoPointer<GList, GList, g_list_free> {
265 : : using AutoPointer::AutoPointer;
266 : : };
267 : :
268 : : template <>
269 : 46 : struct SmartPointer<GSList> : AutoPointer<GSList, GSList, g_slist_free> {
270 : : using AutoPointer::AutoPointer;
271 : : };
272 : :
273 : : } // namespace Gjs
274 : :
275 : : template <>
276 : : struct std::formatter<Gjs::AutoChar> : std::formatter<const char*> {
277 : 876 : auto format(const Gjs::AutoChar& str, std::format_context& cx) const {
278 [ + - ]: 876 : return formatter<const char*>::format(str ? str.get() : "(null)", cx);
279 : : }
280 : : };
|