Skip to content

Commit 5cce179

Browse files
committed
derived_object_types add
1 parent aa8f014 commit 5cce179

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

Examples/class_virtual/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@
1414
da.f()
1515
RuntimeError: Pure virtual function called
1616

17-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# derived_object_types
2+
3+
###### python3 derived_object_types
4+
5+
cd Boost-Python-Examples/Examples/derived_object_types
6+
make
7+
python3 example.py
8+
9+
Python 3.5.2 (default, Aug 18 2017, 17:48:00)
10+
[GCC 5.4.0 20160609] on linux
11+
Type "help", "copyright", "credits" or "license" for more information.
12+
>>> from derived_object_types import *
13+
>>> t = test()
14+
>>> print(t.to_list(1, "ss"))
15+
[1, 'ss']
16+
>>> print(t.to_tuple())
17+
(123, 'D', 'Hello, World', 0.0)
18+
>>> print(t.to_str("hello!"))
19+
HELLO! is bigger than hello!
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <boost/python.hpp>
3+
using namespace boost::python;
4+
5+
struct test
6+
{
7+
list to_list(object x, object y)
8+
{
9+
list lst;
10+
lst.append(x);
11+
lst.append(y);
12+
return lst;
13+
}
14+
tuple to_tuple()
15+
{
16+
return make_tuple(123, 'D', "Hello, World", 0.0);
17+
}
18+
object to_str(str name)
19+
{
20+
str name2 = name.upper();
21+
object msg = "%s is bigger than %s" % make_tuple(name2, name);
22+
return msg;
23+
}
24+
};
25+
26+
BOOST_PYTHON_MODULE(derived_object_types)
27+
{
28+
class_<test>("test")
29+
.def("to_list", &test::to_list)
30+
.def("to_tuple", &test::to_tuple)
31+
.def("to_str", &test::to_str);
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
PYTHON_VERSION = 3.5
2+
TARGET = derived_object_types
3+
4+
CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result
5+
6+
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
7+
8+
# Only need if install from source
9+
10+
BOOST_INC = /usr/local/include/boost
11+
BOOST_LIB = /usr/local/lib
12+
13+
14+
ifeq ($(PYTHON_VERSION), 3.5)
15+
BOOST = lboost_python3
16+
PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m
17+
else
18+
BOOST = lboost_python
19+
PYTHON_VERSION_FINAL = $(PYTHON_VERSION)
20+
endif
21+
22+
23+
$(TARGET).so: $(TARGET).o
24+
g++ $(CFLAGS) -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so
25+
26+
$(TARGET).o: $(TARGET).cpp
27+
g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp

0 commit comments

Comments
 (0)