C++ class

BlitzMax Forums/BlitzMax Programming/C++ class

Yeshu777(Posted 2007) [#1]
Hi,

Am I right in thinking that in order for BlitzMax to use a C++ class I would have to write a .c wrapper to create an instance of the class and to access the member funcitons and then, finally a .bmx wrapper?

Any help, *pointers most appreciated.


Brucey(Posted 2007) [#2]
It would probably be better to use a .cpp wrapper.

You can have the wrapper return a pointer to the C++ object, which you can later use to pass back and do stuff with.

Remember, if you create a C++ object with "new", you may also need to "delete" it later.

:o)


Yeshu777(Posted 2007) [#3]
Ok, thanks for the advice.


Brucey(Posted 2007) [#4]
Perhaps something along the lines of this completely untested code :

test.cpp
class MyClass
{
public:
   MyClass();
   void DoSomething();
}

MyClass::MyClass()
{}

void MyClass::DoSomething()
{
}

extern "C" {

  MyClass * create_new_myclass();
  void myclass_dosomething(MyClass * myclass);

}

MyClass * create_new_myclass() {
    return new MyClass();
}

void myclass_dosomething(MyClass * myclass) {
    myclass->DoSomething();
}


testwrap.bmx
Superstrict

Import "test.cpp"

Extern
  Function create_new_myclass:Byte Ptr()
  Function myclass_dosomething(myclass:Byte Ptr)
End Extern

Local b:Byte Ptr = create_new_myclass()

myclass_dosomething(b)



Yeshu777(Posted 2007) [#5]
Thanks again!

Guess I'll buy BlitzMax then.


Gavin Beard(Posted 2007) [#6]
ohh....thx brucey :-) always wondered myself, just didnt want to ask ;-)

p.s hows you ffmpeg mod coming?


errno!(Posted 2007) [#7]
unless i'm very much mistaken, and i'm not, you can't wrap C++ functions directly.

Well I guess you might if you knew what internal post decorated name the functions had. but the above untested example won't work.

Believe me, I've tried.


Brucey(Posted 2007) [#8]
but the above untested example won't work.


Oh... bugger... well... it seemed a good idea at the time.



FYI :-)

Some "real" code snippets, from my DateTime module (wrapping the C++ Boost libs):

.. creating a new instance of a C++ class ..
time_duration * bmx_time_duration(int hours, int minutes, int seconds, int fraction) {
	return new time_duration(hours, minutes, seconds, fraction);
}

... using that instance ...
int bmx_time_duration_hours(time_duration * d) {
	return d->hours();
}


Which part of that won't work?


Brucey(Posted 2007) [#9]
Sorry... the .bmx bits too... just for clarity :-)

..in the extern..
	Function bmx_time_duration_hours:Int(duration:Byte Ptr)

..in the main code..
	Method hours:Int()
		Return bmx_time_duration_hours(durationPtr)
	End Method



Brucey(Posted 2007) [#10]
Guess I'll buy BlitzMax then.

Geepers... where's my commission? :-p