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