Skip to content

Commit

Permalink
Boost.Python equivalent of test_smart_ptr_private_first_base under py…
Browse files Browse the repository at this point in the history
…bind11 PR #2672.

pybind/pybind11#2672 (comment)
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Dec 21, 2020
0 parents commit debf3a5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Experimental stuff using cctbx_project/libtbx build tools.

## Example bootstrap
```
cd clone # Any name.
git clone https://github.com/SCons/scons.git
git clone https://github.com/cctbx/boost.git
git clone https://github.com/cctbx/cctbx_project.git
git clone https://github.com/rwgk/rwgk_tbx.git
mkdir bintbx # Any other name.
cd bintbx
python3 ../cctbx_project/libtbx/configure.py --compiler=clang rwgk_tbx
source setpaths.sh
make
libtbx.python ../rwgk_tbx/tst_smart_ptr_private_first_base.py
```
14 changes: 14 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import libtbx.load_env
Import("env_etc", "env_no_includes_boost_python_ext")
env = env_no_includes_boost_python_ext.Clone()
env_etc.enable_more_warnings(env=env)
env_etc.include_registry.append(
env=env,
paths=[
env_etc.libtbx_include,
env_etc.boost_adaptbx_include,
env_etc.boost_include,
env_etc.python_include])
env.SharedLibrary(
target="#lib/rwgk_tbx_smart_ptr_private_first_base_ext",
source="smart_ptr_private_first_base_ext.cpp")
Empty file added __init__.py
Empty file.
3 changes: 3 additions & 0 deletions libtbx_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"optional_modules": ["boost"]
}
4 changes: 4 additions & 0 deletions smart_ptr_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_smart_ptr_private_first_base_ext")
from rwgk_tbx_smart_ptr_private_first_base_ext import *
46 changes: 46 additions & 0 deletions smart_ptr_private_first_base_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Boost.Python equivalent of test_smart_ptr_private_first_base under
// pybind11 PR #2672:
// https://github.com/pybind/pybind11/pull/2672#issuecomment-748543648

#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>

#include <memory>

namespace {

struct base {
base() : base_id(100) {}
virtual ~base() = default;
virtual int id() const { return base_id; }
int base_id;
};

struct private_first_base { // Any class with a virtual function will do.
virtual void some_other_virtual_function() const {}
virtual ~private_first_base() = default;
};

struct drvd : private private_first_base, public base {
int id() const override { return 2 * base_id; }
};

inline std::shared_ptr<drvd> make_shared_drvd() {
return std::shared_ptr<drvd>(new drvd);
}

inline int pass_shared_base(std::shared_ptr<base> b) { return b->id(); }

} // namespace

BOOST_PYTHON_MODULE(rwgk_tbx_smart_ptr_private_first_base_ext)
{
namespace py = boost::python;

py::class_<base, std::shared_ptr<base>>("base");
py::class_<drvd, py::bases<base>, std::shared_ptr<drvd>>("drvd");

py::def("make_shared_drvd", make_shared_drvd);
py::def("pass_shared_base", pass_shared_base);
}
14 changes: 14 additions & 0 deletions tst_smart_ptr_private_first_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import absolute_import, division, print_function

import rwgk_tbx.smart_ptr_private_first_base as m
import sys

def run(args):
assert not args
d = m.make_shared_drvd()
i = m.pass_shared_base(d)
assert i == 200
print("Done.")

if (__name__ == "__main__"):
run(sys.argv[1:])

0 comments on commit debf3a5

Please sign in to comment.