Merge lp:~pete-woods/unity-api/glib-pointer-functions into lp:unity-api

Proposed by Pete Woods
Status: Merged
Approved by: Pete Woods
Approved revision: 266
Merged at revision: 266
Proposed branch: lp:~pete-woods/unity-api/glib-pointer-functions
Merge into: lp:unity-api
Prerequisite: lp:~pete-woods/unity-api/gobject-pointer-functions
Diff against target: 240 lines (+181/-0)
7 files modified
debian/changelog (+1/-0)
include/unity/util/GlibMemory.h (+58/-0)
test/gtest/unity/util/CMakeLists.txt (+1/-0)
test/gtest/unity/util/GlibMemory/CMakeLists.txt (+12/-0)
test/gtest/unity/util/GlibMemory/GlibMemory_test.cpp (+107/-0)
test/headers/CMakeLists.txt (+1/-0)
test/headers/includechecker.py (+1/-0)
To merge this branch: bzr merge lp:~pete-woods/unity-api/glib-pointer-functions
Reviewer Review Type Date Requested Status
Unity8 CI Bot continuous-integration Approve
Charles Kerr (community) Approve
Review via email: mp+314967@code.launchpad.net

Commit message

unity::shell::util - add Glib memory management utility functions.

Description of the change

unity::shell::util - add Glib memory management utility functions.

To post a comment you must log in.
Revision history for this message
Charles Kerr (charlesk) wrote :

LGTM.

review: Approve
Revision history for this message
Unity8 CI Bot (unity8-ci-bot) wrote :

PASSED: Continuous integration, rev:268
https://unity8-jenkins.ubuntu.com/job/lp-unity-api-ci/136/
Executed test runs:
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build/3830
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-0-fetch/3858
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=amd64,release=xenial+overlay/3701
        deb: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=amd64,release=xenial+overlay/3701/artifact/output/*zip*/output.zip
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=amd64,release=zesty/3701
        deb: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=amd64,release=zesty/3701/artifact/output/*zip*/output.zip
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=armhf,release=xenial+overlay/3701
        deb: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=armhf,release=xenial+overlay/3701/artifact/output/*zip*/output.zip
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=armhf,release=zesty/3701
        deb: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=armhf,release=zesty/3701/artifact/output/*zip*/output.zip
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=i386,release=xenial+overlay/3701
        deb: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=i386,release=xenial+overlay/3701/artifact/output/*zip*/output.zip
    SUCCESS: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=i386,release=zesty/3701
        deb: https://unity8-jenkins.ubuntu.com/job/build-2-binpkg/arch=i386,release=zesty/3701/artifact/output/*zip*/output.zip

Click here to trigger a rebuild:
https://unity8-jenkins.ubuntu.com/job/lp-unity-api-ci/136/rebuild

review: Approve (continuous-integration)
265. By Pete Woods

unity::util - add GObject shared memory utility classes and helper methods.

266. By Pete Woods

unity::util - add Glib memory management utility functions.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/changelog'
2--- debian/changelog 2017-01-19 13:53:20 +0000
3+++ debian/changelog 2017-01-19 13:53:20 +0000
4@@ -2,6 +2,7 @@
5
6 * unity::util - add GObject shared memory utility classes
7 and helper methods.
8+ * unity::util - add Glib memory management utility functions.
9
10 -- Pete <pete.woods@canonical.com> Wed, 11 Jan 2017 16:07:23 +0000
11
12
13=== added file 'include/unity/util/GlibMemory.h'
14--- include/unity/util/GlibMemory.h 1970-01-01 00:00:00 +0000
15+++ include/unity/util/GlibMemory.h 2017-01-19 13:53:20 +0000
16@@ -0,0 +1,58 @@
17+/*
18+ * Copyright (C) 2017 Canonical Ltd.
19+ *
20+ * This program is free software: you can redistribute it and/or modify
21+ * it under the terms of the GNU Lesser General Public License version 3 as
22+ * published by the Free Software Foundation.
23+ *
24+ * This program is distributed in the hope that it will be useful,
25+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+ * GNU Lesser General Public License for more details.
28+ *
29+ * You should have received a copy of the GNU Lesser General Public License
30+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31+ *
32+ * Authored by: Pete Woods <pete.woods@canonical.com>
33+ */
34+
35+#ifndef UNITY_UTIL_GLIBMEMORY_H
36+#define UNITY_UTIL_GLIBMEMORY_H
37+
38+#include <memory>
39+#include <glib.h>
40+
41+namespace unity
42+{
43+
44+namespace util
45+{
46+
47+#pragma push_macro("G_DEFINE_AUTOPTR_CLEANUP_FUNC")
48+#undef G_DEFINE_AUTOPTR_CLEANUP_FUNC
49+#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
50+inline std::shared_ptr<TypeName> share_glib(TypeName* ptr) \
51+{ \
52+ return std::shared_ptr<TypeName>(ptr, &func); \
53+} \
54+inline std::unique_ptr<TypeName, decltype(&func)> unique_glib(TypeName* ptr) \
55+{ \
56+ return std::unique_ptr<TypeName, decltype(&func)>(ptr, &func); \
57+}
58+
59+#pragma push_macro("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC")
60+#undef G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC
61+#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
62+
63+#define __GLIB_H_INSIDE__
64+#include <glib/glib-autocleanups.h>
65+#undef __GLIB_H_INSIDE__
66+
67+#pragma pop_macro("G_DEFINE_AUTOPTR_CLEANUP_FUNC")
68+#pragma pop_macro("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC")
69+
70+} // namespace until
71+
72+} // namespace unity
73+
74+#endif
75
76=== modified file 'test/gtest/unity/util/CMakeLists.txt'
77--- test/gtest/unity/util/CMakeLists.txt 2017-01-19 13:53:20 +0000
78+++ test/gtest/unity/util/CMakeLists.txt 2017-01-19 13:53:20 +0000
79@@ -1,6 +1,7 @@
80 add_subdirectory(Daemon)
81 add_subdirectory(DefinesPtrs)
82 add_subdirectory(FileIO)
83+add_subdirectory(GlibMemory)
84 add_subdirectory(GObjectMemory)
85 add_subdirectory(IniParser)
86 add_subdirectory(ResourcePtr)
87
88=== added directory 'test/gtest/unity/util/GlibMemory'
89=== added file 'test/gtest/unity/util/GlibMemory/CMakeLists.txt'
90--- test/gtest/unity/util/GlibMemory/CMakeLists.txt 1970-01-01 00:00:00 +0000
91+++ test/gtest/unity/util/GlibMemory/CMakeLists.txt 2017-01-19 13:53:20 +0000
92@@ -0,0 +1,12 @@
93+include_directories(${GLIB_INCLUDE_DIRS})
94+
95+add_executable(GlibMemory_test
96+ GlibMemory_test.cpp
97+ )
98+
99+target_link_libraries(GlibMemory_test
100+ ${TESTLIBS}
101+ ${GLIB_LDFLAGS}
102+ )
103+
104+add_test(GlibMemory_test GlibMemory_test)
105
106=== added file 'test/gtest/unity/util/GlibMemory/GlibMemory_test.cpp'
107--- test/gtest/unity/util/GlibMemory/GlibMemory_test.cpp 1970-01-01 00:00:00 +0000
108+++ test/gtest/unity/util/GlibMemory/GlibMemory_test.cpp 2017-01-19 13:53:20 +0000
109@@ -0,0 +1,107 @@
110+/*
111+ * Copyright (C) 2017 Canonical Ltd
112+ *
113+ * This program is free software: you can redistribute it and/or modify
114+ * it under the terms of the GNU Lesser General Public License version 3 as
115+ * published by the Free Software Foundation.
116+ *
117+ * This program is distributed in the hope that it will be useful,
118+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
119+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
120+ * GNU Lesser General Public License for more details.
121+ *
122+ * You should have received a copy of the GNU Lesser General Public License
123+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
124+ *
125+ * Authored by: Pete Woods <pete.woods@canonical.com>
126+ */
127+
128+#include <unity/util/GlibMemory.h>
129+
130+#include <gtest/gtest.h>
131+#include <iostream>
132+#include <set>
133+#include <string>
134+
135+using namespace std;
136+using namespace unity;
137+using namespace unity::util;
138+
139+namespace
140+{
141+
142+static set<string> keys;
143+
144+static set<string> values;
145+
146+class GlibMemoryTest: public testing::Test
147+{
148+protected:
149+ void SetUp() override
150+ {
151+ keys.clear();
152+ values.clear();
153+ }
154+
155+ static void deleteKey(gpointer data)
156+ {
157+ // Note that we don't g_free the data, because it's a reference to a static string
158+ keys.emplace(string((gchar*) data));
159+ }
160+
161+ static void deleteValue(gpointer data)
162+ {
163+ // Note that we don't g_free the data, because it's a reference to a static string
164+ values.emplace(string((gchar*) data));
165+ }
166+};
167+
168+TEST_F(GlibMemoryTest, Unique)
169+{
170+ {
171+ auto gkf = unique_glib(g_key_file_new());
172+ g_key_file_set_boolean(gkf.get(), "group", "key", TRUE);
173+ EXPECT_EQ(TRUE, g_key_file_get_boolean(gkf.get(), "group", "key", NULL));
174+ }
175+
176+ {
177+ auto variant = unique_glib(g_variant_new_string("hello"));
178+ EXPECT_STREQ("hello", g_variant_get_string(variant.get(), NULL));
179+ }
180+
181+ {
182+ {
183+ auto hash = unique_glib(g_hash_table_new_full(g_str_hash, g_str_equal, deleteKey, deleteValue));
184+ g_hash_table_insert(hash.get(), (gpointer) "hello", (gpointer) "there");
185+ g_hash_table_insert(hash.get(), (gpointer) "hash", (gpointer) "world");
186+ }
187+ EXPECT_EQ(set<string>({"hello", "hash"}), keys);
188+ EXPECT_EQ(set<string>({"there", "world"}), values);
189+ }
190+}
191+
192+TEST_F(GlibMemoryTest, Share)
193+{
194+ {
195+ auto gkf = share_glib(g_key_file_new());
196+ g_key_file_set_boolean(gkf.get(), "group", "key", TRUE);
197+ EXPECT_EQ(TRUE, g_key_file_get_boolean(gkf.get(), "group", "key", NULL));
198+ }
199+
200+ {
201+ auto variant = share_glib(g_variant_new_string("hello"));
202+ EXPECT_STREQ("hello", g_variant_get_string(variant.get(), NULL));
203+ }
204+
205+ {
206+ {
207+ auto hash = share_glib(g_hash_table_new_full(g_str_hash, g_str_equal, deleteKey, deleteValue));
208+ g_hash_table_insert(hash.get(), (gpointer) "hello", (gpointer) "there");
209+ g_hash_table_insert(hash.get(), (gpointer) "hash", (gpointer) "world");
210+ }
211+ EXPECT_EQ(set<string>({"hello", "hash"}), keys);
212+ EXPECT_EQ(set<string>({"there", "world"}), values);
213+ }
214+}
215+
216+}
217
218=== modified file 'test/headers/CMakeLists.txt'
219--- test/headers/CMakeLists.txt 2017-01-19 13:53:20 +0000
220+++ test/headers/CMakeLists.txt 2017-01-19 13:53:20 +0000
221@@ -11,6 +11,7 @@
222 )
223
224 set(exclusions
225+ "GlibMemory.h"
226 "GObjectMemory.h"
227 )
228
229
230=== modified file 'test/headers/includechecker.py'
231--- test/headers/includechecker.py 2017-01-19 13:53:20 +0000
232+++ test/headers/includechecker.py 2017-01-19 13:53:20 +0000
233@@ -37,6 +37,7 @@
234 allowed = {
235 'unity/shell': { 'Qt' }, # Anything under unity/shell can include anything starting with Qt
236 'unity/util/GObjectMemory': { 'glib' }, # The unity/util/GObjectMemory header can include anything starting with glib
237+ 'unity/util/GlibMemory': { 'glib' }, # The unity/util/GlibMemory header can include anything starting with glib
238 }
239
240 def check_file(filename, permitted_includes):

Subscribers

People subscribed via source and target branches

to all changes: