wrapping a set of classes

  • Thread starter Duncan Mac-Vicar Prett
  • Start date
D

Duncan Mac-Vicar Prett

Hello guys, I am wrapping a C++ library and after changing from define=20
singleton method "new" to alloc function and initialize, I ran into this=20
problem, imagine the classes are like those:

class MyOtherClass
{
public:
=A0=A0=A0=A0=A0=A0=A0=A0MyOtherClass(int a, int b);=A0=A0=A0=A0=A0
private:
=A0=A0=A0=A0=A0=A0=A0=A0int a;
=A0=A0=A0=A0=A0=A0=A0=A0int b;
}

class MyClass
{
public:
=A0=A0=A0=A0=A0=A0=A0=A0MyClass(int a, int b);
=A0=A0=A0=A0=A0=A0=A0=A0MyOtherClass myFunc(int c);
private:
=A0=A0=A0=A0=A0=A0=A0=A0int a;
=A0=A0=A0=A0=A0=A0=A0=A0int b;
};


1.- in alloc function, I have to create a MyClass object and wrap it with=20
Data_Wrap_Struct, but I cant create an object at that time, because I have =
no=20
constructor parameter until initialize (and I dont have a default construct=
or=20
either). How do I solve this?
2.- If class MyClass func returns a MyOther class object, whats the best wa=
y=20
to wrap this?, I have even a function that returns a collection of=20
MyOtherClass objects, so I think I have to iterate trough it, and create a=
=20
ruby wrapped object not from scratch but wrapping the current object in the=
=20
collection, and then add all the ruby objects to a Ruby array, in this case=
,=20
whats the best way to wrap it?

Thanks for the help guys!

Duncan
 
R

Richard Dale

Duncan said:
Hello guys, I am wrapping a C++ library and after changing from define
singleton method "new" to alloc function and initialize, I ran into this
problem, imagine the classes are like those:

class MyOtherClass
{
public:
MyOtherClass(int a, int b);
private:
int a;
int b;
}

class MyClass
{
public:
MyClass(int a, int b);
MyOtherClass myFunc(int c);
private:
int a;
int b;
};


1.- in alloc function, I have to create a MyClass object and wrap it with
Data_Wrap_Struct, but I cant create an object at that time, because I have
no constructor parameter until initialize (and I dont have a default
constructor either). How do I solve this?
In QtRuby I solved this problem by running the construct code twice, and
using rb_throw() to jump out of the first attempt as soon as the C++
instance could be created and wrapped with Data_Wrap_Struct, and then
trying again a second time and letting constructor code run until
completion.

Normally this isn't a problem, but with the code example below, it means
that 'In initialize' will be output twice. So as long as you don't have any
code before the call to super in the constructor, it doesn't make any
difference.

class MainWindow < Qt::MainWindow

def initialize()
puts "In initialize"
super
...
end

2.- If class MyClass func returns a MyOther class object, whats the best
way to wrap this?, I have even a function that returns a collection of
MyOtherClass objects, so I think I have to iterate trough it, and create a
ruby wrapped object not from scratch but wrapping the current object in
the collection, and then add all the ruby objects to a Ruby array, in this
case, whats the best way to wrap it?
You seem to be describing the correct way to do it, so I'm not sure what
your question is. You might need to have a hash of C++ instance => ruby
instance, and check in there to see whether a particular C++ instance of
MyOtherClass has already been wrapped in ruby.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top