summaryrefslogtreecommitdiff
path: root/lib/superptr.hpp
blob: 2c9356d93cc02b3fcbde5be1df899aa0f84de435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef SUPERPTR_H_
#define SUPERPTR_H_

#include <glib.h>
#include <gio/gio.h>

#include <memory>

namespace amb {

template<typename T> struct traits;

template<>
struct traits<GVariant> {
  struct delete_functor {
	void operator()(GVariant * p) const {
	  if (p != nullptr)
		g_variant_unref(p);
	}
  };
};

template<>
struct traits<GError> {
  struct delete_functor {
	void operator()(GError * p) const {
	  if (p != nullptr)
		g_error_free(p);
	}
  };
};

template<>
struct traits<GDBusProxy> {
  struct delete_functor {
	void operator()(GDBusProxy * p) const {
	  if (p != nullptr)
		g_object_unref(p);
	}
  };
};

template<>
struct traits<GVariantIter> {
  struct delete_functor {
	void operator()(GVariantIter * p) const {
	  if (p != nullptr)
		g_variant_iter_free(p);
	}
  };
};

template<>
struct traits<gchar> {
  struct delete_functor {
	void operator()(gchar * p) const {
	  if (p != nullptr)
		g_free(p);
	}
  };
};

template <>
struct traits<GDBusConnection> {
  struct delete_functor {
	void operator()(GDBusConnection* p) const {
	  if (p != nullptr)
		g_dbus_connection_close_sync(p, nullptr, nullptr);
	}
  };
};

template<typename T> using super_ptr =
		 ::std::unique_ptr<T, typename traits<T>::delete_functor>;

template<typename T> using gobject_ptr =
		 ::std::unique_ptr<T , std::function<void(T*)> >;

template<typename T> super_ptr<T> make_super(T* t)
{
  return super_ptr<T>(t);
}

template<typename T> ::std::unique_ptr<T> make_unique(T* t)
{
  return ::std::unique_ptr<T>(t);
}

template<typename T> gobject_ptr<T> make_gobject(T* t)
{
	return gobject_ptr<T>(t, [](T* ptr) { if(ptr) g_object_unref(ptr);});
}

}
#endif