My Project
GlibMemory.h
1/*
2 * Copyright (C) 2017 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Pete Woods <pete.woods@canonical.com>
17 * Michi Henning <michi.henning@canonical.com>
18 * James Henstridge <james.henstridge@canonical.com>
19 */
20
21#ifndef LOMIRI_UTIL_GLIBMEMORY_H
22#define LOMIRI_UTIL_GLIBMEMORY_H
23
24#include <memory>
25
26#include <glib.h>
27
28#include <lomiri/util/ResourcePtr.h>
29
30namespace lomiri
31{
32
33namespace util
34{
35
36namespace internal
37{
38
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>>;
42
52template<typename SP>
53class GlibAssigner
54{
55public:
56 typedef typename SP::element_type ElementType;
57
58 GlibAssigner(SP& smart_ptr) noexcept :
59 smart_ptr_(smart_ptr)
60 {
61 }
62
63 GlibAssigner(const GlibAssigner& other) = delete;
64
65 GlibAssigner(GlibAssigner&& other) noexcept:
66 ptr_(other.ptr_), smart_ptr_(other.smart_ptr_)
67 {
68 other.ptr_ = nullptr;
69 }
70
71 ~GlibAssigner() noexcept
72 {
73 smart_ptr_ = SP(ptr_, GlibDeleter<ElementType>());
74 }
75
76 GlibAssigner& operator=(const GlibAssigner& other) = delete;
77
78 operator ElementType**() noexcept
79 {
80 return &ptr_;
81 }
82
83private:
84 ElementType* ptr_ = nullptr;
85
86 SP& smart_ptr_;
87};
88
89struct GSourceUnsubscriber
90{
91 void operator()(guint tag) noexcept
92 {
93 if (tag != 0)
94 {
95 g_source_remove(tag);
96 }
97 }
98};
99
100}
101
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>; \
106namespace internal \
107{ \
108template<> struct GlibDeleter<TypeName> \
109{ \
110 void operator()(TypeName* ptr) noexcept \
111 { \
112 if (ptr) \
113 { \
114 ::func(ptr); \
115 } \
116 } \
117}; \
118}
119
128template<typename T>
129inline internal::GlibSPtr<T> share_glib(T* ptr) noexcept
130{
131 return internal::GlibSPtr<T>(ptr, internal::GlibDeleter<T>());
132}
133
142template<typename T>
143inline internal::GlibUPtr<T> unique_glib(T* ptr) noexcept
144{
145 return internal::GlibUPtr<T>(ptr, internal::GlibDeleter<T>());
146}
147
167template<typename SP>
168inline internal::GlibAssigner<SP> assign_glib(SP& smart_ptr) noexcept
169{
170 return internal::GlibAssigner<SP>(smart_ptr);
171}
172
173using GSourceManager = ResourcePtr<guint, internal::GSourceUnsubscriber>;
174
183inline GSourceManager g_source_manager(guint id)
184{
185 return GSourceManager(id, internal::GSourceUnsubscriber());
186}
187
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)
237
238
241LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(gchar, g_free)
242typedef gchar* gcharv;
243LOMIRI_UTIL_DEFINE_GLIB_SMART_POINTERS(gcharv, g_strfreev)
244
245} // namespace until
246
247} // namespace lomiri
248
249#endif
Top-level namespace for all things Lomiri-related.
Definition: Version.h:38