21 #ifndef LOMIRI_UTIL_GLIBMEMORY_H
22 #define LOMIRI_UTIL_GLIBMEMORY_H
28 #include <lomiri/util/ResourcePtr.h>
39 template<
typename T>
struct GlibDeleter;
40 template<
typename T>
using GlibSPtr = std::shared_ptr<T>;
41 template<
typename T>
using GlibUPtr = std::unique_ptr<T, GlibDeleter<T>>;
56 typedef typename SP::element_type ElementType;
58 GlibAssigner(SP& smart_ptr) noexcept :
63 GlibAssigner(
const GlibAssigner& other) =
delete;
65 GlibAssigner(GlibAssigner&& other) noexcept:
66 ptr_(other.ptr_), smart_ptr_(other.smart_ptr_)
71 ~GlibAssigner() noexcept
73 smart_ptr_ = SP(ptr_, GlibDeleter<ElementType>());
76 GlibAssigner& operator=(
const GlibAssigner& other) =
delete;
78 operator ElementType**() noexcept
84 ElementType* ptr_ =
nullptr;
89 struct GSourceUnsubscriber
91 void operator()(guint tag) noexcept
102 #define LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(TypeName, func) \
103 using TypeName##Deleter = internal::GlibDeleter<TypeName>; \
104 using TypeName##SPtr = internal::GlibSPtr<TypeName>; \
105 using TypeName##UPtr = internal::GlibUPtr<TypeName>; \
108 template<> struct GlibDeleter<TypeName> \
110 void operator()(TypeName* ptr) noexcept \
129 inline internal::GlibSPtr<T> share_glib(T* ptr) noexcept
131 return internal::GlibSPtr<T>(ptr, internal::GlibDeleter<T>());
143 inline internal::GlibUPtr<T> unique_glib(T* ptr) noexcept
145 return internal::GlibUPtr<T>(ptr, internal::GlibDeleter<T>());
167 template<
typename SP>
168 inline internal::GlibAssigner<SP> assign_glib(SP& smart_ptr) noexcept
170 return internal::GlibAssigner<SP>(smart_ptr);
173 using GSourceManager = ResourcePtr<guint, internal::GSourceUnsubscriber>;
183 inline GSourceManager g_source_manager(guint
id)
185 return GSourceManager(
id, internal::GSourceUnsubscriber());
193 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GAsyncQueue, g_async_queue_unref)
194 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GBookmarkFile, g_bookmark_file_free)
195 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GBytes, g_bytes_unref)
196 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GChecksum, g_checksum_free)
197 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GDateTime, g_date_time_unref)
198 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GDir, g_dir_close)
199 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GError, g_error_free)
200 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GHashTable, g_hash_table_unref)
201 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GHmac, g_hmac_unref)
202 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GIOChannel, g_io_channel_unref)
203 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GKeyFile, g_key_file_unref)
204 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GList, g_list_free)
205 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GArray, g_array_unref)
206 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GPtrArray, g_ptr_array_unref)
207 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GByteArray, g_byte_array_unref)
208 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GMainContext, g_main_context_unref)
209 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GMainLoop, g_main_loop_unref)
210 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GSource, g_source_unref)
211 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GMappedFile, g_mapped_file_unref)
212 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GMarkupParseContext, g_markup_parse_context_unref)
213 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GNode, g_node_destroy)
214 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GOptionContext, g_option_context_free)
215 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GOptionGroup, g_option_group_unref)
216 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GPatternSpec, g_pattern_spec_free)
217 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GQueue, g_queue_free)
218 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GRand, g_rand_free)
219 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GRegex, g_regex_unref)
220 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GMatchInfo, g_match_info_unref)
221 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GScanner, g_scanner_destroy)
222 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GSequence, g_sequence_free)
223 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GSList, g_slist_free)
224 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GString, g_autoptr_cleanup_gstring_free)
225 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GStringChunk, g_string_chunk_free)
226 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GThread, g_thread_unref)
227 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GMutex, g_mutex_clear)
228 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GCond, g_cond_clear)
229 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GTimer, g_timer_destroy)
230 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GTimeZone, g_time_zone_unref)
231 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GTree, g_tree_unref)
232 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariant, g_variant_unref)
233 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantBuilder, g_variant_builder_unref)
234 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantIter, g_variant_iter_free)
235 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantDict, g_variant_dict_unref)
236 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantType, g_variant_type_free)
241 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(gchar, g_free)
242 typedef gchar* gcharv;
243 LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(gcharv, g_strfreev)