Skip to content

Commit f4f8dd7

Browse files
committed
overloading half
1 parent 5d7360e commit f4f8dd7

File tree

6 files changed

+182
-23
lines changed

6 files changed

+182
-23
lines changed

Examples/call_policies/README.md

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,18 @@
99
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
1010
[GCC 5.4.0 20160609] on linux
1111
Type "help", "copyright", "credits" or "license" for more information.
12-
>>> import call_policies
13-
>>> y = call_policies.Y()
14-
>>> z = call_policies.Z()
15-
>>> x = call_policies.f(y, z)
16-
In function f
17-
>>> x.pr()
18-
pr val: 3 from x
19-
>>> del y
20-
>>> x.pr()
21-
pr val: 32 from x # the memory used to store x.val has been freed and currently store something else
22-
23-
>>> y = call_policies.Y()
24-
>>> x = call_policies.f(y, z)
25-
In function f
26-
>>> x.pr()
27-
pr val: 3 from x
28-
>>> y.z_value()
12+
>>> from call_policies import *
13+
>>> y = Y()
14+
>>> z = Z()
15+
>>> r = f(y, z)
16+
In function f
17+
>>> y.x.get()
18+
3
19+
>>> r.get()
20+
3
21+
>>> y.x.set(2)
22+
>>> r.get()
2923
2
30-
>>> del z
31-
>>> y.z_value() # same reason as before
32-
0
33-
>>>
34-
3524

3625

3726

Examples/call_policies/call_policies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ BOOST_PYTHON_MODULE(call_policies)
4141
.def("get", &Z::get)
4242
.def("set", &Z::set);
4343
def("f", f, return_value_policy<reference_existing_object>());
44-
def("ff", f, return_internal_reference<1
44+
def("ff", f, return_internal_reference<1,
4545
with_custodian_and_ward<1, 2> >());
4646
}

Examples/overloading/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# overloading
2+
3+
###### python3 overloading
4+
5+
cd Boost-Python-Examples/Examples/overloading
6+
make
7+
python3
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 overloading import *
13+
>>> x = X()
14+
>>> x.f(1)
15+
f(int a)
16+
True
17+
>>> x.f(1, 2)
18+
f(int a, double b)
19+
True
20+
>>> x.f(1,2,3.3)
21+
Traceback (most recent call last):
22+
File "<stdin>", line 1, in <module>
23+
Boost.Python.ArgumentError: Python argument types in
24+
X.f(X, int, int, float)
25+
did not match C++ signature:
26+
f(X {lvalue}, int, int, int)
27+
f(X {lvalue}, int, double, char)
28+
f(X {lvalue}, int, double)
29+
f(X {lvalue}, int)
30+
>>> x.f(1,2,3)
31+
int(a, b, c), a + b + c is: 6
32+
6
33+
34+
35+
>>> not_member_func(4, "z", 2, 1)
36+
not mem func: a: 4 b: z c: 2 d: 1
37+
True
38+
>>> not_member_func(4, "z", 2, 1)
39+
not mem func: a: 4 b: z c: 2 d: 1
40+
True
41+
>>> not_member_func(4, "z")
42+
not mem func: a: 4 b: z c: 2 d: 3
43+
True
44+
>>> not_member_func(4, "z", 1)
45+
not mem func: a: 4 b: z c: 1 d: 3
46+
True
47+
>>> not_member_func(4, "z", 1, 3)
48+
not mem func: a: 4 b: z c: 1 d: 3
49+
True
50+
51+
>>> xx = X()
52+
>>> xx.mem_func(1)
53+
variable argument: a: 1 b:  c: 2 d: 3
54+
True
55+
56+
>>> xxx = X(1)
57+
other constructor
58+
>>> xxx = X(1, "s")
59+
other constructor
60+
>>>
61+
62+
63+
64+
65+

Examples/overloading/makefile

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 = overloading
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

Examples/overloading/overloading.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <boost/python.hpp>
2+
#include <iostream>
3+
#include <string>
4+
5+
using namespace boost::python;
6+
7+
struct X
8+
{
9+
X() = default;
10+
X(int a, char b = 'D', std::string c = "constructor", double d = 0.0) { std::cout << "other constructor" << std::endl;}
11+
bool f(int a)
12+
{
13+
std::cout << "f(int a)" << std::endl;
14+
return true;
15+
}
16+
17+
bool f(int a, double b)
18+
{
19+
std::cout << "f(int a, double b)" << std::endl;
20+
return true;
21+
}
22+
23+
bool f(int a, double b, char c)
24+
{
25+
std::cout << "f(int a, double b, char c)" << std::endl;
26+
return true;
27+
}
28+
29+
int f(int a, int b, int c)
30+
{
31+
int result = a + b + c;
32+
std::cout << "int(a, b, c), a + b + c is: " << result << std::endl;
33+
return a + b + c;
34+
}
35+
36+
bool mem_func(int a, char b = 1, unsigned c = 2, double d = 3)
37+
{
38+
std::cout << "variable argument: a: " << a << " b: " << b << " c: " << c << " d: " << d << std::endl;
39+
return true;
40+
}
41+
};
42+
43+
bool not_mem_func(int a, char b = 1, unsigned c = 2, double d = 3)
44+
{
45+
std::cout << "not mem func: a: " << a << " b: " << b << " c: " << c << " d: " << d << std::endl;
46+
return true;
47+
}
48+
49+
bool (X::*fx1)(int) = &X::f;
50+
bool (X::*fx2)(int, double) = &X::f;
51+
bool (X::*fx3)(int, double, char) = &X::f;
52+
int (X::*fx4)(int, int, int) = &X::f;
53+
54+
/*
55+
// pointer name should be same as member function name, or compile error will be reported
56+
bool (X::*mem_func)(int, char, unsigned, double) = &X::mem_func;
57+
*/
58+
59+
BOOST_PYTHON_FUNCTION_OVERLOADS(not_member_func_overloads, not_mem_func, 1, 4);
60+
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(mem_func_overloads, X::mem_func, 1, 4);
61+
62+
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_mem_overloads, X::f, 1, 3);
63+
64+
BOOST_PYTHON_MODULE(overloading)
65+
{
66+
class_<X>("X")
67+
.def("f", fx1)
68+
.def("f", fx2)
69+
.def("f", fx3)
70+
.def("f", fx4)
71+
.def(init<>())
72+
.def(init<int, optional<char, std::string, double>>())
73+
.def("mem_func", &X::mem_func, mem_func_overloads())
74+
.def("f_mem_func", fx1);
75+
def("not_member_func", not_mem_func, not_member_func_overloads());
76+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* [class_virtual_nopure](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/class_virtual_nopure)
2323
* [operators](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/operators)
2424
* [special_operators](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/special_operators)
25+
* [call_policies](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/call_policies)
26+
* [overloading](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/overloading)
2527

2628
##### Boost Installation with python3 support
2729

0 commit comments

Comments
 (0)