Re: [gtk-list] Re: glist, GValue, GTypes?



Hi Tim,

Tim Janik <timj@gtk.org> writes:

> On 16 Sep 1998, Dirk Luetjens wrote:
> 
> why do you want to cast the image pointer if the original structure
> contains all meningfull members?
> in this case it seems better to just code like:
> 
> if (image->type == IMG_DOUBLE)
>   return image->data_union_thresh.data_double;
> else if (iamge->type == IMG_FLOAT)
>   return image->data_union_thresh.data_float;
> 
> but that's of course just a guess, since i don't know the whole
> problem ;)

Because I can use quick data lokups via defines if I know the type of
the image at compile time. But I can although fall back to the unknown
format, where I can decide in runtime, which element to use. I think
this method is quit common. 

> regarding GValue, it was not intended to serve as a generic multiple value
> storage structure, but is actually just a helper structure for GScanner, and
> thus it caries such odd members as gdouble v_float; gchar *v_comment; or
> gpointer v_symbol; since GScanner attempts to simplify the parsed stuffs as
> much as possible.
> maybe i should rename that to GScannerValue, i guess we would even keep
> source capability with that.

I thought so. 

> anyways, since what you're aiming at is just a generic union and associated
> enum definitions, and thus have to write all code that's going to handle this
> yourself anyways, i guess you're better be off with defining your own

Yes, you are right, but I thought that this task is so common, that it
could be helpful for others, too. 

Have you seen the interface GNUstep uses to encapsulate Values into
objects? 

- NSValue: provide an object-oriented wrapper for the data types
           defined in standard C and Objective C 

initialisation Function:
+ (NSValue*) value: (const void*)value withObjCType: (const char*)type;

where the type string is the obj-c encoding of a complex type.

- NSNumber: provide an object-oriented wrapper for the standard
            C-language number data types (int, double, etc.)  

initialisation Functions:
+ (NSNumber*) numberWithInt: (int)value;
+ (NSNumber*) numberWithShort: (short)value;
+ (NSNumber*) numberWithUnsignedChar: (unsigned char)value;
...

Ok, this adds the overhead of a isa class pointer to every single
type. Second, the glib isn't object oriented. The counterpart in C
would look like:

typedef union _GNumber GNumber;
union _GNumber {
	gint	v_int;
	glong	v_long;
	guchar	v_uchar;
	...
}

typedef struct _GValue GValue;
struct _GValue {
	GTypes	type;
	GNumber	number;
}

perhaps a wrappers around the array functions to store the type element
in the array and not in every element. And to save even more space,
GValue must not be created with the full union, but with some
concreate types:

typedef struct GValue GValue;
struct _GValue {
	GTypes type;
};

typedef struct GValueInt GValueInt;
struct _GValueInt {
	GTypes	type;
	gint	number;
}

One could have conversion functions, too:

gdouble value_get_as_double (Value *value);
...

I know this sound like overkill of storing simple types. But I think
this is a common task and since glib is a general purpose library
(which is very good in my opinion) that I would like to put it in
glib. 

I will try to code my ideas and if you are interested I will mail you
the code. But nevertheless, if you have any enhancement ideas, or
sugestions please contact me, since I will try to code in the sence of 
glib, wether it will be included or not.

Dirk



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]