slang-users mailing list

[2004 Date Index] [2004 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Complex structures (again)


> Can someone help me make something like this available to my SLang code?
> 
> typedef struct {
>   char *fname;
>   char *lname;
> } Name_Type;
> 
> typedef struct {
>   Name_Type name;
>   int age;
> } Person_Type;
> 
> I can make the Name_Type available no problem, but making the
> Person_Type poses some difficulties for me that I have not yet been able
> to figure out.

I create nested structs in SLgtk, but so far only for relatively simple
cases (e.g. the contained struct has no malloc'ed fields), and not more
than 1 level of nesting.

To accomplish it requires a little cheating, though, since one has to
use the quasi-public routines

	extern int _SLang_pop_struct(SLang_Struct_Type **);
	extern void _SLstruct_delete_struct(SLang_Struct_Type *);

I say "quasi" because even though these funcs are not published in
slang.h, they are NOT implemented statically in slstruct.c, so as
long as you include the above prototypes in your code you will be
able to call them at runtime.

To achieve nested structs, you will need to push the container struct onto 
the stack via something like

	SLstruct_create_struct(nfields,names,types,values);

where the type/value of the embedded struct assigned via
something like

	Name  slang_Name_struct_instance = ... whatever...

	SLang_Struct_Type *slang_Name_struct_instance = NULL;
	types[nfields] = SLANG_STRUCT_TYPE;
	values[nfields] = (VOID_STAR) &slang_Name_struct_instance;

and where slang_Name_struct_instance gets its value from something like

 	SLang_push_cstruct((VOID_STAR)&(C_Name_struct_instance), Name_Layout))
	_SLang_pop_struct(&slang_Name_struct_instance))

In plain English, you have to push the contained struct onto the stack,
pop it off, and then store the result in the appropriate field of the
container struct.  For reference you may inspect the push_event() function
defined within src/slgdk.c of the SLgtk distribution.

Hope This Helps,
Mike

_______________________________________________
To unsubscribe, visit http://jedsoft.org/slang/mailinglists.html


[2004 date index] [2004 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]