modules, type and c

BlitzMax Forums/BlitzMax Programming/modules, type and c

PantsOn(Posted 2007) [#1]
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.


PantsOn(Posted 2007) [#2]
anyone? ;-)


Brucey(Posted 2007) [#3]
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


PantsOn(Posted 2007) [#4]
looks good. cheers brucey

I'll have a look tonight.


PantsOn(Posted 2007) [#5]
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.


(tu) ENAY(Posted 2007) [#6]
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.


Brucey(Posted 2007) [#7]
Yep. That would work.