oekopez | 2 Jun 13:26 2009
Picon
Picon

Re: C++ conversion operator and SWIG for Python


> Josh Cherry wrote:
> > 
> > On Tue, 26 May 2009, Philipp Kraft wrote:
> > 
> >> Maybe I wasn't precise enough. It is not the problem to access A from B
> with
> >> some function (get_A is a member of B) but to use b as an A in a "quasi
> duck
> >> typing way".
> >> This works:
> >>>>> ct.print_a(b.get_A()) # but it's ugly and hard to explain to Non-C++
> >> Python programmers
> >>
> >> This should work:
> >>>>> ct.print_a(b) # Simple to use, but how to do it with SWIG?
> >> The idea is to have a "HAS A" relationship, and, in some cases an "IS
> A"
> >> relationship-like behaviour without using inheritance.
> > 
> > You could provide typemaps for A& that accept B.  Alternatively, if the 
> > number of functions is small you could provide overloads that take a B& 
> > for wrapping purposes.
> > 
> Also take a look at %implicitconv documented in the CHANGES file. If 
> that doesn't work, can you add conversion operators dealing with 
> pointers as well as references in your code? If so you should be able to 
> use %types. Let me know and I'll try put together an example.
> 
> William
(Continue reading)

Rémi Coulom | 3 Jun 12:03 2009
Picon

Using python's reference counting for C++ classes

Hi,

I needed to find a way to manage classes that keep pointers or reference 
to other objects, passed as parameters to the constructor or member 
functions. I noticed the ref/unref for reference-counted objects:
http://www.swig.org/Doc1.3/Python.html#Python_nn27a
But I don't want to add that code to my C++, so I tried to find a way 
that uses python's reference counting instead.

I found a solution that I find is much better, so I am describing it 
here in case it is useful to someone, or in case an expert can suggest a 
better alternative. I believe this is such a standard idiom that it 
would be worth having swig support for it. Maybe it already exists but I 
did not find it in the docs.

Here is the declaration of a C++ class (CA.h):

class CA
{
 private:
  const CTest &test;

 public:
  CA(const CTest &testInit): test(testInit) {}
  void Display();
  ~CA();
};

And here is how the swig interface works:

(Continue reading)

Michael Leong | 3 Jun 20:55 2009

SWIG/JNI on Solaris 10 versus Solaris 8

Hi all,

 

I have some C++ code on which I run SWIG to generate Java wrappers. This code works fine on Solaris 8, with Java 1.6.0_13, but when I put it on Solaris 10 with Java 1.5.0_17, it encounters problems. I don’t think it’s the version of Java that’s causing a problem, because I’ve used Java 1.5 with the same code and SWIG on other platforms and it’s worked out ok. Have SWIG’s Java capabilities been tested against Solaris 10? Anything special I should note about it?

 

In depth, here’s the problem:

The Java code successfully calls into the native code, and several C functions successfully call each other (I can tell this by printf, etc). However, one call to a function (about 7-8 calls deep in the call stack) fails. I know which function it’s supposed to call, but it never reaches it (no printf) and instead returns some ridiculously huge quasi-random number which causes my program to register an error and terminate.

 

Any ideas on what might be going on and how to solve would be very much appreciated. Thank you.

-Michael

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Jonathan Drosdeck | 3 Jun 23:09 2009
Picon

Windows Execution failure

I'm swigged my c++ program perfectly under windows, But upon trying it  
on a different computer the module won't import. Do you happen to know  
under what conditions it won't load on another persons machine. In  
this case the python install path was different, so I changed it on  
the development machine and it worked there, but it still wouldn't  
load on the test machine. Is there anything else I should check?

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
prince0 | 4 Jun 11:34 2009
Picon

Performance issue

Hi,

How can I request swig to declare local variables on the same line then when necessary?

I tried “–small” command line option but it doesn’t work like I expect.

 

e.g. swig generates the following C++ wrapper:

 

SWIGEXPORT void * SWIGSTDCALL CSharp_CBACnetDriver_GetCache(void * jarg1)  {

  void * jresult ;

  CBACnetDriver *arg1 = (CBACnetDriver *) 0 ;

  CCache result;

 

  arg1 = (CBACnetDriver *)jarg1;

  result = (arg1)->GetCache();

  jresult = new CCache((const CCache &)result);

  return jresult;

}

 

