giorgi | 21 Nov 15:01 2014
Picon

pass a Python function as argument of a c++ function

Hi,

I'm learning how to use swig to wrap c++ libraries for python.
I'm trying to pass a Python function as argument of a c++ function, but I guess i'm missing something.

Here is my Header funct.hpp

#include <functional> using namespace std; int myfun(int a, int b, function<int(int,int)> f);

Here the Source funct.cpp

#include "funct.hpp" int myfun(int a, int b, function<int(int,int)> f){ return f(a,b);}

The swig file funct.i

%module funct %{ #include "funct.hpp" %} %include "funct.hpp"

The setup.py file is

#!/usr/bin/env python from distutils.core import setup, Extension funct_module = Extension('_funct', sources=['funct_wrap.cxx', 'funct.cpp'], extra_compile_args=['-std=c++11'] ) setup (name = 'funct', version = '0.1', author = "SWIG Docs", description = """Simple swig example from docs""", swig_opts=['-c++'], ext_modules = [funct_module], py_modules = ["funct"], )

Then i try to test the wrapped module executing test.py:

def add(x,y): return x+y import funct x = funct.myfun(1,2,add) print x

but I get the following error

Traceback (most recent call last): File "test.py", line 6, in <module> x = funct.myfun(1,2,add) TypeError: in method 'myfun', argument 3 of type 'function< int (int,int) >' I read the Pointers and callbacks reference but I didn't manage to make it work, I think i have to add something in the funct.i file, but i don't know what and how.

Can anybody help me with this? Thanks in advance!

Daphné


------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Michele De Stefano | 21 Nov 14:43 2014
Picon
Picon

mds-utils 2.1.1 released

I am pleased to announce the release of mds-utils 2.1.1.
This release fixes small bugs present into the 2.1.0 release. Users are strongly encouraged to upgrade to this version.

Features summary
---------------------------

MDS-UTILS provides:
  1. a tool for detecting machine endianity.
  2. utilities for the Boost uBLAS library. Amongst them, some type traits for detecting different uBLAS matrix types.
  3. some useful classes that allow to treat the old C FILE pointer as a C++ stream.
  4. C++ wrappers of the main Python objects, independent of those in Boost Python. Wrappers are provided also for NumPy arrays.
  5. C++ classes that help on treating Python file objects as C++ streams.
  6. a review and refactor of the indexing support in Python extensions. Now access in write mode is supported too.
  7. new C++ to-Python and from-Python converters for some Boost uBlas objects and for standard Python objects. These converters do not depend on Boost Python.
  8. a new sequence iterator that is able to wrap Python sequences and allows also to modify them. This feature does not depend on Boost.Python.
  9. the NDArrayIterator class, that wraps the Numpy C-API iterator and allows easy management of conversions to/from Numpy arrays.
  10. some SWIG interface files, for easy integration with SWIG extensions for Python.

Each class is a well-documented, small, easy to use and it should never be too difficult to learn to use it.
A large percentage of this library makes a heavy usage of the Boost C++ libraries: so, they must be installed on the system. It is assumed that the user is familiar with them.

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Dave Goldsmith | 21 Nov 07:44 2014

%include <windows.i>

SWIG3.0.2, Windows 7 Pro SP1 64-bit, VisualStudio 2010, believe I have all my EV’s set right.

 

Trying to build Examples\Python\class\example

 

Getting linker errors such as:

 

error LNK2019: unresolved external symbol __imp__PyString_AsString referenced in function "char * __cdecl SWIG_Python_str_AsChar(struct _object *)" (?SWIG_Python_str_AsChar <at> <at> YAPADPAU_object <at> <at> <at> Z)                C:\Users\dgoldsmith\Downloads\Quarantine\swigwin-3.0.2\Examples\python\class\example_wrap.obj

 

Is this indicative of the (lack of) windows.i inclusion?  If so, where precisely is it supposed to go?  Is a line such as:

 

__declspec(dllexport) ULONG __stdcall foo(DWORD, __int32);

 

Required for each ”Windows type” like ULONG?  Does anyone have some example files they’d be willing to share?  (A search of the swigwin3.0.2 directory tree for windows.i turned up only 5 hits, only two of which were .i files, one being windows.i itself, and the other, li_windows.i, suggests where to put

 

%include “windows.i”

 

but doesn’t—hey, wait a sec, li_windows.i looks like it has everything in it that one might need—can I just include this, and if so, where exactly?

 

Thanks!

 

David Goldsmith

Senior Software Engineer

Sea-Bird Electronics, Inc.

13431 NE 20th St.

Bellevue, WA 98005 USA

1-425-644-5771

 


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service.
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Sebastien Loriot | 20 Nov 14:24 2014
Picon

Java out of memory

Hello,

I have the following .i file

%module test

%inline %{
   struct A{};
%}

when I run the following example:

public class runme {
   static {
     System.loadLibrary("test");
   }

   public static void main(String argv[]) {
     while(true){
       A a = new A();
       a.delete();
     }
   }
}

I end up with "Exception in thread "main" java.lang.OutOfMemoryError: GC 
overhead limit exceeded"

Did someone already observed that and has an explanation?

I'm using swig 2.0.12 with g++-4.8.3, and observed this with both
OpenJDK and Sun's.

Thanks,

Sebastien.

To generate the bindings I use:
swig2.0 -c++ -java  test.i && g++ -fpic -O3 
-I/usr/lib/jvm/java-6-sun-1.6.0.26/include 
-I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux  -shared test_wrap.cxx 
-o libtest.so
To run the example I use:
javac runme.java && java -Xmx256m runme
Attachment (runme.java): text/x-java, 181 bytes
%module test

%inline %{
  struct A{};
%}
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Francisco Paisana | 19 Nov 20:46 2014
Picon
Picon

Problems converting complex numbers

Hello everyone,

I checked several posts on the internet about this but I can't find a solution. So I am trying to use some C++ functions that receive complex numbers as arguments, and feed them with complex numbers generated in python. So here is my .i file:

/* File: expo.i */
%module expo

%{
#include "expo.h"
%}

%include <std_vector.i>
%include <std_complex.i>
%include "expo.h"

namespace std {
    %numpy_typemaps(complex<float>, NPY_CFLOAT, int);
    %template(IntVec) vector<int>;
    %template(DoubleVec) vector<double>;
    %template(FloatVec) vector<float>;
    %template(CplxVec) vector<complex<float> >;
}


%inline %{
       std::complex<float> doNothing(const std::complex<float> &b) { return b;}
%}

And my expo.h file is:

#include <complex>
#include <vector>

using namespace std;

class ExpoClass {
    int current_sample_;
public:
    ExpoClass();
    void process(const vector<complex<float> > &in);
    void asd(const complex<float> &in);
    int get_sample();
};

complex<float> doNothing2(const complex<float> &b);

When I try to use this class from python this is what happens:

>>> from expo import *
>>> exp = ExpoClass()
>>> exp.asd(complex(1.0,1.0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "expo.py", line 110, in asd
    def asd(self, *args): return _expo.ExpoClass_asd(self, *args)
TypeError: in method 'ExpoClass_asd', argument 2 of type 'complex< float > const &'
>>> doNothing2(complex(1.0,1.0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'doNothing2', argument 1 of type 'complex< float > const &'
>>> doNothing(complex(1.0,1.0))
(1+1j)


So to recap, the methods/functions I defined in my .cpp and .h files can't receive Python complex numbers, but the function doNothing() I defined in the .i file can.

I am running out of ideas of why this happens. It would be great if you could help.

Thanks.

Regards,
Francisco
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Eujain Ting | 18 Nov 02:10 2014
Picon

Wrapping stl set of pointers in python

Hi swig-users,

I'm learning SWIG, and I've come across a problem wrapping std::sets of pointers to c++ objects. 
I can't seem to properly instantiate the set template:
%template (FooSet) std::set<foo*>;
Details are here: http://stackoverflow.com/questions/26640435/wrapping-stl-sets-of-pointers-to-objects

I'd appreciate any help or pointers to resources.

Thanks!
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Marten Pape | 15 Nov 04:50 2014
Picon
Picon

C++ to C#: Interface definition for function pointer with template argument

Hi!
I am a fairly new user to swig and trying to interface a C++ project
with some C# code.
The following C++ code defines a structure from which I can derive a
function pointer type where one argument is a template class:
template<typename SubscriptionData>
struct Callback {
    typedef void(*NotificationFunc)(void *object, SubscriptionData *data);
};
In C/C++ the function pointer gets used in such a way, for example:
(definition of a function that has this function pointer type as argument)
bool subscribe(typename Callback<SubscriptionData>::NotificationFunc
callback, void* object);

To write the swig interface file for a template, I have to add
%template(FinalCSClassName) MyTemplate<int>;

But how do I have to do it in the case with the function pointer?
Finally, I want to realize something like in here:
http://swig.10945.n7.nabble.com/C-pointer-to-method-as-parameter-to-C-td8118.html#a8119

But I'm probably still a bit away from that (+it would be nice, if there
is a more generic way, so that I don't have to do this for every
template specialization).
But one step after another.

Thanks a lot for help,
Marten Pape

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
Eric Wing | 14 Nov 22:19 2014
Picon

How to deal with pure C that uses inheritance?

I am binding a pure C library. In a few places, it has done an
object-oriented C design.

All the "classes" are structs.
Essentially, there is a "base class" struct, in this case "Shape".
Then there are "subclass" structs, such as "Circle".
Circle's first ivar is Shape, and then adds Circle specific stuff to
the struct. There is additional type info contained in the struct.

There is a New/Create function for each specific shape, e.g.
NewCircle(). All of these return a generic Shape*. All the APIs that
use the shapes take signature Shape* and the polymorphism is handled
internally.

To access subclass specific properties (e.g. radius of a circle), in
C, you are simply expected to type cast, e.g. (Circle*)the_shape.

However, I don't know how to deal with this in SWIG through the
language bindings (Lua, JavaScript) where I can't cast.

I gave a long shot, and tried to annotate my .i file with a C++ style
inheritance declaration: e.g.

struct cpCircleShape : public cpShape {

but SWIG doesn't like this. If I change struct to class, then SWIG
says C++ keywords are ignored for C.

Any suggestions?

Thanks,
Eric
--

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

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
Byerley | 13 Nov 12:27 2014
Picon

How to apply INPUT, OUTPUT, INOUT to instance methods

Hi there.
I'm having trouble with dealing with instance methods which receive pointers
to primitive data types.
I'm trying to call these methods from Ruby.

For a function which does not belong to a class, for example 
    int bar(int *x),
I write
    int bar(int *INPUT)
in my *.i file, and I can call this function from Ruby.

However, if the function becomes an instance method, for example
    int SomeClass::bar(int *x),
writing
    int SomeClass::bar(int *INPUT)
does not work.
Ruby tells me that the method was expecting int * but received Fixnum.

If I use only INPUT, I can use %extend and do something like this.
%extend SomeClass {
    int bar(int x) {
        return $self->bar(&x);
    }
}

However, if I wanna use INOUT and OUTPUT, this is not gonna work because I
have to think about return values.

How can I solve this problem?
Thanks.

--
View this message in context: http://swig.10945.n7.nabble.com/How-to-apply-INPUT-OUTPUT-INOUT-to-instance-methods-tp14067.html
Sent from the swig-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
Jakob van Bethlehem | 12 Nov 14:05 2014
Picon

Windows 64 bit support

Hello,

Currently I'm investigating the different techniques currently available to combine C++ and Python on Windows. Swig looks like a very viable option, but an important requirement is being able to compile into 64-bit libraries. On the website it seems Windows support always seems to explicitly mention that 32-bit Windows is supported.

Q: Is this because it is known that 64-bit Windows doesn't work, or because no one ever tried?

Sincerely,
Jakob van Bethlehem
------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
GiuMas | 9 Nov 16:03 2014
Picon

Maltalb SWIG question on Stackoverflow

Hello all,

I have asked this question on Stackoverflow, and I'd like if somebody would add something to the current answers. Many thanks!


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Gmane