libvips/libsrc/iofuncs/object.c

198 lines
4.4 KiB
C
Raw Normal View History

2008-10-15 22:09:22 +02:00
/* abstract base class for all vips objects
*
* Edited from nip's base class, 15/10/08
*/
/*
Copyright (C) 1991-2003 The National Gallery
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
2008-10-20 23:42:46 +02:00
#include <string.h>
2008-10-15 22:09:22 +02:00
#include <vips/vips.h>
#include <vips/internal.h>
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif /*WITH_DMALLOC*/
/* Our signals.
*/
enum {
2008-10-16 16:08:12 +02:00
SIG_CHANGED, /* VipsObject has changed somehow */
2008-10-15 22:09:22 +02:00
SIG_LAST
};
2008-10-16 16:08:12 +02:00
static guint vips_object_signals[SIG_LAST] = { 0 };
2008-10-15 22:09:22 +02:00
2008-11-28 23:38:40 +01:00
G_DEFINE_ABSTRACT_TYPE( VipsObject, vips_object, G_TYPE_OBJECT );
2008-10-15 22:09:22 +02:00
void *
2008-10-20 19:10:40 +02:00
vips_object_changed( VipsObject *object )
2008-10-15 22:09:22 +02:00
{
2008-10-20 19:10:40 +02:00
g_return_val_if_fail( object != NULL, NULL );
g_return_val_if_fail( VIPS_IS_OBJECT( object ), NULL );
2008-10-15 22:09:22 +02:00
#ifdef DEBUG
2008-10-16 16:08:12 +02:00
printf( "vips_object_changed: " );
2008-10-20 19:10:40 +02:00
vips_object_print( object );
2008-10-15 22:09:22 +02:00
#endif /*DEBUG*/
2008-10-20 19:10:40 +02:00
g_signal_emit( G_OBJECT( object ),
2008-10-16 16:08:12 +02:00
vips_object_signals[SIG_CHANGED], 0 );
2008-10-15 22:09:22 +02:00
return( NULL );
}
2008-11-30 14:01:48 +01:00
void
vips_object_print_class( VipsObjectClass *class )
{
im_buf_t buf;
char str[1000];
im_buf_init_static( &buf, str, 1000 );
class->print_class( class, &buf );
printf( "%s\n", im_buf_all( &buf ) );
}
2008-10-20 19:10:40 +02:00
void
vips_object_print( VipsObject *object )
{
2008-11-30 14:01:48 +01:00
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
2008-10-20 23:42:46 +02:00
im_buf_t buf;
char str[1000];
2008-11-30 14:01:48 +01:00
vips_object_print_class( class );
2008-10-20 23:42:46 +02:00
im_buf_init_static( &buf, str, 1000 );
2008-11-30 14:01:48 +01:00
class->print( object, &buf );
printf( "\n%s (%p)\n", im_buf_all( &buf ), object );
2008-10-20 19:10:40 +02:00
}
2008-10-15 22:09:22 +02:00
static void
2008-10-16 16:08:12 +02:00
vips_object_dispose( GObject *gobject )
2008-10-15 22:09:22 +02:00
{
#ifdef DEBUG
2008-10-20 19:10:40 +02:00
VipsObject *object = VIPS_OBJECT( gobject );
2008-10-16 16:08:12 +02:00
printf( "vips_object_dispose: " );
2008-10-20 19:10:40 +02:00
vips_object_print( object );
2008-10-15 22:09:22 +02:00
#endif /*DEBUG*/
2008-11-28 23:38:40 +01:00
G_OBJECT_CLASS( vips_object_parent_class )->dispose( gobject );
2008-10-15 22:09:22 +02:00
}
static void
2008-10-16 16:08:12 +02:00
vips_object_finalize( GObject *gobject )
2008-10-15 22:09:22 +02:00
{
2008-10-20 19:10:40 +02:00
VipsObject *object = VIPS_OBJECT( gobject );
2008-10-15 22:09:22 +02:00
2008-10-20 23:42:46 +02:00
#ifdef DEBUG
2008-10-16 16:08:12 +02:00
printf( "vips_object_finalize: " );
2008-10-20 19:10:40 +02:00
vips_object_print( object );
2008-10-15 22:09:22 +02:00
#endif /*DEBUG*/
2008-10-20 23:42:46 +02:00
IM_FREE( object->name );
2008-10-15 22:09:22 +02:00
2008-11-28 23:38:40 +01:00
G_OBJECT_CLASS( vips_object_parent_class )->finalize( gobject );
2008-10-20 19:10:40 +02:00
}
static void
vips_object_real_changed( VipsObject *object )
2008-10-15 22:09:22 +02:00
{
2008-10-20 23:42:46 +02:00
#ifdef DEBUG
VipsObject *object = VIPS_OBJECT( gobject );
printf( "vips_object_real_changed: " );
vips_object_print( object );
#endif /*DEBUG*/
2008-10-15 22:09:22 +02:00
}
2008-11-30 14:01:48 +01:00
static void
vips_object_real_print_class( VipsObjectClass *class, im_buf_t *buf )
{
im_buf_appendf( buf, "%s", G_OBJECT_CLASS_NAME( class ) );
if( class->nickname )
im_buf_appendf( buf, " (%s)", class->nickname );
if( class->description )
im_buf_appendf( buf, ", %s", class->description );
}
2008-10-15 22:09:22 +02:00
static void
2008-10-20 23:42:46 +02:00
vips_object_real_print( VipsObject *object, im_buf_t *buf )
2008-10-15 22:09:22 +02:00
{
2008-10-20 23:42:46 +02:00
if( object->name )
im_buf_appendf( buf, "\"%s\"", object->name );
2008-10-15 22:09:22 +02:00
}
static void
2008-10-16 16:08:12 +02:00
vips_object_class_init( VipsObjectClass *class )
2008-10-15 22:09:22 +02:00
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
2008-10-16 16:08:12 +02:00
gobject_class->dispose = vips_object_dispose;
gobject_class->finalize = vips_object_finalize;
2008-10-15 22:09:22 +02:00
2008-10-16 16:08:12 +02:00
class->changed = vips_object_real_changed;
2008-11-30 14:01:48 +01:00
class->print_class = vips_object_real_print_class;
2008-10-20 19:10:40 +02:00
class->print = vips_object_real_print;
class->nickname = "object";
class->description = _( "VIPS base class" );
2008-10-15 22:09:22 +02:00
2008-10-16 16:08:12 +02:00
vips_object_signals[SIG_CHANGED] = g_signal_new( "changed",
2008-10-15 22:09:22 +02:00
G_OBJECT_CLASS_TYPE( gobject_class ),
G_SIGNAL_RUN_FIRST,
2008-10-16 16:08:12 +02:00
G_STRUCT_OFFSET( VipsObjectClass, changed ),
2008-10-15 22:09:22 +02:00
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0 );
}
static void
2008-10-20 19:10:40 +02:00
vips_object_init( VipsObject *object )
2008-10-15 22:09:22 +02:00
{
#ifdef DEBUG
2008-10-16 16:08:12 +02:00
printf( "vips_object_init: " );
2008-10-20 19:10:40 +02:00
vips_object_print( object );
2008-10-15 22:09:22 +02:00
#endif /*DEBUG*/
}
void
2008-10-20 19:10:40 +02:00
vips_object_set_name( VipsObject *object, const char *name )
2008-10-15 22:09:22 +02:00
{
2008-10-20 23:42:46 +02:00
IM_SETSTR( object->name, name );
vips_object_changed( object );
2008-10-15 22:09:22 +02:00
}
2008-10-20 19:10:40 +02:00