Created
July 3, 2012 14:16
-
-
Save emctoo/3039982 to your computer and use it in GitHub Desktop.
Boost.Python example
This file contains 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
// compile by: g++ hello.cxx -shared -fPIC -o hello.so -I/usr/include/python2.7 -lboost_python | |
#include <string> | |
#include <boost/python.hpp> | |
using std::string; | |
using namespace boost::python; | |
string hello() { return "Hello, world!"; } | |
// add a argument | |
string hello(const string &str) { return "hello " + str; } | |
// add two different type arguments | |
string hello(const string &str, int x) { | |
string tmp = "hello "; | |
for (int i=0; i<x; ++i) { tmp += str + " "; } | |
return tmp; | |
} | |
string (*fp1)() = hello; | |
string (*fp2)(const string &str) = hello; | |
string (*fp3)(const string &str, int x) = hello; | |
class demo_class { | |
private: | |
string msg; | |
public: | |
static string s_hello; | |
demo_class(): msg("boost") {} | |
string hellox(int x = 1) { | |
string tmp = s_hello; | |
for(int i=0; i<x; i++) { tmp += msg + " "; } | |
return tmp; | |
} | |
}; | |
string demo_class::s_hello = "hello"; | |
BOOST_PYTHON_MODULE(hello) | |
{ | |
def("hello", fp1, "Welcome !"); | |
def("hello", fp2, arg("str")); | |
def("hello", fp3, ("str", "x")); | |
class_<demo_class>("demo", "doc string") | |
.def("hello", &demo_class::hellox, arg("x") = 1) | |
.def_readwrite("shello", &demo_class::s_hello); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment