-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Boost.Python equivalent of test_private_first_base under pybind11 PR …
…#2672. pybind/pybind11#2672
- Loading branch information
Ralf W. Grosse-Kunstleve
committed
Dec 24, 2020
1 parent
6fe7d25
commit 97919e3
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from __future__ import absolute_import, division, print_function | ||
import boost_adaptbx.boost.python as bp | ||
ext = bp.import_ext("rwgk_tbx_private_first_base_ext") | ||
from rwgk_tbx_private_first_base_ext import * |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Boost.Python equivalent of test_private_first_base under | ||
// pybind11 PR #2672: | ||
// https://github.com/pybind/pybind11/pull/2672 | ||
|
||
#include <boost/python/class.hpp> | ||
#include <boost/python/def.hpp> | ||
#include <boost/python/manage_new_object.hpp> | ||
#include <boost/python/module.hpp> | ||
#include <boost/python/return_value_policy.hpp> | ||
|
||
namespace { | ||
|
||
struct base { | ||
base() : base_id(100) {} | ||
virtual ~base() = default; | ||
virtual int id() const { return base_id; } | ||
base(const base&) = default; | ||
int base_id; | ||
}; | ||
|
||
struct private_first_base { // Any class with a virtual function will do. | ||
private_first_base() {} | ||
virtual void some_other_virtual_function() const {} | ||
virtual ~private_first_base() = default; | ||
private_first_base(const private_first_base&) = default; | ||
}; | ||
|
||
struct drvd : private private_first_base, public base { | ||
drvd() {} | ||
int id() const override { return 2 * base_id; } | ||
}; | ||
|
||
inline drvd* make_drvd() { return new drvd; } | ||
inline base* make_drvd_up_cast() { return new drvd; } | ||
|
||
inline int pass_base(const base* b) { return b->id(); } | ||
inline int pass_drvd(const drvd* d) { return d->id(); } | ||
|
||
} // namespace | ||
|
||
BOOST_PYTHON_MODULE(rwgk_tbx_private_first_base_ext) | ||
{ | ||
namespace py = boost::python; | ||
|
||
py::class_<base>("base"); | ||
py::class_<drvd, py::bases<base>>("drvd"); | ||
|
||
py::def("make_drvd", make_drvd, | ||
py::return_value_policy<py::manage_new_object>()); | ||
py::def("make_drvd_up_cast", make_drvd_up_cast, | ||
py::return_value_policy<py::manage_new_object>()); | ||
py::def("pass_base", pass_base); | ||
py::def("pass_drvd", pass_drvd); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import rwgk_tbx.private_first_base as m | ||
import sys | ||
|
||
|
||
def test_make_drvd_pass_base(): | ||
d = m.make_drvd() | ||
i = m.pass_base(d) | ||
assert i == 200 | ||
|
||
|
||
def test_make_drvd_up_cast_pass_drvd(): | ||
b = m.make_drvd_up_cast() | ||
# the base return is down-cast immediately. | ||
assert b.__class__.__name__ == "drvd" | ||
i = m.pass_drvd(b) | ||
assert i == 200 | ||
|
||
|
||
def run(args): | ||
assert not args | ||
test_make_drvd_pass_base() | ||
test_make_drvd_up_cast_pass_drvd() | ||
print("Done.") | ||
|
||
|
||
if (__name__ == "__main__"): | ||
run(sys.argv[1:]) |