shared_ptr and directors.

View: New views
7 Messages — Rating Filter:   Alert me  

shared_ptr and directors.

by Andrea-60 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I would like to ask some help about the use of directors for virtual functions that return a shared_ptr.

This is a self contained example: compiled with swig -c++ -java a.i

The key method is A::b() which returns a boost_shared_ptr<A>.
I would like to subclass A in Java so that this function is entirely implemented in Java.
The class B does not have any problem.

When I look at the code created for A::b() in Java I see

class A {
   public BPtr b() {
     return new BPtr(aJNI.A_b(swigCPtr, this), true);
   }
}

If I wanted to create a subclass of A, I would have to implement a new method b() returning a BPtr.
But there is no way to create a BPtr from a class inheriting from B.
I was expecting a method in BPtr returning a B, but there isn't.

Everything works very well if I change my functions to return raw pointers rather than shared_ptr.

Any idea?

%module (directors="1") a

%include "shared_ptr.i"

class B
{
  public:
   virtual double value() = 0;
   virtual ~B() {}
};

class A
{
  public:
   virtual boost::shared_ptr<B> b() = 0;
   virtual ~A() {}
};

class B1 : public B
{
  public:
   virtual double value() {return 1.0;}
};

class A1 : public A
{
  public:
   virtual boost::shared_ptr<B> b()
     {
       return boost::shared_ptr<B>(new B1());
     }
};

double foo(A * a)
{
   return a->b()->value();
}


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: shared_ptr and directors.

by Andrea-60 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 21/10/09 11:18, Andrea wrote:

> I would like to ask some help about the use of directors for virtual functions that return a shared_ptr.
>
> This is a self contained example: compiled with swig -c++ -java a.i
>
> The key method is A::b() which returns a boost_shared_ptr<A>.
> I would like to subclass A in Java so that this function is entirely implemented in Java.
> The class B does not have any problem.
>
> When I look at the code created for A::b() in Java I see
>
> class A {
>     public BPtr b() {
>       return new BPtr(aJNI.A_b(swigCPtr, this), true);
>     }
> }
>
> If I wanted to create a subclass of A, I would have to implement a new method b() returning a BPtr.
> But there is no way to create a BPtr from a class inheriting from B.
> I was expecting a method in BPtr returning a B, but there isn't.
>
> Everything works very well if I change my functions to return raw pointers rather than shared_ptr.

I've found a workaround.
Basically what is missing is a function like (in Java)

BPtr transformB(B b);

which can be easily added to the c++ file as

boost::shared_ptr<B> transformB(B * b)
{
   return boost::shared_ptr<B>(b);
}

then I can write

class MyA {
      public BPtr b() {
        return transformB(new MyB());
      }
}

my question then is: why the shared_ptr<B> does not expose a constructor that takes a B
(i.e. a B * in C++)?


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: shared_ptr and directors.

by Andrea-60 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 21/10/09 12:50, Andrea wrote:

> On 21/10/09 11:18, Andrea wrote:
> I've found a workaround.
> Basically what is missing is a function like (in Java)
>
> BPtr transformB(B b);
>
> which can be easily added to the c++ file as
>
> boost::shared_ptr<B>  transformB(B * b)
> {
>     return boost::shared_ptr<B>(b);
> }
>

I am not too sure any more about that.
Is the memory ownership ok?

> then I can write
>
> class MyA {
>        public BPtr b() {
>          return transformB(new MyB());
>        }
> }
>
> my question then is: why the shared_ptr<B>  does not expose a constructor that takes a B
> (i.e. a B * in C++)?


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: shared_ptr and directors.

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Andrea wrote:
> I would like to ask some help about the use of directors for virtual functions that return a shared_ptr.
>
> This is a self contained example: compiled with swig -c++ -java a.i
>
> The key method is A::b() which returns a boost_shared_ptr<A>.
> I would like to subclass A in Java so that this function is entirely implemented in Java.
> The class B does not have any problem.
>
Andrea,

There is currently no support for shared_ptr and the directors feature.
The director typemaps need to be written in order to support this. These
typemaps are: directorin, directorout, javadirectorin, javadirectorout.

William

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: shared_ptr and directors.

by Andrea-60 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 27/10/09 07:38, William S Fulton wrote:

> Andrea wrote:
>> I would like to ask some help about the use of directors for virtual functions that return a shared_ptr.
>>
>> This is a self contained example: compiled with swig -c++ -java a.i
>>
>> The key method is A::b() which returns a boost_shared_ptr<A>.
>> I would like to subclass A in Java so that this function is entirely implemented in Java.
>> The class B does not have any problem.
>>
> Andrea,
>
> There is currently no support for shared_ptr and the directors feature.
> The director typemaps need to be written in order to support this. These
> typemaps are: directorin, directorout, javadirectorin, javadirectorout.

On the other hand if I return a raw pointer I get a warning

" Returning a pointer or reference in a director method is not recommended. "

Does it mean that in directors I can only return by value?


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: shared_ptr and directors.

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Andrea wrote:

> On 27/10/09 07:38, William S Fulton wrote:
>> Andrea wrote:
>>> I would like to ask some help about the use of directors for virtual functions that return a shared_ptr.
>>>
>>> This is a self contained example: compiled with swig -c++ -java a.i
>>>
>>> The key method is A::b() which returns a boost_shared_ptr<A>.
>>> I would like to subclass A in Java so that this function is entirely implemented in Java.
>>> The class B does not have any problem.
>>>
>> Andrea,
>>
>> There is currently no support for shared_ptr and the directors feature.
>> The director typemaps need to be written in order to support this. These
>> typemaps are: directorin, directorout, javadirectorin, javadirectorout.
>
> On the other hand if I return a raw pointer I get a warning
>
> " Returning a pointer or reference in a director method is not recommended. "
>
> Does it mean that in directors I can only return by value?
>
Take a close look at the generated code with pointers and you'll see the
memory complications, with no clear solution. However, I very much doubt
these problems would exist if you used shared_ptr and the director
typemaps for shared_ptr were written.

William

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: shared_ptr and directors.

by Andrea-60 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 01/11/09 21:03, William S Fulton wrote:

>>
>> On the other hand if I return a raw pointer I get a warning
>>
>> " Returning a pointer or reference in a director method is not recommended. "
>>
>> Does it mean that in directors I can only return by value?
>>
> Take a close look at the generated code with pointers and you'll see the
> memory complications, with no clear solution. However, I very much doubt
> these problems would exist if you used shared_ptr and the director
> typemaps for shared_ptr were written.

I've written typemaps for customized types, but never for directors.
If you could give some hints about problems/solutions, you would make my life a lot easier.

Andrea


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user