All pastes #436033 Raw Edit

Mine

public text v1 · immutable
·published 2007-04-12 09:41 UTC
rendered paste body
// so.c
// gcc -Wall `pkg-config --libs --cflags glib-2.0 gobject-2.0` -o so so.c
#include <glib-object.h>

/* *****************SHOULD BE HEADER FILE???************************* */

/* prototypes */
#define SIMPLE_OBJECT_TYPE (simple_object_get_type ())
#define SIMPLE_OBJECT (obj)  (G_TYPE_CHECK_INSTANCE_CAST ((obj), SIMPLE_OBJECT_TYPE, SimpleObject))

GType simple_object_get_type ();


typedef struct _SimpleObjectPrivate SimpleObjectPrivate;

typedef struct _SimpleObject SimpleObject;
struct _SimpleObject {
	GObject              parent;

	SimpleObjectPrivate *priv;
	
	/* Properties */
	gint	first;
	gint	second;
	
	//Unique Datafields and Methods Here!
};

typedef struct _SimpleObjectClass SimpleObjectClass;
struct _SimpleObjectClass {
	GObjectClass parent_class;
	
	
	//Class-wide Datafields and Methods Here!
};


/* ************* normally in a c file ************************ **/

G_DEFINE_TYPE (SimpleObject, 
	       simple_object, 
	       G_TYPE_OBJECT);

//Track properties?
enum {
	PROP_FIRST,
	PROP_SECOND
};

//TODO: set/get are same function!!! use a pointer? ASK

//is static needed here??? Not in tutorial for this or class_init...
static void
simple_object_set_property(	GObject *object,
							guint prop_id,
							GValue *value,
							GParamSpec *pspec)
{
	SimpleObject *self = SIMPLE_OBJECT(object);
	switch(prop_id){
		case PROP_FIRST:	g_value_set_int(value, self-> first); break;
		case PROP_SECOND:	g_value_set_int(value, self-> second); break;
	//	default; /* No Property Found */
	}
}

//is static needed here??? Not in tutorial for this or class_init...
static void
simple_object_get_property(	GObject *object,
							guint prop_id,
							GValue *value,
							GParamSpec *pspec)
{
	SimpleObject *self = SIMPLE_OBJECT(object);
	switch(prop_id){
		case PROP_FIRST:	g_value_set_int(value, self-> first); break;
		case PROP_SECOND:	g_value_set_int(value, self-> second); break;
	//	default; /* No Property Found */
	}
}

static void
simple_object_class_init (SimpleObjectClass *klass)
{
	GObjectClass *this_class = G_OBJECT_CLASS (klass);
	SimpleObjectClass *so_class;
	GParamSpec *spec;
	
	this_class->set_property	= simple_object_set_property;
	this_class->get_property 	= simple_object_get_property;
	
	//Define property: first
	spec = g_param_spec_int ( 	"prop-first",			/* property name */
								"first",				/* nickname */
								"The First Property", 	/* description */ 
								5, 						/* minimum */ 
								10, 					/* maximum */ 
								5, 						/* default */ 
								G_PARAM_READABLE |G_PARAM_WRITABLE /* GParamSpecFlags */ ); 
	g_object_class_install_property(this_class, PROP_FIRST, spec);
	
	//Define property: Second
	spec = g_param_spec_int ( 	"prop-second",			/* property name */
								"second",				/* nickname */
								"The Second Property", 	/* description */ 
								1, 						/* minimum */ 
								3, 						/* maximum */ 
								2, 						/* default */ 
								G_PARAM_READABLE |G_PARAM_WRITABLE /* GParamSpecFlags */ ); 
	g_object_class_install_property(this_class, PROP_SECOND, spec);
}

static void
simple_object_init (SimpleObject *so)
{
	//no init stuff here...
}

/* **********************NEW FILE************************************* */

int
main (int argc, char **argv)
{
	g_type_init ();
	SimpleObject *obj = g_object_new (SIMPLE_OBJECT_TYPE, NULL);
	int tmp;
	//do something with it

	gprint ("Property: first \n");				
	simple_object_get_property(G_OBJECT(obj), "prop-first", &tmp, NULL);
	gprint ("Default: %d\n", tmp);
	
	gprint ("Property: second \n");				
	simple_object_get_property(G_OBJECT(obj), "prop-second", &tmp, NULL);
	gprint ("Default: %d\n", tmp);
	

	g_object_unref (obj);

	return 0;
}