Skip to content

Commit

Permalink
Boost.Python equivalent of test_private_first_base under pybind11 PR …
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Dec 24, 2020
1 parent 6fe7d25 commit 97919e3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ env_etc.include_registry.append(
env_etc.boost_adaptbx_include,
env_etc.boost_include,
env_etc.python_include])
env.SharedLibrary(
target="#lib/rwgk_tbx_private_first_base_ext",
source="private_first_base_ext.cpp")
env.SharedLibrary(
target="#lib/rwgk_tbx_smart_ptr_private_first_base_ext",
source="smart_ptr_private_first_base_ext.cpp")
4 changes: 4 additions & 0 deletions private_first_base.py
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 *
54 changes: 54 additions & 0 deletions private_first_base_ext.cpp
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);
}
29 changes: 29 additions & 0 deletions tst_private_first_base.py
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:])

0 comments on commit 97919e3

Please sign in to comment.