Last active
December 10, 2015 00:09
-
-
Save tsbertalan/4349180 to your computer and use it in GitHub Desktop.
How to return a double * from swig as a Python list? Use a std::vector instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
returnList.py | |
_returnList.so | |
*_wrap.cxx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "returnList.h" | |
#include <iostream> | |
int main(){ | |
FooGiver *fg = new FooGiver(); | |
// for( std::vector<double>::const_iterator i = fg->wGiver().begin(); i != fg->wGiver().end(); ++i){ | |
// std::cout << *i << ' '; | |
// } | |
// std::cout << std::endl; | |
delete fg; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CXXFLAGS = -g -Wall -std=c++0x # -pedantic | |
PYTHON_INCLUDE_DIR = "/usr/include/python2.6" | |
all: swig-return | |
clean: | |
$(RM) *.o | |
$(RM) .depend | |
$(RM) *_wrap.cxx | |
$(RM) *.so | |
$(RM) *.pyc | |
$(RM) returnList.py | |
swig-return: | |
swig -python -c++ returnList.swg | |
$(CXX) -fPIC -shared -o _returnList.so returnList.cc returnList_wrap.cxx -I$(PYTHON_INCLUDE_DIR) | |
python returnList_demo.py | |
cpp-only: | |
g++ -g returnList.cc cppUser.cc -o cppUser && ./cppUser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "returnList.h" | |
FooGiver::FooGiver(){ | |
toReturn = std::vector<double>(4); | |
for(int i=0; i<toReturn.size(); i++){ | |
toReturn[i] = i * 2; | |
} | |
} | |
FooGiver::~FooGiver(){ | |
// delete toReturn; | |
; | |
} | |
std::vector<double> FooGiver::foo(){ | |
return toReturn; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
class FooGiver { | |
public: | |
FooGiver(); | |
~FooGiver(); | |
std::vector<double> foo(); | |
private: | |
std::vector<double> toReturn; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%module returnList | |
%include "std_vector.i" | |
namespace std { | |
%template(IntVector) vector<int>; | |
%template(DoubleVector) vector<double>; | |
} | |
%include "returnList.h" | |
%{ | |
#include "returnList.h" | |
%} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import returnList | |
giver = returnList.FooGiver() | |
foo = giver.foo() | |
print foo | |
print [v for v in foo] | |
print type(foo) | |
print list(foo) | |
print type(list(foo)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment