Created
June 14, 2012 10:58
-
-
Save alvesjnr/2929625 to your computer and use it in GitHub Desktop.
Another 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
#setup.py | |
from setuptools import setup | |
from setuptools.extension import Extension | |
import os.path | |
import sys | |
include_dirs = ["/usr/include/boost","."] | |
libraries=["boost_python-py27"] | |
library_dirs=['/usr/local/lib', "/usr/lib/python2.7"] | |
files = ["square.cpp", "wrapper.cpp"] | |
setup(name="square", | |
ext_modules=[ | |
Extension("square",files, | |
library_dirs=library_dirs, | |
libraries=libraries, | |
include_dirs=include_dirs, | |
depends=[]), | |
] | |
) |
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
#include <cmath> | |
#include "square.hpp" | |
void Square::set_side(double side){ | |
this->side = side; | |
this->area = side*side; | |
} | |
void Square::set_area(double area){ | |
this->area = area; | |
this->side = std::sqrt(area); | |
} | |
double Square::get_area(void){ | |
return this->area; | |
} | |
double Square::get_side(void){ | |
return this->side; | |
} |
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
class Square{ | |
private: | |
double side; | |
double area; | |
public: | |
void set_area(double area); | |
void set_side(double side); | |
double get_area(void); | |
double get_side(void); | |
}; |
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
#include <Python.h> | |
#include <boost/python.hpp> | |
#include "square.hpp" | |
using namespace boost::python; | |
BOOST_PYTHON_MODULE(square) | |
{ | |
// Create the Python type object for our extension class | |
class_<Square>("Square") | |
.def("set_side", &Square::set_side) | |
.def("set_area", &Square::set_area) | |
.def("get_side", &Square::get_side) | |
.def("get_area", &Square::get_area) | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment