summaryrefslogtreecommitdiff
path: root/tests/test-floating.c
blob: 8e8ba5de2675ed76074e7db7b837ae0aa686156c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * test-floating.c - Source for TestFloatingWithSinkFunc and TestFloatingWithoutSinkFunc
 * Copyright (C) 2010 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "test-floating.h"

/* TestFloatingWithSinkFunc */

G_DEFINE_TYPE(TestFloatingWithSinkFunc, test_floating_with_sink_func, G_TYPE_INITIALLY_UNOWNED)

static void
test_floating_with_sink_func_finalize (GObject *gobject)
{
  TestFloatingWithSinkFunc *object = TEST_FLOATING_WITH_SINK_FUNC (gobject);

  if (g_object_is_floating (object))
    {
      g_warning ("A floating object was finalized. This means that someone\n"
		 "called g_object_unref() on an object that had only a floating\n"
		 "reference; the initial floating reference is not owned by anyone\n"
		 "and must be removed with g_object_ref_sink().");
    }
  
  G_OBJECT_CLASS (test_floating_with_sink_func_parent_class)->finalize (gobject);
}

static void
test_floating_with_sink_func_class_init (TestFloatingWithSinkFuncClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = test_floating_with_sink_func_finalize;
}

static void
test_floating_with_sink_func_init (TestFloatingWithSinkFunc *self)
{
}

void
sink_test_floating_with_sink_func (GObject *object)
{
    if (g_object_is_floating(object)) {
	    g_object_ref_sink(object);
    }
}

/* TestFloatingWithoutSinkFunc */

G_DEFINE_TYPE(TestFloatingWithoutSinkFunc, test_floating_without_sink_func, G_TYPE_INITIALLY_UNOWNED)

static void
test_floating_without_sink_func_finalize (GObject *gobject)
{
  TestFloatingWithoutSinkFunc *object = TEST_FLOATING_WITHOUT_SINK_FUNC (gobject);

  if (g_object_is_floating (object))
    {
      g_warning ("A floating object was finalized. This means that someone\n"
		 "called g_object_unref() on an object that had only a floating\n"
		 "reference; the initial floating reference is not owned by anyone\n"
		 "and must be removed without g_object_ref_sink().");
    }

  G_OBJECT_CLASS (test_floating_without_sink_func_parent_class)->finalize (gobject);
}

static void
test_floating_without_sink_func_class_init (TestFloatingWithoutSinkFuncClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = test_floating_without_sink_func_finalize;
}

static void
test_floating_without_sink_func_init (TestFloatingWithoutSinkFunc *self)
{
}

/* TestOwnedByLibrary */

G_DEFINE_TYPE(TestOwnedByLibrary, test_owned_by_library, G_TYPE_OBJECT)

static GSList *obl_instance_list = NULL;

static void
test_owned_by_library_class_init (TestOwnedByLibraryClass *klass)
{
}

static void
test_owned_by_library_init (TestOwnedByLibrary *self)
{
    g_object_ref (self);
    obl_instance_list = g_slist_prepend (obl_instance_list, self);
}

void
test_owned_by_library_release (TestOwnedByLibrary *self)
{
    obl_instance_list = g_slist_remove (obl_instance_list, self);
    g_object_unref (self);
}

GSList *
test_owned_by_library_get_instance_list (void)
{
    return obl_instance_list;
}

/* TestFloatingAndSunk
 * This object is mimicking the GtkWindow behaviour, ie a GInitiallyUnowned subclass
 * whose floating reference has already been sunk when g_object_new() returns it.
 * The reference is already sunk because the instance is already owned by the instance
 * list.
 */

G_DEFINE_TYPE(TestFloatingAndSunk, test_floating_and_sunk, G_TYPE_INITIALLY_UNOWNED)

static GSList *fas_instance_list = NULL;

static void
test_floating_and_sunk_class_init (TestFloatingAndSunkClass *klass)
{
}

static void
test_floating_and_sunk_init (TestFloatingAndSunk *self)
{
    g_object_ref_sink (self);
    fas_instance_list = g_slist_prepend (fas_instance_list, self);
}

void
test_floating_and_sunk_release (TestFloatingAndSunk *self)
{
    fas_instance_list = g_slist_remove (fas_instance_list, self);
    g_object_unref (self);
}

GSList *
test_floating_and_sunk_get_instance_list (void)
{
    return fas_instance_list;
}