Wrapping a set of classes

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?
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?

Thanks for the help guys!

Duncan

Duncan Mac-Vicar Prett wrote:

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