I would like it to generate the following code instead:

 

SWIGEXPORT void * SWIGSTDCALL CSharp_CBACnetDriver_GetCache(void * jarg1)  {

  CBACnetDriver *  arg1 = (CBACnetDriver *)jarg1;

  CCache result = (arg1)->GetCache();

  void * jresult = new CCache((const CCache &)result);

  return jresult;

}

 

This second code will go faster because the constructor of the CCache class will be called only one.

Moreover it would work better since I won’t have memory leaks. The fact is the constructor of my class CCache allocates memory via the new operator but does not have a copy constructor.

 

Thanks for your help.

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Lubomir Konstantinov | 4 Jun 15:46 2009
Picon

selective enum generation

Hi,

I need to wrap some 3rd party C+ code with Java.

The problem I am having is that some of the enums are used as flags, so I need to use swig's type unsafe enums, but that clobbers all the 'normal' enums and converts them to int. Is there any way to specify the code generation on an enum per enum basis?

TIA,
Lubo

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Costas | 4 Jun 16:41 2009

Re: Java/C++: Can I use the clone() pattern with Directors?

William,

Thanks for this.  I re-factored my code to get back to the original design, and in doing that I think I solved the problem (inadvertently):  my original design called for use of shared_ptr to handle the dependency injection, in a situation very similar to this: http://www.nabble.com/Swigging-boost::shared_ptr-td18597081.html  I don't have the chops to follow your solution to that problem, so I ended up refactoring shared_ptr away and it looks like everything's working now, with a straight director, as in your example (although I need to test a lot more thoroughly).  One more vote though for fixing shared_ptr/director integration...

Thanks again,

Costas




On Thu, May 28, 2009 at 12:53 PM, William S Fulton <wsf <at> fultondesigns.co.uk> wrote:
Costas wrote:


On Wed, May 27, 2009 at 9:13 PM, Costas Malamas <costas <at> malamas.com <mailto:costas <at> malamas.com>> wrote:



   On Tue, May 26, 2009 at 11:02 PM, William S Fulton
   <wsf <at> fultondesigns.co.uk <mailto:wsf <at> fultondesigns.co.uk>> wrote:

       Costas wrote:

           Hello all,

           I am trying to wrap a simple C++ class hierarchy in Java,
           using directors.
           The C++ is fairly trivial, but uses the clone() pattern,
           which is giving me
           trouble:

           class Base {
            ...

            Base(const Base& other)
            { ... };

            virtual Base* clone() {
               return Base(*this);
            }

            ...
           }

           class Extended : public Base {
            ...

            Extended(const Extended& other)
            { ... };

            virtual Base* clone() {
              return Extended(*this);
            }

            ...
           }

           Now, I can extend this using directors in Java, by just
           renaming clone() to
           instance() and using the %newobject directive for clone().
            The copy
           constructor works fine, but whenever my C++ uses clone() and
           goes to the
           Java version, I get access violation errors which crash the JVM.

           Has anybody tried a similar pattern? I've tried refactoring
           this a bunch of
           times that ended up messing up the design more than fixing
           it, so now I'd
           like to get the above (original) design working.  My only
           half-baked
           suspicion is that the renaming of clone() may be still
           creating some
           conflict with Java's own clone() when called from C++.

           Any input or ideas are welcome and thanks in advance.


       Don't you get a warning about not using pointers for virtual
       director method return types? The memory handling is impossible
       to deal with if I remember right and is probably what you are
       suffering.

       William

 Honestly, I don't remember (the original design was multiple refactorings ago), but I don't think so.  What I don't quite get is why if the director mechanism is smart/robust enough to deal with a copy constructor, it falls apart on clone().  And if the memory handling is this hairy what's the advisable/standard practice when doing dependency injection?


I've put together an example and can't get a seg fault. Can you modify my example to demonstrate? The seg fault could just be your dodgy c++ code ;)

William

// File: example.i
%module(directors="1") example

%feature("director") Base;
%feature("director") Extended;

%include <std_string.i>

