modules, type and c

BlitzMax Forums/BlitzMax Programming/modules, type and c

I've been looking at exmaple code, docs (very limited) and do not understand how I can create a type in blitz that can access structs in c.

ie
blitzmax file
---
Type temp_typ
Field x:int,y:int
Field data:datastruct
end type

c file
typedef struct {
int length; /**< length of data stream */
unsigned char *data; /**< Pointer to start data */
} datastruct;
---

I've managed to get blitz to compile the c code and include it in a wrapper, I just can't work out how to pass data between the 2.
Any help would be fanatstic.

anyone? ;-)

Perhaps :
Type temp_typ
    Field x:int,y:int
    Field data:byte ptr
end type


And have your c / C++ glue create an instance of the struct, that you will have to clean up yourself.

The following is how I tend to go about it. Whether or not this is a GOOD way, I wouldn't know. It's the way it works for me - and I'm not a C/C++ programmer...

example c++ glue :
extern "C" {

  datastruct * create_a_new_struct();
  void free_struct(datastruct * d);

}

datastruct * create_a_new_struct() {
  return new datastruct;
}

void free_struct(datastruct * d) {
  delete d;
}

In your bmx code...
Extern
   Function create_a_new_struct:Byte Ptr()
   Function free_struct(data:Byte ptr)
End Extern

You can then call create_a_new_struct to get yourself a pointer to some memory representing your datastruct.

If the data is intended to only last as long as the Type it belongs to, you can have it free nicely by doing this in a Delete() method - which is called when GC decides the instance of your Type can be cleaned...
Type temp_typ
    Field x:int,y:int
    Field data:byte ptr

    Method Delete()
        If data Then
            free_struct(data)
            data = Null
        End If
    End Method
end type


I'll leave the rest up to you... ;-)

Disclaimer : None of the above code has been tested / typed into an IDE... use your imagination :-p

looks good. cheers brucey

I'll have a look tonight.

just reading through it now properly to understand it.. so in theory, in Blitz I could add a New() Method to automatically create the new datastruct?

Type temp_typ
    Field x:int,y:int
    Field data:byte ptr

    Method New()
        data =  create_a_newstruct()
    end method

    Method Delete()
        If data Then
            free_struct(data)
            data = Null
        End If
    End Method
end type

agian not typed into blitz. will try tonight.

Hi Rich, long time no see. Yep that's all I had to say :) If I knew the answer I'd give you some help.

Yep. That would work.