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 UNITY_UTIL_GLIBMEMORY_H
22 #define UNITY_UTIL_GLIBMEMORY_H
23 
24 #include <memory>
25 
26 #include <glib.h>
27 
28 #include <unity/util/ResourcePtr.h>
29 
30 namespace unity
31 {
32 
33 namespace util
34 {
35 
36 namespace internal
37 {
38 
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>>;
42 
52 template<typename SP>
53 class GlibAssigner
54 {
55 public:
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 
83 private:
84  ElementType* ptr_ = nullptr;
85 
86  SP& smart_ptr_;
87 };
88 
89 struct 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 UNITY_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>; \
106 namespace internal \
107 { \
108 template<> struct GlibDeleter<TypeName> \
109 { \
110  void operator()(TypeName* ptr) noexcept \
111  { \
112  if (ptr) \
113  { \
114  ::func(ptr); \
115  } \
116  } \
117 }; \
118 }
119 
128 template<typename T>
129 inline internal::GlibSPtr<T> share_glib(T* ptr) noexcept
130 {
131  return internal::GlibSPtr<T>(ptr, internal::GlibDeleter<T>());
132 }
133 
142 template<typename T>
143 inline internal::GlibUPtr<T> unique_glib(T* ptr) noexcept
144 {
145  return internal::GlibUPtr<T>(ptr, internal::GlibDeleter<T>());
146 }
147 
167 template<typename SP>
168 inline internal::GlibAssigner<SP> assign_glib(SP& smart_ptr) noexcept
169 {
170  return internal::GlibAssigner<SP>(smart_ptr);
171 }
172 
173 using GSourceManager = ResourcePtr<guint, internal::GSourceUnsubscriber>;
174 
183 inline GSourceManager g_source_manager(guint id)
184 {
185  return GSourceManager(id, internal::GSourceUnsubscriber());
186 }
187 
193 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GAsyncQueue, g_async_queue_unref)
194 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GBookmarkFile, g_bookmark_file_free)
195 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GBytes, g_bytes_unref)
196 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GChecksum, g_checksum_free)
197 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GDateTime, g_date_time_unref)
198 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GDir, g_dir_close)
199 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GError, g_error_free)
200 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GHashTable, g_hash_table_unref)
201 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GHmac, g_hmac_unref)
202 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GIOChannel, g_io_channel_unref)
203 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GKeyFile, g_key_file_unref)
204 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GList, g_list_free)
205 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GArray, g_array_unref)
206 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GPtrArray, g_ptr_array_unref)
207 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GByteArray, g_byte_array_unref)
208 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GMainContext, g_main_context_unref)
209 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GMainLoop, g_main_loop_unref)
210 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GSource, g_source_unref)
211 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GMappedFile, g_mapped_file_unref)
212 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GMarkupParseContext, g_markup_parse_context_unref)
213 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GNode, g_node_destroy)
214 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GOptionContext, g_option_context_free)
215 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GOptionGroup, g_option_group_unref)
216 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GPatternSpec, g_pattern_spec_free)
217 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GQueue, g_queue_free)
218 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GRand, g_rand_free)
219 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GRegex, g_regex_unref)
220 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GMatchInfo, g_match_info_unref)
221 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GScanner, g_scanner_destroy)
222 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GSequence, g_sequence_free)
223 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GSList, g_slist_free)
224 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GString, g_autoptr_cleanup_gstring_free)
225 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GStringChunk, g_string_chunk_free)
226 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GThread, g_thread_unref)
227 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GMutex, g_mutex_clear)
228 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GCond, g_cond_clear)
229 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GTimer, g_timer_destroy)
230 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GTimeZone, g_time_zone_unref)
231 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GTree, g_tree_unref)
232 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariant, g_variant_unref)
233 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantBuilder, g_variant_builder_unref)
234 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantIter, g_variant_iter_free)
235 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantDict, g_variant_dict_unref)
236 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GVariantType, g_variant_type_free)
237 #if GLIB_CHECK_VERSION(2, 58, 0)
238 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(GRefString, g_ref_string_release)
239 #endif
240 
244 #if GLIB_CHECK_VERSION(2, 57, 2)
245 typedef GRefStringSPtr gcharSPtr;
246 typedef GRefStringUPtr gcharUPtr;
247 #else
248 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(gchar, g_free)
249 #endif
250 typedef gchar* gcharv;
251 UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(gcharv, g_strfreev)
252 
253 } // namespace until
254 
255 } // namespace unity
256 
257 #endif
Top-level namespace for all things Unity-related.
Definition: Version.h:37