%newobject clone();
%inline %{
#include <string>
class Base {
public:

 Base(const std::string& s) : myString(s) {}
 Base(const Base& other) : myString(other.myString) {};
 virtual ~Base() {}

 virtual Base* clone() {
    return new Base(*this);
 }

 virtual std::string getString() {
   return myString;
 }

 static Base* createClone(Base *b) {
   return b->clone();
 }
private:
 std::string myString;
};

class Extended : public Base {
public:
 Extended(const std::string& s) : Base(s + " Extended") {};
 Extended(const Extended& other) : Base(other) {};

 virtual Base* clone() {
   return new Extended(*this);
 }
};
%}



------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
swadhin pradhan | 4 Jun 22:33 2009
Picon

converting enum and function pointer from python to c and viceversa

sir,
      I am a novish to SWIG. I dont know much about it. I am having
problem to convert the function pointers from python to c. I am using
typemaps. Please help me.

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
William S Fulton | 6 Jun 01:07 2009
Picon

Re: Performance issue

prince0 wrote:
> Hi,
> 
> How can I request swig to declare local variables on the same line then 
> when necessary?
> 
> I tried “–small” command line option but it doesn’t work like I expect.
> 
>  
> 
> e.g. swig generates the following C++ wrapper:
> 
>  
> 
> SWIGEXPORT void * SWIGSTDCALL CSharp_CBACnetDriver_GetCache(void * jarg1)  {
> 
>   void * jresult ;
> 
>   CBACnetDriver *arg1 = (CBACnetDriver *) 0 ;
> 
>   CCache result;
> 
>  
> 
>   arg1 = (CBACnetDriver *)jarg1;
> 
>   result = (arg1)->GetCache();
> 
>   jresult = new CCache((const CCache &)result);
> 
>   return jresult;
> 
> }
> 
>  
> 
> I would like it to generate the following code instead:
> 
>  
> 
> SWIGEXPORT void * SWIGSTDCALL CSharp_CBACnetDriver_GetCache(void * jarg1)  {
> 
>   CBACnetDriver *  arg1 = (CBACnetDriver *)jarg1;
> 
>   CCache result = (arg1)->GetCache();
> 
>   void * jresult = new CCache((const CCache &)result);
> 
>   return jresult;
> 
> }
> 
>  
> 
> This second code will go faster because the constructor of the CCache 
> class will be called only one.
> 
> Moreover it would work better since I won’t have memory leaks. The fact 
> is the constructor of my class CCache allocates memory via the new 
> operator but does not have a copy constructor.
> 
Read about the optimal attribute in the out typemap:
http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_optimal
You might also want to look at the valuewrapper feature:
http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_nn19

William

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
William S Fulton | 6 Jun 01:18 2009
Picon

Re: SWIG/JNI on Solaris 10 versus Solaris 8

Michael Leong wrote:
> Hi all,
> 
>  
> 
> I have some C++ code on which I run SWIG to generate Java wrappers. This 
> code works fine on Solaris 8, with Java 1.6.0_13, but when I put it on 
> Solaris 10 with Java 1.5.0_17, it encounters problems. I don’t think 
> it’s the version of Java that’s causing a problem, because I’ve used 
> Java 1.5 with the same code and SWIG on other platforms and it’s worked 
> out ok. Have SWIG’s Java capabilities been tested against Solaris 10? 
> Anything special I should note about it?
> 
Works fine for me on Solaris 10.

>  
> 
> In depth, here’s the problem:
> 
> The Java code successfully calls into the native code, and several C 
> functions successfully call each other (I can tell this by printf, etc). 
> However, one call to a function (about 7-8 calls deep in the call stack) 
> fails. I know which function it’s supposed to call, but it never reaches 
> it (no printf) and instead returns some ridiculously huge quasi-random 
> number which causes my program to register an error and terminate.
> 
>  
> 
> Any ideas on what might be going on and how to solve would be very much 
> appreciated. Thank you.

Could be some undefined behaviour of your C++ code, which in practical 
terms results in different behaviour on different platforms. Debug it 
using Sun Studio or dbx. You can seamlessly step from Java into C++ 
code. Also use some memory testing tools, some ideas: 
http://developers.sun.com/solaris/articles/dmalloc_solaris.html
I'd also check that all your compilers and OS are configured properly by 
running all the SWIG examples as they do work on Solaris 10.

William

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get

Gmane