swig

Exposing a C++ class instance to a python embedded interpreter

微笑、不失礼 提交于 2019-12-18 11:07:14
问题 I am looking for a simple way to expose a C++ class instance to a python embedded interpreter. I have a C++ library. This library is wrapped (using swig for the moment) and I am able to use it from the python interpreter I have a C++ main program which instanciates a Foo class from my library and embeds a python interpreter I would like to expose my C++ world instance of Foo to the python world (and seen as a Foo class). Is this possible, if so, how? I think it's almost like in the first

How to create an extension to already wrapped library via SWIG?

白昼怎懂夜的黑 提交于 2019-12-18 09:44:08
问题 I have a library. It is wraped via SWIG. I want to create a plugin to extend it. Plugin requires a class from already wrapped library to run having something like void init( oldT old); . Library is used from Java and c#. Now this plugin also will be used from there. Library and plugin are separate dll's. How to tall SWIG that I already have that oldT type wrapped when creating binding for plugin? 回答1: You're looking for %import in the .i file of your plugin. You'll need to either have (or

extending 'incomplete' types (SWIG)

女生的网名这么多〃 提交于 2019-12-18 09:20:46
问题 I'm looking for a way to extend (i.e. add new members to a type using the %extend directive) a type that is defined in the library file itself while the header files of the library provide only a forward declaration for the type. Treating the type as if its definition is known at compile time, leads to the following warning: Warning 303: %extend defined for an undeclared class [name of the type]. Is anyone aware of a solution or a workaround for this problem? I'm sure there is one, since SWIG

How do I get SWIG to automatically wrap an emulated “this” pointer to a C struct?

邮差的信 提交于 2019-12-18 06:11:46
问题 I've got a simple C "class" I have implemented, using function pointers in a struct to implement the member functions, and passing a pointer to the struct as the first argument to each function, similar to the implicit "this" pointer in C++. %module mytest %{ typedef struct mytest mytest; struct mytest { int data; int (*func1)(mytest *,int); void (*func2)(mytest *,int); }; int f1(mytest *me,int n) { return me->data + n; } void f2(mytest *me,int n) { me->data += n; } mytest *mytestNew(int n) {

How to extend a templated c++ class in python with SWIG to allow the [] operator

[亡魂溺海] 提交于 2019-12-18 04:54:30
问题 I have a templated c++ array class which uses the standard vector class: #include <vector> #include <string> using namespace std; template<typename T> class Array1D{ private: vector<T> data_; int xsize_; public: Array1D(): xsize_(0) {}; // creates vector of size nx and sets each element to t Array1D(const int& nx, const T& t): xsize_(nx) { data_.resize(xsize_, t); } T& operator()(int i) {return data_[i];} T& operator[](int i) {return data_[i];} }; My SWIG interface file looks like %module

Understand Op Registration and Kernel Linking in TensorFlow

∥☆過路亽.° 提交于 2019-12-17 18:04:07
问题 I am fairly new to TensorFlow and right now looking into the custom op development. I have already read the official tutorial but I feel a lot of things happen behind the scenes and I do not always want to put my custom ops in user_ops directory. As such, I took up an example word2vec which uses a custom "Skipgram" op whose registration is defined here: /word2vec_ops.cc and whose kernel implementation is here: /word2vec_kernels.cc Looking at the build file, I tried to build individual targets

Pass an array to a wrapped function as pointer+size or range

只愿长相守 提交于 2019-12-17 07:40:00
问题 Given a header like: #include <iostream> #include <algorithm> #include <iterator> inline void foo(const signed char *arr, size_t sz) { std::copy_n(arr, sz, std::ostream_iterator<int>(std::cout, "\n")); } inline void bar(const signed char *begin, const signed char *end) { std::copy(begin, end, std::ostream_iterator<int>(std::cout, "\n")); } (I used C++11 here for convenience, this could be either C or C++ if you changed the implementations though) How can I wrap these functions to take just an

How should I write the .i file to wrap callbacks in Java or C#

北战南征 提交于 2019-12-17 06:13:34
问题 My C program uses callback functions which are periodically called. I want to be able to handle the callback functions in a Java or C# program. How should I write the .i file to achieve this? The C callback looks so: static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata) 回答1: You can do this if you have a chance to pass some data around with the callback, but you'll need to write some JNI glue. I put together a complete example of how you might map C

c/c++封装成python包

坚强是说给别人听的谎言 提交于 2019-12-16 11:44:09
参考网址:https://blog.csdn.net/tiankongtiankong01/article/details/80420033 SWIG (Simplified Wrapper and Interface Generator) 是用来为C和C++程序构造脚本语言接口的软件开发工具。SWIG 实际上是一个编译器,获取C/C++的声明,用一个壳包起来,以便通过其他语言访问这些声明。因此,SWIG 最大的好处就是将脚本语言的开发效率和 C/C++ 的运行效率结合起来。 一:准备源文件 文件1:EncryptTool.h int EncryptFile(const char *szInputFile, const char *szOutputFile); int DecryptFile(const char *szInputFile, const char *szOutputFile); 文件2:EncryptTool.cpp # 属于文件1的引用文件或者说是依赖包,后面动态链接将其封装起来 #include <iostream> using namespace std; int EncryptFile(const char *szInputFile, const char *szOutputFile) { char str[] = "jiami"; cout <<

How can type 'SwigPyObject be registered using copy_reg.pickle in Python?

為{幸葍}努か 提交于 2019-12-14 03:48:44
问题 I have a class method which I would like to use with multiprocessing.Pool for parallelisation. As class instances are not pickleable, I have used the following: import copy_reg import types def _reduce_method(m): if m.im_self is None: return getattr, (m.im_class, m.im_func.func_name) else: return getattr, (m.im_self, m.im_func.func_name) copy_reg.pickle(types.MethodType, _reduce_method) This works no problem. However, within my class I use the GDAL module (https://pypi.org/project/GDAL/) for