fixes compile
[platform/upstream/ibus-libpinyin.git] / src / PYPointer.h
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21 #ifndef __PY_POINTER_H_
22 #define __PY_POINTER_H_
23
24 #include <glib-object.h>
25
26 namespace PY {
27
28 template<typename T>
29 struct Pointer {
30 public:
31     Pointer (T *p = NULL) : m_p (NULL)
32     {
33         set (p);
34     }
35
36     ~Pointer (void)
37     {
38         set (NULL);
39     }
40
41     void set (T * p)
42     {
43         if (m_p) {
44             g_object_unref (m_p);
45         }
46
47         m_p = p;
48         if (p) {
49         #if 0
50             g_debug ("%s, floating = %d",G_OBJECT_TYPE_NAME (p), g_object_is_floating (p));
51         #endif
52             g_object_ref_sink (p);
53         }
54     }
55
56     Pointer<T> &operator = (T *p)
57     {
58         set (p);
59         return *this;
60     }
61
62     Pointer<T> &operator = (const Pointer<T> & p)
63     {
64         set (p.m_p);
65         return *this;
66     }
67
68     const T * operator-> (void) const
69     {
70         return m_p;
71     }
72
73     T * operator-> (void)
74     {
75         return m_p;
76     }
77
78     operator T * (void) const
79     {
80         return m_p;
81     }
82
83     operator gboolean (void) const
84     {
85         return m_p != NULL;
86     }
87
88 private:
89     T *m_p;
90 };
91
92 };
93
94 #endif