Martin Ling | 19 Apr 23:42 2014
Picon

Problems using %attributeval with std::vector (Python)

Hi,

I'm wrapping a C++ class into Python, which includes the method:

std::vector<std::shared_ptr<Channel>> Device::get_channels();

I'm using shared_ptr.i and std_vector.i, and instantiating their
templates for the types involved. This all works okay and if I call
device.get_channels() from the Python side, I get a Python tuple
of shared_ptr<Channel> wrappers, which is fine.

What I want though is to turn this into an attribute. So I add to the
interface file:

%attributeval(Device, std::vector<std::shared_ptr<Channel>>,
        channels, get_channels);

But when I read the device.channels attribute in Python, instead of a
tuple I get a proxy object for a vector pointer. This is a problem for
me, because it's important those shared_ptrs actually get copied when
the result is read into the Python side, or the objects they point to
are lost.

So I can do e.g:

[c.name for c in device.get_channels()]

but:

[c.name for c in device.channels]
(Continue reading)

opatry | 16 Apr 15:43 2014

Write my own Java implementation of a method

Hi, I want to expose my C++ pure virtual classes with Java interfaces. To do so, I've already tried some solution around the directors feature. Unfortunately, some use cases can't be solved with this solution. >From the following C++ code:

class Foo { public: virtual ~Foo(){} void bar() = 0; }; class Zorg { public: void work(Foo* foo); ; I wish I can have the following Java code: interface IFoo { void bar(); } class Zorg { void work(IFoo foo) { // not MyModule.work(Foo.getCPtr(foo), foo)); // I do not control the implementations of IFoo MyOwnNatives.work(foo); } } And then write manually MyOwnNatives void Java_org_acme_MyOwnNatives_work(jobject jfoo) { Foo* foo = MyFooImpl(jfoo); } The director feature do the proxy job in Java, here, I do it in C++. It's simpler when dealing with Java interfaces prototype and implementation of these interfaces done by end user. The GC handling is a pain to handle between Java side, JNI side and C++ side. Is there any way to do so? Or an alternative solution?
View this message in context: Write my own Java implementation of a method
Sent from the swig-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
chinchi | 16 Apr 13:09 2014
Picon

SWIG Import-helper Problem

Hi,

I got some weired errors:
I generated sucessfully a module with SWIG from C++ which I could used the
first time in python. It worked. But after I try to change so. on the
file.cxx, the file.h and the interfacefile and compiled again, it doesn't
work anymore.
I removed all the changed things to the state where it did work and
recompiled it with:

$ swig -python -c++ file.i
$ g++ -fPIC -c file.cxx file_wrap.cxx -I/usr/include/python2.7
$ g++ -shared file.o file_wrap.o -o _filename.so

All this steps worked fine, but when I try to import
<<import filename as f
pyhton got the following error in the generated file.py from SWIG:

line 22, in swig_import_helper
    _mod = imp.load_module('_test', fp, pathname, description)

I looked in the file.py at line 22:
$if fp is not None:
            try:
                _mod = imp.load_module('_test', fp, pathname, description)

Can somebody tell me please what to do?
Thank you,
best regards Vanessa

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
Eric Wing | 15 Apr 00:24 2014
Picon

Why doesn't this array typemap from the documentation work when the parameters are reversed?

I copied the following from the Lua/SWIG documentation concerning
arrays. The first one works where the first parameter is an array, and
the second is the size. But in my real library, the parameters come in
the reverse order and when I try to flip the SWIG typemap, it gives
the warning:

Warning 453: Can't apply (int,myVect *INOUT). No typemaps are defined.

// using typemaps
%include <typemaps.i>

// This works
%apply (myVect *INOUT,int) {(myVect* arr,int len)};
extern void sort_double(myVect* arr, int len); // the function to wrap

// This doesn't work
%apply (int,myVect *INOUT) {(int len,myVect* arr)};
extern void sort_double2(int len, myVect* arr); // the function to wrap

Why is this and how do I handle this correctly?

Thanks,
Eric
--

-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
chinchi | 12 Apr 23:18 2014
Picon

Operator Overloading in c++

Hi,

I'm really not so familiar with operator overloading in C++ in combination
with Python by using SWIG. Maybe you can help me because I searched in the
internet but didn't get it.
I wrote a Vector class and overloaded the +,- and ()operator. I want to
access my class via the python through the swig interface.

I read that I have to use so. like:
%rename(__sub_assign__)	VECTOR::operator-=(const VECTOR &INOUT);
%rename(__add_assign__) VECTOR::operator+=(const VECTOR &INOUT);
%rename(__len__)  VECTOR::operator()(int i);

,but it does not work, I still can't use it. Is there a way to access my
class from python and use the advantage of operator overloading? How does
my interfacefile.i should look when my header file is:

#ifndef VECTOR_H
#define VECTOR_H

class VECTOR{

