1 /* a managed gobject
2  */
3 
4 /*
5 
6     Copyright (C) 1991-2003 The National Gallery
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 
22  */
23 
24 /*
25 
26     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
27 
28  */
29 
30 #include "ip.h"
31 
32 /*
33 #define DEBUG
34  */
35 
36 static ManagedClass *parent_class = NULL;
37 
38 static void
managedgobject_dispose(GObject * gobject)39 managedgobject_dispose( GObject *gobject )
40 {
41 	Managedgobject *managedgobject = MANAGEDGOBJECT( gobject );
42 
43 #ifdef DEBUG
44 	printf( "managedgobject_dispose: " );
45 	iobject_print( IOBJECT( managedgobject ) );
46 #endif /*DEBUG*/
47 
48 	IM_FREEF( g_object_unref, managedgobject->object );
49 
50 	G_OBJECT_CLASS( parent_class )->dispose( gobject );
51 }
52 
53 static void
managedgobject_info(iObject * iobject,VipsBuf * buf)54 managedgobject_info( iObject *iobject, VipsBuf *buf )
55 {
56 	Managedgobject *managedgobject = MANAGEDGOBJECT( iobject );
57 
58 	if( VIPS_IS_OBJECT( managedgobject->object ) )
59 		vips_object_summary( VIPS_OBJECT( managedgobject->object ),
60 			buf );
61 	else
62 		IOBJECT_CLASS( parent_class )->info( iobject, buf );
63 }
64 
65 
66 static void
managedgobject_class_init(ManagedgobjectClass * class)67 managedgobject_class_init( ManagedgobjectClass *class )
68 {
69 	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
70 	iObjectClass *iobject_class = IOBJECT_CLASS( class );
71 
72 	parent_class = g_type_class_peek_parent( class );
73 
74 	gobject_class->dispose = managedgobject_dispose;
75 
76 	iobject_class->info = managedgobject_info;
77 }
78 
79 static void
managedgobject_init(Managedgobject * managedgobject)80 managedgobject_init( Managedgobject *managedgobject )
81 {
82 #ifdef DEBUG
83 	printf( "managedgobject_init: %p\n", managedgobject );
84 #endif /*DEBUG*/
85 
86 	managedgobject->object = NULL;
87 }
88 
89 GType
managedgobject_get_type(void)90 managedgobject_get_type( void )
91 {
92 	static GType type = 0;
93 
94 	if( !type ) {
95 		static const GTypeInfo info = {
96 			sizeof( ManagedgobjectClass ),
97 			NULL,           /* base_init */
98 			NULL,           /* base_finalize */
99 			(GClassInitFunc) managedgobject_class_init,
100 			NULL,           /* class_finalize */
101 			NULL,           /* class_data */
102 			sizeof( Managedgobject ),
103 			32,             /* n_preallocs */
104 			(GInstanceInitFunc) managedgobject_init,
105 		};
106 
107 		type = g_type_register_static( TYPE_MANAGED,
108 			"Managedgobject", &info, 0 );
109 	}
110 
111 	return( type );
112 }
113 
114 Managedgobject *
managedgobject_new(Heap * heap,GObject * object)115 managedgobject_new( Heap *heap, GObject *object )
116 {
117 	Managedgobject *managedgobject =
118 		g_object_new( TYPE_MANAGEDGOBJECT, NULL );
119 
120 	managed_link_heap( MANAGED( managedgobject ), heap );
121 	managedgobject->object = object;
122 	g_object_ref( object );
123 
124 	MANAGED( managedgobject )->hash = GPOINTER_TO_UINT( object );
125 
126 	return( managedgobject );
127 }
128