mercredi 8 janvier 2020

proxy of

Calling the below code from python:

from f_p import form_p
print(form_p([1,2,3]))

Gives me below error

Error:

(<f_p.mapiv; proxy of <Swig Object of type 'std::map< char,int > *' at 0x7f09778fc270> >,)

How to resolve it? I am trying to create a wrapper for my cpp code in swig. Code files:

f_p.i:

%module f_p
#define SWIGPYTHON_BUILTIN

%{
  #include "numpy/arrayobject.h"
  #define SWIG_FILE_WITH_INIT  /* To import_array() below */
  #include "f_p.h"
%}

%include "std_map.i"
%import "std_deque.i" 
// %include "numpy.i"
%import "std_vector.i" 

#include <deque>

%template() std::vector<int>;

%template (map) std::map<char,int>;
%template(MapDeque) std::deque<std::map<char, int>>;


%include "f_p.h"

f_p.cpp:

#include <deque> 
#include <iostream> 
using namespace std; 

#include <vector>
#include <map>  

deque<map<char, int>> form_p(vector<int> inp_list) 
{
    map<char, int> my_map = {
    { 'A', 1 },
    { 'B', 2 },
    { 'C', 3 }
    };
    deque<map<char, int>> mydeque;
    mydeque.push_back(my_map); 
    return mydeque;
}

f_p.h:

#ifndef F_P_H
#define f_P_H
#include <stdio.h>
#include <deque>
#include <map>
#include <vector>

/* Define function prototype */
std::deque<std::map<char, int>> form_p(std::vector<int> inp_list) ;
#endif

build.sh:

rm *.o f_p_wrap.cpp _f_p.so f_p.py
rm -rf __pycache__

g++ -O3 -march=native -fPIC -c f_p.cpp

swig -python -c++ -o f_p_wrap.cpp f_p.i

# Next, compile the wrapper code:

g++ -O3 -march=native -w -fPIC -c $(pkg-config --cflags --libs python3) -I /home/kriti/anaconda3/lib/python3.7/site-packages/numpy/core/include f_p.cpp f_p_wrap.cpp

g++ -std=c++11 -O3 -march=native -shared f_p.o f_p_wrap.o -o _f_p.so -lm

I ain't able to get the output. I don't know how to work with deque thing. for map and vector I am bale to produce the results but not with the deque and when there is char in map.

Aucun commentaire:

Enregistrer un commentaire