	private:
		int size;
		double* vector;

	public:

		VECTOR(int);
		~VECTOR();
		double getsize();
		VECTOR(const VECTOR&);
		double& operator()(int i);
		//friend std::fstream& operator << (std::fstream&,VECTOR& );
		double getvector(int);
		VECTOR& operator-(const VECTOR&);
		VECTOR& operator+(const VECTOR&);

};

#endi

Can somebody give me a hint?

with best regards,
Nessi

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
Nikola Radnovic | 11 Apr 01:42 2014
Picon

Wrapping a C++ library in Python that calls the Python C API

Hi everyone,


I have trouble solving this error that came up while using SWIG.


C++ library is wrapped in Python using SWIG. We can instantiate a class in Python and when we call a method in this class which uses a Python C API this error comes up:

 

Fatal Python error: PyThreadState_Get: no current thread

Abort trap: 6

 

It seems this is a problem with Python calling C which calls Python, no? Any ideas?


I appreciate any help, thank you!

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
petergabriel | 10 Apr 16:41 2014

C++ _TCHAR to C# string

Hi,

I am trying to use SWIG to generate a C# dll from some C++  6 code.

I have managed to get it working where the input/output parameters are int,
double or bool, but I'm stuck with how to get it to convert a C++ _TCHAR to
a string in C#.

Where the C++ code uses _TCHARs as the parameter types, no matter what I've
tried in the SWIG interface file, the C# parameters for any methods which
should take a string are coming up as SWIGTYPE_p__TCHAR, which is as far as
I can tell is what SWIG produces when it doesn't know what it should use.

I'm sure that this should be a case of using the right typemap in the SWIG
interface file, but nothing I've tried so far has worked, any suggestions
would be much appreciated!

Cheers,
Peter

--
View this message in context: http://swig.10945.n7.nabble.com/C-TCHAR-to-C-string-tp13745.html
Sent from the swig-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
Eric Wing | 10 Apr 09:58 2014
Picon

Is there a Lua typemap for fixed-length arrays as inputs?

I want to bind some functions like the following (from OpenAL) to Lua:
void alSourcefv( ALuint sid, ALenum param, const ALfloat* alsourcefv_values );

The last parameter is really an array of length 3, so you might read it like:
void alSourcefv( ALuint sid, ALenum param, const ALfloat alsourcefv_values[3] );

I was hoping there was some built-in typemap so I could do something like:
%apply ALfloat[3] INPUT {ALfloat alsourcefv_values[3]};

such that from Lua, the following would just work:
alSourcefv(sid, param, {0.0, 0.0, 0.0})

Is there anything like I'm looking for?

Thanks,
Eric
--

-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
Arnaud Diederen | 8 Apr 14:48 2014

Re: Directors, __stdcall virtual method, compilation error on windows.


Hi Alexander,

On 04/07/2014 04:32 PM, Alexander Borovikov wrote:
> Hmmm... interesting case, I haven't faced a similar issue.
> I'm afraid that isn't a smart solution, but I would %extend the original
> struct user_lvar_visitor_t with some non-virtual method (with a
> handle_retrieved_info call inside) to fit the calling convention and
> would ignore with feature("nodirector") the original method.
> Please write about the solution if you find a smart one.

I see.

Here's what I tried:
---
> %extend user_lvar_visitor_t {
> 	int my_handle_retrieved_info(lvar_saved_info_t const &lv)
>       {
> 		return handle_retrieved_info(lv);
> 	}
> }
...
> %feature("director") user_lvar_visitor_t;
> %feature("nodirector") user_lvar_visitor_t::handle_retrieved_info;
---

That indeeds allow the director to compile, although with a warning:
---
> ../../include/hexrays.hpp:605: Warning 517: Director class 'user_lvar_visitor_t' is abstract,
abstract method 'handle_retrieved_info' is not accesible, maybe due to multiple inheritance or
'nodirector' feature
---
Hehe. Fair enough.

But although it "compiles" (for a definition of "compiling" where 
warnings are ignored), I think it's a little early to claim victory: The 
produced "SwigDirector_user_lvar_visitor_t" lacks the implementation for 
'handle_retrieved_info', that will dispatch to the Python function.

In other words, using 'nodirector', we have made that method 
non-overridable; thereby defeating the very purpose of 'director'ing the 
user_lvar_visitor_t (or am I missing something?)

Still, thank you for the suggestion; I also wanted to make sure I wasn't 
missing something and/or did something wrong!

Best regards,
     Arnaud.

> Regards,
> Alex.
>
>
> 2014-04-07 18:30 GMT+04:00 Alexander Borovikov <dsbalbor <at> gmail.com
> <mailto:dsbalbor <at> gmail.com>>:
>
>     Hmmm... interesting case, I haven't faced a similar issue.
>     I'm afraid that isn't a smart solution, but I would %extend the
>     original struct user_lvar_visitor_t with some non-virtual method,
>     something like:
>
>
>     2014-04-07 14:59 GMT+04:00 Arnaud Diederen <arnaud <at> hex-rays.com
>     <mailto:arnaud <at> hex-rays.com>>:
>
>
>         Hello SWIG users,
>
>         I have a bit of a problem getting the SWIG-generated file to
>         compile.
>
>         Here's a snippet of the header file:
>         ---
>          > struct user_lvar_visitor_t
>          > {
>          >   virtual int __stdcall handle_retrieved_info(const
>         lvar_saved_info_t &lv) = 0;
>         (...)
>         ---
>
>         And here's what SWIG generates:
>         ---
>          > struct SwigDirector_user_lvar_visitor_t : public
>         user_lvar_visitor_t, public Swig::Director {
>          >
>          > public:
>          >     SwigDirector_user_lvar_visitor_t(PyObject *self);
>          >     virtual int handle_retrieved_info(lvar_saved_info_t const
>         &lv);
>         (...)
>         ---
>
>         I understand that SWIG doesn't care about calling conventions as
>         usually
>         just _calls_ functions (and doesn't override them), but in the
>         case of
>         the "director" it is important: this file doesn't compile on
>         windows:
>         ---
>          >
>         z:\current\plugins\idapython\obj\x86_win_vc_32\idaapi_include.h(223)
>         : error C2695: 'SwigDirector_user_lvar_visitor_t::handle_
>          > retrieved_info': overriding virtual function differs from
>         'user_lvar_visitor_t::handle_retrieved_info' only by calling convent
>          > ion
>         ---
>
>         Instances of those directors will be passed to functions
>         implemented by
>         a plugin, and those plugin functions will call into the virtual
>         methods.
>         Therefore, it is important that the calling convention matches
>         that of
>         the defining class.
>
>         I'm not sure how I should tell SWIG to generate those directors
>         with the
>         same calling convention, though.. Any recommendation?
>
>         Any info/pointers are welcome, as my solution will be an awful
>         hack: add
>         a post-processing phase and inject the calling conventions at
>         that time.
>
>         Thanks,
>                Arnaud.
>
>         --
>         Arnaud Diederen
>         Hex-Rays SA: state of the art binary software analysis tools
>
>         ------------------------------------------------------------------------------
>         Put Bad Developers to Shame
>         Dominate Development with Jenkins Continuous Integration
>         Continuously Automate Build, Test & Deployment
>         Start a new project now. Try Jenkins in the cloud.
>         http://p.sf.net/sfu/13600_Cloudbees_APR
>         _______________________________________________
>         Swig-user mailing list
>         Swig-user <at> lists.sourceforge.net
>         <mailto:Swig-user <at> lists.sourceforge.net>
>         https://lists.sourceforge.net/lists/listinfo/swig-user
>
>
>

--

-- 
Arnaud Diederen
Hex-Rays SA: state of the art binary software analysis tools

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
Arnaud Diederen | 7 Apr 12:59 2014

Directors, __stdcall virtual method, compilation error on windows.


Hello SWIG users,

I have a bit of a problem getting the SWIG-generated file to compile.

Here's a snippet of the header file:
---
> struct user_lvar_visitor_t
> {
>   virtual int __stdcall handle_retrieved_info(const lvar_saved_info_t &lv) = 0;
(...)
---

And here's what SWIG generates:
---
> struct SwigDirector_user_lvar_visitor_t : public user_lvar_visitor_t, public Swig::Director {
>
> public:
>     SwigDirector_user_lvar_visitor_t(PyObject *self);
>     virtual int handle_retrieved_info(lvar_saved_info_t const &lv);
(...)
---

I understand that SWIG doesn't care about calling conventions as usually 
just _calls_ functions (and doesn't override them), but in the case of 
the "director" it is important: this file doesn't compile on windows:
---
> z:\current\plugins\idapython\obj\x86_win_vc_32\idaapi_include.h(223) : error C2695: 'SwigDirector_user_lvar_visitor_t::handle_
> retrieved_info': overriding virtual function differs from
'user_lvar_visitor_t::handle_retrieved_info' only by calling convent
> ion
---

Instances of those directors will be passed to functions implemented by 
a plugin, and those plugin functions will call into the virtual methods. 
Therefore, it is important that the calling convention matches that of 
the defining class.

I'm not sure how I should tell SWIG to generate those directors with the 
same calling convention, though.. Any recommendation?

Any info/pointers are welcome, as my solution will be an awful hack: add 
a post-processing phase and inject the calling conventions at that time.

Thanks,
      Arnaud.

--

-- 
Arnaud Diederen
Hex-Rays SA: state of the art binary software analysis tools

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees_APR
Arnaud Diederen | 7 Apr 12:47 2014

Re: %extend'ing a template, replacing its destructor.


Hi Alexander,

On 04/04/2014 11:09 AM, Alexander Borovikov wrote:
> Hi Arnaud,
> just a couple of words on the issue. I usually replaced standard dtors
> using:
> %feature("action") MyClass::~MyClass
> {
> //some actions;
> }
> Regards,
> Alex.

Thanks for the suggestion! I'll try that as soon as I have something 
working & stable (I still have one issue.)

        Arnaud.

>
>
> 2014-04-03 19:35 GMT+04:00 Arnaud Diederen <arnaud <at> hex-rays.com
> <mailto:arnaud <at> hex-rays.com>>:
>
>
>     Hi,
>
>     [Just replying to myself, in case it would help others.]
>
>     On 04/02/2014 05:43 PM, Arnaud Diederen wrote:
>      >
>      > I tried many variations around the following:
>      > ---
>      >> %template(cfuncptr_t) qrefcnt_t<cfunc_t>;
>      >>
>      >> ...
>      >>
>      >> %extend qrefcnt_t<cfunc_t> {
>      >>    ~qrefcnt_t(void)
>      >>    {
>      >>      hexrays_deregister_python_cfuncptr_t_instance($self);
>      >>      delete $self;
>      >>    }
>      >> }
>      >> %ignore qrefcnt_t<cfunc_t>::~qrefcnt_t(void);
>      > ---
>      >
>      > But without success so far:
>
>
>     This does what I want:
>     ---
>      > extend qrefcnt_t<cfunc_t> {
>      >   ~qrefcnt_t<cfunc_t>(void)
>      >   {
>      >     hexrays_deregister_python_cfuncptr_t_instance($self);
>      >     delete $self;
>      >   }
>      > }
>      > %ignore qrefcnt_t<cfunc_t>::~qrefcnt_t(void);
>      > %template(cfuncptr_t) qrefcnt_t<cfunc_t>;
>     ---
>
>     Note how the destructor _must_ have template parameters. Without those,
>     it won't work.
>     However, the %ignore directive _must not_ have template parameters
>     inside the destructor's name.
>
>
>     Best,
>              Arnaud.
>
>
>     --
>     Arnaud Diederen
>     Hex-Rays SA: state of the art binary software analysis tools
>
>     ------------------------------------------------------------------------------
>     _______________________________________________
>     Swig-user mailing list
>     Swig-user <at> lists.sourceforge.net <mailto:Swig-user <at> lists.sourceforge.net>
>     https://lists.sourceforge.net/lists/listinfo/swig-user
>
>

--

-- 
Arnaud Diederen
Hex-Rays SA: state of the art binary software analysis tools

------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees_APR

Gmane