Rich Rollman | 24 Apr 17:41 2014
Picon

PHP mixed type

I have the following C method in a library which I am trying use with PHP. The method returns an error code and returns the value of an integer passed as a pointer:

 

int getMyAge(int *age)

{

  if able to find age

  {

     *age = // value;

     return 0;

  }

  else

    return error_code;

}

 

In PHP I would like the SWIG-generated wrapper to return FALSE when an error occurs and the age as an integer when successful. PHP models this in other APIs by returning the “mixed” type.  Is there a way to do this with custom typemaps in SWIG?

 

I am able to use simple typemaps in SWIG to get the SWIG-generated wrapper to return an array which contains the error code and age. I know I could write a PHP function around this which provides the behavior I want. I was wondering if there is a way to do this with typemaps or other SWIG capabilities without the need for wrapper functions.

 

I am using SWIG 3.0.0 to expose this C method to PHP (ver. 5.3.10).

 

Thanks in advance for any help you can provide.

 

Rich

 

 

------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Joel Andersson | 21 Apr 12:39 2014
Picon

MATLAB module - work in progress

Hi all,

As some of you might have seen from mails on the swig-devel list, I have been doing some work on a MATLAB module. The module is far from being ready to be included in mainline SWIG repo, but it might already be useful to some of you if you are prepared to hack a bit when encountering bugs or missing features. What currently works are basically the examples "class", "simple", "contract", "constants", "enum", "funcptr", "funcptr2", "reference", "variables" and "template" in the examples directory (https://github.com/jaeandersson/swig/tree/matlab/Examples/matlab).

The code is located in my SWIG fork: https://github.com/jaeandersson/swig (branch "matlab"). I encourage you to fork the repo and make pull requests if you fix something. To build it, run autogen.sh in the root and then configure-make-make install. Generate wrapper by "swig -matlab example.i" or "swig -c++ -matlab example.i" depending on example. Then compile the generated mex by "mex example_wrap.cxx example.cxx" or equivalent from inside MATLAB.

I am very open for collaboration on the module, to be able to finish it quicker. My focus is otherwise to generate wrappers for the project I am working on and finishing the module is secondary.

Best regards,
Joel
------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
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]

...will cause a segfault.

How can I get the attribute to provide the same result as the method?

(I'm currently using SWIG 2.0.7, on Ubuntu 12.04)

Martin

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

Gmane