roxen.lists.pike.general

Subject Author Date
Re: cmod inherit question H. William Welliver III <hww3[at]riverweb[dot]com> 04-11-2009
I'll take a stab at providing some places to look for answers...

I am a little conused, though... Control inherits HostApiInfo and also  
contains one as a member variable?

You might want to take a look at the source for the Public.Xapian  
module. It uses inheritance, though not really any PIKEVARS, though  
the data is accessed very similarly, due to the next little bit of  
information:

Though CMOD takes care of a lot of the basics for you, you're still  
essentially working on the C-level, so you'll need to write some C  
using the pike C api in order to get certain things done. As an  
example, in order to create an object of a particular type, you'll  
need to use one of the C-level functions that cause objects to be  
created, such as clone_object(). Also note that you'll need to push  
arguments for the clone, if any, onto the stack.

 From a practical standpoint, a PIKEVAR declaration just generates a  
struct member of the appropriate C-level pike datatype that gets added  
to the storage for the class. Then, I'm pretty sure it also causes the  
C-level struct to get mapped to the variable so that it's accessible  
at the Pike-level. I don't think that specifying the classname in the  
pikevar does anything useful for you; it probably just gets converted  
to an svalue placeholder or something similar.

I find it helpful to look at the source generated by the  
precompiler... there are a lot of macros that are useful in working  
with storage, and, for example, you'll be able to see what the struct  
* program is called so that you can clone the HostApiInfo class, and  
you'll see the macro that converts an object's generic storage into  
that of the class you want to access... your line for THIS->info- 
 >structVer will probably become OBJ2_HOSTAPIINFO(THIS->info)- 
 >structVersion or something similar.

Finally, I've gathered some info on C-level modules at the Pike  
Wiki... it's not completely organized, and there are certainly a lot  
of unanswered questions, but perhaps you'll find something useful.

http://www.gotpike.org/PikeWiki/index.pike/PikeDevel/C%20Modules

Good luck!

Bill

On Nov 3, 2009, at 3:48 PM, Yvan Vander Sanden wrote:

> hi.
>
> Something i'm confused about (again :-). I've made this class/ 
> structure:
>
> PIKECLASS HostApiInfo {
>     PIKEVAR int structVersion;
>     PIKEVAR int type;
>     PIKEVAR string name;
>     PIKEVAR int deviceCount;
>     PIKEVAR int defaultInputDevice;
>     PIKEVAR int defaultOutputDevice;
>
>     INIT{
>
>     }
>
>     EXIT{
>
>     }
> }
>
> And want to use it in the next class:
>
> PIKECLASS Control {
>     INHERIT HostApiInfo;
>     PIKEVAR HostApiInfo Info;
>
>     PIKEFUN HostApiInfo get_host_api_info(int hostApi) {
>         const PaHostApiInfo * info;
>         info = Pa_GetHostApiInfo(hostApi);
>         THIS->Info->structVersion = info->structVersion;
>         [...]
>         RETURN(THIS->Info);
>     }
>
> etc...
>
> But it doesn't work that way. At least the HostApiInfo class was  
> recognised as a PIKEVAR, but when i use the structVersion function  
> the compiler complains:
>
>     error: ‘struct object’ has no member named ‘structVersion’
>
> I know, the structure is not initialized. But where and how to do  
> that? I tried something like
>
>    THIS->Info = HostApiInfo();
>
> But that didn't help. Probably I could get around it this time by  
> using a mapping instead of my own struct, but i'm gonna need the  
> real thing sooner or later anyway, and using a mapping for this is  
> just not my idea of good programming :-)
>
> If anyone knows more about that, some hints would really help. Thanks!
>
> yvan
>
> -- 
> Copyright only exists in the imagination of those who do not have any.