Skip to content

Commit

Permalink
Added a function to extract intrinsic typed variables from python int…
Browse files Browse the repository at this point in the history
…o C++
  • Loading branch information
Chris Sullivan committed Apr 19, 2016
1 parent 3b51734 commit f9276a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions example_external.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <vector>
#include <iostream>
#include <string>
#include <pylingual.hpp>

int main(){

try{

pylingual py;

py.call("x = 42");
auto x = py.get<double>("x");
cout << x << endl;

return 0;


}catch(boost::python::error_already_set const &){
//Parse and output the exception
string perror_str = parse_python_exception();
cout << "Error in Python: " << perror_str << endl;
}
}
5 changes: 5 additions & 0 deletions include/pylingual.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class pylingual {
return Vec;
}

template<typename T>
T get(const char* var_to_extract) {
return extract<T>(main_module.attr(var_to_extract));
}

template<typename T>
void call(const char* code_to_execute,char* var_to_extract,vector<T> &output) {
exec(code_to_execute,main_namespace);
Expand Down

0 comments on commit f9276a1

Please sign in to comment.