~unity-team/unity-api/trunk

« back to all changes in this revision

Viewing changes to include/unity/util/GlibMemory.h

  • Committer: Bileto Bot
  • Date: 2017-01-19 13:54:21 UTC
  • mfrom: (264.1.2 trunk)
  • Revision ID: ci-train-bot@canonical.com-20170119135421-po3yg9p4j50vux8l
unity::util - add Glib memory management utility functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
18
 
 
19
#ifndef UNITY_UTIL_GLIBMEMORY_H
 
20
#define UNITY_UTIL_GLIBMEMORY_H
 
21
 
 
22
#include <memory>
 
23
#include <glib.h>
 
24
 
 
25
namespace unity
 
26
{
 
27
 
 
28
namespace util
 
29
{
 
30
 
 
31
#pragma push_macro("G_DEFINE_AUTOPTR_CLEANUP_FUNC")
 
32
#undef G_DEFINE_AUTOPTR_CLEANUP_FUNC
 
33
#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
 
34
inline std::shared_ptr<TypeName> share_glib(TypeName* ptr) \
 
35
{ \
 
36
    return std::shared_ptr<TypeName>(ptr, &func); \
 
37
} \
 
38
inline std::unique_ptr<TypeName, decltype(&func)> unique_glib(TypeName* ptr) \
 
39
{ \
 
40
    return std::unique_ptr<TypeName, decltype(&func)>(ptr, &func); \
 
41
}
 
42
 
 
43
#pragma push_macro("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC")
 
44
#undef G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC
 
45
#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
 
46
 
 
47
#define __GLIB_H_INSIDE__
 
48
#include <glib/glib-autocleanups.h>
 
49
#undef __GLIB_H_INSIDE__
 
50
 
 
51
#pragma pop_macro("G_DEFINE_AUTOPTR_CLEANUP_FUNC")
 
52
#pragma pop_macro("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC")
 
53
 
 
54
}  // namespace until
 
55
 
 
56
}  // namespace unity
 
57
 
 
58
#endif